Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions providers/anthropic/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,16 +547,19 @@ func toPrompt(prompt fantasy.Prompt, sendReasoningData bool) ([]anthropic.TextBl
continue
}
// TODO: handle other file types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably remove this comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, we could still handle pdfs, audio, etc, couldn't we?

if !strings.HasPrefix(file.MediaType, "image/") {
continue
if strings.HasPrefix(file.MediaType, "image/") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick, but convert into a switch statement. Clearer and more consistent with similar code (as the other files you edited).

Suggested change
if strings.HasPrefix(file.MediaType, "image/") {
switch {
case strings.HasPrefix(file.MediaType, "image/"):

base64Encoded := base64.StdEncoding.EncodeToString(file.Data)
imageBlock := anthropic.NewImageBlockBase64(file.MediaType, base64Encoded)
if cacheControl != nil {
imageBlock.OfImage.CacheControl = anthropic.NewCacheControlEphemeralParam()
}
anthropicContent = append(anthropicContent, imageBlock)
}

base64Encoded := base64.StdEncoding.EncodeToString(file.Data)
imageBlock := anthropic.NewImageBlockBase64(file.MediaType, base64Encoded)
if cacheControl != nil {
imageBlock.OfImage.CacheControl = anthropic.NewCacheControlEphemeralParam()
if strings.HasPrefix(file.MediaType, "text/") {
anthropicContent = append(anthropicContent, anthropic.NewDocumentBlock(anthropic.PlainTextSourceParam{
Data: string(file.Data),
}))
}
anthropicContent = append(anthropicContent, imageBlock)
}
}
} else if msg.Role == fantasy.MessageRoleTool {
Expand Down
5 changes: 5 additions & 0 deletions providers/openai/language_model_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ func DefaultToPrompt(prompt fantasy.Prompt, _, _ string) ([]openai.ChatCompletio
}

switch {
case strings.HasPrefix(filePart.MediaType, "text/"):
base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data)
content = append(content, openai.FileContentPart(openai.ChatCompletionContentPartFileFileParam{
FileData: param.NewOpt(base64Encoded),
}))
case strings.HasPrefix(filePart.MediaType, "image/"):
// Handle image files
base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data)
Expand Down
5 changes: 5 additions & 0 deletions providers/openaicompat/language_model_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func ToPromptFunc(prompt fantasy.Prompt, _, _ string) ([]openaisdk.ChatCompletio
}

switch {
case strings.HasPrefix(filePart.MediaType, "text/"):
base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data)
content = append(content, openaisdk.FileContentPart(openaisdk.ChatCompletionContentPartFileFileParam{
FileData: param.NewOpt(base64Encoded),
}))
case strings.HasPrefix(filePart.MediaType, "image/"):
// Handle image files
base64Encoded := base64.StdEncoding.EncodeToString(filePart.Data)
Expand Down
Loading