Skip to content

Commit 74f69b4

Browse files
committed
python function code
1 parent 5d1ac46 commit 74f69b4

File tree

1 file changed

+63
-44
lines changed

1 file changed

+63
-44
lines changed

articles/ai-services/openai/how-to/assistant-functions.md

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ services: cognitive-services
66
manager: nitinme
77
ms.service: azure-ai-openai
88
ms.topic: how-to
9-
ms.date: 05/22/2024
10-
author: mrbullwinkle
11-
ms.author: mbullwin
9+
ms.date: 09/04/2024
10+
author: aahill
11+
ms.author: aahi
1212
recommendations: false
1313

1414
---
@@ -29,8 +29,7 @@ To use all features of function calling including parallel functions, you need t
2929

3030
### API Versions
3131

32-
- `2024-02-15-preview`
33-
- `2024-05-01-preview`
32+
Api versions starting with `2024-02-15-preview`.
3433

3534
## Example function definition
3635

@@ -46,40 +45,27 @@ from openai import AzureOpenAI
4645

4746
client = AzureOpenAI(
4847
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
49-
api_version="2024-02-15-preview",
48+
api_version="2024-07-01-preview",
5049
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
5150
)
5251

5352
assistant = client.beta.assistants.create(
53+
name="Weather Bot",
5454
instructions="You are a weather bot. Use the provided functions to answer questions.",
55-
model="gpt-4-1106-preview", #Replace with model deployment name
55+
model="gpt-4", #Replace with model deployment name
5656
tools=[{
5757
"type": "function",
5858
"function": {
59-
"name": "getCurrentWeather",
59+
"name": "get_weather",
6060
"description": "Get the weather in location",
6161
"parameters": {
6262
"type": "object",
6363
"properties": {
64-
"location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
65-
"unit": {"type": "string", "enum": ["c", "f"]}
64+
"location": {"type": "string", "description": "The city name, for example San Francisco"}
6665
},
6766
"required": ["location"]
6867
}
6968
}
70-
}, {
71-
"type": "function",
72-
"function": {
73-
"name": "getNickname",
74-
"description": "Get the nickname of a city",
75-
"parameters": {
76-
"type": "object",
77-
"properties": {
78-
"location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
79-
},
80-
"required": ["location"]
81-
}
82-
}
8369
}]
8470
)
8571
```
@@ -169,35 +155,68 @@ When you initiate a **Run** with a user Message that triggers the function, the
169155

170156
## Submitting function outputs
171157

172-
You can then complete the **Run** by submitting the tool output from the function(s) you call. Pass the `tool_call_id` referenced in the `required_action` object above to match output to each function call.
158+
You can then complete the **Run** by submitting the tool output from the function(s) you call. Pass the `tool_call_id` referenced in the `required_action` object to match output to each function call.
173159

174160

175161
# [Python 1.x](#tab/python)
176162

177163
```python
178-
from openai import AzureOpenAI
179-
180-
client = AzureOpenAI(
181-
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
182-
api_version="2024-02-15-preview",
183-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
184-
)
185164

165+
# Example function
166+
def get_weather():
167+
return "It's 80 degrees F and slightly cloudy."
168+
169+
# Create a thread
170+
thread = client.beta.threads.create()
186171

187-
run = client.beta.threads.runs.submit_tool_outputs(
188-
thread_id=thread.id,
189-
run_id=run.id,
190-
tool_outputs=[
191-
{
192-
"tool_call_id": call_ids[0],
193-
"output": "22C",
194-
},
195-
{
196-
"tool_call_id": call_ids[1],
197-
"output": "LA",
198-
},
199-
]
172+
#Add a user question to the thread
173+
message = client.beta.threads.messages.create(
174+
thread_id=thread.id,
175+
role="user",
176+
content="What is the weather in Seattle?"
200177
)
178+
179+
run = client.beta.threads.runs.create_and_poll(
180+
thread_id=thread.id,
181+
assistant_id= assistant.id,
182+
instructions="",
183+
)
184+
185+
# Define the list to store tool outputs
186+
tool_outputs = []
187+
188+
# Loop through each tool in the required action section
189+
for tool in run.required_action.submit_tool_outputs.tool_calls:
190+
# get data from the weather function
191+
if tool.function.name == "get_weather":
192+
weather = get_weather()
193+
tool_outputs.append({
194+
"tool_call_id": tool.id,
195+
"output": weather
196+
})
197+
198+
# Submit all tool outputs at once after collecting them in a list
199+
if tool_outputs:
200+
try:
201+
run = client.beta.threads.runs.submit_tool_outputs_and_poll(
202+
thread_id=thread.id,
203+
run_id=run.id,
204+
tool_outputs=tool_outputs
205+
)
206+
print("Tool outputs submitted successfully.")
207+
except Exception as e:
208+
print("Failed to submit tool outputs:", e)
209+
else:
210+
print("No tool outputs to submit.")
211+
212+
if run.status == 'completed':
213+
print("run status: ", run.status)
214+
messages = client.beta.threads.messages.list(thread_id=thread.id)
215+
print(messages.to_json(indent=2))
216+
217+
else:
218+
print("run status: ", run.status)
219+
print (run.last_error.message)
201220
```
202221

203222
# [REST](#tab/rest)

0 commit comments

Comments
 (0)