-
Notifications
You must be signed in to change notification settings - Fork 901
/
completions.ts
161 lines (145 loc) · 5.96 KB
/
completions.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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import * as Core from '../../../core';
import { APIResource } from '../../../resource';
import { ChatCompletionRunner, ChatCompletionFunctionRunnerParams } from '../../../lib/ChatCompletionRunner';
import {
ChatCompletionStreamingRunner,
ChatCompletionStreamingFunctionRunnerParams,
} from '../../../lib/ChatCompletionStreamingRunner';
import { BaseFunctionsArgs } from '../../../lib/RunnableFunction';
import { RunnerOptions } from '../../../lib/AbstractChatCompletionRunner';
import { ChatCompletionToolRunnerParams } from '../../../lib/ChatCompletionRunner';
import { ChatCompletionStreamingToolRunnerParams } from '../../../lib/ChatCompletionStreamingRunner';
import { ChatCompletionStream, type ChatCompletionStreamParams } from '../../../lib/ChatCompletionStream';
import {
ChatCompletion,
ChatCompletionCreateParamsNonStreaming,
ChatCompletionMessage,
ChatCompletionMessageToolCall,
} from '../../chat/completions';
import { ExtractParsedContentFromParams, parseChatCompletion, validateInputTools } from '../../../lib/parser';
export {
ChatCompletionStreamingRunner,
type ChatCompletionStreamingFunctionRunnerParams,
} from '../../../lib/ChatCompletionStreamingRunner';
export {
type RunnableFunction,
type RunnableFunctions,
type RunnableFunctionWithParse,
type RunnableFunctionWithoutParse,
ParsingFunction,
ParsingToolFunction,
} from '../../../lib/RunnableFunction';
export { type ChatCompletionToolRunnerParams } from '../../../lib/ChatCompletionRunner';
export { type ChatCompletionStreamingToolRunnerParams } from '../../../lib/ChatCompletionStreamingRunner';
export { ChatCompletionStream, type ChatCompletionStreamParams } from '../../../lib/ChatCompletionStream';
export {
ChatCompletionRunner,
type ChatCompletionFunctionRunnerParams,
} from '../../../lib/ChatCompletionRunner';
export interface ParsedFunction extends ChatCompletionMessageToolCall.Function {
parsed_arguments?: unknown;
}
export interface ParsedFunctionToolCall extends ChatCompletionMessageToolCall {
function: ParsedFunction;
}
export interface ParsedChatCompletionMessage<ParsedT> extends ChatCompletionMessage {
parsed: ParsedT | null;
tool_calls: Array<ParsedFunctionToolCall>;
}
export interface ParsedChoice<ParsedT> extends ChatCompletion.Choice {
message: ParsedChatCompletionMessage<ParsedT>;
}
export interface ParsedChatCompletion<ParsedT> extends ChatCompletion {
choices: Array<ParsedChoice<ParsedT>>;
}
export type ChatCompletionParseParams = ChatCompletionCreateParamsNonStreaming;
export class Completions extends APIResource {
parse<Params extends ChatCompletionParseParams, ParsedT = ExtractParsedContentFromParams<Params>>(
body: Params,
options?: Core.RequestOptions,
): Core.APIPromise<ParsedChatCompletion<ParsedT>> {
validateInputTools(body.tools);
return this._client.chat.completions
.create(body, {
...options,
headers: {
...options?.headers,
'X-Stainless-Helper-Method': 'beta.chat.completions.parse',
},
})
._thenUnwrap((completion) => parseChatCompletion(completion, body));
}
/**
* @deprecated - use `runTools` instead.
*/
runFunctions<FunctionsArgs extends BaseFunctionsArgs>(
body: ChatCompletionFunctionRunnerParams<FunctionsArgs>,
options?: Core.RequestOptions,
): ChatCompletionRunner<null>;
runFunctions<FunctionsArgs extends BaseFunctionsArgs>(
body: ChatCompletionStreamingFunctionRunnerParams<FunctionsArgs>,
options?: Core.RequestOptions,
): ChatCompletionStreamingRunner<null>;
runFunctions<FunctionsArgs extends BaseFunctionsArgs>(
body:
| ChatCompletionFunctionRunnerParams<FunctionsArgs>
| ChatCompletionStreamingFunctionRunnerParams<FunctionsArgs>,
options?: Core.RequestOptions,
): ChatCompletionRunner<null> | ChatCompletionStreamingRunner<null> {
if (body.stream) {
return ChatCompletionStreamingRunner.runFunctions(
this._client,
body as ChatCompletionStreamingFunctionRunnerParams<FunctionsArgs>,
options,
);
}
return ChatCompletionRunner.runFunctions(
this._client,
body as ChatCompletionFunctionRunnerParams<FunctionsArgs>,
options,
);
}
/**
* A convenience helper for using tool calls with the /chat/completions endpoint
* which automatically calls the JavaScript functions you provide and sends their
* results back to the /chat/completions endpoint, looping as long as the model
* requests function calls.
*
* For more details and examples, see
* [the docs](https://github.com/openai/openai-node#automated-function-calls)
*/
runTools<
Params extends ChatCompletionToolRunnerParams<any>,
ParsedT = ExtractParsedContentFromParams<Params>,
>(body: Params, options?: RunnerOptions): ChatCompletionRunner<ParsedT>;
runTools<
Params extends ChatCompletionStreamingToolRunnerParams<any>,
ParsedT = ExtractParsedContentFromParams<Params>,
>(body: Params, options?: RunnerOptions): ChatCompletionStreamingRunner<ParsedT>;
runTools<
Params extends ChatCompletionToolRunnerParams<any> | ChatCompletionStreamingToolRunnerParams<any>,
ParsedT = ExtractParsedContentFromParams<Params>,
>(
body: Params,
options?: RunnerOptions,
): ChatCompletionRunner<ParsedT> | ChatCompletionStreamingRunner<ParsedT> {
if (body.stream) {
return ChatCompletionStreamingRunner.runTools(
this._client,
body as ChatCompletionStreamingToolRunnerParams<any>,
options,
);
}
return ChatCompletionRunner.runTools(this._client, body as ChatCompletionToolRunnerParams<any>, options);
}
/**
* Creates a chat completion stream
*/
stream<Params extends ChatCompletionStreamParams, ParsedT = ExtractParsedContentFromParams<Params>>(
body: Params,
options?: Core.RequestOptions,
): ChatCompletionStream<ParsedT> {
return ChatCompletionStream.createChatCompletion(this._client, body, options);
}
}