83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package ollama
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveModelsFallsBackToInstalled(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/tags", func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"models": []map[string]any{
|
|
{"name": "qwen3:8b"},
|
|
{"name": "nomic-embed-text:latest"},
|
|
}})
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
|
|
c := New(srv.URL, "qwen3:4b", "embeddinggemma")
|
|
st := c.ResolveModels(context.Background())
|
|
if !st.Reachable || !st.ChatReady || !st.EmbeddingReady {
|
|
t.Fatalf("status=%+v", st)
|
|
}
|
|
if st.Model != "qwen3:8b" {
|
|
t.Fatalf("chat=%q", st.Model)
|
|
}
|
|
if st.EmbeddingModel != "nomic-embed-text:latest" {
|
|
t.Fatalf("embed=%q", st.EmbeddingModel)
|
|
}
|
|
if !st.Fallback {
|
|
t.Fatal("fallback should be reported")
|
|
}
|
|
}
|
|
|
|
func TestAutoUsesInstalledModels(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/tags", func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"models": []map[string]any{
|
|
{"name": "llama3.2:3b"}, {"name": "embeddinggemma:latest"},
|
|
}})
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
c := New(srv.URL, "auto", "auto")
|
|
st := c.ResolveModels(context.Background())
|
|
if st.Model != "llama3.2:3b" || st.EmbeddingModel != "embeddinggemma:latest" {
|
|
t.Fatalf("status=%+v", st)
|
|
}
|
|
}
|
|
|
|
func TestChatWithHistorySendsRealMessagesAndContextOptions(t *testing.T) {
|
|
var got chatRequest
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/tags", func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"models": []map[string]any{{"name": "gemma3:4b"}}})
|
|
})
|
|
mux.HandleFunc("/api/chat", func(w http.ResponseWriter, r *http.Request) {
|
|
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"message": map[string]any{"role": "assistant", "content": "ok"}})
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
c := New(srv.URL, "auto", "auto")
|
|
c.ConfigureGeneration(GenerationOptions{NumCtx: 16384, NumPredict: 1200, Temperature: .15}, GenerationOptions{NumCtx: 8192, NumPredict: 700})
|
|
if _, err := c.ChatWithHistory(context.Background(), "system", []Turn{{Role: "user", Content: "Montag Nudeln"}, {Role: "assistant", Content: "verstanden"}}, "stattdessen Dienstag", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.Messages) != 4 {
|
|
t.Fatalf("messages=%d", len(got.Messages))
|
|
}
|
|
if got.Messages[1].Content != "Montag Nudeln" || got.Messages[2].Role != "assistant" {
|
|
t.Fatalf("history=%+v", got.Messages)
|
|
}
|
|
if got.Options == nil || got.Options.NumCtx != 16384 || got.Options.NumPredict != 1200 {
|
|
t.Fatalf("options=%+v", got.Options)
|
|
}
|
|
}
|