Skip to content

Commit

Permalink
feat: handle function calls
Browse files Browse the repository at this point in the history
BREAKING CHANGE: funciton calls are handled by the createChat abstraction
Just define the function itself inside the functions, e.g.
```diff
{
  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: {
+    location: "Albuquerque",
+    temperature: "72",
+    unit: "fahrenheit",
+    forecast: ["sunny", "windy"],
+  },
},
```
  • Loading branch information
lucgagan committed Jun 25, 2023
1 parent 29d21e6 commit f3743c5
Show file tree
Hide file tree
Showing 8 changed files with 3,855 additions and 95 deletions.
38 changes: 14 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const chat = createChat({
await chat.sendMessage("continue the sequence: a b c", {
onUpdate: (message) => {
console.log(message);
}
},
});
```

Expand Down Expand Up @@ -175,8 +175,8 @@ const response = await chat.sendMessage(
options: {
maxTokens: 1,
// token 34093 is "boo"
logitBias: { "34093": 100 }
}
logitBias: { "34093": 100 },
},
}
);

Expand Down Expand Up @@ -211,33 +211,23 @@ const chat = createChat({
},
required: ["location"],
},
function: async ({ location }) => {
return {
location: "Albuquerque",
temperature: "72",
unit: "fahrenheit",
forecast: ["sunny", "windy"],
};
},
},
],
functionCall: "auto",
});

const responseWantingFunction = await chat.sendMessage(
"What is the weather in Albuquerque?"
);
const response = await chat.sendMessage("What is the weather in Albuquerque?");

if (responseWantingFunction.function_call?.name === "get_current_weather") {
const response = await chat.sendMessage(
JSON.stringify({
location: "Albuquerque",
temperature: "72",
unit: "fahrenheit",
forecast: ["sunny", "windy"],
}),
{
functionName: "get_current_weather"
}
);

console.log(response.content);
// "The weather in Albuquerque is 72 degrees fahrenheit, sunny with a light breeze."
} else {
// handle responses like normal otherwise
}
console.log(response.content);
// "The weather in Albuquerque is 72 degrees fahrenheit, sunny with a light breeze."
```

## My other projects
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"ajv": "^8.12.0",
"ts-custom-error": "^3.3.1",
"zod": "^3.21.4"
},
Expand Down

0 comments on commit f3743c5

Please sign in to comment.