Skip to content

Commit 14d59fc

Browse files
authored
Enhance README with tool use modes and examples
Added detailed explanations for tool use modes, including examples for open loop and closed loop execution.
1 parent 61cf689 commit 14d59fc

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

README.md

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,74 @@ Because of their special behavior of being preserved on context window overflow,
141141

142142
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.
143143

144-
Here’s an example of how to use the `tools` option:
144+
There are 2 tool use modes: with automatic execution (closed loop) and without automatic execution (open loop)
145+
146+
Regardless of with or without automatic execution, the session creation and appending signature are the same. Here’s an example:
147+
148+
```js
149+
const session = await LanguageModel.create({
150+
initialPrompts: [
151+
{
152+
role: "system",
153+
content: `You are a helpful assistant. You can use tools to help the user.`
154+
}
155+
],
156+
tools: [
157+
{
158+
name: "getWeather",
159+
description: "Get the weather in a location.",
160+
inputSchema: {
161+
type: "object",
162+
properties: {
163+
location: {
164+
type: "string",
165+
description: "The city to check for the weather condition.",
166+
},
167+
},
168+
required: ["location"],
169+
},
170+
}
171+
]
172+
});
173+
```
174+
175+
In this example, the `tools` array defines a `getWeather` tool, specifying its name, description and input schema.
176+
177+
Few shot examples of tool use can be appended like so:
178+
179+
```js
180+
await session.append([
181+
{role: "user", content: "What is the weather in Seattle?"},
182+
{role: "tool-call", content: {type: "tool-call", value: {callID:" get_weather_1", name: "get_weather", arguments: {location:"Seattle"}}},
183+
{role: "tool-result", content: {type: "tool-response", value: {callID: "get_weather_1", name: "get_weather", result: [{type:"object", value: {temperature: "55F", humidity: "67%"}}]}},
184+
{role: "assistant", content: "The temperature in Seattle is 55F and humidity is 67%"},
185+
]);
186+
```
187+
188+
Note that "role" and "type" now supports "tool-call" and "tool-result". `content.result` is a list of a dictionary of `type` and `value`, where `type` can be `{"text", "image", "audio", "object" }` and `value` is `any`.
189+
190+
#### Open Loop:
191+
192+
Without automatic execution, the API will return a `ToolCall` object with `callId` (a unique identifier of this tool call), `name` (name of the tool), and `arguments` (a dictionary fitting the JSON input schema of the tool's declaration), and client is expected to handle the tool execution and append the tool result back to the session.
193+
194+
Example:
195+
196+
```js
197+
const result = await session.prompt("What is the weather in Seattle?");
198+
if (result.type=="tool-call") {
199+
if (result.name == "get_weather") {
200+
const tool_result = getWeather(result.arguments.location);
201+
session.prompt([{role:"tool-result", content: {type: "tool-result", value: {callId: result.callID, name: result.name, result: [{type:"object", value: tool_result}]}}}])
202+
}
203+
}
204+
```
205+
206+
Note that we always require tool-response to immediately follow tool-call generated by the model.
207+
208+
209+
#### Closed Loop:
210+
211+
To enable automatic execution, add a `execute` function for each tool's implementation, and add a `toolUseConfig` to indicate that execution is enabled and pose a max number of tool calls invoked in a single session generation:
145212
146213
```js
147214
const session = await LanguageModel.create({
@@ -171,13 +238,14 @@ const session = await LanguageModel.create({
171238
return JSON.stringify(await res.json());
172239
},
173240
}
174-
]
241+
],
242+
toolUseConfig: {enabled: true, max_tool_calls: 5},
175243
});
176244

177245
const result = await session.prompt("What is the weather in Seattle?");
178246
```
179247
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.
248+
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.
181249
182250
#### Concurrent tool use
183251

0 commit comments

Comments
 (0)