54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package vision
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
|
)
|
|
|
|
func TestOpenAICompatibleAdapter_Describe(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/chat/completions" {
|
|
t.Errorf("path = %s, want /chat/completions", r.URL.Path)
|
|
}
|
|
var body map[string]any
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode body: %v", err)
|
|
}
|
|
msgs, _ := body["messages"].([]any)
|
|
if len(msgs) != 1 {
|
|
t.Fatalf("messages = %d, want 1", len(msgs))
|
|
}
|
|
content, _ := msgs[0].(map[string]any)["content"].([]any)
|
|
if len(content) != 2 {
|
|
t.Fatalf("content parts = %d, want 2", len(content))
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = io.WriteString(w, `{"choices":[{"message":{"content":"a red car"}}]}`)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ad := NewOpenAICompatible(config.LLMConfig{BaseURL: srv.URL, Model: "vision-"})
|
|
text, err := ad.Describe(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}, "")
|
|
if err != nil {
|
|
t.Fatalf("Describe error: %v", err)
|
|
}
|
|
if text != "a red car" {
|
|
t.Errorf("text = %q, want a red car", text)
|
|
}
|
|
}
|
|
|
|
func TestOpenAICompatibleAdapter_NotConfigured(t *testing.T) {
|
|
t.Parallel()
|
|
ad := NewOpenAICompatible(config.LLMConfig{})
|
|
if _, err := ad.Describe(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}, ""); err == nil {
|
|
t.Fatal("want error for empty base url, got nil")
|
|
}
|
|
}
|