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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
package ollama
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"time"
"within.website/x/valid"
"within.website/x/web"
)
type Client struct {
baseURL string
}
func NewClient(baseURL string) *Client {
return &Client{
baseURL: baseURL,
}
}
func NewLocalClient() *Client {
return NewClient("http://localhost:11434")
}
type Message struct {
Content string `json:"content"`
Role string `json:"role"`
Images [][]byte `json:"images"`
}
type CompleteRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Format *string `json:"format,omitempty"`
Template *string `json:"template,omitempty"`
Stream bool `json:"stream"`
Options map[string]any `json:"options"`
KeepAlive time.Duration `json:"keep_alive"`
}
type CompleteResponse struct {
Model string `json:"model"`
CreatedAt time.Time `json:"created_at"`
Message Message `json:"message"`
Done bool `json:"done"`
TotalDuration float64 `json:"total_duration"`
LoadDuration float64 `json:"load_duration"`
PromptEvalCount int64 `json:"prompt_eval_count"`
PromptEvalDuration int64 `json:"prompt_eval_duration"`
EvalCount int64 `json:"eval_count"`
EvalDuration int64 `json:"eval_duration"`
}
func (c *Client) Chat(ctx context.Context, inp *CompleteRequest) (*CompleteResponse, error) {
inp.KeepAlive = 24 * time.Hour
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(inp); err != nil {
return nil, fmt.Errorf("ollama: error encoding request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/chat", buf)
if err != nil {
return nil, fmt.Errorf("ollama: error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("ollama: error making request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
var result CompleteResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("ollama: error decoding response: %w", err)
}
return &result, nil
}
// HallucinateOpts contains the options for the Hallucinate function.
type HallucinateOpts struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
}
func p[T any](v T) *T {
return &v
}
// Hallucinate prompts the model to hallucinate a "valid" JSON response to the given input.
func Hallucinate[T valid.Interface](ctx context.Context, c *Client, opts HallucinateOpts) (*T, error) {
inp := &CompleteRequest{
Model: opts.Model,
Messages: opts.Messages,
KeepAlive: 24 * time.Hour,
Format: p("json"),
Stream: true,
}
tries := 0
for tries <= 5 {
tries++
ctx, cancel := context.WithCancel(ctx)
defer cancel()
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(inp); err != nil {
return nil, fmt.Errorf("ollama: error encoding request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/chat", buf)
if err != nil {
return nil, fmt.Errorf("ollama: error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("ollama: error making request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
whitespaceCount := 0
dec := json.NewDecoder(resp.Body)
buf = bytes.NewBuffer(nil)
for {
var cr CompleteResponse
err := dec.Decode(&cr)
if err != nil {
if !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("ollama: error decoding response: %w", err)
} else {
break
}
}
//slog.Debug("got response", "response", cr.Message.Content)
if _, err := fmt.Fprint(buf, cr.Message.Content); err != nil {
return nil, fmt.Errorf("ollama: error writing response to buffer: %w", err)
}
for _, r := range cr.Message.Content {
if r == '\n' {
whitespaceCount++
}
}
if whitespaceCount > 10 {
cancel()
}
//slog.Debug("buffer is now", "buffer", buf.String())
var result T
if err := json.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&result); err != nil {
//slog.Debug("error decoding response", "err", err)
continue
}
if err := result.Valid(); err != nil {
slog.Debug("error validating response", "err", err)
continue
}
//slog.Debug("got valid response", "response", result)
cancel()
return &result, nil
}
}
return nil, fmt.Errorf("ollama: failed to hallucinate a valid response after 5 tries")
}
|