|
1
|
package llm |
|
2
|
|
|
3
|
import ( |
|
4
|
"context" |
|
5
|
"encoding/json" |
|
6
|
"net/http" |
|
7
|
"net/http/httptest" |
|
8
|
"testing" |
|
9
|
) |
|
10
|
|
|
11
|
func TestOllamaSummarize(t *testing.T) { |
|
12
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
13
|
if r.Method != "POST" { |
|
14
|
t.Errorf("expected POST request, got %s", r.Method) |
|
15
|
} |
|
16
|
if r.URL.Path != "/api/generate" { |
|
17
|
t.Errorf("unexpected path: %s", r.URL.Path) |
|
18
|
} |
|
19
|
|
|
20
|
var req struct { |
|
21
|
Model string `json:"model"` |
|
22
|
Prompt string `json:"prompt"` |
|
23
|
Stream bool `json:"stream"` |
|
24
|
} |
|
25
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
|
26
|
t.Fatalf("decode request: %v", err) |
|
27
|
} |
|
28
|
|
|
29
|
resp := map[string]any{ |
|
30
|
"response": "ollama response", |
|
31
|
} |
|
32
|
_ = json.NewEncoder(w).Encode(resp) |
|
33
|
})) |
|
34
|
defer srv.Close() |
|
35
|
|
|
36
|
p := newOllamaProvider(BackendConfig{ |
|
37
|
Backend: "ollama", |
|
38
|
Model: "test-model", |
|
39
|
}, srv.URL, srv.Client()) |
|
40
|
|
|
41
|
got, err := p.Summarize(context.Background(), "test prompt") |
|
42
|
if err != nil { |
|
43
|
t.Fatalf("Summarize failed: %v", err) |
|
44
|
} |
|
45
|
if got != "ollama response" { |
|
46
|
t.Errorf("got %q, want %q", got, "ollama response") |
|
47
|
} |
|
48
|
} |
|
49
|
|
|
50
|
func TestOllamaDiscoverModels(t *testing.T) { |
|
51
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
52
|
if r.Method != "GET" { |
|
53
|
t.Errorf("expected GET request, got %s", r.Method) |
|
54
|
} |
|
55
|
if r.URL.Path != "/api/tags" { |
|
56
|
t.Errorf("unexpected path: %s", r.URL.Path) |
|
57
|
} |
|
58
|
|
|
59
|
resp := map[string]any{ |
|
60
|
"models": []map[string]any{ |
|
61
|
{"name": "model1"}, |
|
62
|
{"name": "model2"}, |
|
63
|
}, |
|
64
|
} |
|
65
|
_ = json.NewEncoder(w).Encode(resp) |
|
66
|
})) |
|
67
|
defer srv.Close() |
|
68
|
|
|
69
|
p := newOllamaProvider(BackendConfig{ |
|
70
|
Backend: "ollama", |
|
71
|
}, srv.URL, srv.Client()) |
|
72
|
|
|
73
|
models, err := p.DiscoverModels(context.Background()) |
|
74
|
if err != nil { |
|
75
|
t.Fatalf("DiscoverModels failed: %v", err) |
|
76
|
} |
|
77
|
|
|
78
|
if len(models) != 2 { |
|
79
|
t.Errorf("got %d models, want 2", len(models)) |
|
80
|
} |
|
81
|
} |
|
82
|
|