Skip to content

Commit f6f099c

Browse files
committed
Update parallel to show execution
1 parent 02c0c36 commit f6f099c

File tree

5 files changed

+157
-19
lines changed

5 files changed

+157
-19
lines changed

function_calling_fewshots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def search_database(search_query: str, price_filter: dict | None = None) -> dict
101101
"type": "function",
102102
"function": {"name": "search_database", "arguments": '{"search_query":"climbing gear outside"}'},
103103
}
104-
]
104+
],
105105
},
106106
{
107107
"role": "tool",

function_calling_parallel.py

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
import os
3+
from concurrent.futures import ThreadPoolExecutor
24

35
import azure.identity
46
import openai
@@ -76,17 +78,83 @@
7678
},
7779
]
7880

81+
82+
# ---------------------------------------------------------------------------
83+
# Tool (function) implementations
84+
# ---------------------------------------------------------------------------
85+
def lookup_weather(city_name: str | None = None, zip_code: str | None = None) -> str:
86+
"""Looks up the weather for given city_name and zip_code."""
87+
location = city_name or zip_code or "unknown"
88+
# In a real implementation, call an external weather API here.
89+
return {
90+
"location": location,
91+
"condition": "rain showers",
92+
"rain_mm_last_24h": 7,
93+
"recommendation": "Good day for indoor activities if you dislike drizzle.",
94+
}
95+
96+
97+
def lookup_movies(city_name: str | None = None, zip_code: str | None = None) -> str:
98+
"""Returns a list of movies playing in the given location."""
99+
location = city_name or zip_code or "unknown"
100+
# A real implementation could query a cinema listings API.
101+
return {
102+
"location": location,
103+
"movies": [
104+
{"title": "The Quantum Reef", "rating": "PG-13"},
105+
{"title": "Storm Over Harbour Bay", "rating": "PG"},
106+
{"title": "Midnight Koala", "rating": "R"},
107+
],
108+
}
109+
110+
111+
messages = [
112+
{"role": "system", "content": "You are a tourism chatbot."},
113+
{"role": "user", "content": "is it rainy enough in sydney to watch movies and which ones are on?"},
114+
]
79115
response = client.chat.completions.create(
80116
model=MODEL_NAME,
81-
messages=[
82-
{"role": "system", "content": "You are a tourism chatbot."},
83-
{"role": "user", "content": "is it rainy enough in sydney to watch movies and which ones are on?"},
84-
],
117+
messages=messages,
85118
tools=tools,
86119
tool_choice="auto",
87120
)
88121

