Skip to content

Commit 38ffa5e

Browse files
authored
Merge pull request #17 from ChuckJonas/chat-function-support
support for passing "functions" to chat completions
2 parents 5cfe16c + e74f60a commit 38ffa5e

3 files changed

Lines changed: 105 additions & 2 deletions

File tree

examples/chatCompletionFunction.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { OpenAI } from "../mod.ts";
2+
3+
const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);
4+
5+
const chatCompletion = await openAI.createChatCompletion({
6+
model: "gpt-3.5-turbo",
7+
messages: [
8+
{"role": "user", "content": "What is the weather like in Boston?"}
9+
],
10+
function_call: { name: "get_current_weather" },
11+
functions: [
12+
{
13+
"name": "get_current_weather",
14+
"description": "Get the current weather in a given location",
15+
"parameters": {
16+
"type": "object",
17+
"properties": {
18+
"location": {
19+
"type": "string",
20+
"description": "The city and state, e.g. San Francisco, CA"
21+
},
22+
"unit": {
23+
"type": "string",
24+
"enum": ["celsius", "fahrenheit"]
25+
}
26+
},
27+
"required": ["location"]
28+
}
29+
}
30+
]
31+
});
32+
33+
console.log(chatCompletion);

src/openai.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class OpenAI {
168168
async createChatCompletion(
169169
options: ChatCompletionOptions,
170170
): Promise<ChatCompletion> {
171-
return await this.#request(`/chat/completions`, {
171+
const resp = await this.#request(`/chat/completions`, {
172172
model: options.model,
173173
messages: options.messages,
174174
temperature: options.temperature,
@@ -180,7 +180,15 @@ export class OpenAI {
180180
frequency_penalty: options.frequencyPenalty,
181181
logit_bias: options.logitBias,
182182
user: options.user,
183-
});
183+
functions: options.functions,
184+
function_call: options.function_call,
185+
}) as ChatCompletion;
186+
187+
// null coalesce content to empty string as discussed in PR #17
188+
resp?.choices?.forEach(
189+
(choice) => (choice.message.content = choice.message.content ?? "")
190+
);
191+
return resp;
184192
}
185193

186194
/**
@@ -213,6 +221,8 @@ export class OpenAI {
213221
frequency_penalty: options.frequencyPenalty,
214222
logit_bias: options.logitBias,
215223
user: options.user,
224+
functions: options.functions,
225+
function_call: options.function_call
216226
}),
217227
},
218228
);

src/types.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,64 @@ export interface ChatCompletionOptions {
197197
* https://platform.openai.com/docs/api-reference/chat/create#chat/create-user
198198
*/
199199
user?: string;
200+
201+
/**
202+
* A list of functions the model may generate JSON inputs for.
203+
* https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions
204+
*/
205+
functions?: ChatCompletionOptionsFunction[];
206+
207+
/**
208+
* Controls how the model responds to function calls.
209+
* "none" means the model does not call a function, and responds to the end-user.
210+
* "auto" means the model can pick between an end-user or calling a function.
211+
* Specifying a particular function via {"name":\ "my_function"} forces the model to call that function.
212+
* "none" is the default when no functions are present. "auto" is the default if functions are present.
213+
* https://platform.openai.com/docs/api-reference/chat/create#chat/create-function_call
214+
*/
215+
function_call?: 'none' | 'auto' | { name: string };
200216
}
201217

218+
export type ChatCompletionOptionsFunction = {
219+
name: string;
220+
description: string;
221+
parameters: ObjectSchema;
222+
};
223+
224+
type JSONSchema = (
225+
| ObjectSchema
226+
| StringSchema
227+
| NumberSchema
228+
| BooleanSchema
229+
| ArraySchema
230+
) & { description?: string };
231+
232+
type ObjectSchema = {
233+
type: "object";
234+
properties: Record<string, JSONSchema>;
235+
required: string[];
236+
};
237+
238+
type ArraySchema = {
239+
type: "array";
240+
items: JSONSchema;
241+
};
242+
243+
type StringSchema = {
244+
type: "string";
245+
enum?: string[];
246+
};
247+
248+
type NumberSchema = {
249+
type: "number";
250+
minimum?: number;
251+
maximum?: number;
252+
};
253+
254+
type BooleanSchema = {
255+
type: "boolean";
256+
};
257+
202258
export interface EditOptions {
203259
/**
204260
* ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint.
@@ -616,6 +672,10 @@ export interface ChatCompletion {
616672
name?: string;
617673
role: "system" | "assistant" | "user";
618674
content: string;
675+
function_call?: {
676+
"name": string,
677+
"arguments": string
678+
}
619679
};
620680
finish_reason: string;
621681
}[];

0 commit comments

Comments
 (0)