Merge branch 'main' of ssh://nuc.dudenhoeffer.casa:222/steve/go-llm

This commit is contained in:
2024-12-28 19:49:26 -05:00
3 changed files with 143 additions and 200 deletions

View File

@@ -4,8 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
anth "github.com/liushuangls/go-anthropic/v2"
"io"
"log"
"net/http"
anth "github.com/liushuangls/go-anthropic/v2"
)
type anthropic struct {
@@ -61,17 +64,47 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
for _, img := range msg.Images {
if img.Base64 != "" {
m.Content = append(m.Content, anth.NewImageMessageContent(anth.MessageContentImageSource{
Type: "base64",
MediaType: img.ContentType,
Data: img.Base64,
}))
m.Content = append(m.Content, anth.NewImageMessageContent(
anth.NewMessageContentSource(
anth.MessagesContentSourceTypeBase64,
img.ContentType,
img.Base64,
)))
} else if img.Url != "" {
m.Content = append(m.Content, anth.NewImageMessageContent(anth.MessageContentImageSource{
Type: "url",
MediaType: img.ContentType,
Data: img.Url,
}))
// download the image
cl, err := http.NewRequest(http.MethodGet, img.Url, nil)
if err != nil {
log.Println("failed to create request", err)
continue
}
resp, err := http.DefaultClient.Do(cl)
if err != nil {
log.Println("failed to download image", err)
continue
}
defer resp.Body.Close()
img.ContentType = resp.Header.Get("Content-Type")
// read the image
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("failed to read image", err)
continue
}
// base64 encode the image
img.Base64 = string(b)
m.Content = append(m.Content, anth.NewImageMessageContent(
anth.NewMessageContentSource(
anth.MessagesContentSourceTypeBase64,
img.ContentType,
img.Base64,
)))
}
}