-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcompletion.go
More file actions
363 lines (331 loc) · 13.9 KB
/
completion.go
File metadata and controls
363 lines (331 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
355
356
357
358
359
360
361
362
363
// 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 (
"context"
"net/http"
"slices"
"github.com/llamastack/llama-stack-client-go/internal/apijson"
"github.com/llamastack/llama-stack-client-go/internal/requestconfig"
"github.com/llamastack/llama-stack-client-go/option"
"github.com/llamastack/llama-stack-client-go/packages/param"
"github.com/llamastack/llama-stack-client-go/packages/respjson"
"github.com/llamastack/llama-stack-client-go/packages/ssestream"
"github.com/llamastack/llama-stack-client-go/shared/constant"
)
// CompletionService 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 [NewCompletionService] method instead.
type CompletionService struct {
Options []option.RequestOption
}
// NewCompletionService 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 NewCompletionService(opts ...option.RequestOption) (r CompletionService) {
r = CompletionService{}
r.Options = opts
return
}
// Create completion. Generate an OpenAI-compatible completion for the given prompt
// using the specified model.
func (r *CompletionService) New(ctx context.Context, body CompletionNewParams, opts ...option.RequestOption) (res *CompletionNewResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "v1/completions"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Create completion. Generate an OpenAI-compatible completion for the given prompt
// using the specified model.
func (r *CompletionService) NewStreaming(ctx context.Context, body CompletionNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[CompletionNewResponse]) {
var (
raw *http.Response
err error
)
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithJSONSet("stream", true)}, opts...)
path := "v1/completions"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &raw, opts...)
return ssestream.NewStream[CompletionNewResponse](ssestream.NewDecoder(raw), err)
}
// Response from an OpenAI-compatible completion request.
type CompletionNewResponse struct {
ID string `json:"id,required"`
Choices []CompletionNewResponseChoice `json:"choices,required"`
Created int64 `json:"created,required"`
Model string `json:"model,required"`
Object constant.TextCompletion `json:"object,required"`
// 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
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CompletionNewResponse) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// A choice from an OpenAI-compatible completion response.
type CompletionNewResponseChoice struct {
FinishReason string `json:"finish_reason,required"`
Index int64 `json:"index,required"`
Text string `json:"text,required"`
// The log probabilities for the tokens in the message from an OpenAI-compatible
// chat completion response.
Logprobs CompletionNewResponseChoiceLogprobs `json:"logprobs"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
FinishReason respjson.Field
Index respjson.Field
Text respjson.Field
Logprobs respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CompletionNewResponseChoice) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponseChoice) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The log probabilities for the tokens in the message from an OpenAI-compatible
// chat completion response.
type CompletionNewResponseChoiceLogprobs struct {
// (Optional) The log probabilities for the tokens in the message
Content []CompletionNewResponseChoiceLogprobsContent `json:"content"`
// (Optional) The log probabilities for the tokens in the message
Refusal []CompletionNewResponseChoiceLogprobsRefusal `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 CompletionNewResponseChoiceLogprobs) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponseChoiceLogprobs) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The log probability for a token from an OpenAI-compatible chat completion
// response.
type CompletionNewResponseChoiceLogprobsContent struct {
Token string `json:"token,required"`
Logprob float64 `json:"logprob,required"`
TopLogprobs []CompletionNewResponseChoiceLogprobsContentTopLogprob `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 CompletionNewResponseChoiceLogprobsContent) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponseChoiceLogprobsContent) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The top log probability for a token from an OpenAI-compatible chat completion
// response.
type CompletionNewResponseChoiceLogprobsContentTopLogprob 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 CompletionNewResponseChoiceLogprobsContentTopLogprob) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponseChoiceLogprobsContentTopLogprob) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The log probability for a token from an OpenAI-compatible chat completion
// response.
type CompletionNewResponseChoiceLogprobsRefusal struct {
Token string `json:"token,required"`
Logprob float64 `json:"logprob,required"`
TopLogprobs []CompletionNewResponseChoiceLogprobsRefusalTopLogprob `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 CompletionNewResponseChoiceLogprobsRefusal) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponseChoiceLogprobsRefusal) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The top log probability for a token from an OpenAI-compatible chat completion
// response.
type CompletionNewResponseChoiceLogprobsRefusalTopLogprob 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 CompletionNewResponseChoiceLogprobsRefusalTopLogprob) RawJSON() string { return r.JSON.raw }
func (r *CompletionNewResponseChoiceLogprobsRefusalTopLogprob) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CompletionNewParams struct {
// The identifier of the model to use. The model must be registered with Llama
// Stack and available via the /models endpoint.
Model string `json:"model,required"`
// The prompt to generate a completion for.
Prompt CompletionNewParamsPromptUnion `json:"prompt,omitzero,required"`
// (Optional) The number of completions to generate.
BestOf param.Opt[int64] `json:"best_of,omitzero"`
// (Optional) Whether to echo the prompt.
Echo param.Opt[bool] `json:"echo,omitzero"`
// (Optional) The penalty for repeated tokens.
FrequencyPenalty param.Opt[float64] `json:"frequency_penalty,omitzero"`
// (Optional) The log probabilities to use.
Logprobs param.Opt[bool] `json:"logprobs,omitzero"`
// (Optional) The maximum number of tokens to generate.
MaxTokens param.Opt[int64] `json:"max_tokens,omitzero"`
// (Optional) The number of completions to generate.
N param.Opt[int64] `json:"n,omitzero"`
// (Optional) The penalty for repeated tokens.
PresencePenalty param.Opt[float64] `json:"presence_penalty,omitzero"`
// (Optional) The seed to use.
Seed param.Opt[int64] `json:"seed,omitzero"`
// (Optional) The suffix that should be appended to the completion.
Suffix param.Opt[string] `json:"suffix,omitzero"`
// (Optional) The temperature to use.
Temperature param.Opt[float64] `json:"temperature,omitzero"`
// (Optional) The top p to use.
TopP param.Opt[float64] `json:"top_p,omitzero"`
// (Optional) The user to use.
User param.Opt[string] `json:"user,omitzero"`
// (Optional) The logit bias to use.
LogitBias map[string]float64 `json:"logit_bias,omitzero"`
// (Optional) The stop tokens to use.
Stop CompletionNewParamsStopUnion `json:"stop,omitzero"`
// (Optional) The stream options to use.
StreamOptions map[string]CompletionNewParamsStreamOptionUnion `json:"stream_options,omitzero"`
paramObj
}
func (r CompletionNewParams) MarshalJSON() (data []byte, err error) {
type shadow CompletionNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CompletionNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type CompletionNewParamsPromptUnion struct {
OfString param.Opt[string] `json:",omitzero,inline"`
OfStringArray []string `json:",omitzero,inline"`
OfIntArray []int64 `json:",omitzero,inline"`
OfArrayOfIntArray [][]int64 `json:",omitzero,inline"`
paramUnion
}
func (u CompletionNewParamsPromptUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfString, u.OfStringArray, u.OfIntArray, u.OfArrayOfIntArray)
}
func (u *CompletionNewParamsPromptUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *CompletionNewParamsPromptUnion) asAny() any {
if !param.IsOmitted(u.OfString) {
return &u.OfString.Value
} else if !param.IsOmitted(u.OfStringArray) {
return &u.OfStringArray
} else if !param.IsOmitted(u.OfIntArray) {
return &u.OfIntArray
} else if !param.IsOmitted(u.OfArrayOfIntArray) {
return &u.OfArrayOfIntArray
}
return nil
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type CompletionNewParamsStopUnion struct {
OfString param.Opt[string] `json:",omitzero,inline"`
OfStringArray []string `json:",omitzero,inline"`
paramUnion
}
func (u CompletionNewParamsStopUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfString, u.OfStringArray)
}
func (u *CompletionNewParamsStopUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *CompletionNewParamsStopUnion) asAny() any {
if !param.IsOmitted(u.OfString) {
return &u.OfString.Value
} else if !param.IsOmitted(u.OfStringArray) {
return &u.OfStringArray
}
return nil
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type CompletionNewParamsStreamOptionUnion struct {
OfBool param.Opt[bool] `json:",omitzero,inline"`
OfFloat param.Opt[float64] `json:",omitzero,inline"`
OfString param.Opt[string] `json:",omitzero,inline"`
OfAnyArray []any `json:",omitzero,inline"`
paramUnion
}
func (u CompletionNewParamsStreamOptionUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfBool, u.OfFloat, u.OfString, u.OfAnyArray)
}
func (u *CompletionNewParamsStreamOptionUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *CompletionNewParamsStreamOptionUnion) asAny() any {
if !param.IsOmitted(u.OfBool) {
return &u.OfBool.Value
} else if !param.IsOmitted(u.OfFloat) {
return &u.OfFloat.Value
} else if !param.IsOmitted(u.OfString) {
return &u.OfString.Value
} else if !param.IsOmitted(u.OfAnyArray) {
return &u.OfAnyArray
}
return nil
}