Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix multiple function invocation #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/createChat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,52 @@ test("overrides function call", async () => {
assert.equal(getCurrentWeather.mock.calls.length, 0);
});

test("calls a user defined function more than once", async () => {
const getCurrentWeather = mock.fn(({ location }: { location: string }) => {
return {
location,
temperature: "72",
unit: "fahrenheit",
forecast: ["sunny", "windy"],
};
});

const chat = createChat({
apiKey: OPENAI_API_KEY,
model: "gpt-3.5-turbo-0613",
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"],
},
function: getCurrentWeather,
},
],
});

await chat.sendMessage("Tell me the weather in Albuquerque and Chicago?");

assert.equal(getCurrentWeather.mock.calls.length, 2);
assert.equal(
getCurrentWeather.mock.calls[0].arguments[0].location,
"Albuquerque"
);
assert.equal(
getCurrentWeather.mock.calls[1].arguments[0].location,
"Chicago"
);
});

test("overrides message options", async () => {
const chat = createChat({
apiKey: OPENAI_API_KEY,
Expand Down
5 changes: 4 additions & 1 deletion src/createChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const createChat = (

messages.push(omit(choice, "finishReason"));

if (choice.function_call) {
while (choice.function_call) {
const functionName = choice.function_call.name;

const userFunction = userFunctions[functionName];
Expand All @@ -204,6 +204,9 @@ export const createChat = (

choice = await complete(messageOptions);

messages.push(omit(choice, "finishReason"));

// TODO record a trail of function calls
choice.functionCall = {
name: functionName,
arguments: functionArgs,
Expand Down
Loading