If you try to run this lesson with the latest OpenAI module, you will receive the following error:
APIRemovedInV1:
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
Below is an updated version of the L1 Jupyter Notebook which uses the latest OpenAI ChatCompletion API. Alternatively you can pin your installation to the old version, e.g. `pip install openai==0.28 but it seems more useful to learn with newer API.
Save the stuff below as L1-openai_functions_student.ipynb and it should work.
{
"cells": [
{
"cell_type": "markdown",
"id": "a3df44d6-62d0-4324-8052-419503a6b040",
"metadata": {},
"source": [
"# OpenAI Function Calling\n"
]
},
{
"cell_type": "markdown",
"id": "59b038f2-759a-42e9-ab02-eca264b93ee5",
"metadata": {},
"source": [
"**Notes**:\n",
"- LLM's don't always produce the same results. The results you see in this notebook may differ from the results you see in the video.\n",
"- Notebooks results are temporary. Download the notebooks to your local machine if you wish to save your results."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3d1a7aac-599c-4653-b497-49fe9a31a07d",
"metadata": {
"height": 114,
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"import openai\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"openai.api_key = os.environ['OPENAI_API_KEY']"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e036b435-e842-40a3-8e1c-1d5d716394c6",
"metadata": {
"height": 233
},
"outputs": [],
"source": [
"import json\n",
"\n",
"# Example dummy function hard coded to return the same weather\n",
"# In production, this could be your backend API or an external API\n",
"\n",
"\n",
"def get_current_weather(location, unit=\"fahrenheit\"):\n",
" \"\"\"Get the current weather in a given location\"\"\"\n",
" weather_info = {\n",
" \"location\": location,\n",
" \"temperature\": \"72\",\n",
" \"unit\": unit,\n",
" \"forecast\": [\"sunny\", \"windy\"],\n",
" }\n",
" return json.dumps(weather_info)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "290fae11-d9af-40f8-9b78-3d6a847737b2",
"metadata": {
"height": 318
},
"outputs": [],
"source": [
"# define a function\n",
"functions = [\n",
" {\n",
" \"name\": \"get_current_weather\",\n",
" \"description\": \"Get the current weather in a given location\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, CA\",\n",
" },\n",
" \"unit\": {\"type\": \"string\", \"enum\": [\"celsius\", \"fahrenheit\"]},\n",
" },\n",
" \"required\": [\"location\"],\n",
" },\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5b5e2abe-7cf0-4b00-8c08-b3df91d78eaa",
"metadata": {
"height": 114
},
"outputs": [],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What's the weather like in Boston?\"\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "654fce05-7ef6-49d7-8d78-f190ecf3f0dd",
"metadata": {
"height": 29
},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"\n",
"client = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0e341605-5e3e-44d1-b64e-79c121aeb8d8",
"metadata": {
"height": 29
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8Tal5Vp3Y6O5SdKIq7E1BUOdyPFLp', choices=[Choice(finish_reason='function_call', index=0, message=ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\\n \"location\": \"Boston, MA\"\\n}', name='get_current_weather'), tool_calls=None))], created=1702063503, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=18, prompt_tokens=82, total_tokens=100))\n"
]
}
],
"source": [
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5748f7ce-9c74-435f-b5dc-d04e627675e3",
"metadata": {
"height": 29
},
"outputs": [],
"source": [
"response_message = response.choices[0].message"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "a7f733bd-e75b-4a84-9cbe-1a8c695015a3",
"metadata": {
"height": 29
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\\n \"location\": \"Boston, MA\"\\n}', name='get_current_weather'), tool_calls=None)\n"
]
}
],
"source": [
"print(response_message)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "9d8fa467-d9b3-4d62-b067-f6e8788b2907",
"metadata": {
"height": 29
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(response_message.content)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FunctionCall(arguments='{\\n \"location\": \"Boston, MA\"\\n}', name='get_current_weather')\n"
]
}
],
"source": [
"print(response_message.function_call)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"location\": \"Boston, MA\"\n",
"}\n"
]
}
],
"source": [
"print(response_message.function_call.arguments)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "433a51b6-9c92-4765-85aa-285dccf7748b",
"metadata": {
"height": 29
},
"outputs": [
{
"data": {
"text/plain": [
"{'location': 'Boston, MA'}"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json.loads(response_message.function_call.arguments)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "675d9372-4388-4f18-b44c-e291668ea46d",
"metadata": {
"height": 29
},
"outputs": [],
"source": [
"args = json.loads(response_message.function_call.arguments)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "3cbb1aec-454a-4a34-9a6b-351ee3759a3a",
"metadata": {
"height": 29
},
"outputs": [
{
"data": {
"text/plain": [
"'{\"location\": {\"location\": \"Boston, MA\"}, \"temperature\": \"72\", \"unit\": \"fahrenheit\", \"forecast\": [\"sunny\", \"windy\"]}'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_current_weather(args)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "8c2cbe66-784a-40ff-a268-7bd0f984d5b8",
"metadata": {
"height": 114
},
"outputs": [],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"hi!\",\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "8300232d-4f02-478b-bba2-d47173422866",
"metadata": {
"height": 97
},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "3e35564e-8f66-4b06-b14a-03e24a202a47",
"metadata": {
"height": 29
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbBWfWxB9tAxHYTVDXwbkt4081ds', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='Hello! How can I assist you today?', role='assistant', function_call=None, tool_calls=None))], created=1702065142, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=10, prompt_tokens=76, total_tokens=86))\n"
]
}
],
"source": [
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "e2af9f72-1cb9-4a97-b030-22562ecab99d",
"metadata": {
"height": 233
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbC4UcTCnWdlTGjPCgY1M5Dv0rqY', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='Hello! How can I assist you today?', role='assistant', function_call=None, tool_calls=None))], created=1702065176, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=10, prompt_tokens=76, total_tokens=86))\n"
]
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"hi!\",\n",
" }\n",
"]\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions,\n",
" function_call=\"auto\",\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "6ba8cafc-f785-4595-9e3c-48b06424ee8b",
"metadata": {
"height": 233
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbCMxDwoawgSk3YPkAo2VRYOdLgJ', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='Hello! How can I assist you today?', role='assistant', function_call=None, tool_calls=None))], created=1702065194, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=9, prompt_tokens=77, total_tokens=86))\n"
]
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"hi!\",\n",
" }\n",
"]\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions,\n",
" function_call=\"none\",\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "ca7cc5a7-1572-4171-9016-9ec2871d389b",
"metadata": {
"height": 233
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbF9P7gcH1CtutsqMbNScw22E0dm', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='{\\n \"location\": \"Boston, MA\",\\n \"unit\": \"celsius\"\\n}', role='assistant', function_call=None, tool_calls=None))], created=1702065367, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=19, prompt_tokens=82, total_tokens=101))\n"
]
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What's the weather in Boston?\",\n",
" }\n",
"]\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions,\n",
" function_call=\"none\",\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "282a92ba-5677-4c72-b556-d29e6a4152a0",
"metadata": {
"height": 233
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbFErYyGbhUic6oHCj6WgX8dpk2H', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\\n \"location\": \"San Francisco, CA\"\\n}', name='get_current_weather'), tool_calls=None))], created=1702065372, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=12, prompt_tokens=83, total_tokens=95))\n"
]
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"hi!\",\n",
" }\n",
"]\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions,\n",
" function_call={\"name\": \"get_current_weather\"},\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "3c5229a4-2700-48b4-b2b9-3b1e1535f903",
"metadata": {
"height": 233
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbFGJVrxHaYnhtSdeKCUbotmnnkB', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\\n\"location\": \"Boston, MA\"\\n}', name='get_current_weather'), tool_calls=None))], created=1702065374, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=10, prompt_tokens=89, total_tokens=99))\n"
]
}
],
"source": [
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"What's the weather like in Boston!\",\n",
" }\n",
"]\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
" functions=functions,\n",
" function_call={\"name\": \"get_current_weather\"},\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "e2f3a9d7-9f30-4524-a952-5dd87c6d2eef",
"metadata": {
"height": 29
},
"outputs": [],
"source": [
"messages.append(response.choices[0].message)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "c817376f-3a7f-4448-acdd-1639c70d42e4",
"metadata": {
"height": 46
},
"outputs": [],
"source": [
"args = json.loads(response_message.function_call.arguments)\n",
"observation = get_current_weather(args)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "63454808-10a2-4301-9977-89aa79018152",
"metadata": {
"height": 131
},
"outputs": [],
"source": [
"messages.append(\n",
" {\n",
" \"role\": \"function\",\n",
" \"name\": \"get_current_weather\",\n",
" \"content\": observation,\n",
" }\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "f323fb69-c907-4f19-a2d9-80d828b4a5c2",
"metadata": {
"height": 97
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-8TbFNtNKUK8cuL2zgDRfZCCp9xdnW', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='The current weather in Boston is 72°F and sunny with windy conditions.', role='assistant', function_call=None, tool_calls=None))], created=1702065381, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=15, prompt_tokens=76, total_tokens=91))\n"
]
}
],
"source": [
"response = client.chat.completions.create(\n",
" model=\"gpt-3.5-turbo-0613\",\n",
" messages=messages,\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf4bedc6-6342-4d69-9e19-181d2b0aa243",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "fad839a6-83e9-46a2-9d39-d7be8cbcc6f8",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f71b4b8-3a1c-4c19-9d65-d50340207dca",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca98ceb2-0feb-49c6-9889-70e9b52300e1",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec827289-00c7-444e-aeb2-1e7404fbc15c",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fcd3cb6-a782-427a-bd34-46ce47e5f707",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7eef7189-688b-4d79-b11b-67b147a00352",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6733b4e-a958-421c-a3c8-635e91428f34",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "93d4ecd5-14c1-492e-84ec-b4f6bb508e1c",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e262ae6-092b-4f9e-a1cc-cc1704ecfe76",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5bc5cd3-84f4-4d05-b941-575cafd3e14e",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0ca8690-50b3-42dc-b95c-364bbcd14581",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "48e64656-d3ee-47a5-96e0-766652f28138",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a27659fe-3720-4e13-83d0-fe2d52b03075",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5c16168-2988-4540-a921-b78e299ab5d2",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "53a1d9ed-fb4e-4f3f-a8df-4a6d67c3ec1a",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "94ed11cb-5a90-49a5-aecb-48081d146af6",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "206f0256-9cb4-4f3b-8614-0b69a4eeaac9",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e442de8e-ad55-4730-a43b-0c2bc2066b31",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5854155b-57f8-49f2-b595-6b6163d9db2a",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8637d7d-d6cc-4c55-b3cc-8dc51a35efb5",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a450da3-ef87-44f7-872c-39a1a714a015",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "abd24043-91e6-4838-8ce0-7f02802cf62a",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "63167545-4805-4534-a8c0-9e3161edc02a",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "eea5a420-9c52-441c-b868-6ce69c498477",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ab51675-a078-4704-b63b-9f722d86b0d9",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d4c2a24-c011-4c44-a78e-611143ff3034",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "600778a5-a4d5-44aa-b3a7-172c82e74eed",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "60557042-93b5-4172-8aaa-1219ea35addb",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "693f24b4-0796-4c2d-86ab-0d23766551d3",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "611029f7-e4c5-45b0-bcc9-9ec214a32686",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e20ed315-b80a-4d82-bda8-41cc54b30b36",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "be374e72-c849-4962-bb50-33607824bab1",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a7702f2-cee6-4649-a662-6d1dc9d49e0c",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "37a2ba14-9908-48b1-adad-1ef764114254",
"metadata": {
"height": 29
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}