89122
print(f"Response from {MODEL_NAME} on {API_HOST}: \n")
90-
for message in response.choices[0].message.tool_calls:
91-
print(message.function.name)
92-
print(message.function.arguments)
123+
124+
# Map function names to actual functions
125+
available_functions = {
126+
"lookup_weather": lookup_weather,
127+
"lookup_movies": lookup_movies,
128+
}
129+
130+
# Execute all tool calls in parallel using ThreadPoolExecutor
131+
if response.choices[0].message.tool_calls:
132+
tool_calls = response.choices[0].message.tool_calls
133+
print(f"Model requested {len(tool_calls)} tool call(s):\n")
134+
135+
# Add the assistant's message (with tool calls) to the conversation
136+
messages.append(response.choices[0].message)
137+
138+
with ThreadPoolExecutor() as executor:
139+
# Submit all tool calls to the thread pool
140+
futures = []
141+
for tool_call in tool_calls:
142+
function_name = tool_call.function.name
143+
arguments = json.loads(tool_call.function.arguments)
144+
print(f"Tool request: {function_name}({arguments})")
145+
146+
if function_name in available_functions:
147+
future = executor.submit(available_functions[function_name], **arguments)
148+
futures.append((tool_call, function_name, future))
149+
150+
# Add each tool result to the conversation
151+
for tool_call, function_name, future in futures:
152+
result = future.result()
153+
messages.append({"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result)})
154+
155+
# Get final response from the model with all tool results
156+
final_response = client.chat.completions.create(model=MODEL_NAME, messages=messages, tools=tools)
157+
print("Assistant:")
158+
print(final_response.choices[0].message.content)
159+
else:
160+
print(response.choices[0].message.content)

function_calling_while_loop.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def lookup_movies(city_name: str | None = None, zip_code: str | None = None) ->
124124
print(f"Model: {MODEL_NAME} on Host: {API_HOST}\n")
125125

126126
while True:
127+
print("Calling model...\n")
127128
response = client.chat.completions.create(
128129
model=MODEL_NAME,
129130
messages=messages, # includes prior tool outputs

spanish/function_calling_parallel.py

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
import os
3+
from concurrent.futures import ThreadPoolExecutor
24

35
import azure.identity
46
import openai
@@ -76,20 +78,86 @@
7678
},
7779
]
7880

81+
82+
# ---------------------------------------------------------------------------
83+
# Tool (function) implementations
84+
# ---------------------------------------------------------------------------
85+
def lookup_weather(city_name: str | None = None, zip_code: str | None = None) -> str:
86+
"""Looks up the weather for given city_name and zip_code."""
87+
location = city_name or zip_code or "unknown"
88+
# In a real implementation, call an external weather API here.
89+
return {
90+
"location": location,
91+
"condition": "rain showers",
92+
"rain_mm_last_24h": 7,
93+
"recommendation": "Good day for indoor activities if you dislike drizzle.",
94+
}
95+
96+
97+
def lookup_movies(city_name: str | None = None, zip_code: str | None = None) -> str:
98+
"""Returns a list of movies playing in the given location."""
99+
location = city_name or zip_code or "unknown"
100+
# A real implementation could query a cinema listings API.
101+
return {
102+
"location": location,
103+
"movies": [
104+
{"title": "The Quantum Reef", "rating": "PG-13"},
105+
{"title": "Storm Over Harbour Bay", "rating": "PG"},
106+
{"title": "Midnight Koala", "rating": "R"},
107+
],
108+
}
109+
110+
111+
messages = [
112+
{"role": "system", "content": "Eres un chatbot de turismo."},
113+
{
114+
"role": "user",
115+
"content": "¿Está lloviendo lo suficiente en Sídney como para ver películas y cuáles estan en los cines?",
116+
},
117+
]
79118
response = client.chat.completions.create(
80119
model=MODEL_NAME,
81-
messages=[
82-
{"role": "system", "content": "Eres un chatbot de turismo."},
83-
{
84-
"role": "user",
85-
"content": "¿Está lloviendo lo suficiente en Sídney como para ver películas y cuáles estan en los cines?",
86-
},
87-
],
120+
messages=messages,
88121
tools=tools,
89122
tool_choice="auto",
90123
)
91124

92-
print(f"Respuesta de {API_HOST}: \n")
93-
for message in response.choices[0].message.tool_calls:
94-
print(message.function.name)
95-
print(message.function.arguments)
125+
print(f"Respuesta de {MODEL_NAME} en {API_HOST}: \n")
126+
127+
# Map function names to actual functions
128+
available_functions = {
129+
"lookup_weather": lookup_weather,
130+
"lookup_movies": lookup_movies,
131+
}
132+
133+
# Execute all tool calls in parallel using ThreadPoolExecutor
134+
if response.choices[0].message.tool_calls:
135+
tool_calls = response.choices[0].message.tool_calls
136+
print(f"El modelo solicitó {len(tool_calls)} llamada(s) de herramienta:\n")
137+
138+
# Add the assistant's message (with tool calls) to the conversation
139+
messages.append(response.choices[0].message)
140+
141+
with ThreadPoolExecutor() as executor:
142+
# Submit all tool calls to the thread pool
143+
futures = []
144+
for tool_call in tool_calls:
145+
function_name = tool_call.function.name
146+
arguments = json.loads(tool_call.function.arguments)
147+
print(f"Solicitud de herramienta: {function_name}({arguments})")
148+
149+
if function_name in available_functions:
150+
future = executor.submit(available_functions[function_name], **arguments)
151+
futures.append((tool_call, function_name, future))
152+
153+
# Add each tool result to the conversation
154+
for tool_call, function_name, future in futures:
155+
result = future.result()
156+
messages.append({"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result)})
157+
158+
# Get final response from the model with all tool results
159+
final_response = client.chat.completions.create(model=MODEL_NAME, messages=messages, tools=tools)
160+
print("Asistente:")
161+
print(final_response.choices[0].message.content)
162+
else:
163+
print(response.choices[0].message.content)

spanish/function_calling_while_loop.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def lookup_movies(city_name: str | None = None, zip_code: str | None = None) ->
109109
print(f"Modelo: {MODEL_NAME} en Host: {API_HOST}\n")
110110

111111
while True:
112+
print("Invocando el modelo...\n")
112113
response = client.chat.completions.create(
113114
model=MODEL_NAME,
114115
messages=messages,

0 commit comments

Comments
 (0)