Skip to content

Commit

Permalink
fixed ch11
Browse files Browse the repository at this point in the history
  • Loading branch information
corradocavalli committed Nov 17, 2023
1 parent 1090320 commit 38f2fea
Show file tree
Hide file tree
Showing 2 changed files with 498 additions and 486 deletions.
85 changes: 50 additions & 35 deletions 11-integrating-with-function-calling/Lesson11-FunctionCalling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@
" - `Microsoft Learn Catalog API` to help users find courses based on the request of the user \n",
" - `Function Calling` to take the user's query and send it to a function to make the API request. \n",
"\n",
"To get started, let's look at why we would want to use function calling in the first place: "
"To get started, let's look at why we would want to use function calling in the first place: \n",
"\n",
"print(\"Messages in next request:\")\n",
"print(messages)\n",
"print()\n",
"\n",
"second_response = client.chat.completions.create(\n",
" messages=messages,\n",
" model=deployment,\n",
" function_call=\"auto\",\n",
" functions=functions,\n",
" temperature=0\n",
" ) # get a new response from GPT where it can see the function response\n",
"\n",
"\n",
"print(second_response.choices[0].message)"
]
},
{
Expand Down Expand Up @@ -133,12 +148,17 @@
"outputs": [],
"source": [
"import os\n",
"import openai\n",
"import json\n",
"openai.api_type = \"azure\"\n",
"openai.api_base = \"YOUR OPENAI API BASE URL\"\n",
"openai.api_version = \"2023-07-01-preview\"\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")"
"from openai import AzureOpenAI\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"client = AzureOpenAI(\n",
" api_key=os.environ['AZURE_OPENAI_KEY'], # this is also the default, it can be omitted\n",
" api_version = \"2023-07-01-preview\"\n",
" )\n",
"\n",
"deployment=os.environ['AZURE_OPENAI_DEPLOYMENT']"
]
},
{
Expand All @@ -154,12 +174,11 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"openai_response1 = openai.ChatCompletion.create(\n",
" engine=\"gpt-function\", \n",
"openai_response1 = client.chat.completions.create(\n",
" model=deployment, \n",
" messages = [{'role': 'user', 'content': prompt1}]\n",
")\n",
"openai_response1['choices'][0]['message']['content'] "
"openai_response1.choices[0].message.content "
]
},
{
Expand All @@ -168,14 +187,11 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"openai_response2 = openai.ChatCompletion.create(\n",
" engine=\"gpt-function\",\n",
" messages = [{'role': 'user', 'content': prompt2 }]\n",
"openai_response2 = client.chat.completions.create(\n",
" model=deployment, \n",
" messages = [{'role': 'user', 'content': prompt2}]\n",
")\n",
"openai_response2['choices'][0]['message']['content'] \n",
"\n"
"openai_response2.choices[0].message.content"
]
},
{
Expand All @@ -185,7 +201,7 @@
"outputs": [],
"source": [
"# Loading the response as a JSON object\n",
"json_response1 = json.loads(openai_response1['choices'][0]['message']['content'])\n",
"json_response1 = json.loads(openai_response1.choices[0].message.content)\n",
"json_response1"
]
},
Expand All @@ -196,7 +212,7 @@
"outputs": [],
"source": [
"# Loading the response as a JSON object\n",
"json_response2 = json.loads(openai_response2['choices'][0]['message']['content'])\n",
"json_response2 = json.loads(openai_response2.choices[0].message.content )\n",
"json_response2"
]
},
Expand Down Expand Up @@ -379,12 +395,12 @@
"metadata": {},
"outputs": [],
"source": [
"response = openai.ChatCompletion.create( engine=\"gpt-function\", \n",
" messages=messages, \n",
"response = client.chat.completions.create(model=deployment, \n",
" messages=messages,\n",
" functions=functions, \n",
" function_call=\"auto\", ) \n",
" function_call=\"auto\") \n",
"\n",
"print(response['choices'][0]['message'])\n"
"print(response.choices[0].message)"
]
},
{
Expand Down Expand Up @@ -426,7 +442,7 @@
"metadata": {},
"outputs": [],
"source": [
"response_message = response[\"choices\"][0][\"message\"]"
"response_message = response.choices[0].message"
]
},
{
Expand Down Expand Up @@ -480,21 +496,20 @@
"outputs": [],
"source": [
"# Check if the model wants to call a function\n",
"if response_message.get(\"function_call\"):\n",
"if response_message.function_call.name:\n",
" print(\"Recommended Function call:\")\n",
" print(response_message.get(\"function_call\"))\n",
" print(response_message.function_call.name)\n",
" print()\n",
"\n",
"\n",
" # Call the function. \n",
" function_name = response_message[\"function_call\"][\"name\"]\n",
" function_name = response_message.function_call.name\n",
"\n",
" available_functions = {\n",
" \"search_courses\": search_courses,\n",
" }\n",
" function_to_call = available_functions[function_name] \n",
"\n",
" function_args = json.loads(response_message[\"function_call\"][\"arguments\"])\n",
" function_args = json.loads(response_message.function_call.arguments)\n",
" function_response = function_to_call(**function_args)\n",
"\n",
" print(\"Output of function call:\")\n",
Expand All @@ -505,10 +520,10 @@
" # Add the assistant response and function response to the messages\n",
" messages.append( # adding assistant response to messages\n",
" {\n",
" \"role\": response_message[\"role\"],\n",
" \"role\": response_message.role,\n",
" \"function_call\": {\n",
" \"name\": function_name,\n",
" \"arguments\": response_message[\"function_call\"][\"arguments\"],\n",
" \"arguments\": response_message.function_call.arguments,\n",
" },\n",
" \"content\": None\n",
" }\n",
Expand Down Expand Up @@ -540,16 +555,16 @@
"print(messages)\n",
"print()\n",
"\n",
"second_response = openai.ChatCompletion.create(\n",
"second_response = client.chat.completions.create(\n",
" messages=messages,\n",
" engine=\"gpt-function\",\n",
" model=deployment,\n",
" function_call=\"auto\",\n",
" functions=functions,\n",
" temperature=0\n",
" ) # get a new response from GPT where it can see the function response\n",
"\n",
"\n",
"print(second_response[\"choices\"][0][\"message\"])"
"print(second_response.choices[0].message)"
]
},
{
Expand Down Expand Up @@ -581,7 +596,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.10.8"
},
"orig_nbformat": 4
},
Expand Down
Loading

0 comments on commit 38f2fea

Please sign in to comment.