Skip to content

Commit 2797399

Browse files
committed
feat(genapi): parallel function call
1 parent 03d4059 commit 2797399

File tree

1 file changed

+104
-10
lines changed

1 file changed

+104
-10
lines changed

ai-data/generative-apis/how-to/use-function-calling.mdx

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,106 @@ if response.choices[0].message.tool_calls:
184184
print(final_response.choices[0].message.content)
185185
```
186186

187+
### Parallel function calling
188+
189+
In addition to one function call described above, you can also call multiple functions in a single turn.
190+
This section shows an example for how you can use parallel function calling.
191+
192+
Define the tools:
193+
194+
```
195+
def open_floor_space(floor_number: int) -> bool:
196+
"""Opens up the specified floor for party space by unlocking doors and moving furniture."""
197+
print(f"Floor {floor_number} is now open party space!")
198+
return True
199+
200+
def set_lobby_vibe(party_mode: bool) -> str:
201+
"""Switches lobby screens and lighting to party mode."""
202+
status = "party mode activated!" if party_mode else "back to business mode"
203+
print(f"Lobby is now in {status}")
204+
return "The lobby is ready to party!"
205+
206+
def prep_snack_station(activate: bool) -> bool:
207+
"""Converts the cafeteria into a snack and drink station."""
208+
print(f"Snack station is {'open and stocked!' if activate else 'closed.'}")
209+
return True
210+
```
211+
212+
Define the specifications:
213+
214+
```
215+
tools = [
216+
{
217+
"type": "function",
218+
"function": {
219+
"name": "open_floor_space",
220+
"description": "Opens up an entire floor for the party",
221+
"parameters": {
222+
"type": "object",
223+
"properties": {
224+
"floor_number": {
225+
"type": "integer",
226+
"description": "Which floor to open up"
227+
}
228+
},
229+
"required": ["floor_number"]
230+
}
231+
}
232+
},
233+
{
234+
"type": "function",
235+
"function": {
236+
"name": "set_lobby_vibe",
237+
"description": "Transform lobby atmosphere into party mode",
238+
"parameters": {
239+
"type": "object",
240+
"properties": {
241+
"party_mode": {
242+
"type": "boolean",
243+
"description": "True for party, False for business"
244+
}
245+
},
246+
"required": ["party_mode"]
247+
}
248+
}
249+
},
250+
{
251+
"type": "function",
252+
"function": {
253+
"name": "prep_snack_station",
254+
"description": "Set up the snack and drink station",
255+
"parameters": {
256+
"type": "object",
257+
"properties": {
258+
"activate": {
259+
"type": "boolean",
260+
"description": "True to open, False to close"
261+
}
262+
},
263+
"required": ["activate"]
264+
}
265+
}
266+
}
267+
]
268+
```
269+
270+
Next, call the model with proper instructions
271+
272+
```
273+
system_prompt = """
274+
You are an office party control assistant. When asked to transform the office into a party space, you should:
275+
1. Open up a floor for the party
276+
2. Transform the lobby into party mode
277+
3. Set up the snack station
278+
Make all these changes at once for an instant office party!
279+
"""
280+
281+
messages = [
282+
{"role": "system", "content": system_prompt},
283+
{"role": "user", "content": "Turn this office building into a party!"}
284+
]
285+
```
286+
187287
## Best practices
188288

189289
When implementing function calling, follow these guidelines for optimal results:
@@ -220,14 +320,8 @@ For more information about function calling and advanced implementations, refer
220320
- [JSON Schema Specification](https://json-schema.org/specification)
221321
- [Chat Completions API Reference](/ai-data/generative-apis/api-cli/using-chat-api/)
222322

223-
## Conclusion
224-
225-
Function calling significantly extends the capabilities of language models by allowing them to interact with external tools and APIs. By following the patterns and best practices outlined in this guide, you can create robust applications that combine the power of language models with practical functionality.
323+
Function calling significantly extends the capabilities of language models by allowing them to interact with external tools and APIs.
226324

227-
When implementing function calling, remember to:
228-
- Design clear and focused function definitions
229-
- Handle multi-turn conversations appropriately
230-
- Follow best practices for optimal performance
231-
- Implement proper error handling and validation
232-
233-
Experiment with both simple and complex implementations to determine which approach best suits your application's needs.
325+
<Message type="note">
326+
We can't wait to see what you will build with function calls. Tell us what you are up to, share your experiments on Scaleway's Slack community #ai
327+
</Message>

0 commit comments

Comments
 (0)