Skip to content

Commit d026610

Browse files
fpagnyRoRoJ
andauthored
feat(genapi): add snippets for tool call with stream mode (#5906)
* feat(genapi): add snippets for tool call with stream mode * Update pages/generative-apis/how-to/use-function-calling.mdx --------- Co-authored-by: Rowena Jones <36301604+RoRoJ@users.noreply.github.com>
1 parent 402f57a commit d026610

File tree

1 file changed

+81
-3
lines changed

1 file changed

+81
-3
lines changed

pages/generative-apis/how-to/use-function-calling.mdx

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ This section shows an example for how you can use parallel function calling.
202202

203203
Define the tools:
204204

205-
```
205+
```python
206206
def open_floor_space(floor_number: int) -> bool:
207207
"""Opens up the specified floor for party space by unlocking doors and moving furniture."""
208208
print(f"Floor {floor_number} is now open party space!")
@@ -222,7 +222,7 @@ def prep_snack_station(activate: bool) -> bool:
222222

223223
Define the specifications:
224224

225-
```
225+
```python
226226
tools = [
227227
{
228228
"type": "function",
@@ -280,7 +280,7 @@ tools = [
280280

281281
Next, call the model with proper instructions
282282

283-
```
283+
```python
284284
system_prompt = """
285285
You are an office party control assistant. When asked to transform the office into a party space, you should:
286286
1. Open up a floor for the party
@@ -295,6 +295,84 @@ messages = [
295295
]
296296
```
297297

298+
### Tool calling with stream mode
299+
300+
Tool calling can be performed using stream mode.
301+
302+
<Message type="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
330+
tool_calls.append({
331+
"id": choice.delta.tool_calls[0].id,
332+
"type": "function",
333+
"function":{
334+
"name": choice.delta.tool_calls[0].function.name,
335+
"arguments": ""
336+
}
337+
})
338+
tool_call_index = choice.delta.tool_calls[0].index
339+
if choice.delta.tool_calls[0].function.arguments: # Store function arguments
340+
tool_calls[tool_call_index]["function"]["arguments"] += choice.delta.tool_calls[0].function.arguments
341+
if choice.finish_reason and (chunk.choices[0].finish_reason == "tool_calls"):
342+
tool_call_required = True
343+
344+
# Process the tool call
345+
if tool_call_required and (len(tool_calls) >= 1):
346+
tool_call = tool_calls[0]
347+
348+
# Execute the function
349+
if tool_call["function"]["name"] == "get_flight_schedule":
350+
function_args = json.loads(tool_call["function"]["arguments"])
351+
function_response = get_flight_schedule(**function_args)
352+
353+
# Add results to the conversation
354+
messages.extend([
355+
{
356+
"role": "assistant",
357+
"content": None,
358+
"tool_calls": [tool_call]
359+
},
360+
{
361+
"role": "tool",
362+
"name": tool_call["function"]["name"],
363+
"content": json.dumps(function_response),
364+
"tool_call_id": tool_call["id"]
365+
}
366+
])
367+
368+
# Get final response
369+
final_response = client.chat.completions.create(
370+
model="llama-3.1-70b-instruct",
371+
messages=messages
372+
)
373+
print(final_response.choices[0].message.content)
374+
```
375+
298376
## Code example for Responses API
299377

300378
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

Comments
 (0)