-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinference.ts
415 lines (357 loc) · 12.4 KB
/
inference.ts
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../resource';
import { APIPromise } from '../core';
import * as Core from '../core';
import * as InferenceAPI from './inference';
import * as Shared from './shared';
import { Stream } from '../streaming';
export class Inference extends APIResource {
/**
* Generate a chat completion for the given messages using the specified model.
*/
chatCompletion(
body: InferenceChatCompletionParamsNonStreaming,
options?: Core.RequestOptions,
): APIPromise<Shared.ChatCompletionResponse>;
chatCompletion(
body: InferenceChatCompletionParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<ChatCompletionResponseStreamChunk>>;
chatCompletion(
body: InferenceChatCompletionParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<ChatCompletionResponseStreamChunk> | Shared.ChatCompletionResponse>;
chatCompletion(
body: InferenceChatCompletionParams,
options?: Core.RequestOptions,
): APIPromise<Shared.ChatCompletionResponse> | APIPromise<Stream<ChatCompletionResponseStreamChunk>> {
return this._client.post('/v1/inference/chat-completion', {
body,
...options,
stream: body.stream ?? false,
}) as APIPromise<Shared.ChatCompletionResponse> | APIPromise<Stream<ChatCompletionResponseStreamChunk>>;
}
/**
* Generate a completion for the given content using the specified model.
*/
completion(
body: InferenceCompletionParamsNonStreaming,
options?: Core.RequestOptions,
): APIPromise<CompletionResponse>;
completion(
body: InferenceCompletionParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<CompletionResponse>>;
completion(
body: InferenceCompletionParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<CompletionResponse> | CompletionResponse>;
completion(
body: InferenceCompletionParams,
options?: Core.RequestOptions,
): APIPromise<CompletionResponse> | APIPromise<Stream<CompletionResponse>> {
return this._client.post('/v1/inference/completion', {
body,
...options,
stream: body.stream ?? false,
}) as APIPromise<CompletionResponse> | APIPromise<Stream<CompletionResponse>>;
}
/**
* Generate embeddings for content pieces using the specified model.
*/
embeddings(
body: InferenceEmbeddingsParams,
options?: Core.RequestOptions,
): Core.APIPromise<EmbeddingsResponse> {
return this._client.post('/v1/inference/embeddings', { body, ...options });
}
}
/**
* A chunk of a streamed chat completion response.
*/
export interface ChatCompletionResponseStreamChunk {
/**
* The event containing the new content
*/
event: ChatCompletionResponseStreamChunk.Event;
metrics?: Array<ChatCompletionResponseStreamChunk.Metric>;
}
export namespace ChatCompletionResponseStreamChunk {
/**
* The event containing the new content
*/
export interface Event {
/**
* Content generated since last event. This can be one or more tokens, or a tool
* call.
*/
delta: Shared.ContentDelta;
/**
* Type of the event
*/
event_type: 'start' | 'complete' | 'progress';
/**
* Optional log probabilities for generated tokens
*/
logprobs?: Array<InferenceAPI.TokenLogProbs>;
/**
* Optional reason why generation stopped, if complete
*/
stop_reason?: 'end_of_turn' | 'end_of_message' | 'out_of_tokens';
}
export interface Metric {
metric: string;
span_id: string;
timestamp: string;
trace_id: string;
type: 'metric';
unit: string;
value: number;
attributes?: Record<string, string | number | boolean | null>;
}
}
/**
* Response from a completion request.
*/
export interface CompletionResponse {
/**
* The generated completion text
*/
content: string;
/**
* Reason why generation stopped
*/
stop_reason: 'end_of_turn' | 'end_of_message' | 'out_of_tokens';
/**
* Optional log probabilities for generated tokens
*/
logprobs?: Array<TokenLogProbs>;
}
/**
* Response containing generated embeddings.
*/
export interface EmbeddingsResponse {
/**
* List of embedding vectors, one per input content. Each embedding is a list of
* floats. The dimensionality of the embedding is model-specific; you can check
* model metadata using /models/{model_id}
*/
embeddings: Array<Array<number>>;
}
/**
* Log probabilities for generated tokens.
*/
export interface TokenLogProbs {
/**
* Dictionary mapping tokens to their log probabilities
*/
logprobs_by_token: Record<string, number>;
}
export type InferenceChatCompletionParams =
| InferenceChatCompletionParamsNonStreaming
| InferenceChatCompletionParamsStreaming;
export interface InferenceChatCompletionParamsBase {
/**
* List of messages in the conversation
*/
messages: Array<Shared.Message>;
/**
* The identifier of the model to use. The model must be registered with Llama
* Stack and available via the /models endpoint.
*/
model_id: string;
/**
* (Optional) If specified, log probabilities for each token position will be
* returned.
*/
logprobs?: InferenceChatCompletionParams.Logprobs;
/**
* (Optional) Grammar specification for guided (structured) decoding. There are two
* options: - `ResponseFormat.json_schema`: The grammar is a JSON schema. Most
* providers support this format. - `ResponseFormat.grammar`: The grammar is a BNF
* grammar. This format is more flexible, but not all providers support it.
*/
response_format?: Shared.ResponseFormat;
/**
* Parameters to control the sampling strategy
*/
sampling_params?: Shared.SamplingParams;
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream?: boolean;
/**
* (Optional) Whether tool use is required or automatic. Defaults to
* ToolChoice.auto. .. deprecated:: Use tool_config instead.
*/
tool_choice?: 'auto' | 'required';
/**
* (Optional) Configuration for tool use.
*/
tool_config?: InferenceChatCompletionParams.ToolConfig;
/**
* (Optional) Instructs the model how to format tool calls. By default, Llama Stack
* will attempt to use a format that is best adapted to the model. -
* `ToolPromptFormat.json`: The tool calls are formatted as a JSON object. -
* `ToolPromptFormat.function_tag`: The tool calls are enclosed in a
* <function=function_name> tag. - `ToolPromptFormat.python_list`: The tool calls
* are output as Python syntax -- a list of function calls. .. deprecated:: Use
* tool_config instead.
*/
tool_prompt_format?: 'json' | 'function_tag' | 'python_list';
/**
* (Optional) List of tool definitions available to the model
*/
tools?: Array<InferenceChatCompletionParams.Tool>;
}
export namespace InferenceChatCompletionParams {
/**
* (Optional) If specified, log probabilities for each token position will be
* returned.
*/
export interface Logprobs {
/**
* How many tokens (for each position) to return log probabilities for.
*/
top_k?: number;
}
/**
* (Optional) Configuration for tool use.
*/
export interface ToolConfig {
/**
* (Optional) Config for how to override the default system prompt. -
* `SystemMessageBehavior.append`: Appends the provided system message to the
* default system prompt. - `SystemMessageBehavior.replace`: Replaces the default
* system prompt with the provided system message. The system message can include
* the string '{{function_definitions}}' to indicate where the function definitions
* should be inserted.
*/
system_message_behavior: 'append' | 'replace';
/**
* (Optional) Whether tool use is required or automatic. Defaults to
* ToolChoice.auto.
*/
tool_choice?: 'auto' | 'required';
/**
* (Optional) Instructs the model how to format tool calls. By default, Llama Stack
* will attempt to use a format that is best adapted to the model. -
* `ToolPromptFormat.json`: The tool calls are formatted as a JSON object. -
* `ToolPromptFormat.function_tag`: The tool calls are enclosed in a
* <function=function_name> tag. - `ToolPromptFormat.python_list`: The tool calls
* are output as Python syntax -- a list of function calls.
*/
tool_prompt_format?: 'json' | 'function_tag' | 'python_list';
}
export interface Tool {
tool_name: 'brave_search' | 'wolfram_alpha' | 'photogen' | 'code_interpreter' | (string & {});
description?: string;
parameters?: Record<string, Shared.ToolParamDefinition>;
}
export type InferenceChatCompletionParamsNonStreaming =
InferenceAPI.InferenceChatCompletionParamsNonStreaming;
export type InferenceChatCompletionParamsStreaming = InferenceAPI.InferenceChatCompletionParamsStreaming;
}
export interface InferenceChatCompletionParamsNonStreaming extends InferenceChatCompletionParamsBase {
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream?: false;
}
export interface InferenceChatCompletionParamsStreaming extends InferenceChatCompletionParamsBase {
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream: true;
}
export type InferenceCompletionParams =
| InferenceCompletionParamsNonStreaming
| InferenceCompletionParamsStreaming;
export interface InferenceCompletionParamsBase {
/**
* The content to generate a completion for
*/
content: Shared.InterleavedContent;
/**
* The identifier of the model to use. The model must be registered with Llama
* Stack and available via the /models endpoint.
*/
model_id: string;
/**
* (Optional) If specified, log probabilities for each token position will be
* returned.
*/
logprobs?: InferenceCompletionParams.Logprobs;
/**
* (Optional) Grammar specification for guided (structured) decoding
*/
response_format?: Shared.ResponseFormat;
/**
* (Optional) Parameters to control the sampling strategy
*/
sampling_params?: Shared.SamplingParams;
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream?: boolean;
}
export namespace InferenceCompletionParams {
/**
* (Optional) If specified, log probabilities for each token position will be
* returned.
*/
export interface Logprobs {
/**
* How many tokens (for each position) to return log probabilities for.
*/
top_k?: number;
}
export type InferenceCompletionParamsNonStreaming = InferenceAPI.InferenceCompletionParamsNonStreaming;
export type InferenceCompletionParamsStreaming = InferenceAPI.InferenceCompletionParamsStreaming;
}
export interface InferenceCompletionParamsNonStreaming extends InferenceCompletionParamsBase {
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream?: false;
}
export interface InferenceCompletionParamsStreaming extends InferenceCompletionParamsBase {
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream: true;
}
export interface InferenceEmbeddingsParams {
/**
* List of contents to generate embeddings for. Note that content can be
* multimodal. The behavior depends on the model and provider. Some models may only
* support text.
*/
contents: Array<Shared.InterleavedContent>;
/**
* The identifier of the model to use. The model must be an embedding model
* registered with Llama Stack and available via the /models endpoint.
*/
model_id: string;
}
export declare namespace Inference {
export {
type ChatCompletionResponseStreamChunk as ChatCompletionResponseStreamChunk,
type CompletionResponse as CompletionResponse,
type EmbeddingsResponse as EmbeddingsResponse,
type TokenLogProbs as TokenLogProbs,
type InferenceChatCompletionParams as InferenceChatCompletionParams,
type InferenceChatCompletionParamsNonStreaming as InferenceChatCompletionParamsNonStreaming,
type InferenceChatCompletionParamsStreaming as InferenceChatCompletionParamsStreaming,
type InferenceCompletionParams as InferenceCompletionParams,
type InferenceCompletionParamsNonStreaming as InferenceCompletionParamsNonStreaming,
type InferenceCompletionParamsStreaming as InferenceCompletionParamsStreaming,
type InferenceEmbeddingsParams as InferenceEmbeddingsParams,
};
}