mirror of
https://github.com/james-m-jordan/openai-cookbook.git
synced 2025-05-09 19:32:38 +00:00
Minor bug fix (#1278)
This commit is contained in:
parent
f854f6f0ca
commit
a254b498a4
@ -51,13 +51,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
"id": "dab872c5",
|
"id": "dab872c5",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:23.563149Z",
|
"end_time": "2024-07-12T22:41:58.148850Z",
|
||||||
"start_time": "2024-05-15T17:45:22.925978Z"
|
"start_time": "2024-07-12T22:41:58.133412Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import json\n",
|
"import json\n",
|
||||||
"from openai import OpenAI\n",
|
"from openai import OpenAI\n",
|
||||||
@ -66,9 +68,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"GPT_MODEL = \"gpt-4o\"\n",
|
"GPT_MODEL = \"gpt-4o\"\n",
|
||||||
"client = OpenAI()"
|
"client = OpenAI()"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 2
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -83,13 +83,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
"id": "745ceec5",
|
"id": "745ceec5",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:28.816345Z",
|
"end_time": "2024-07-12T22:41:59.531820Z",
|
||||||
"start_time": "2024-05-15T17:45:28.814155Z"
|
"start_time": "2024-07-12T22:41:59.529870Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))\n",
|
"@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))\n",
|
||||||
"def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL):\n",
|
"def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL):\n",
|
||||||
@ -105,19 +107,19 @@
|
|||||||
" print(\"Unable to generate ChatCompletion response\")\n",
|
" print(\"Unable to generate ChatCompletion response\")\n",
|
||||||
" print(f\"Exception: {e}\")\n",
|
" print(f\"Exception: {e}\")\n",
|
||||||
" return e\n"
|
" return e\n"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 3
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
"id": "c4d1c99f",
|
"id": "c4d1c99f",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:30.003910Z",
|
"end_time": "2024-07-12T22:42:00.463896Z",
|
||||||
"start_time": "2024-05-15T17:45:30.001259Z"
|
"start_time": "2024-07-12T22:42:00.461258Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def pretty_print_conversation(messages):\n",
|
"def pretty_print_conversation(messages):\n",
|
||||||
" role_to_color = {\n",
|
" role_to_color = {\n",
|
||||||
@ -138,9 +140,7 @@
|
|||||||
" print(colored(f\"assistant: {message['content']}\\n\", role_to_color[message[\"role\"]]))\n",
|
" print(colored(f\"assistant: {message['content']}\\n\", role_to_color[message[\"role\"]]))\n",
|
||||||
" elif message[\"role\"] == \"function\":\n",
|
" elif message[\"role\"] == \"function\":\n",
|
||||||
" print(colored(f\"function ({message['name']}): {message['content']}\\n\", role_to_color[message[\"role\"]]))\n"
|
" print(colored(f\"function ({message['name']}): {message['content']}\\n\", role_to_color[message[\"role\"]]))\n"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 4
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -155,13 +155,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
"id": "d2e25069",
|
"id": "d2e25069",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:31.794879Z",
|
"end_time": "2024-07-12T22:42:01.676606Z",
|
||||||
"start_time": "2024-05-15T17:45:31.792617Z"
|
"start_time": "2024-07-12T22:42:01.674348Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"tools = [\n",
|
"tools = [\n",
|
||||||
" {\n",
|
" {\n",
|
||||||
@ -213,9 +215,7 @@
|
|||||||
" }\n",
|
" }\n",
|
||||||
" },\n",
|
" },\n",
|
||||||
"]"
|
"]"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 5
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -228,13 +228,26 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
"id": "518d6827",
|
"id": "518d6827",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:35.282310Z",
|
"end_time": "2024-07-12T22:42:03.726604Z",
|
||||||
"start_time": "2024-05-15T17:45:33.861496Z"
|
"start_time": "2024-07-12T22:42:03.154689Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"ChatCompletionMessage(content='Sure, can you please provide me with the name of your city and state?', role='assistant', function_call=None, tool_calls=None)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 60,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages = []\n",
|
"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\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
|
||||||
@ -245,20 +258,7 @@
|
|||||||
"assistant_message = chat_response.choices[0].message\n",
|
"assistant_message = chat_response.choices[0].message\n",
|
||||||
"messages.append(assistant_message)\n",
|
"messages.append(assistant_message)\n",
|
||||||
"assistant_message\n"
|
"assistant_message\n"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"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": {},
|
"attachments": {},
|
||||||
@ -271,13 +271,26 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
"id": "23c42a6e",
|
"id": "23c42a6e",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:43.553403Z",
|
"end_time": "2024-07-12T22:42:05.778263Z",
|
||||||
"start_time": "2024-05-15T17:45:42.205590Z"
|
"start_time": "2024-07-12T22:42:05.277346Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_xb7QwwNnx90LkmhtlW0YrgP2', function=Function(arguments='{\"location\":\"Glasgow, Scotland\",\"format\":\"celsius\"}', name='get_current_weather'), type='function')])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 61,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages.append({\"role\": \"user\", \"content\": \"I'm in Glasgow, Scotland.\"})\n",
|
"messages.append({\"role\": \"user\", \"content\": \"I'm in Glasgow, Scotland.\"})\n",
|
||||||
"chat_response = chat_completion_request(\n",
|
"chat_response = chat_completion_request(\n",
|
||||||
@ -286,20 +299,7 @@
|
|||||||
"assistant_message = chat_response.choices[0].message\n",
|
"assistant_message = chat_response.choices[0].message\n",
|
||||||
"messages.append(assistant_message)\n",
|
"messages.append(assistant_message)\n",
|
||||||
"assistant_message\n"
|
"assistant_message\n"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_Dn2RJJSxzDm49vlVTehseJ0k', function=Function(arguments='{\"location\":\"Glasgow, Scotland\",\"format\":\"celsius\"}', name='get_current_weather'), type='function')])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 7,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 7
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -312,13 +312,26 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
"id": "fa232e54",
|
"id": "fa232e54",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:47.090638Z",
|
"end_time": "2024-07-12T22:42:07.575820Z",
|
||||||
"start_time": "2024-05-15T17:45:46.302475Z"
|
"start_time": "2024-07-12T22:42:07.018764Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"ChatCompletionMessage(content='To provide you with the weather forecast for Glasgow, Scotland, could you please specify the number of days you would like the forecast for?', role='assistant', function_call=None, tool_calls=None)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 62,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages = []\n",
|
"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\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
|
||||||
@ -329,20 +342,7 @@
|
|||||||
"assistant_message = chat_response.choices[0].message\n",
|
"assistant_message = chat_response.choices[0].message\n",
|
||||||
"messages.append(assistant_message)\n",
|
"messages.append(assistant_message)\n",
|
||||||
"assistant_message\n"
|
"assistant_message\n"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"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)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 8,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 8
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -355,33 +355,33 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
"id": "c7d8a543",
|
"id": "c7d8a543",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:49.790820Z",
|
"end_time": "2024-07-12T22:42:09.587530Z",
|
||||||
"start_time": "2024-05-15T17:45:48.847752Z"
|
"start_time": "2024-07-12T22:42:08.666795Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_34PBraFdNN6KR95uD5rHF8Aw', function=Function(arguments='{\"location\":\"Glasgow, Scotland\",\"format\":\"celsius\",\"num_days\":5}', name='get_n_day_weather_forecast'), type='function')]))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 63,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages.append({\"role\": \"user\", \"content\": \"5 days\"})\n",
|
"messages.append({\"role\": \"user\", \"content\": \"5 days\"})\n",
|
||||||
"chat_response = chat_completion_request(\n",
|
"chat_response = chat_completion_request(\n",
|
||||||
" messages, tools=tools\n",
|
" messages, tools=tools\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chat_response.choices[0]\n"
|
"chat_response.choices[0]\n"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_Yg5ydH9lHhLjjYQyXbNvh004', function=Function(arguments='{\"location\":\"Glasgow, Scotland\",\"format\":\"celsius\",\"num_days\":5}', name='get_n_day_weather_forecast'), type='function')]))"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 9,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 9
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -403,13 +403,26 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
"id": "559371b7",
|
"id": "559371b7",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:54.194255Z",
|
"end_time": "2024-07-12T22:42:12.216712Z",
|
||||||
"start_time": "2024-05-15T17:45:52.975746Z"
|
"start_time": "2024-07-12T22:42:11.714246Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_FImGxrLowOAOszCaaQqQWmEN', function=Function(arguments='{\"location\":\"Toronto, Canada\",\"format\":\"celsius\",\"num_days\":7}', name='get_n_day_weather_forecast'), type='function')])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 64,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# in this cell we force the model to use get_n_day_weather_forecast\n",
|
"# in this cell we force the model to use get_n_day_weather_forecast\n",
|
||||||
"messages = []\n",
|
"messages = []\n",
|
||||||
@ -419,30 +432,30 @@
|
|||||||
" messages, tools=tools, tool_choice={\"type\": \"function\", \"function\": {\"name\": \"get_n_day_weather_forecast\"}}\n",
|
" messages, tools=tools, tool_choice={\"type\": \"function\", \"function\": {\"name\": \"get_n_day_weather_forecast\"}}\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chat_response.choices[0].message"
|
"chat_response.choices[0].message"
|
||||||
],
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"id": "a7ab0f58",
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-07-12T22:42:14.264601Z",
|
||||||
|
"start_time": "2024-07-12T22:42:13.001306Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_aP8ZEtGcyseL0btTMYxTCKbk', function=Function(arguments='{\"location\":\"Toronto, Canada\",\"format\":\"celsius\",\"num_days\":1}', name='get_n_day_weather_forecast'), type='function')])"
|
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_n84kYFqjNFDPNGDEnjnrd2KC', function=Function(arguments='{\"location\": \"Toronto, Canada\", \"format\": \"celsius\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_AEs3AFhJc9pn42hWSbHTaIDh', function=Function(arguments='{\"location\": \"Toronto, Canada\", \"format\": \"celsius\", \"num_days\": 3}', name='get_n_day_weather_forecast'), type='function')])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 10,
|
"execution_count": 65,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"execution_count": 10
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"id": "a7ab0f58",
|
|
||||||
"metadata": {
|
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2024-05-15T17:45:56.841233Z",
|
|
||||||
"start_time": "2024-05-15T17:45:55.433397Z"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"# if we don't force the model to use get_n_day_weather_forecast it may not\n",
|
"# if we don't force the model to use get_n_day_weather_forecast it may not\n",
|
||||||
"messages = []\n",
|
"messages = []\n",
|
||||||
@ -452,20 +465,7 @@
|
|||||||
" messages, tools=tools\n",
|
" messages, tools=tools\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chat_response.choices[0].message"
|
"chat_response.choices[0].message"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_5HqCVRaAoBuU0uTlO3MUwaWX', function=Function(arguments='{\"location\": \"Toronto, Canada\", \"format\": \"celsius\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_C9kCha28xHEsxYl4PxZ1l5LI', function=Function(arguments='{\"location\": \"Toronto, Canada\", \"format\": \"celsius\", \"num_days\": 3}', name='get_n_day_weather_forecast'), type='function')])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 11,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 11
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -478,13 +478,26 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
"id": "acfe54e6",
|
"id": "acfe54e6",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:45:59.800346Z",
|
"end_time": "2024-07-12T22:42:16.928643Z",
|
||||||
"start_time": "2024-05-15T17:45:59.289603Z"
|
"start_time": "2024-07-12T22:42:16.295006Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"ChatCompletionMessage(content=\"Sure, I'll get the current weather for Toronto, Canada in Celsius.\", role='assistant', function_call=None, tool_calls=None)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 66,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages = []\n",
|
"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\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
|
||||||
@ -493,20 +506,7 @@
|
|||||||
" messages, tools=tools, tool_choice=\"none\"\n",
|
" messages, tools=tools, tool_choice=\"none\"\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chat_response.choices[0].message\n"
|
"chat_response.choices[0].message\n"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"ChatCompletionMessage(content=\"I'll get the current weather for Toronto, Canada in Celsius.\", role='assistant', function_call=None, tool_calls=None)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 12,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 12
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -520,13 +520,27 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
"id": "380eeb68",
|
"id": "380eeb68",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:04.048553Z",
|
"end_time": "2024-07-12T22:42:18.988762Z",
|
||||||
"start_time": "2024-05-15T17:46:01.273501Z"
|
"start_time": "2024-07-12T22:42:18.041914Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[ChatCompletionMessageToolCall(id='call_ObhLiJwaHwc3U1KyB4Pdpx8y', function=Function(arguments='{\"location\": \"San Francisco, CA\", \"format\": \"fahrenheit\", \"num_days\": 4}', name='get_n_day_weather_forecast'), type='function'),\n",
|
||||||
|
" ChatCompletionMessageToolCall(id='call_5YRgeZ0MGBMFKE3hZiLouwg7', function=Function(arguments='{\"location\": \"Glasgow, SCT\", \"format\": \"celsius\", \"num_days\": 4}', name='get_n_day_weather_forecast'), type='function')]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 67,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages = []\n",
|
"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\": \"system\", \"content\": \"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.\"})\n",
|
||||||
@ -537,21 +551,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"assistant_message = chat_response.choices[0].message.tool_calls\n",
|
"assistant_message = chat_response.choices[0].message.tool_calls\n",
|
||||||
"assistant_message"
|
"assistant_message"
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"[ChatCompletionMessageToolCall(id='call_pFdKcCu5taDTtOOfX14vEDRp', function=Function(arguments='{\"location\": \"San Francisco, CA\", \"format\": \"fahrenheit\", \"num_days\": 4}', name='get_n_day_weather_forecast'), type='function'),\n",
|
|
||||||
" ChatCompletionMessageToolCall(id='call_Veeyp2hYJOKp0wT7ODxmTjaS', function=Function(arguments='{\"location\": \"Glasgow, UK\", \"format\": \"celsius\", \"num_days\": 4}', name='get_n_day_weather_forecast'), type='function')]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 13,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 13
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -579,19 +579,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
"id": "30f6b60e",
|
"id": "30f6b60e",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:07.270851Z",
|
"end_time": "2024-07-12T22:42:20.742187Z",
|
||||||
"start_time": "2024-05-15T17:46:07.265545Z"
|
"start_time": "2024-07-12T22:42:20.737751Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
|
||||||
"import sqlite3\n",
|
|
||||||
"\n",
|
|
||||||
"conn = sqlite3.connect(\"data/Chinook.db\")\n",
|
|
||||||
"print(\"Opened database successfully\")"
|
|
||||||
],
|
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
@ -601,17 +596,24 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"execution_count": 14
|
"source": [
|
||||||
|
"import sqlite3\n",
|
||||||
|
"\n",
|
||||||
|
"conn = sqlite3.connect(\"data/Chinook.db\")\n",
|
||||||
|
"print(\"Opened database successfully\")"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
"id": "abec0214",
|
"id": "abec0214",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:09.345308Z",
|
"end_time": "2024-07-12T22:42:21.370623Z",
|
||||||
"start_time": "2024-05-15T17:46:09.342998Z"
|
"start_time": "2024-07-12T22:42:21.368246Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def get_table_names(conn):\n",
|
"def get_table_names(conn):\n",
|
||||||
" \"\"\"Return a list of table names.\"\"\"\n",
|
" \"\"\"Return a list of table names.\"\"\"\n",
|
||||||
@ -638,9 +640,7 @@
|
|||||||
" columns_names = get_column_names(conn, table_name)\n",
|
" columns_names = get_column_names(conn, table_name)\n",
|
||||||
" table_dicts.append({\"table_name\": table_name, \"column_names\": columns_names})\n",
|
" table_dicts.append({\"table_name\": table_name, \"column_names\": columns_names})\n",
|
||||||
" return table_dicts\n"
|
" return table_dicts\n"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 15
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -653,13 +653,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 16,
|
||||||
"id": "0c0104cd",
|
"id": "0c0104cd",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:11.303746Z",
|
"end_time": "2024-07-12T22:42:22.668456Z",
|
||||||
"start_time": "2024-05-15T17:46:11.301210Z"
|
"start_time": "2024-07-12T22:42:22.665839Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"database_schema_dict = get_database_info(conn)\n",
|
"database_schema_dict = get_database_info(conn)\n",
|
||||||
"database_schema_string = \"\\n\".join(\n",
|
"database_schema_string = \"\\n\".join(\n",
|
||||||
@ -668,9 +670,7 @@
|
|||||||
" for table in database_schema_dict\n",
|
" for table in database_schema_dict\n",
|
||||||
" ]\n",
|
" ]\n",
|
||||||
")"
|
")"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 16
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -683,13 +683,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 17,
|
||||||
"id": "0258813a",
|
"id": "0258813a",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:16.569530Z",
|
"end_time": "2024-07-12T22:42:24.156291Z",
|
||||||
"start_time": "2024-05-15T17:46:16.567801Z"
|
"start_time": "2024-07-12T22:42:24.154372Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"tools = [\n",
|
"tools = [\n",
|
||||||
" {\n",
|
" {\n",
|
||||||
@ -715,9 +717,7 @@
|
|||||||
" }\n",
|
" }\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
"]"
|
"]"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 17
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
@ -732,13 +732,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
"id": "65585e74",
|
"id": "65585e74",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:19.198723Z",
|
"end_time": "2024-07-12T22:42:25.444734Z",
|
||||||
"start_time": "2024-05-15T17:46:19.197043Z"
|
"start_time": "2024-07-12T22:42:25.442757Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def ask_database(conn, query):\n",
|
"def ask_database(conn, query):\n",
|
||||||
" \"\"\"Function to query SQLite database with a provided SQL query.\"\"\"\n",
|
" \"\"\"Function to query SQLite database with a provided SQL query.\"\"\"\n",
|
||||||
@ -747,9 +749,7 @@
|
|||||||
" except Exception as e:\n",
|
" except Exception as e:\n",
|
||||||
" results = f\"query failed with error: {e}\"\n",
|
" results = f\"query failed with error: {e}\"\n",
|
||||||
" return results"
|
" return results"
|
||||||
],
|
]
|
||||||
"outputs": [],
|
|
||||||
"execution_count": 18
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -767,13 +767,23 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
"id": "e8b7cb9cdc7a7616",
|
"id": "e8b7cb9cdc7a7616",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2024-05-15T17:46:25.725379Z",
|
"end_time": "2024-07-12T22:42:28.395683Z",
|
||||||
"start_time": "2024-05-15T17:46:24.255505Z"
|
"start_time": "2024-07-12T22:42:27.415626Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_wDN8uLjq2ofuU6rVx1k8Gw0e', function=Function(arguments='{\"query\":\"SELECT Album.Title, COUNT(Track.TrackId) AS TrackCount FROM Album INNER JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Album.Title ORDER BY TrackCount DESC LIMIT 1;\"}', name='ask_database'), type='function')])\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"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",
|
"# 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",
|
"messages = [{\n",
|
||||||
@ -793,27 +803,27 @@
|
|||||||
"messages.append(response_message)\n",
|
"messages.append(response_message)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(response_message)"
|
"print(response_message)"
|
||||||
],
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 20,
|
||||||
|
"id": "351c39def3417776",
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-07-12T22:42:30.439519Z",
|
||||||
|
"start_time": "2024-07-12T22:42:29.799492Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"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"
|
"The album with the most tracks is titled \"Greatest Hits,\" which contains 57 tracks.\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"execution_count": 19
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"id": "351c39def3417776",
|
|
||||||
"metadata": {
|
|
||||||
"ExecuteTime": {
|
|
||||||
"end_time": "2024-05-15T17:46:30.346444Z",
|
|
||||||
"start_time": "2024-05-15T17:46:29.699046Z"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Step 2: determine if the response from the model includes a tool call. \n",
|
"# Step 2: determine if the response from the model includes a tool call. \n",
|
||||||
"tool_calls = response_message.tool_calls\n",
|
"tool_calls = response_message.tool_calls\n",
|
||||||
@ -821,8 +831,8 @@
|
|||||||
" # If true the model will return the name of the tool / function to call and the argument(s) \n",
|
" # If true the model will return the name of the tool / function to call and the argument(s) \n",
|
||||||
" tool_call_id = tool_calls[0].id\n",
|
" tool_call_id = tool_calls[0].id\n",
|
||||||
" tool_function_name = tool_calls[0].function.name\n",
|
" tool_function_name = tool_calls[0].function.name\n",
|
||||||
" tool_query_string = eval(tool_calls[0].function.arguments)['query']\n",
|
" tool_query_string = json.loads(tool_calls[0].function.arguments)['query']\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # Step 3: Call the function and retrieve results. Append the results to the messages list. \n",
|
" # Step 3: Call the function and retrieve results. Append the results to the messages list. \n",
|
||||||
" if tool_function_name == 'ask_database':\n",
|
" if tool_function_name == 'ask_database':\n",
|
||||||
" results = ask_database(conn, tool_query_string)\n",
|
" results = ask_database(conn, tool_query_string)\n",
|
||||||
@ -846,17 +856,7 @@
|
|||||||
"else: \n",
|
"else: \n",
|
||||||
" # Model did not identify a function to call, result can be returned to the user \n",
|
" # Model did not identify a function to call, result can be returned to the user \n",
|
||||||
" print(response_message.content) "
|
" print(response_message.content) "
|
||||||
],
|
]
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"The album with the most tracks is titled \"Greatest Hits,\" and it contains 57 tracks.\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"execution_count": 20
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
"attachments": {},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user