@@ -143,17 +143,17 @@ If you need to cancel a stream, you can `break` from a `for await` loop or call
143
143
144
144
### Automated function calls
145
145
146
- We provide ` openai.beta.chat.completions.runFunctions({…}) ` and ` openai.beta.chat.completions.runTools({…}) `
147
- convenience helpers for using function calls with the ` /chat/completions ` endpoint
146
+ We provide the ` openai.beta.chat.completions.runTools({…}) `
147
+ convenience helper for using function tool calls with the ` /chat/completions ` endpoint
148
148
which automatically call the JavaScript functions you provide
149
149
and sends their results back to the ` /chat/completions ` endpoint,
150
- looping as long as the model requests function calls.
150
+ looping as long as the model requests tool calls.
151
151
152
152
If you pass a ` parse ` function, it will automatically parse the ` arguments ` for you
153
153
and returns any parsing errors to the model to attempt auto-recovery.
154
154
Otherwise, the args will be passed to the function you provide as a string.
155
155
156
- If you pass ` function_call: {name: …} ` or ` tool_choice: {function: {name: …}} ` instead of ` auto ` ,
156
+ If you pass ` tool_choice: {function: {name: …}} ` instead of ` auto ` ,
157
157
it returns immediately after calling that function (and only loops to auto-recover parsing errors).
158
158
159
159
``` ts
@@ -163,21 +163,27 @@ const client = new OpenAI();
163
163
164
164
async function main() {
165
165
const runner = client .beta .chat .completions
166
- .runFunctions ({
166
+ .runTools ({
167
167
model: ' gpt-3.5-turbo' ,
168
168
messages: [{ role: ' user' , content: ' How is the weather this week?' }],
169
- functions : [
169
+ tools : [
170
170
{
171
- function: getCurrentLocation ,
172
- parameters: { type: ' object' , properties: {} },
171
+ type: ' function' ,
172
+ function: {
173
+ function: getCurrentLocation ,
174
+ parameters: { type: ' object' , properties: {} },
175
+ },
173
176
},
174
177
{
175
- function: getWeather ,
176
- parse: JSON .parse , // or use a validation library like zod for typesafe parsing.
177
- parameters: {
178
- type: ' object' ,
179
- properties: {
180
- location: { type: ' string' },
178
+ type: ' function' ,
179
+ function: {
180
+ function: getWeather ,
181
+ parse: JSON .parse , // or use a validation library like zod for typesafe parsing.
182
+ parameters: {
183
+ type: ' object' ,
184
+ properties: {
185
+ location: { type: ' string' },
186
+ },
181
187
},
182
188
},
183
189
},
@@ -203,17 +209,19 @@ async function getWeather(args: { location: string }) {
203
209
main ();
204
210
205
211
// {role: "user", content: "How's the weather this week?"}
206
- // {role: "assistant", function_call: " getCurrentLocation", arguments: "{}"}
207
- // {role: "function ", name: "getCurrentLocation", content: "Boston"}
208
- // {role: "assistant", function_call: " getWeather", arguments: '{"location": "Boston"}'}
209
- // {role: "function ", name: "getWeather", content: '{"temperature": "50degF", "preciptation": "high"}'}
212
+ // {role: "assistant", tool_calls: [{type: "function", function: {name: " getCurrentLocation", arguments: "{}"}, id: "123 "}
213
+ // {role: "tool ", name: "getCurrentLocation", content: "Boston", tool_call_id: "123 "}
214
+ // {role: "assistant", tool_calls: [{type: "function", function: {name: " getWeather", arguments: '{"location": "Boston"}'}, id: "1234"}] }
215
+ // {role: "tool ", name: "getWeather", content: '{"temperature": "50degF", "preciptation": "high"}', tool_call_id: "1234" }
210
216
// {role: "assistant", content: "It's looking cold and rainy - you might want to wear a jacket!"}
211
217
//
212
218
// Final content: "It's looking cold and rainy - you might want to wear a jacket!"
213
219
```
214
220
215
221
Like with ` .stream() ` , we provide a variety of [ helpers and events] ( helpers.md#events ) .
216
222
223
+ Note that ` runFunctions ` was previously available as well, but has been deprecated in favor of ` runTools ` .
224
+
217
225
Read more about various examples such as with integrating with [ zod] ( helpers.md#integrate-with-zod ) ,
218
226
[ next.js] ( helpers.md#integrate-wtih-next-js ) , and [ proxying a stream to the browser] ( helpers.md#proxy-streaming-to-a-browser ) .
219
227
0 commit comments