changed the way images are passed

This commit is contained in:
Steve Dudenhoeffer 2024-10-07 16:33:57 -04:00
parent 8e1001b5b7
commit b8dc1d1a96
3 changed files with 39 additions and 18 deletions

View File

@ -58,12 +58,20 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
}) })
} }
if msg.ImageBase64 != "" { for _, img := range msg.Images {
m.Content = append(m.Content, anth.NewImageMessageContent(anth.MessageContentImageSource{ if img.Base64 != "" {
Type: "base64", m.Content = append(m.Content, anth.NewImageMessageContent(anth.MessageContentImageSource{
MediaType: "image/png", Type: "base64",
Data: msg.ImageBase64, MediaType: img.ContentType,
})) Data: img.Base64,
}))
} else if img.Url != "" {
m.Content = append(m.Content, anth.NewImageMessageContent(anth.MessageContentImageSource{
Type: "url",
MediaType: img.ContentType,
Data: img.Url,
}))
}
} }
// if this has the same role as the previous message, we can append it to the previous message // if this has the same role as the previous message, we can append it to the previous message

14
llm.go
View File

@ -12,11 +12,17 @@ const (
RoleAssistant Role = "assistant" RoleAssistant Role = "assistant"
) )
type Image struct {
Base64 string
ContentType string
Url string
}
type Message struct { type Message struct {
Role Role Role Role
Name string Name string
Text string Text string
ImageBase64 string // ImageBase64 is the base64 string if the message contains an image, empty string otherwise. Images []Image
} }
type Request struct { type Request struct {

View File

@ -25,15 +25,22 @@ func (o openai) requestToOpenAIRequest(request Request) oai.ChatCompletionReques
Name: msg.Name, Name: msg.Name,
} }
if msg.ImageBase64 != "" { for _, img := range msg.Images {
part := oai.ChatMessagePart{ if img.Base64 != "" {
Type: "image_url", m.MultiContent = append(m.MultiContent, oai.ChatMessagePart{
ImageURL: &oai.ChatMessageImageURL{ Type: "image_url",
URL: fmt.Sprintf("data:image/jpeg;base64,%s", msg.ImageBase64), ImageURL: &oai.ChatMessageImageURL{
}, URL: fmt.Sprintf("data:%s;base64,%s", img.ContentType, img.Base64),
},
})
} else if img.Url != "" {
m.MultiContent = append(m.MultiContent, oai.ChatMessagePart{
Type: "image_url",
ImageURL: &oai.ChatMessageImageURL{
URL: img.Url,
},
})
} }
m.MultiContent = append(m.MultiContent, part)
} }
res.Messages = append(res.Messages, m) res.Messages = append(res.Messages, m)