-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathchat.go
More file actions
354 lines (324 loc) · 13.9 KB
/
chat.go
File metadata and controls
354 lines (324 loc) · 13.9 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the terms described in the LICENSE file in
// the root directory of this source tree.
//
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package llamastackclient
import (
"github.com/llamastack/llama-stack-client-go/internal/apijson"
"github.com/llamastack/llama-stack-client-go/option"
"github.com/llamastack/llama-stack-client-go/packages/respjson"
"github.com/llamastack/llama-stack-client-go/shared/constant"
)
// ChatService contains methods and other services that help with interacting with
// the llama-stack-client API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewChatService] method instead.
type ChatService struct {
Options []option.RequestOption
Completions ChatCompletionService
}
// NewChatService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewChatService(opts ...option.RequestOption) (r ChatService) {
r = ChatService{}
r.Options = opts
r.Completions = NewChatCompletionService(opts...)
return
}
// Chunk from a streaming response to an OpenAI-compatible chat completion request.
type ChatCompletionChunk struct {
// The ID of the chat completion
ID string `json:"id,required"`
// List of choices
Choices []ChatCompletionChunkChoice `json:"choices,required"`
// The Unix timestamp in seconds when the chat completion was created
Created int64 `json:"created,required"`
// The model that was used to generate the chat completion
Model string `json:"model,required"`
// The object type, which will be "chat.completion.chunk"
Object constant.ChatCompletionChunk `json:"object,required"`
// Token usage information (typically included in final chunk with stream_options)
Usage ChatCompletionChunkUsage `json:"usage"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
Choices respjson.Field
Created respjson.Field
Model respjson.Field
Object respjson.Field
Usage respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunk) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunk) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// A chunk choice from an OpenAI-compatible chat completion streaming response.
type ChatCompletionChunkChoice struct {
// The delta from the chunk
Delta ChatCompletionChunkChoiceDelta `json:"delta,required"`
// The reason the model stopped generating
FinishReason string `json:"finish_reason,required"`
// The index of the choice
Index int64 `json:"index,required"`
// (Optional) The log probabilities for the tokens in the message
Logprobs ChatCompletionChunkChoiceLogprobs `json:"logprobs"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Delta respjson.Field
FinishReason respjson.Field
Index respjson.Field
Logprobs respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoice) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoice) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The delta from the chunk
type ChatCompletionChunkChoiceDelta struct {
// (Optional) The content of the delta
Content string `json:"content"`
// (Optional) The reasoning content from the model (non-standard, for o1/o3 models)
ReasoningContent string `json:"reasoning_content"`
// (Optional) The refusal of the delta
Refusal string `json:"refusal"`
// (Optional) The role of the delta
Role string `json:"role"`
// (Optional) The tool calls of the delta
ToolCalls []ChatCompletionChunkChoiceDeltaToolCall `json:"tool_calls"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Content respjson.Field
ReasoningContent respjson.Field
Refusal respjson.Field
Role respjson.Field
ToolCalls respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceDelta) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceDelta) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Tool call specification for OpenAI-compatible chat completion responses.
type ChatCompletionChunkChoiceDeltaToolCall struct {
// Must be "function" to identify this as a function call
Type constant.Function `json:"type,required"`
// (Optional) Unique identifier for the tool call
ID string `json:"id"`
// (Optional) Function call details
Function ChatCompletionChunkChoiceDeltaToolCallFunction `json:"function"`
// (Optional) Index of the tool call in the list
Index int64 `json:"index"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Type respjson.Field
ID respjson.Field
Function respjson.Field
Index respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceDeltaToolCall) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceDeltaToolCall) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// (Optional) Function call details
type ChatCompletionChunkChoiceDeltaToolCallFunction struct {
// (Optional) Arguments to pass to the function as a JSON string
Arguments string `json:"arguments"`
// (Optional) Name of the function to call
Name string `json:"name"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Arguments respjson.Field
Name respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceDeltaToolCallFunction) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceDeltaToolCallFunction) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// (Optional) The log probabilities for the tokens in the message
type ChatCompletionChunkChoiceLogprobs struct {
// (Optional) The log probabilities for the tokens in the message
Content []ChatCompletionChunkChoiceLogprobsContent `json:"content"`
// (Optional) The log probabilities for the tokens in the message
Refusal []ChatCompletionChunkChoiceLogprobsRefusal `json:"refusal"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Content respjson.Field
Refusal respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceLogprobs) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceLogprobs) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The log probability for a token from an OpenAI-compatible chat completion
// response.
type ChatCompletionChunkChoiceLogprobsContent struct {
Token string `json:"token,required"`
Logprob float64 `json:"logprob,required"`
TopLogprobs []ChatCompletionChunkChoiceLogprobsContentTopLogprob `json:"top_logprobs,required"`
Bytes []int64 `json:"bytes"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Token respjson.Field
Logprob respjson.Field
TopLogprobs respjson.Field
Bytes respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceLogprobsContent) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceLogprobsContent) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The top log probability for a token from an OpenAI-compatible chat completion
// response.
type ChatCompletionChunkChoiceLogprobsContentTopLogprob struct {
Token string `json:"token,required"`
Logprob float64 `json:"logprob,required"`
Bytes []int64 `json:"bytes"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Token respjson.Field
Logprob respjson.Field
Bytes respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceLogprobsContentTopLogprob) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceLogprobsContentTopLogprob) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The log probability for a token from an OpenAI-compatible chat completion
// response.
type ChatCompletionChunkChoiceLogprobsRefusal struct {
Token string `json:"token,required"`
Logprob float64 `json:"logprob,required"`
TopLogprobs []ChatCompletionChunkChoiceLogprobsRefusalTopLogprob `json:"top_logprobs,required"`
Bytes []int64 `json:"bytes"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Token respjson.Field
Logprob respjson.Field
TopLogprobs respjson.Field
Bytes respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceLogprobsRefusal) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceLogprobsRefusal) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The top log probability for a token from an OpenAI-compatible chat completion
// response.
type ChatCompletionChunkChoiceLogprobsRefusalTopLogprob struct {
Token string `json:"token,required"`
Logprob float64 `json:"logprob,required"`
Bytes []int64 `json:"bytes"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Token respjson.Field
Logprob respjson.Field
Bytes respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkChoiceLogprobsRefusalTopLogprob) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkChoiceLogprobsRefusalTopLogprob) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Token usage information (typically included in final chunk with stream_options)
type ChatCompletionChunkUsage struct {
// Number of tokens in the completion
CompletionTokens int64 `json:"completion_tokens,required"`
// Number of tokens in the prompt
PromptTokens int64 `json:"prompt_tokens,required"`
// Total tokens used (prompt + completion)
TotalTokens int64 `json:"total_tokens,required"`
// Token details for output tokens in OpenAI chat completion usage.
CompletionTokensDetails ChatCompletionChunkUsageCompletionTokensDetails `json:"completion_tokens_details"`
// Token details for prompt tokens in OpenAI chat completion usage.
PromptTokensDetails ChatCompletionChunkUsagePromptTokensDetails `json:"prompt_tokens_details"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CompletionTokens respjson.Field
PromptTokens respjson.Field
TotalTokens respjson.Field
CompletionTokensDetails respjson.Field
PromptTokensDetails respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkUsage) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkUsage) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Token details for output tokens in OpenAI chat completion usage.
type ChatCompletionChunkUsageCompletionTokensDetails struct {
// Number of tokens used for reasoning (o1/o3 models)
ReasoningTokens int64 `json:"reasoning_tokens"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ReasoningTokens respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkUsageCompletionTokensDetails) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkUsageCompletionTokensDetails) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Token details for prompt tokens in OpenAI chat completion usage.
type ChatCompletionChunkUsagePromptTokensDetails struct {
// Number of tokens retrieved from cache
CachedTokens int64 `json:"cached_tokens"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
CachedTokens respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ChatCompletionChunkUsagePromptTokensDetails) RawJSON() string { return r.JSON.raw }
func (r *ChatCompletionChunkUsagePromptTokensDetails) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}