Skip to content

Commit af717e8

Browse files
authored
Add tool use support
Closes #7.
1 parent ab9f257 commit af717e8

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

README.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -137,45 +137,47 @@ const result = await multiUserSession.prompt([
137137

138138
Because of their special behavior of being preserved on context window overflow, system prompts cannot be provided this way.
139139

140-
### Emulating tool use or function-calling via assistant-role prompts
140+
### Tool use
141141

142-
A special case of the above is using the assistant role to emulate tool use or function-calling, by marking a response as coming from the assistant side of the conversation:
142+
The Prompt API supports **tool use** via the `tools` option, allowing you to define external capabilities that a language model can invoke in a model-agnostic way. Each tool is represented by an object that includes an `execute` member that specifies the JavaScript function to be called. When the language model initiates a tool use request, the user agent calls the corresponding `execute` function and sends the result back to the model.
143+
144+
Here’s an example of how to use the `tools` option:
143145

144146
```js
145147
const session = await LanguageModel.create({
146-
initialPrompts: [{
147-
role: "system",
148-
content: `
149-
You are a helpful assistant. You have access to the following tools:
150-
- calculator: A calculator. To use it, write "CALCULATOR: <expression>" where <expression> is a valid mathematical expression.
151-
`
152-
}]
148+
initialPrompts: [
149+
{
150+
role: "system",
151+
content: `You are a helpful assistant. You can use tools to help the user.`
152+
}
153+
],
154+
tools: [
155+
{
156+
name: "getWeather",
157+
description: "Get the weather in a location.",
158+
inputSchema: {
159+
type: "object",
160+
properties: {
161+
location: {
162+
type: "string",
163+
description: "The city to check for the weather condition.",
164+
},
165+
},
166+
required: ["location"],
167+
},
168+
async execute({ location }) {
169+
const res = await fetch("https://weatherapi.example/?location=" + location);
170+
// Returns the result as a JSON string.
171+
return JSON.stringify(await res.json());
172+
},
173+
}
174+
]
153175
});
154176

155-
async function promptWithCalculator(prompt) {
156-
const result = await session.prompt(prompt);
157-
158-
// Check if the assistant wants to use the calculator tool.
159-
const match = /^CALCULATOR: (.*)$/.exec(result);
160-
if (match) {
161-
const expression = match[1];
162-
const mathResult = evaluateMathExpression(expression);
163-
164-
// Add the result to the session so it's in context going forward.
165-
await session.prompt([{ role: "assistant", content: mathResult }]);
166-
167-
// Return it as if that's what the assistant said to the user.
168-
return mathResult;
169-
}
170-
171-
// The assistant didn't want to use the calculator. Just return its response.
172-
return result;
173-
}
174-
175-
console.log(await promptWithCalculator("What is 2 + 2?"));
177+
const result = await session.prompt("What is the weather in Seattle?");
176178
```
177179

178-
We'll likely explore more specific APIs for tool- and function-calling in the future; follow along in [issue #7](https://github.com/webmachinelearning/prompt-api/issues/7).
180+
In this example, the `tools` array defines a `getWeather` tool, specifying its name, description, input schema, and `execute` implementation. When the language model determines that a tool call is needed, the user agent invokes the `getWeather` tool's `execute()` function with the provided arguments and returns the result to the model, which can then incorporate it into its response.
179181

180182
### Multimodal inputs
181183

index.bs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ interface LanguageModelParams {
7575
readonly attribute float maxTemperature;
7676
};
7777

78+
79+
callback LanguageModelToolFunction = Promise<DOMString> (any... arguments);
80+
81+
// A description of a tool call that a language model can invoke.
82+
dictionary LanguageModelTool {
83+
required DOMString name;
84+
required DOMString description;
85+
// JSON schema for the input parameters.
86+
required object inputSchema;
87+
// The function to be invoked by user agent on behalf of language model.
88+
required LanguageModelToolFunction execute;
89+
};
90+
7891
dictionary LanguageModelCreateCoreOptions {
7992
// Note: these two have custom out-of-range handling behavior, not in the IDL layer.
8093
// They are unrestricted double so as to allow +Infinity without failing.
@@ -83,6 +96,7 @@ dictionary LanguageModelCreateCoreOptions {
8396

8497
sequence<LanguageModelExpected> expectedInputs;
8598
sequence<LanguageModelExpected> expectedOutputs;
99+
sequence<LanguageModelTool> tools;
86100
};
87101

88102
dictionary LanguageModelCreateOptions : LanguageModelCreateCoreOptions {

0 commit comments

Comments
 (0)