Skip to content

Commit 33b3401

Browse files
author
Yazeed Alaudah
committed
update third notebook
1 parent 7096ae1 commit 33b3401

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

Basic_Samples/Functions/functions_with_bing_search.ipynb

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
20+
"import json\n",
21+
"import os\n",
2022
"import requests\n",
21-
"import json \n",
22-
"import openai \n",
23+
"from openai import AzureOpenAI\n",
2324
"\n",
2425
"# Load config values\n",
2526
"with open(r'config.json') as config_file:\n",
2627
" config_details = json.load(config_file)\n",
2728
" \n",
2829
"\n",
2930
"\n",
30-
"# Configure OpenAI environment variables\n",
31-
"openai.api_key = config_details['OPENAI_API_KEY']\n",
32-
"openai.api_base = config_details['OPENAI_API_BASE']\n",
33-
"openai.api_type = \"azure\" \n",
34-
"openai.api_version = config_details['OPENAI_API_VERSION']\n",
31+
"client = AzureOpenAI(\n",
32+
" azure_endpoint=config_details[\"AZURE_OPENAI_ENDPOINT\"], # The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n",
33+
" api_key=os.getenv(\"AZURE_OPENAI_KEY\"), # The API key for your Azure OpenAI resource.\n",
34+
" api_version=config_details[\"OPENAI_API_VERSION\"], # This version supports function calling\n",
35+
")\n",
3536
"\n",
36-
"deployment_name = config_details['DEPLOYMENT_NAME'] # You need to use the 0613 version or newer of gpt-35-turbo or gpt-4 to work with functions\n",
37+
"model_name = config_details['MODEL_NAME'] # You need to ensure the version of the model you are using supports the function calling feature\n",
3738
"\n",
3839
"bing_search_subscription_key = config_details['BING_SEARCH_SUBSCRIPTION_KEY']\n",
3940
"bing_search_url = \"https://api.bing.microsoft.com/v7.0/search\""
@@ -129,31 +130,35 @@
129130
" {\"role\": \"user\", \"content\": \"How tall is mount rainier?\"}]\n",
130131
"\n",
131132
" \n",
132-
"functions = [ \n",
133+
"tools = [ \n",
133134
" {\n",
134-
" \"name\": \"search_bing\",\n",
135-
" \"description\": \"Searches bing to get up to date information from the web\",\n",
136-
" \"parameters\": {\n",
137-
" \"type\": \"object\",\n",
138-
" \"properties\": {\n",
139-
" \"query\": {\n",
140-
" \"type\": \"string\",\n",
141-
" \"description\": \"The search query\",\n",
142-
" }\n",
135+
" \"type\": \"function\",\n",
136+
" \"function\": {\n",
137+
" \"name\": \"search_bing\",\n",
138+
" \"description\": \"Searches bing to get up to date information from the web\",\n",
139+
" \"parameters\": {\n",
140+
" \"type\": \"object\",\n",
141+
" \"properties\": {\n",
142+
" \"query\": {\n",
143+
" \"type\": \"string\",\n",
144+
" \"description\": \"The search query\",\n",
145+
" }\n",
146+
" },\n",
147+
" \"required\": [\"query\"],\n",
143148
" },\n",
144-
" \"required\": [\"query\"],\n",
145-
" },\n",
149+
" }\n",
146150
" }\n",
151+
" \n",
147152
"]\n",
148153
"\n",
149-
"response = openai.ChatCompletion.create(\n",
150-
" deployment_id=deployment_name, # You need to use the 0613 version or newer of gpt-35-turbo or gpt-4 to work with functions\n",
151-
" messages=messages,\n",
152-
" functions=functions,\n",
153-
" function_call=\"auto\", \n",
154-
")\n",
154+
"response = client.chat.completions.create(\n",
155+
" model=model_name,\n",
156+
" messages=messages,\n",
157+
" tools=tools,\n",
158+
" tool_choice=\"auto\",\n",
159+
" )\n",
155160
"\n",
156-
"print(response['choices'][0]['message'])"
161+
"print(response.choices[0].message)"
157162
]
158163
},
159164
{
@@ -172,32 +177,32 @@
172177
"def run_multiturn_conversation(messages, functions, available_functions, deployment_name):\n",
173178
" # Step 1: send the conversation and available functions to GPT\n",
174179
"\n",
175-
" response = openai.ChatCompletion.create(\n",
176-
" deployment_id=deployment_name,\n",
180+
" response = client.chat.completions.create(\n",
177181
" messages=messages,\n",
178-
" functions=functions,\n",
179-
" function_call=\"auto\", \n",
180-
" temperature=0\n",
182+
" tools=tools,\n",
183+
" tool_choice=\"auto\",\n",
184+
" model=model_name,\n",
185+
" temperature=0,\n",
181186
" )\n",
182187
"\n",
183188
" # Step 2: check if GPT wanted to call a function\n",
184-
" while response[\"choices\"][0][\"finish_reason\"] == 'function_call':\n",
185-
" response_message = response[\"choices\"][0][\"message\"]\n",
189+
" while response.choices[0].finish_reason == \"tool_calls\":\n",
190+
" response_message = response.choices[0].message\n",
186191
" print(\"Recommended Function call:\")\n",
187-
" print(response_message.get(\"function_call\"))\n",
192+
" print(response_message.tool_calls[0])\n",
188193
" print()\n",
189194
" \n",
190195
" # Step 3: call the function\n",
191196
" # Note: the JSON response may not always be valid; be sure to handle errors\n",
192197
" \n",
193-
" function_name = response_message[\"function_call\"][\"name\"]\n",
198+
" function_name = response_message.tool_calls[0].function.name\n",
194199
" \n",
195200
" # verify function exists\n",
196201
" if function_name not in available_functions:\n",
197202
" return \"Function \" + function_name + \" does not exist\"\n",
198203
" function_to_call = available_functions[function_name] \n",
199204
" \n",
200-
" function_args = json.loads(response_message[\"function_call\"][\"arguments\"])\n",
205+
" function_args = json.loads(response_message.tool_calls[0].function.arguments)\n",
201206
" function_response = function_to_call(**function_args)\n",
202207
" \n",
203208
" print(\"Output of function call:\")\n",
@@ -209,10 +214,10 @@
209214
" # adding assistant response to messages\n",
210215
" messages.append(\n",
211216
" {\n",
212-
" \"role\": response_message[\"role\"],\n",
217+
" \"role\": response_message.role,\n",
213218
" \"function_call\": {\n",
214-
" \"name\": response_message[\"function_call\"][\"name\"],\n",
215-
" \"arguments\": response_message[\"function_call\"][\"arguments\"],\n",
219+
" \"name\": response_message.tool_calls[0].function.name,\n",
220+
" \"arguments\": response_message.tool_calls[0].function.arguments,\n",
216221
" },\n",
217222
" \"content\": None\n",
218223
" }\n",
@@ -232,12 +237,12 @@
232237
" print(message)\n",
233238
" print()\n",
234239
"\n",
235-
" response = openai.ChatCompletion.create(\n",
240+
" response = client.chat.completions.create(\n",
236241
" messages=messages,\n",
237-
" deployment_id=deployment_name,\n",
238-
" function_call=\"auto\",\n",
239-
" functions=functions,\n",
240-
" temperature=0\n",
242+
" tools=tools,\n",
243+
" tool_choice=\"auto\",\n",
244+
" model=model_name,\n",
245+
" temperature=0,\n",
241246
" ) # get a new response from GPT where it can see the function response\n",
242247
"\n",
243248
" return response"
@@ -284,10 +289,10 @@
284289
"\n",
285290
"available_functions = {'search_bing': search}\n",
286291
"\n",
287-
"result = run_multiturn_conversation(messages, functions, available_functions, deployment_name)\n",
292+
"result = run_multiturn_conversation(messages, tools, available_functions)\n",
288293
"\n",
289294
"print(\"Final response:\")\n",
290-
"print(result['choices'][0]['message']['content'])"
295+
"print(result.choices[0].message)"
291296
]
292297
},
293298
{

Basic_Samples/Functions/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ openai==1.12.0
55
jupyter
66

77
# Other packages needed to run the notebook samples
8+
requests
89
pytz
910
pandas
1011
tenacity

Basic_Samples/Functions/working_with_functions.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
" api_version=config_details[\"OPENAI_API_VERSION\"], # This version supports function calling\n",
5252
")\n",
5353
"\n",
54-
"model_name = config_details[\"MODEL_NAME\"]"
54+
"model_name = config_details[\"MODEL_NAME\"] # You need to ensure the version of the model you are using supports the function calling feature"
5555
]
5656
},
5757
{

0 commit comments

Comments
 (0)