-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.go
645 lines (558 loc) · 17.9 KB
/
openai.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//nolint:tagliatelle
package fun
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"sync"
"time"
"github.com/rs/zerolog"
"github.com/santhosh-tekuri/jsonschema/v6"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/x"
"gitlab.com/tozd/identifier"
)
//nolint:mnd
var openAIModels = map[string]struct { //nolint:gochecknoglobals
MaxContextLength int
MaxResponseLength int
}{
"gpt-4o-2024-11-20": {
MaxContextLength: 128_000,
MaxResponseLength: 16_384,
},
"gpt-4o-2024-08-06": {
MaxContextLength: 128_000,
MaxResponseLength: 16_384,
},
"gpt-4o-2024-05-13": {
MaxContextLength: 128_000,
MaxResponseLength: 4_096,
},
"gpt-4o-mini-2024-07-18": {
MaxContextLength: 128_000,
MaxResponseLength: 16_384,
},
"o1-preview-2024-09-12": {
MaxContextLength: 128_000,
MaxResponseLength: 32_768,
},
"o1-mini-2024-09-12": {
MaxContextLength: 128_000,
MaxResponseLength: 65_536,
},
"gpt-4-turbo-2024-04-09": {
MaxContextLength: 128_000,
MaxResponseLength: 4_096,
},
}
var openAIRateLimiter = keyedRateLimiter{ //nolint:gochecknoglobals
mu: sync.RWMutex{},
limiters: map[string]map[string]any{},
}
type openAIJSONSchema struct {
Description string `json:"description,omitempty"`
Name string `json:"name"`
Schema json.RawMessage `json:"schema"`
Strict bool `json:"strict"`
}
type openAIResponseFormat struct {
Type string `json:"type"`
JSONSchema openAIJSONSchema `json:"json_schema"`
}
// TODO: How can we make parameters optional?
// See: https://gitlab.com/tozd/go/fun/-/issues/3
type openAIFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputJSONSchema json.RawMessage `json:"parameters"`
Strict bool `json:"strict"`
}
type openAITool struct {
Type string `json:"type"`
Function openAIFunction `json:"function"`
tool TextTooler
}
type openAIRequest struct {
Messages []openAIMessage `json:"messages"`
Model string `json:"model"`
Seed int `json:"seed"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
ResponseFormat *openAIResponseFormat `json:"response_format,omitempty"`
Tools []openAITool `json:"tools,omitempty"`
}
type openAIToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
}
type openAIMessage struct {
Role string `json:"role"`
Content *string `json:"content,omitempty"`
Refusal *string `json:"refusal,omitempty"`
ToolCalls []openAIToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
type openAIResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
ServiceTier *string `json:"service_tier,omitempty"`
Choices []struct {
Index int `json:"index"`
Message openAIMessage `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
Error *struct {
Message string `json:"message"`
Type string `json:"type"`
Code *string `json:"code,omitempty"`
Param *string `json:"param,omitempty"`
} `json:"error,omitempty"`
}
var _ TextProvider = (*OpenAITextProvider)(nil)
// OpenAITextProvider is a [TextProvider] which provides integration with
// text-based [OpenAI] AI models.
//
// [OpenAI]: https://openai.com/
type OpenAITextProvider struct {
// Client is a HTTP client to be used for API calls. If not provided
// a rate-limited retryable HTTP client is initialized instead.
Client *http.Client `json:"-"`
// APIKey is the API key to be used for API calls.
APIKey string `json:"-"`
// Model is the name of the model to be used.
Model string `json:"model"`
// MaxContextLength is the maximum total number of tokens allowed to be used
// with the underlying AI model (i.e., the maximum context window).
// If not provided, heuristics are used to determine it automatically.
MaxContextLength int `json:"maxContextLength"`
// MaxResponseLength is the maximum number of tokens allowed to be used in
// a response with the underlying AI model. If not provided, heuristics
// are used to determine it automatically.
MaxResponseLength int `json:"maxResponseLength"`
// See: https://github.com/invopop/jsonschema/issues/148
// ForceOutputJSONSchema when set to true requests the AI model to force
// the output JSON Schema for its output. When true, consider using
// meaningful property names and use "description" JSON Schema field to
// describe to the AI model what each property is. When true, the JSON
// Schema must have "title" field to name the JSON Schema and consider
// using "description" field to describe the JSON Schema itself.
//
// There are currently limitations on the JSON Schema imposed by OpenAI,
// so JSON Schema automatically determined from the Output type fails,
// e.g., only "object" top-level type can be used, all properties must
// be required, "additionalProperties" must be set to false, top-level
// $ref is not supported. This further means that only structs can be
// used as Output types.
ForceOutputJSONSchema bool `json:"forceOutputJsonSchema"`
// Seed is used to control the randomness of the AI model. Default is 0.
Seed int `json:"seed"`
// Temperature is how creative should the AI model be.
// Default is 0 which means not at all.
Temperature float64 `json:"temperature"`
rateLimiterKey string
messages []openAIMessage
tools []openAITool
outputJSONSchema json.RawMessage
outputJSONSchemaName string
outputJSONSchemaDescription string
}
func (o OpenAITextProvider) MarshalJSON() ([]byte, error) {
// We define a new type to not recurse into this same MarshalJSON.
type P OpenAITextProvider
t := struct {
Type string `json:"type"`
P
}{
Type: "openai",
P: P(o),
}
return x.MarshalWithoutEscapeHTML(t)
}
// Init implements [TextProvider] interface.
func (o *OpenAITextProvider) Init(_ context.Context, messages []ChatMessage) errors.E {
if o.messages != nil {
return errors.WithStack(ErrAlreadyInitialized)
}
o.messages = []openAIMessage{}
for _, message := range messages {
o.messages = append(o.messages, openAIMessage{
Role: message.Role,
Content: &message.Content,
Refusal: nil,
ToolCalls: nil,
ToolCallID: "",
})
}
o.rateLimiterKey = fmt.Sprintf("%s-%s", o.APIKey, o.Model)
if o.Client == nil {
o.Client = newClient(
func(req *http.Request) error {
// Rate limit retries.
return openAIRateLimiter.Take(req.Context(), o.rateLimiterKey, map[string]int{
"rpm": 1,
"tpm": o.MaxContextLength, // TODO: Can we provide a better estimate?
})
},
parseRateLimitHeaders,
func(limitRequests, limitTokens, remainingRequests, remainingTokens int, resetRequests, resetTokens time.Time) {
openAIRateLimiter.Set(o.rateLimiterKey, map[string]any{
"rpm": resettingRateLimit{
Limit: limitRequests,
Remaining: remainingRequests,
Window: time.Minute,
Resets: resetRequests,
},
"tpm": resettingRateLimit{
Limit: limitTokens,
Remaining: remainingTokens,
Window: time.Minute,
Resets: resetTokens,
},
})
},
)
}
if o.MaxContextLength == 0 {
o.MaxContextLength = openAIModels[o.Model].MaxContextLength
}
if o.MaxContextLength == 0 {
return errors.New("MaxContextLength not set")
}
if o.MaxResponseLength == 0 {
o.MaxResponseLength = openAIModels[o.Model].MaxResponseLength
}
if o.MaxResponseLength == 0 {
return errors.New("MaxResponseLength not set")
}
return nil
}
// Chat implements [TextProvider] interface.
func (o *OpenAITextProvider) Chat(ctx context.Context, message ChatMessage) (string, errors.E) { //nolint:maintidx
callID := identifier.New().String()
var callRecorder *TextRecorderCall
if recorder := GetTextRecorder(ctx); recorder != nil {
callRecorder = recorder.newCall(callID, o)
defer recorder.recordCall(callRecorder)
}
logger := zerolog.Ctx(ctx).With().Str("fun", callID).Logger()
ctx = logger.WithContext(ctx)
messages := slices.Clone(o.messages)
messages = append(messages, openAIMessage{
Role: message.Role,
Content: &message.Content,
Refusal: nil,
ToolCalls: nil,
ToolCallID: "",
})
if callRecorder != nil {
for _, message := range messages {
o.recordMessage(callRecorder, message)
}
callRecorder.notify("", nil)
}
for {
oReq := openAIRequest{
Messages: messages,
Model: o.Model,
Seed: o.Seed,
Temperature: o.Temperature,
MaxTokens: o.MaxResponseLength, // TODO: Can we provide a better estimate?
ResponseFormat: nil,
Tools: o.tools,
}
if o.outputJSONSchema != nil {
oReq.ResponseFormat = &openAIResponseFormat{
Type: "json_schema",
JSONSchema: openAIJSONSchema{
Description: o.outputJSONSchemaDescription,
Name: o.outputJSONSchemaName,
Schema: o.outputJSONSchema,
Strict: true,
},
}
}
request, errE := x.MarshalWithoutEscapeHTML(oReq)
if errE != nil {
return "", errE
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.openai.com/v1/chat/completions", bytes.NewReader(request))
if err != nil {
return "", errors.WithStack(err)
}
req.Header.Add("Authorization", "Bearer "+o.APIKey)
req.Header.Add("Content-Type", "application/json")
// Rate limit the initial request.
errE = openAIRateLimiter.Take(ctx, o.rateLimiterKey, map[string]int{
"rpm": 1,
"tpm": o.MaxContextLength, // TODO: Can we provide a better estimate?
})
if errE != nil {
return "", errE
}
start := time.Now()
resp, err := o.Client.Do(req)
var apiRequest string
if resp != nil {
apiRequest = resp.Header.Get("X-Request-Id")
}
if err != nil {
errE = errors.Prefix(err, ErrAPIRequestFailed)
if apiRequest != "" {
errors.Details(errE)["apiRequest"] = apiRequest
}
return "", errE
}
defer resp.Body.Close()
defer io.Copy(io.Discard, resp.Body) //nolint:errcheck
if apiRequest == "" {
return "", errors.WithStack(ErrMissingRequestID)
}
var response openAIResponse
errE = x.DecodeJSON(resp.Body, &response)
if errE != nil {
errors.Details(errE)["apiRequest"] = apiRequest
return "", errE
}
apiCallDuration := time.Since(start)
if response.Error != nil {
return "", errors.WithDetails(
ErrAPIResponseError,
"body", response.Error,
"apiRequest", apiRequest,
)
}
if len(response.Choices) != 1 {
return "", errors.WithDetails(
ErrUnexpectedNumberOfMessages,
"number", len(response.Choices),
"apiRequest", apiRequest,
)
}
if callRecorder != nil {
callRecorder.addUsedTokens(
apiRequest,
o.MaxContextLength,
o.MaxResponseLength,
response.Usage.PromptTokens,
response.Usage.CompletionTokens,
nil,
nil,
)
callRecorder.addUsedTime(
apiRequest,
0,
0,
apiCallDuration,
)
o.recordMessage(callRecorder, response.Choices[0].Message)
callRecorder.notify("", nil)
}
if response.Usage.TotalTokens >= o.MaxContextLength {
return "", errors.WithDetails(
ErrUnexpectedNumberOfTokens,
"content", *response.Choices[0].Message.Content,
"prompt", response.Usage.PromptTokens,
"response", response.Usage.CompletionTokens,
"total", response.Usage.TotalTokens,
"maxTotal", o.MaxContextLength,
"maxResponse", o.MaxResponseLength,
"apiRequest", apiRequest,
)
}
if response.Choices[0].Message.Role != roleAssistant {
return "", errors.WithDetails(
ErrUnexpectedRole,
"role", response.Choices[0].Message.Role,
"apiRequest", apiRequest,
)
}
if response.Choices[0].FinishReason == "tool_calls" {
if len(response.Choices[0].Message.ToolCalls) == 0 {
return "", errors.WithDetails(
ErrUnexpectedNumberOfMessages,
"number", len(response.Choices[0].Message.ToolCalls),
"apiRequest", apiRequest,
)
}
// We have already recorded this message above.
messages = append(messages, response.Choices[0].Message)
// We make space for tool results (one per tool call) so that the messages slice
// does not grow when appending below and invalidate pointers goroutines keep.
messages = slices.Grow(messages, len(response.Choices[0].Message.ToolCalls))
if callRecorder != nil {
// We grow the slice inside call recorder as well.
callRecorder.prepareForToolMessages(len(response.Choices[0].Message.ToolCalls))
}
var wg sync.WaitGroup
for _, toolCall := range response.Choices[0].Message.ToolCalls {
messages = append(messages, openAIMessage{
Role: roleTool,
Content: nil,
Refusal: nil,
ToolCalls: nil,
ToolCallID: toolCall.ID,
})
result := &messages[len(messages)-1]
toolCtx := ctx
var toolMessage *TextRecorderMessage
if callRecorder != nil {
toolCtx, toolMessage = callRecorder.startToolMessage(ctx, toolCall.ID)
}
wg.Add(1)
go func() {
defer wg.Done()
o.callToolWrapper(toolCtx, apiRequest, toolCall, result, callRecorder, toolMessage)
}()
}
wg.Wait()
continue
}
if response.Choices[0].FinishReason != stopReason {
return "", errors.WithDetails(
ErrUnexpectedStop,
"reason", response.Choices[0].FinishReason,
"apiRequest", apiRequest,
)
}
if response.Choices[0].Message.Refusal != nil {
return "", errors.WithDetails(
ErrRefused,
"refusal", *response.Choices[0].Message.Refusal,
"apiRequest", apiRequest,
)
}
if response.Choices[0].Message.Content == nil {
return "", errors.WithDetails(
ErrUnexpectedMessageType,
"apiRequest", apiRequest,
)
}
return *response.Choices[0].Message.Content, nil
}
}
// InitOutputJSONSchema implements [WithOutputJSONSchema] interface.
func (o *OpenAITextProvider) InitOutputJSONSchema(_ context.Context, schema []byte) errors.E {
if !o.ForceOutputJSONSchema {
return nil
}
if schema == nil {
return errors.Errorf(`%w: output JSON Schema is missing`, ErrInvalidJSONSchema)
}
if o.outputJSONSchema != nil {
return errors.WithStack(ErrAlreadyInitialized)
}
o.outputJSONSchema = schema
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(schema))
if err != nil {
return errors.WithStack(err)
}
o.outputJSONSchemaName = getString(s, "title")
o.outputJSONSchemaDescription = getString(s, "description")
if o.outputJSONSchemaName == "" {
return errors.Errorf(`%w: JSON Schema is missing "title" field which is used for required JSON Schema "name" for OpenAI API`, ErrInvalidJSONSchema)
}
return nil
}
// InitTools implements [WithTools] interface.
func (o *OpenAITextProvider) InitTools(ctx context.Context, tools map[string]TextTooler) errors.E {
if o.tools != nil {
return errors.WithStack(ErrAlreadyInitialized)
}
o.tools = []openAITool{}
for name, tool := range tools {
errE := tool.Init(ctx)
if errE != nil {
errors.Details(errE)["name"] = name
return errE
}
o.tools = append(o.tools, openAITool{
Type: "function",
Function: openAIFunction{
Name: name,
Description: tool.GetDescription(),
InputJSONSchema: tool.GetInputJSONSchema(),
Strict: true,
},
tool: tool,
})
}
return nil
}
func (o *OpenAITextProvider) callToolWrapper( //nolint:dupl
ctx context.Context, apiRequest string, toolCall openAIToolCall, result *openAIMessage, callRecorder *TextRecorderCall, toolMessage *TextRecorderMessage,
) {
if callRecorder != nil {
defer func() {
callRecorder.notify("", nil)
}()
}
defer func() {
if err := recover(); err != nil {
content := fmt.Sprintf("Error: %s", err)
result.Content = &content
toolMessage.setContent(content, true)
}
}()
defer func() {
toolMessage.setToolCalls(GetTextRecorder(ctx).Calls())
}()
logger := zerolog.Ctx(ctx).With().Str("tool", toolCall.ID).Logger()
ctx = logger.WithContext(ctx)
output, duration, errE := o.callTool(ctx, toolCall)
if errE != nil {
zerolog.Ctx(ctx).Warn().Err(errE).Str("name", toolCall.Function.Name).Str("apiRequest", apiRequest).
Str("tool", toolCall.ID).RawJSON("input", json.RawMessage(toolCall.Function.Arguments)).Msg("tool error")
content := "Error: " + errE.Error()
result.Content = &content
toolMessage.setContent(content, true)
} else {
result.Content = &output
toolMessage.setContent(output, false)
}
toolMessage.setToolDuration(duration)
}
func (o *OpenAITextProvider) callTool(ctx context.Context, toolCall openAIToolCall) (string, Duration, errors.E) {
var tool TextTooler
for _, t := range o.tools {
if t.Function.Name == toolCall.Function.Name {
tool = t.tool
break
}
}
if tool == nil {
return "", 0, errors.Errorf("%w: %s", ErrToolNotFound, toolCall.Function.Name)
}
start := time.Now()
output, errE := tool.Call(ctx, json.RawMessage(toolCall.Function.Arguments))
duration := time.Since(start)
return output, Duration(duration), errE
}
func (o *OpenAITextProvider) recordMessage(recorder *TextRecorderCall, message openAIMessage) {
if message.Role == roleTool {
panic(errors.New("recording tool result message should not happen"))
} else if message.Content != nil {
recorder.addMessage(message.Role, *message.Content, "", "", false)
} else if message.Refusal != nil {
recorder.addMessage(message.Role, *message.Refusal, "", "", true)
}
for _, tool := range message.ToolCalls {
recorder.addMessage(roleToolUse, tool.Function.Arguments, tool.ID, tool.Function.Name, false)
}
}