You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
awaitfastChemistryRag.queryRag({ prompt: "What is the atomic number of gold?" }),
48
+
(awaitfastChemistryRag.queryRag({ prompt: "What is the atomic number of gold?" })).content,
49
49
);
50
50
51
51
// handle the Response object
@@ -74,63 +74,60 @@ console.log(
74
74
75
75
The `@gel/ai` package supports tool calls, allowing you to extend the capabilities of the AI model with your own functions. Here's how to use them:
76
76
77
-
1.**Define the tool:** Create a `SystemMessage`that describes the tool, its parameters, and when it should be used.
78
-
2.**Send the request:** Send a request to the model using `queryRag`, including the user's prompt and the tool definition.
79
-
3.**Handle the tool call:** If the model decides to use the tool, it will return an `AssistantMessage` with a `tool_calls` array. Your code needs to:
77
+
1.**Define your tools**: Create an array of `ToolDefinition` objects that describe your functions, their parameters, and what they do.
78
+
2.**Send the request**: Call `queryRag` or `streamRag` with the user's prompt and the `tools` array. You can also use the `tool_choice` parameter to control how the model uses your tools.
79
+
3.**Handle the tool call**: If the model decides to use a tool, it will return an `AssistantMessage` with a `tool_calls` array. Your code needs to:
80
80
a. Parse the `tool_calls` array to identify the tool and its arguments.
81
-
b. Execute the tool.
81
+
b. Execute the tool and get the result.
82
82
c. Create a `ToolMessage` with the result.
83
-
d. Send the `ToolMessage` back to the model.
84
-
4.**Receive the final response:** The model will use the tool's output to generate a final response.
83
+
d. Send the `ToolMessage` back to the model in a new request.
84
+
4.**Receive the final response**: The model will use the tool's output to generate a final response.
85
85
86
86
### Example
87
87
88
88
```typescript
89
89
importtype {
90
90
Message,
91
-
SystemMessage,
91
+
ToolDefinition,
92
92
UserMessage,
93
93
ToolMessage,
94
94
AssistantMessage,
95
95
} from"@gel/ai";
96
96
97
-
// 1. Define the tool in a system message
98
-
const systemMessage:SystemMessage= {
99
-
role: "system",
100
-
content: `
101
-
You have access to a tool called "get_weather" that takes a city as a parameter.
102
-
Use this tool to answer questions about the weather.
103
-
The tool definition is:
104
-
{
105
-
"name": "get_weather",
106
-
"description": "Get the current weather for a given city.",
107
-
"parameters": {
108
-
"type": "object",
109
-
"properties": {
110
-
"city": {
111
-
"type": "string",
112
-
"description": "The city to get the weather for."
113
-
}
97
+
// 1. Define your tools
98
+
const tools:ToolDefinition[] = [
99
+
{
100
+
type: "function",
101
+
name: "get_weather",
102
+
description: "Get the current weather for a given city.",
103
+
parameters: {
104
+
type: "object",
105
+
properties: {
106
+
city: {
107
+
type: "string",
108
+
description: "The city to get the weather for.",
114
109
},
115
-
"required": ["city"]
116
-
}
117
-
}
118
-
`,
119
-
};
110
+
},
111
+
required: ["city"],
112
+
},
113
+
},
114
+
];
120
115
121
116
// 2. Send the request
122
117
const userMessage:UserMessage= {
123
118
role: "user",
124
119
content: [{ type: "text", text: "What's the weather like in London?" }],
0 commit comments