Skip to content

Commit 4627fae

Browse files
author
Jill Grant
authored
Merge pull request #126 from aahill/quick-fix
AOAI function code update
2 parents 2a9b5a0 + 4759aa7 commit 4627fae

File tree

1 file changed

+71
-92
lines changed

1 file changed

+71
-92
lines changed

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

Lines changed: 71 additions & 92 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
```
@@ -90,40 +76,25 @@ assistant = client.beta.assistants.create(
9076
> With Azure OpenAI the `model` parameter requires model deployment name. If your model deployment name is different than the underlying model name then you would adjust your code to ` "model": "{your-custom-model-deployment-name}"`.
9177
9278
```console
93-
curl https://YOUR_RESOURCE_NAME.openai.azure.com/openai/assistants?api-version=2024-02-15-preview \
79+
curl https://YOUR_RESOURCE_NAME.openai.azure.com/openai/assistants?api-version=2024-07-01-preview \
9480
-H "api-key: $AZURE_OPENAI_API_KEY" \
9581
-H "Content-Type: application/json" \
9682
-d '{
9783
"instructions": "You are a weather bot. Use the provided functions to answer questions.",
98-
"tools": [{
99-
"type": "function",
100-
"function": {
101-
"name": "getCurrentWeather",
102-
"description": "Get the weather in location",
103-
"parameters": {
104-
"type": "object",
105-
"properties": {
106-
"location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
107-
"unit": {"type": "string", "enum": ["c", "f"]}
108-
},
109-
"required": ["location"]
110-
}
111-
}
112-
},
113-
{
84+
tools=[{
11485
"type": "function",
115-
"function": {
116-
"name": "getNickname",
117-
"description": "Get the nickname of a city",
118-
"parameters": {
119-
"type": "object",
120-
"properties": {
121-
"location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"}
122-
},
123-
"required": ["location"]
124-
}
125-
}
126-
}],
86+
"function": {
87+
"name": "get_weather",
88+
"description": "Get the weather in location",
89+
"parameters": {
90+
"type": "object",
91+
"properties": {
92+
"location": {"type": "string", "description": "The city name, for example San Francisco"}
93+
},
94+
"required": ["location"]
95+
}
96+
}
97+
}],
12798
"model": "gpt-4-1106-preview"
12899
}'
129100
```
@@ -149,18 +120,10 @@ When you initiate a **Run** with a user Message that triggers the function, the
149120
"id": "call_abc123",
150121
"type": "function",
151122
"function": {
152-
"name": "getCurrentWeather",
153-
"arguments": "{\"location\":\"San Francisco\"}"
123+
"name": "get_weather",
124+
"arguments": "{\"location\":\"Seattle\"}"
154125
}
155126
},
156-
{
157-
"id": "call_abc456",
158-
"type": "function",
159-
"function": {
160-
"name": "getNickname",
161-
"arguments": "{\"location\":\"Los Angeles\"}"
162-
}
163-
}
164127
]
165128
}
166129
},
@@ -169,50 +132,66 @@ When you initiate a **Run** with a user Message that triggers the function, the
169132

170133
## Submitting function outputs
171134

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.
135+
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.
173136

174137

175138
# [Python 1.x](#tab/python)
176139

177140
```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-
)
185-
186141

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-
]
200-
)
142+
# Example function
143+
def get_weather():
144+
return "It's 80 degrees F and slightly cloudy."
145+
146+
# Define the list to store tool outputs
147+
tool_outputs = []
148+
149+
# Loop through each tool in the required action section
150+
for tool in run.required_action.submit_tool_outputs.tool_calls:
151+
# get data from the weather function
152+
if tool.function.name == "get_weather":
153+
weather = get_weather()
154+
tool_outputs.append({
155+
"tool_call_id": tool.id,
156+
"output": weather
157+
})
158+
159+
# Submit all tool outputs at once after collecting them in a list
160+
if tool_outputs:
161+
try:
162+
run = client.beta.threads.runs.submit_tool_outputs_and_poll(
163+
thread_id=thread.id,
164+
run_id=run.id,
165+
tool_outputs=tool_outputs
166+
)
167+
print("Tool outputs submitted successfully.")
168+
except Exception as e:
169+
print("Failed to submit tool outputs:", e)
170+
else:
171+
print("No tool outputs to submit.")
172+
173+
if run.status == 'completed':
174+
print("run status: ", run.status)
175+
messages = client.beta.threads.messages.list(thread_id=thread.id)
176+
print(messages.to_json(indent=2))
177+
178+
else:
179+
print("run status: ", run.status)
180+
print (run.last_error.message)
201181
```
202182

203183
# [REST](#tab/rest)
204184

185+
In the following example, replace `output` with the output of the function you want to use.
186+
205187
```console
206-
curl https://YOUR_RESOURCE_NAME.openai.azure.com/openai/threads/thread_abc123/runs/run_123/submit_tool_outputs?api-version=2024-02-15-preview \
188+
curl https://YOUR_RESOURCE_NAME.openai.azure.com/openai/threads/thread_abc123/runs/run_123/submit_tool_outputs?api-version=2024-07-01-preview \
207189
-H "Content-Type: application/json" \
208-
-H "api-key: $AZURE_OPENAI_API_KEY" \
190+
-H "api-key: 851c6e0b83744d8c8fc2a07eab098376" \
209191
-d '{
210192
"tool_outputs": [{
211-
"tool_call_id": "call_abc123",
212-
"output": "{"temperature": "22", "unit": "celsius"}"
213-
}, {
214-
"tool_call_id": "call_abc456",
215-
"output": "{"nickname": "LA"}"
193+
"tool_call_id": "call_123",
194+
"output": "{\"60 degrees F and raining\"}"
216195
}]
217196
}'
218197
```

0 commit comments

Comments
 (0)