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
You are an office party control assistant. When asked to transform the office into a party space, you should:
286
286
1. Open up a floor for the party
@@ -295,6 +295,84 @@ messages = [
295
295
]
296
296
```
297
297
298
+
### Tool calling with stream mode
299
+
300
+
Tool calling can be performed using stream mode.
301
+
302
+
<Messagetype="note">
303
+
Most workflows using tools will require multiple steps before a final useful answer can be provided the end user. Since stream mode adds an additional complexity to parse elements from each events, we recommend disabling stream mode when using tool calling for the first time.
304
+
</Message>
305
+
306
+
Because tool arguments are formatted in `JSON` but sent gradually in each event, multiple response events need to be aggregated and then parsed as `JSON` to perform the tool call.
307
+
If you want to use tool calls with streaming, replace the last part of your code with the following:
308
+
309
+
```python
310
+
# Make the API call
311
+
response = client.chat.completions.create(
312
+
model="llama-3.1-70b-instruct",
313
+
messages=messages,
314
+
tools=tools,
315
+
tool_choice="auto",
316
+
stream=True
317
+
)
318
+
319
+
tool_calls = []
320
+
tool_call_index =0
321
+
tool_call_required =False
322
+
323
+
for chunk in response:
324
+
if chunk.choices and (len(chunk.choices) >=1):
325
+
choice = chunk.choices[0]
326
+
if choice.delta.content: # Pass text content
327
+
pass
328
+
if choice.delta.tool_calls:
329
+
if choice.delta.tool_calls[0].function.name: # Store function name and id
See the OpenAPI documentation for a fully worked example on [function calling using the Responses API](https://platform.openai.com/docs/guides/function-calling#function-tool-example). Note that Scaleway's support of the Responses API is currently at beta stage - [find out more](/generative-apis/how-to/query-language-models/#chat-completions-api-or-responses-api).
0 commit comments