-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
279 lines (238 loc) · 6.89 KB
/
client.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package bellman
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/modfin/bellman/models/embed"
"github.com/modfin/bellman/models/gen"
"github.com/modfin/bellman/prompt"
"github.com/modfin/bellman/tools"
"io"
"log/slog"
"net/http"
"net/url"
"sync/atomic"
)
const Provider = "Bellman"
type Bellman struct {
Log *slog.Logger `json:"-"`
url string
key Key
}
func (g *Bellman) Provider() string {
return Provider
}
type Key struct {
Name string
Token string
}
func (l Key) String() string {
return l.Name + "_" + l.Token
}
func New(url string, key Key) *Bellman {
return &Bellman{
url: url,
key: key,
}
}
func (g *Bellman) log(msg string, args ...any) {
if g.Log == nil {
return
}
g.Log.Debug("[bellman/bellman] "+msg, args...)
}
var bellmanRequestNo int64
func (v *Bellman) EmbedModels() ([]embed.Model, error) {
u, err := url.JoinPath(v.url, "embed", "models")
if err != nil {
return nil, fmt.Errorf("could not join url %s; %w", v.url, err)
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, fmt.Errorf("could not create bellman request; %w", err)
}
req.Header.Set("Authorization", "Bearer "+v.key.String())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not post bellman request to %s; %w", u, err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read bellman response; %w", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d; %s", res.StatusCode, string(body))
}
var models []embed.Model
err = json.Unmarshal(body, &models)
if err != nil {
return nil, fmt.Errorf("could not unmarshal bellman response; %w", err)
}
return models, nil
}
func (v *Bellman) GenModels() ([]gen.Model, error) {
u, err := url.JoinPath(v.url, "gen", "models")
if err != nil {
return nil, fmt.Errorf("could not join url %s; %w", v.url, err)
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, fmt.Errorf("could not create bellman request; %w", err)
}
req.Header.Set("Authorization", "Bearer "+v.key.String())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not post bellman request to %s; %w", u, err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read bellman response; %w", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d; %s", res.StatusCode, string(body))
}
var models []gen.Model
err = json.Unmarshal(body, &models)
if err != nil {
return nil, fmt.Errorf("could not unmarshal bellman response; %w", err)
}
return models, nil
}
func (v *Bellman) Embed(request embed.Request) (*embed.Response, error) {
var reqc = atomic.AddInt64(&bellmanRequestNo, 1)
u, err := url.JoinPath(v.url, "embed")
if err != nil {
return nil, fmt.Errorf("could not join url %s; %w", v.url, err)
}
body, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("could not marshal bellman request; %w", err)
}
ctx := request.Ctx
if ctx == nil {
ctx = context.Background()
}
req, err := http.NewRequestWithContext(ctx, "POST", u, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("could not create bellman request; %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+v.key.String())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not post bellman request to %s; %w", u, err)
}
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read bellman response; %w", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d; %s", res.StatusCode, string(body))
}
var response embed.Response
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("could not unmarshal bellman response; %w", err)
}
v.log("[embed] response", "request", reqc, "model", request.Model.FQN(), "token-total", response.Metadata.TotalTokens)
return &response, nil
}
func (a *Bellman) Generator(options ...gen.Option) *gen.Generator {
var gen = &gen.Generator{
Prompter: &generator{
bellman: a,
},
Request: gen.Request{},
}
for _, op := range options {
gen = op(gen)
}
return gen
}
func (g *Bellman) SetLogger(logger *slog.Logger) *Bellman {
g.Log = logger
return g
}
type generator struct {
bellman *Bellman
request gen.Request
}
func (g *generator) SetRequest(request gen.Request) {
g.request = request
}
func (g *generator) Prompt(conversation ...prompt.Prompt) (*gen.Response, error) {
var reqc = atomic.AddInt64(&bellmanRequestNo, 1)
u, err := url.JoinPath(g.bellman.url, "gen")
if err != nil {
return nil, fmt.Errorf("could not join url %s; %w", g.bellman.url, err)
}
request := gen.FullRequest{
Request: g.request,
Prompts: conversation,
}
toolBelt := map[string]*tools.Tool{}
for _, tool := range g.request.Tools {
toolBelt[tool.Name] = &tool
}
g.bellman.log("[gen] request",
"request", reqc,
"model", g.request.Model.FQN(),
"tools", len(g.request.Tools) > 0,
"tool_choice", g.request.ToolConfig != nil,
"output_schema", g.request.OutputSchema != nil,
"system_prompt", g.request.SystemPrompt != "",
"temperature", g.request.Temperature,
"top_p", g.request.TopP,
"max_tokens", g.request.MaxTokens,
"stop_sequences", g.request.StopSequences,
)
body, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("could not marshal bellman request; %w", err)
}
ctx := g.request.Context
if ctx == nil {
ctx = context.Background()
}
req, err := http.NewRequestWithContext(ctx, "POST", u, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("could not create bellman request; %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+g.bellman.key.String())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not post bellman request to %s; %w", u, err)
}
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("could not read bellman response; %w", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d; %s", res.StatusCode, string(body))
}
response := gen.Response{}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("could not unmarshal bellman response; %w", err)
}
g.bellman.log("[gen] response",
"request", reqc,
"model", g.request.Model.FQN(),
"token-input", response.Metadata.InputTokens,
"token-output", response.Metadata.OutputTokens,
"token-total", response.Metadata.TotalTokens,
)
// adding reference to tools
for i, _ := range response.Tools {
tool := response.Tools[i]
tool.Ref = toolBelt[tool.Name]
response.Tools[i] = tool
}
return &response, nil
}