You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/ai-services/openai/how-to/function-calling.md
-72Lines changed: 0 additions & 72 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -176,10 +176,7 @@ If you want to describe a function that doesn't accept any parameters, use `{"ty
176
176
177
177
### Managing the flow with functions
178
178
179
-
# [OpenAI Python 0.28.1](#tab/python)
180
-
181
179
```python
182
-
# This is only a partial code example we aren't defining an actual search_hotels function, so without further modification this code will not execute successfully. For a fully functioning example visit out samples.
183
180
184
181
response= openai.ChatCompletion.create(
185
182
deployment_id="gpt-35-turbo-0613",
@@ -233,75 +230,6 @@ else:
233
230
print(response["choices"][0]["message"])
234
231
```
235
232
236
-
# [OpenAI Python 1.x](#tab/python-new)
237
-
238
-
```python
239
-
# This is only a partial code example we aren't defining an actual search_hotels function, so without further modification this code will not execute successfully.
240
-
241
-
import os
242
-
from openai import AzureOpenAI
243
-
244
-
client= AzureOpenAI(
245
-
api_key= os.getenv("AZURE_OPENAI_KEY"),
246
-
api_version="2023-10-01-preview",
247
-
azure_endpoint= os.getenv("AZURE_OPENAI_ENDPOINT"
248
-
)
249
-
250
-
response= client.chat.completions.create(
251
-
model="gpt-35-turbo-0613", # model = deployment_name
252
-
messages=messages,
253
-
functions=functions,
254
-
function_call="auto",
255
-
)
256
-
response_message= response.choices[0].message
257
-
258
-
# Check if the model wants to call a function
259
-
if response_message.get("function_call"):
260
-
261
-
# Call the function. The JSON response may not always be valid so make sure to handle errors
messages.append( # adding function response to messages
284
-
{
285
-
"role": "function",
286
-
"name": function_name,
287
-
"content": function_response,
288
-
}
289
-
)
290
-
291
-
# Call the API again to get the final response from the model
292
-
second_response= client.chat.completions.create(
293
-
messages=messages,
294
-
model="gpt-35-turbo-0613"#model = deployment_name
295
-
# optionally, you could provide functions in the second call as well
296
-
)
297
-
print(second_response.choices[0].message)
298
-
else:
299
-
print(response.choices[0].message)
300
-
301
-
```
302
-
303
-
---
304
-
305
233
In the example above, we don't do any validation or error handling so you'll want to make sure to add that to your code.
306
234
307
235
For a full example of working with functions, see the [sample notebook on function calling](https://aka.ms/oai/functions-samples). You can also apply more complex logic to chain multiple function calls together, which is covered in the sample as well.
0 commit comments