aboutsummaryrefslogtreecommitdiff
path: root/web/ollama/ollama_test.go
blob: e3110091239e54f10fb786372e856f1f30f0f52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ollama

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
)

func mkFakeStreamedOllamaChat(response string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		enc := json.NewEncoder(w)
		enc.Encode(CompleteResponse{
			Model: "fake/for:testing",
			Message: Message{
				Content: response,
			},
		})

		enc.Encode(CompleteResponse{
			Done: true,
		})
	}
}

func mkBetterFakeStreamedOllamaChat(responses []string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		enc := json.NewEncoder(w)
		for _, resp := range responses {
			enc.Encode(CompleteResponse{
				Model: "fake/for:testing",
				Message: Message{
					Content: resp,
				},
			})
		}

		enc.Encode(CompleteResponse{
			Done: true,
		})
	}
}

type fakeMessage struct {
	Content string `json:"content"`
}

func (fm fakeMessage) Valid() error {
	if fm.Content != "hello, world" {
		return fmt.Errorf("expected %q, got %q", "hello, world", fm.Content)
	}

	return nil
}

func TestHallucinateSimple(t *testing.T) {
	srv := httptest.NewServer(mkFakeStreamedOllamaChat(`{"content": "hello, world"}`))
	defer srv.Close()

	c := NewClient(srv.URL)

	resp, err := Hallucinate[fakeMessage](context.Background(), c, HallucinateOpts{
		Model: "fake/for:testing",
		Messages: []Message{
			{
				Content: "hello",
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	}

	if resp.Content != "hello, world" {
		t.Fatalf("expected %q, got %q", "hello, world", resp.Content)
	}
}

func TestHallucinateMultiple(t *testing.T) {
	srv := httptest.NewServer(mkBetterFakeStreamedOllamaChat([]string{
		`{`,
		`"`,
		`content`,
		`"`,
		`:`,
		`"`,
		`hello`,
		`, `,
		`world`,
		`"`,
		`}`,
	}))
	defer srv.Close()

	c := NewClient(srv.URL)

	resp, err := Hallucinate[fakeMessage](context.Background(), c, HallucinateOpts{
		Model: "fake/for:testing",
		Messages: []Message{
			{
				Content: "hello",
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	}

	if resp.Content != "hello, world" {
		t.Fatalf("expected %q, got %q", "hello, world", resp.Content)
	}
}