-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchat_complete.go
More file actions
595 lines (509 loc) · 18.3 KB
/
Copy pathchat_complete.go
File metadata and controls
595 lines (509 loc) · 18.3 KB
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
package gai
import (
"context"
"encoding/json"
"fmt"
"iter"
"github.com/invopop/jsonschema"
)
// ThinkingLevel controls how much reasoning effort the model applies.
// Type-only abstraction: the universal off value [ThinkingLevelNone] is defined here, and
// every other value is published per client because each provider speaks its own vocabulary.
// See [maragu.dev/gai/clients/openai], [maragu.dev/gai/clients/google], and
// [maragu.dev/gai/clients/anthropic] for the constants their target APIs accept; passing a
// level a given client does not recognise will panic at the client boundary.
type ThinkingLevel string
// ThinkingLevelNone disables thinking entirely. This is the only value defined in core because it
// is the only one with a universal semantic — every provider has some way to opt out of thinking.
// All other levels are published per client.
const ThinkingLevelNone ThinkingLevel = "none"
// Temperature controls the randomness of model sampling, where lower values are more
// deterministic and higher values more varied. The accepted range and exact behaviour
// are provider-specific; see [maragu.dev/gai/clients/openai], [maragu.dev/gai/clients/google],
// and [maragu.dev/gai/clients/anthropic] for how each forwards the value.
type Temperature float64
// String satisfies [fmt.Stringer].
func (t Temperature) String() string {
return fmt.Sprintf("%.2f", t)
}
// Float64 returns the temperature as a plain float64, for use with APIs that take a float64.
func (t Temperature) Float64() float64 {
return float64(t)
}
// ChatCompleteRequest for a chat model.
type ChatCompleteRequest struct {
MaxCompletionTokens *int
Messages []Message
ResponseSchema *Schema
System *string
Temperature *Temperature
ThinkingLevel *ThinkingLevel
ToolChoice ToolChoice
Tools []Tool
}
// ToolChoiceMode constrains how the model decides whether to call a tool.
// The zero value of [ChatCompleteRequest.ToolChoice] preserves each provider's default
// behaviour, which is equivalent to [ToolChoiceModeAuto].
type ToolChoiceMode string
const (
// ToolChoiceModeAuto lets the model decide whether to call a tool. This is the default.
ToolChoiceModeAuto ToolChoiceMode = "auto"
// ToolChoiceModeAny forces the model to call one of the provided tools, of its choosing.
ToolChoiceModeAny ToolChoiceMode = "any"
// ToolChoiceModeTool forces the model to call the specific tool named in [ToolChoice.Name].
ToolChoiceModeTool ToolChoiceMode = "tool"
)
// ToolChoice constrains the model's tool-calling behaviour for a [ChatCompleteRequest].
// All three clients translate it equivalently: [ToolChoiceModeAuto] leaves the choice to the
// model, [ToolChoiceModeAny] forces some tool call, and [ToolChoiceModeTool] forces a call to
// the tool named in [ToolChoice.Name]. The zero value preserves each provider's default
// behaviour, equivalent to [ToolChoiceModeAuto]. Validate with [ToolChoice.Validate].
type ToolChoice struct {
Mode ToolChoiceMode
// Name is the tool to force, required when Mode is [ToolChoiceModeTool] and rejected otherwise.
Name string
}
// Validate checks the ToolChoice against the request's tools and reports the first problem,
// or nil if the choice is well-formed. The zero value validates trivially as auto, so that
// clients can call it unconditionally before forwarding a request. The rules are:
//
// - [ToolChoiceModeTool] requires a non-empty Name that matches one of tools.
// - The zero Mode, [ToolChoiceModeAuto], and [ToolChoiceModeAny] reject a non-empty Name.
// - Any other Mode value is rejected.
//
// Bad input is caller data rather than a programming error, so violations are returned as
// errors instead of panicking — unlike unrecognised library constants such as [ThinkingLevel].
func (tc ToolChoice) Validate(tools []Tool) error {
switch tc.Mode {
case "", ToolChoiceModeAuto, ToolChoiceModeAny:
if tc.Name != "" {
return fmt.Errorf("tool choice name %q is only valid with mode %q", tc.Name, ToolChoiceModeTool)
}
return nil
case ToolChoiceModeTool:
if tc.Name == "" {
return fmt.Errorf("tool choice mode %q requires a tool name", ToolChoiceModeTool)
}
for _, tool := range tools {
if tool.Name == tc.Name {
return nil
}
}
return fmt.Errorf("tool choice name %q does not match any provided tool", tc.Name)
default:
return fmt.Errorf("unknown tool choice mode %q", tc.Mode)
}
}
type Message struct {
Role MessageRole
Parts []Part
}
// NewUserTextMessage is a convenience function to create a new user text message.
func NewUserTextMessage(text string) Message {
return Message{
Role: MessageRoleUser,
Parts: []Part{
TextPart(text),
},
}
}
// NewUserDataMessage is a convenience function to create a new user data message.
func NewUserDataMessage(mimeType string, data []byte) Message {
return Message{
Role: MessageRoleUser,
Parts: []Part{
DataPart(mimeType, data),
},
}
}
// NewModelTextMessage is a convenience function to create a new model text message.
func NewModelTextMessage(text string) Message {
return Message{
Role: MessageRoleModel,
Parts: []Part{
TextPart(text),
},
}
}
func NewUserToolResultMessage(result ToolResult) Message {
return Message{
Role: MessageRoleUser,
Parts: []Part{
{
Type: PartTypeToolResult,
toolResult: &result,
},
},
}
}
// MessageRole for [Message].
type MessageRole string
const (
MessageRoleUser MessageRole = "user"
MessageRoleModel MessageRole = "model"
)
// Part is a single piece of content, such as text, data, a tool call, or a tool result.
// Used in both [Message] and [EmbedRequest].
//
// Data is stored as a byte slice rather than an io.Reader because all known provider
// SDKs (Google genai, OpenAI, Anthropic) require the full data in memory, either as
// []byte or base64-encoded string. A streaming reader would be consumed on first use,
// making Parts single-use and breaking message replay, multi-turn conversations, and
// multi-scorer evaluations. See https://github.com/maragudk/gai/issues/169.
type Part struct {
Type PartType
Data []byte
MIMEType string
text *string
toolCall *ToolCall
toolResult *ToolResult
}
// MarshalText satisfies [encoding.TextMarshaler].
func (m Part) MarshalText() ([]byte, error) {
switch m.Type {
case PartTypeText:
return []byte(m.Text()), nil
case PartTypeThought:
return []byte("[thought: " + m.Thought() + "]"), nil
case PartTypeData:
return []byte(fmt.Sprintf("[data: %v, %v bytes]", m.MIMEType, len(m.Data))), nil
case PartTypeToolCall:
return []byte("[tool_call: " + m.toolCall.Name + "]"), nil
case PartTypeToolResult:
return []byte("[tool_result: " + m.toolResult.Name + "]"), nil
default:
return []byte("[unknown part type]"), nil
}
}
// Text returns the text content. Panics if the part is not [PartTypeText].
func (m Part) Text() string {
if m.Type != PartTypeText {
panic("not text type")
}
if m.text == nil {
panic("text not set")
}
return *m.text
}
// Thought returns the thought content. Panics if the part is not [PartTypeThought].
func (m Part) Thought() string {
if m.Type != PartTypeThought {
panic("not thought type")
}
if m.text == nil {
panic("thought not set")
}
return *m.text
}
// ToolCall returns the tool call. Panics if the part is not [PartTypeToolCall].
func (m Part) ToolCall() ToolCall {
if m.Type != PartTypeToolCall {
panic("not tool call type")
}
return *m.toolCall
}
// ToolResult returns the tool result. Panics if the part is not [PartTypeToolResult].
func (m Part) ToolResult() ToolResult {
if m.Type != PartTypeToolResult {
panic("not tool result type")
}
return *m.toolResult
}
// PartType for [Part].
type PartType string
const (
PartTypeData PartType = "data"
PartTypeText PartType = "text"
// PartTypeThought is a streamed thinking/reasoning part. Providers vary in whether they
// expose the model's chain-of-thought as text — Google Gemini emits Thought parts when
// thinking is enabled, Anthropic surfaces thinking blocks via the streaming API, and
// OpenAI Chat Completions does not stream reasoning text and so never produces this type.
PartTypeThought PartType = "thought"
PartTypeToolCall PartType = "tool_call"
PartTypeToolResult PartType = "tool_result"
)
// Deprecated: Use [Part] instead.
type MessagePart = Part
// Deprecated: Use [PartType] instead.
type MessagePartType = PartType
const (
// Deprecated: Use [PartTypeData] instead.
MessagePartTypeData = PartTypeData
// Deprecated: Use [PartTypeText] instead.
MessagePartTypeText = PartTypeText
// Deprecated: Use [PartTypeToolCall] instead.
MessagePartTypeToolCall = PartTypeToolCall
// Deprecated: Use [PartTypeToolResult] instead.
MessagePartTypeToolResult = PartTypeToolResult
)
// Deprecated: Use [TextPart] instead.
func TextMessagePart(text string) Part { return TextPart(text) }
// Deprecated: Use [DataPart] instead.
func DataMessagePart(mimeType string, data []byte) Part { return DataPart(mimeType, data) }
// TextPart creates a text [Part].
func TextPart(text string) Part {
return Part{
Type: PartTypeText,
text: &text,
}
}
// ThoughtPart creates a thought [Part] carrying streamed model reasoning text.
// See [PartTypeThought] for which providers actually emit these.
func ThoughtPart(text string) Part {
return Part{
Type: PartTypeThought,
text: &text,
}
}
// DataPart creates a data [Part] with the given MIME type and content.
// Data is stored as a byte slice for safe reuse across multiple reads.
// The caller must not mutate the slice after passing it.
// See https://github.com/maragudk/gai/issues/169.
// Panics if mimeType is empty or data is empty.
func DataPart(mimeType string, data []byte) Part {
if mimeType == "" {
panic("MIME type must not be empty")
}
if len(data) == 0 {
panic("data must not be empty")
}
return Part{
Type: PartTypeData,
Data: data,
MIMEType: mimeType,
}
}
// ToolCallPart creates a tool call [Part].
func ToolCallPart(id, name string, args json.RawMessage) Part {
return Part{
Type: PartTypeToolCall,
toolCall: &ToolCall{
ID: id,
Name: name,
Args: args,
},
}
}
type ChatCompleteResponseUsage struct {
PromptTokens int
ThoughtsTokens int
CompletionTokens int
}
// ChatCompleteFinishReason describes why the model stopped generating tokens.
type ChatCompleteFinishReason string
const (
// ChatCompleteFinishReasonUnknown indicates that the provider did not supply a recognised termination code.
ChatCompleteFinishReasonUnknown ChatCompleteFinishReason = "unknown"
// ChatCompleteFinishReasonStop indicates that generation stopped naturally or due to a configured stop sequence.
ChatCompleteFinishReasonStop ChatCompleteFinishReason = "stop"
// ChatCompleteFinishReasonLength indicates that generation hit the configured token limit.
ChatCompleteFinishReasonLength ChatCompleteFinishReason = "length"
// ChatCompleteFinishReasonContentFilter indicates that a platform-level moderation filter blocked the content.
ChatCompleteFinishReasonContentFilter ChatCompleteFinishReason = "content_filter"
// ChatCompleteFinishReasonToolCalls indicates that the model requested a tool invocation mid-response.
ChatCompleteFinishReasonToolCalls ChatCompleteFinishReason = "tool_calls"
// ChatCompleteFinishReasonRefusal indicates that the model produced a refusal message of its own accord.
ChatCompleteFinishReasonRefusal ChatCompleteFinishReason = "refusal"
)
// ChatCompleteResponseMetadata contains metadata about the request and response, for example, token usage.
type ChatCompleteResponseMetadata struct {
Usage ChatCompleteResponseUsage
// FinishReason is optional; nil indicates the provider omitted a finish signal entirely.
FinishReason *ChatCompleteFinishReason
}
// ChatCompleteResponse for [ChatCompleter].
// Construct with [NewChatCompleteResponse].
// Note that the [ChatCompleteResponse.Meta] field is a pointer, because it's updated continuously
// until the streaming response with [ChatCompleteResponse.Parts] is complete.
type ChatCompleteResponse struct {
Meta *ChatCompleteResponseMetadata
partsFunc iter.Seq2[Part, error]
}
func NewChatCompleteResponse(partsFunc iter.Seq2[Part, error]) ChatCompleteResponse {
return ChatCompleteResponse{
partsFunc: partsFunc,
}
}
func (c ChatCompleteResponse) Parts() iter.Seq2[Part, error] {
return c.partsFunc
}
// ChatCompleter is satisfied by models supporting chat completion.
// Streaming chat completion is preferred where possible, so that methods on [ChatCompleteResponse],
// like [ChatCompleteResponse.Parts], can be used to stream the response.
type ChatCompleter interface {
ChatComplete(ctx context.Context, req ChatCompleteRequest) (ChatCompleteResponse, error)
}
func Ptr[T any](v T) *T {
return &v
}
// Tool definition.
type Tool struct {
Name string
Description string
Schema ToolSchema
Execute ToolFunction
Summarize ToolFunction
}
// ToolSchema in JSON Schema format of the arguments the tool accepts.
type ToolSchema struct {
Properties map[string]*Schema
}
func GenerateToolSchema[T any]() ToolSchema {
schema := GenerateSchema[T]()
return ToolSchema{
Properties: schema.Properties,
}
}
// SchemaType is the primitive type of a [Schema].
type SchemaType string
const (
// SchemaTypeString is the OpenAPI string type.
SchemaTypeString SchemaType = "string"
// SchemaTypeNumber is the OpenAPI number type.
SchemaTypeNumber SchemaType = "number"
// SchemaTypeInteger is the OpenAPI integer type.
SchemaTypeInteger SchemaType = "integer"
// SchemaTypeBoolean is the OpenAPI boolean type.
SchemaTypeBoolean SchemaType = "boolean"
// SchemaTypeArray is the OpenAPI array type.
SchemaTypeArray SchemaType = "array"
// SchemaTypeObject is the OpenAPI object type.
SchemaTypeObject SchemaType = "object"
)
type Schema struct {
// Optional. The value should be validated against any (one or more) of the subschemas
// in the list.
AnyOf []*Schema `json:"anyOf,omitempty"`
// Optional. Default value of the data.
Default any `json:"default,omitempty"`
// Optional. The description of the data.
Description string `json:"description,omitempty"`
// Optional. Possible values of the element of primitive type with enum format. Examples:
// 1. We can define direction as : {type:STRING, format:enum, enum:["EAST", NORTH",
// "SOUTH", "WEST"]} 2. We can define apartment number as : {type:INTEGER, format:enum,
// enum:["101", "201", "301"]}
Enum []string `json:"enum,omitempty"`
// Optional. Example of the object. Will only populated when the object is the root.
Example any `json:"example,omitempty"`
// Optional. The format of the data. Supported formats: for NUMBER type: "float", "double"
// for INTEGER type: "int32", "int64" for STRING type: "email", "byte", etc
Format string `json:"format,omitempty"`
// Optional. SCHEMA FIELDS FOR TYPE ARRAY Schema of the elements of Type.ARRAY.
Items *Schema `json:"items,omitempty"`
// Optional. Maximum number of the elements for Type.ARRAY.
MaxItems *int64 `json:"maxItems,omitempty,string"`
// Optional. Maximum value of the Type.INTEGER and Type.NUMBER
Maximum *float64 `json:"maximum,omitempty"`
// Optional. Minimum number of the elements for Type.ARRAY.
MinItems *int64 `json:"minItems,omitempty,string"`
// Optional. Minimum value of the Type.INTEGER and Type.NUMBER.
Minimum *float64 `json:"minimum,omitempty"`
// Optional. SCHEMA FIELDS FOR TYPE OBJECT Properties of Type.OBJECT.
Properties map[string]*Schema `json:"properties,omitempty"`
// Optional. The order of the properties. Not a standard field in open API spec. Only
// used to support the order of the properties.
PropertyOrdering []string `json:"propertyOrdering,omitempty"`
// Optional. Required properties of Type.OBJECT.
Required []string `json:"required,omitempty"`
// Optional. The title of the Schema.
Title string `json:"title,omitempty"`
// Optional. The type of the data.
Type SchemaType `json:"type,omitempty"`
}
// GenerateSchema from any type.
// See github.com/invopop/jsonschema for struct tags etc.
func GenerateSchema[T any]() Schema {
reflector := jsonschema.Reflector{
AllowAdditionalProperties: false,
DoNotReference: true,
}
var v T
schema := reflector.Reflect(v)
return convertJSONSchemaToSchema(schema)
}
func convertJSONSchemaToSchema(js *jsonschema.Schema) Schema {
s := Schema{
Description: js.Description,
Title: js.Title,
Default: js.Default,
Format: js.Format,
}
// Convert example (Examples is a slice, use first one if available)
if len(js.Examples) > 0 {
s.Example = js.Examples[0]
}
// Convert type
if js.Type != "" {
switch js.Type {
case "string", "number", "integer", "boolean", "array", "object":
s.Type = SchemaType(js.Type)
default:
panic("unsupported schema type " + js.Type)
}
}
// Convert enum
if len(js.Enum) > 0 {
s.Enum = make([]string, len(js.Enum))
for i, v := range js.Enum {
s.Enum[i] = fmt.Sprint(v)
}
}
// Convert numeric constraints (json.Number is a string)
if js.Minimum != "" {
if min, err := js.Minimum.Float64(); err == nil {
s.Minimum = &min
}
}
if js.Maximum != "" {
if max, err := js.Maximum.Float64(); err == nil {
s.Maximum = &max
}
}
// Convert array constraints
if js.MinItems != nil {
minItems := int64(*js.MinItems)
s.MinItems = &minItems
}
if js.MaxItems != nil {
maxItems := int64(*js.MaxItems)
s.MaxItems = &maxItems
}
if js.Items != nil {
converted := convertJSONSchemaToSchema(js.Items)
s.Items = &converted
}
// Convert object constraints
if js.Properties != nil && js.Properties.Len() > 0 {
s.Properties = make(map[string]*Schema)
s.PropertyOrdering = make([]string, 0, js.Properties.Len())
// Iterate through ordered map
for pair := js.Properties.Oldest(); pair != nil; pair = pair.Next() {
converted := convertJSONSchemaToSchema(pair.Value)
s.Properties[pair.Key] = &converted
s.PropertyOrdering = append(s.PropertyOrdering, pair.Key)
}
}
s.Required = js.Required
// Convert anyOf
if len(js.AnyOf) > 0 {
s.AnyOf = make([]*Schema, len(js.AnyOf))
for i, v := range js.AnyOf {
converted := convertJSONSchemaToSchema(v)
s.AnyOf[i] = &converted
}
}
return s
}
type ToolFunction func(ctx context.Context, rawArgs json.RawMessage) (string, error)
type ToolCall struct {
ID string
Name string
Args json.RawMessage
}
// TODO tool result can be string but also other types, such as image!
type ToolResult struct {
ID string
Name string
Content string
Err error
}