"ChatCompletionMessage(content='Sure, could you please tell me the location for which you would like to know the weather?', role='assistant', function_call=None, tool_calls=None)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-15T17:45:35.282310Z",
"start_time": "2024-05-15T17:45:33.861496Z"
}
],
},
"source": [
"messages = []\n",
"messages.append({\"role\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
"ChatCompletionMessage(content=\"I need to know your location to provide you with the current weather. Could you please specify the city and state (or country) you're in?\", role='assistant', function_call=None, tool_calls=None)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 6
},
{
"attachments": {},
@ -246,14 +271,27 @@
},
{
"cell_type": "code",
"execution_count": 7,
"id": "23c42a6e",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-15T17:45:43.553403Z",
"start_time": "2024-05-15T17:45:42.205590Z"
}
},
"source": [
"messages.append({\"role\": \"user\", \"content\": \"I'm in Glasgow, Scotland.\"})\n",
"ChatCompletionMessage(content='Sure, I can help you with that. How many days would you like to get the weather forecast for?', role='assistant', function_call=None, tool_calls=None)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-15T17:45:47.090638Z",
"start_time": "2024-05-15T17:45:46.302475Z"
}
],
},
"source": [
"messages = []\n",
"messages.append({\"role\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
"ChatCompletionMessage(content='Please specify the number of days (x) for which you want the weather forecast for Glasgow, Scotland.', role='assistant', function_call=None, tool_calls=None)"
"messages.append({\"role\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"Give me the current weather (use Celcius) for Toronto, Canada.\"})\n",
"ChatCompletionMessage(content=\"I'll get the current weather for Toronto, Canada in Celsius.\", role='assistant', function_call=None, tool_calls=None)"
]
},
"execution_count": 12,
@ -443,15 +506,7 @@
"output_type": "execute_result"
}
],
"source": [
"messages = []\n",
"messages.append({\"role\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"Give me the current weather (use Celcius) for Toronto, Canada.\"})\n",
"chat_response = chat_completion_request(\n",
" messages, tools=tools, tool_choice=\"none\"\n",
")\n",
"chat_response.choices[0].message\n"
]
"execution_count": 12
},
{
"cell_type": "markdown",
@ -460,20 +515,35 @@
"source": [
"### Parallel Function Calling\n",
"\n",
"Newer models like gpt-4-1106-preview or gpt-3.5-turbo-1106 can call multiple functions in one turn."
"Newer models such as gpt-4o or gpt-3.5-turbo can call multiple functions in one turn."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "380eeb68",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-15T17:46:04.048553Z",
"start_time": "2024-05-15T17:46:01.273501Z"
}
},
"source": [
"messages = []\n",
"messages.append({\"role\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"what is the weather going to be like in San Francisco and Glasgow over the next 4 days\"})\n",
"messages.append({\"role\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"what is the weather going to be like in San Francisco and Glasgow over the next 4 days\"})\n",
" results = f\"Error: function {message.tool_calls[0].function.name} does not exist\"\n",
" return results"
],
"outputs": [],
"execution_count": 18
},
{
"cell_type": "markdown",
"id": "8f6885e9f0af5c40",
"metadata": {},
"source": [
"##### Steps to invoke a function call using Chat Completions API: \n",
"\n",
"**Step 1**: Prompt the model with content that may result in model selecting a tool to use. The description of the tools such as a function names and signature is defined in the 'Tools' list and passed to the model in API call. If selected, the function name and parameters are included in the response.<br>\n",
" \n",
"**Step 2**: Check programmatically if model wanted to call a function. If true, proceed to step 3. <br> \n",
"**Step 3**: Extract the function name and parameters from response, call the function with parameters. Append the result to messages. <br> \n",
"**Step 4**: Invoke the chat completions API with the message list to get the response. "
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "38c55083",
"metadata": {},
"id": "e8b7cb9cdc7a7616",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-15T17:46:25.725379Z",
"start_time": "2024-05-15T17:46:24.255505Z"
}
},
"source": [
"# Step #1: Prompt with content that may result in function call. In this case the model can identify the information requested by the user is potentially available in the database schema passed to the model in Tools description. \n",
"messages = [{\n",
" \"role\":\"user\", \n",
" \"content\": \"What is the name of the album with the most tracks?\"\n",
"\u001b[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n",
"\u001b[0m\n",
"\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n",
"\u001b[0m\n",
"\u001b[34massistant: Function(arguments='{\\n \"query\": \"SELECT Artist.Name, COUNT(Track.TrackId) AS TrackCount FROM Artist JOIN Album ON Artist.ArtistId = Album.ArtistId JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Artist.ArtistId ORDER BY TrackCount DESC LIMIT 5;\"\\n}', name='ask_database')\n",
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_bXMf903yO78sdsMZble4yu90', function=Function(arguments='{\"query\":\"SELECT A.Title, COUNT(T.TrackId) AS TrackCount FROM Album A JOIN Track T ON A.AlbumId = T.AlbumId GROUP BY A.Title ORDER BY TrackCount DESC LIMIT 1;\"}', name='ask_database'), type='function')])\n"
]
}
],
"source": [
"messages = []\n",
"messages.append({\"role\": \"system\", \"content\": \"Answer user questions by generating SQL queries against the Chinook Music Database.\"})\n",
"messages.append({\"role\": \"user\", \"content\": \"Hi, who are the top 5 artists by number of tracks?\"})\n",
" print(f\"Error: function {tool_function_name} does not exist\")\n",
"else: \n",
" # Model did not identify a function to call, result can be returned to the user \n",
" print(response_message.content) "
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.\n",
"\u001b[0m\n",
"\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n",
"\u001b[0m\n",
"\u001b[34massistant: Function(arguments='{\\n \"query\": \"SELECT Artist.Name, COUNT(Track.TrackId) AS TrackCount FROM Artist JOIN Album ON Artist.ArtistId = Album.ArtistId JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Artist.ArtistId ORDER BY TrackCount DESC LIMIT 5;\"\\n}', name='ask_database')\n",
"\u001b[32muser: What is the name of the album with the most tracks?\n",
"\u001b[0m\n",
"\u001b[34massistant: Function(arguments='{\\n \"query\": \"SELECT Album.Title, COUNT(Track.TrackId) AS TrackCount FROM Album JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Album.AlbumId ORDER BY TrackCount DESC LIMIT 1;\"\\n}', name='ask_database')\n",