fix junie's bad idea

This commit is contained in:
2025-07-21 22:53:11 -04:00
parent c73c63a8aa
commit e0adc40661
4 changed files with 145 additions and 41 deletions

View File

@@ -1,20 +1,17 @@
package go_llm
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"log"
"log/slog"
"net/http"
"gitea.stevedudenhoeffer.com/steve/go-llm/utils"
anth "github.com/liushuangls/go-anthropic/v2"
)
@@ -83,7 +80,6 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
}
if img.Base64 != "" {
// Anthropic models expect images to be < 5MiB in size
raw, err := base64.StdEncoding.DecodeString(img.Base64)
@@ -93,39 +89,15 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
// Check if image size exceeds 5MiB (5242880 bytes)
if len(raw) >= 5242880 {
// Decode the image
imgData, format, err := image.Decode(bytes.NewReader(raw))
compressed, err := utils.CompressImage(img.Base64, 5*1024*1024)
// just replace the image with the compressed one
if err != nil {
log.Println("failed to decode image", err)
continue
}
var buf bytes.Buffer
switch format {
case "jpeg", "jpg":
err = jpeg.Encode(&buf, imgData, &jpeg.Options{Quality: 60})
case "png":
// For PNG, use a higher compression level
enc := &png.Encoder{
CompressionLevel: png.BestCompression,
}
err = enc.Encode(&buf, imgData)
case "gif":
err = gif.Encode(&buf, imgData, &gif.Options{
NumColors: 128,
})
default:
continue
}
if err != nil {
log.Println("failed to encode image", err)
continue
}
// Update the base64 string
img.Base64 = base64.StdEncoding.EncodeToString(buf.Bytes())
img.Base64 = compressed
}
m.Content = append(m.Content, anth.NewImageMessageContent(