-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinference.ts
346 lines (301 loc) · 10.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
// 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 });
}
}
export interface ChatCompletionResponseStreamChunk {
/**
* The event containing the new content
*/
event: ChatCompletionResponseStreamChunk.Event;
}
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 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>;
}
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>>;
}
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.
*/
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';
/**
* (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;
}
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,
};
}