Skip to content

Commit

Permalink
Merge pull request #17 from ChuckJonas/chat-function-support
Browse files Browse the repository at this point in the history
support for passing "functions" to chat completions
  • Loading branch information
load1n9 authored Jun 19, 2023
2 parents 5cfe16c + e74f60a commit 38ffa5e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
33 changes: 33 additions & 0 deletions examples/chatCompletionFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { OpenAI } from "../mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const chatCompletion = await openAI.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{"role": "user", "content": "What is the weather like in Boston?"}
],
function_call: { name: "get_current_weather" },
functions: [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
]
});

console.log(chatCompletion);
14 changes: 12 additions & 2 deletions src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class OpenAI {
async createChatCompletion(
options: ChatCompletionOptions,
): Promise<ChatCompletion> {
return await this.#request(`/chat/completions`, {
const resp = await this.#request(`/chat/completions`, {
model: options.model,
messages: options.messages,
temperature: options.temperature,
Expand All @@ -180,7 +180,15 @@ export class OpenAI {
frequency_penalty: options.frequencyPenalty,
logit_bias: options.logitBias,
user: options.user,
});
functions: options.functions,
function_call: options.function_call,
}) as ChatCompletion;

// null coalesce content to empty string as discussed in PR #17
resp?.choices?.forEach(
(choice) => (choice.message.content = choice.message.content ?? "")
);
return resp;
}

/**
Expand Down Expand Up @@ -213,6 +221,8 @@ export class OpenAI {
frequency_penalty: options.frequencyPenalty,
logit_bias: options.logitBias,
user: options.user,
functions: options.functions,
function_call: options.function_call
}),
},
);
Expand Down
60 changes: 60 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,64 @@ export interface ChatCompletionOptions {
* https://platform.openai.com/docs/api-reference/chat/create#chat/create-user
*/
user?: string;

/**
* A list of functions the model may generate JSON inputs for.
* https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions
*/
functions?: ChatCompletionOptionsFunction[];

/**
* Controls how the model responds to function calls.
* "none" means the model does not call a function, and responds to the end-user.
* "auto" means the model can pick between an end-user or calling a function.
* Specifying a particular function via {"name":\ "my_function"} forces the model to call that function.
* "none" is the default when no functions are present. "auto" is the default if functions are present.
* https://platform.openai.com/docs/api-reference/chat/create#chat/create-function_call
*/
function_call?: 'none' | 'auto' | { name: string };
}

export type ChatCompletionOptionsFunction = {
name: string;
description: string;
parameters: ObjectSchema;
};

type JSONSchema = (
| ObjectSchema
| StringSchema
| NumberSchema
| BooleanSchema
| ArraySchema
) & { description?: string };

type ObjectSchema = {
type: "object";
properties: Record<string, JSONSchema>;
required: string[];
};

type ArraySchema = {
type: "array";
items: JSONSchema;
};

type StringSchema = {
type: "string";
enum?: string[];
};

type NumberSchema = {
type: "number";
minimum?: number;
maximum?: number;
};

type BooleanSchema = {
type: "boolean";
};

export interface EditOptions {
/**
* ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint.
Expand Down Expand Up @@ -616,6 +672,10 @@ export interface ChatCompletion {
name?: string;
role: "system" | "assistant" | "user";
content: string;
function_call?: {
"name": string,
"arguments": string
}
};
finish_reason: string;
}[];
Expand Down

0 comments on commit 38ffa5e

Please sign in to comment.