Examples and guides for using the OpenAI API
Ted/remove eval from function calling examples (#518)
Ted Sanders31a3a7b406eb
diff --git a/examples/How_to_call_functions_with_chat_models.ipynb b/examples/How_to_call_functions_with_chat_models.ipynbindex 550ca0d5..97ac9f76 100644--- a/examples/How_to_call_functions_with_chat_models.ipynb+++ b/examples/How_to_call_functions_with_chat_models.ipynb@@ -31,51 +31,14 @@},{"cell_type": "code",- "execution_count": 1,+ "execution_count": null,"id": "80e71f33","metadata": {"pycharm": {"is_executing": true}},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "Requirement already satisfied: scipy in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (1.10.1)\n",- "Requirement already satisfied: numpy<1.27.0,>=1.19.5 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from scipy) (1.24.3)\n",- "Requirement already satisfied: tenacity in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (8.2.2)\n",- "Requirement already satisfied: tiktoken in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (0.4.0)\n",- "Requirement already satisfied: regex>=2022.1.18 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from tiktoken) (2023.6.3)\n",- "Requirement already satisfied: requests>=2.26.0 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from tiktoken) (2.31.0)\n",- "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (3.1.0)\n",- "Requirement already satisfied: idna<4,>=2.5 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (3.4)\n",- "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (2.0.3)\n",- "Requirement already satisfied: certifi>=2017.4.17 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (2023.5.7)\n",- "Requirement already satisfied: termcolor in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (2.3.0)\n",- "Requirement already satisfied: openai in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (0.27.8)\n",- "Requirement already satisfied: requests>=2.20 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from openai) (2.31.0)\n",- "Requirement already satisfied: tqdm in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.65.0)\n",- "Requirement already satisfied: aiohttp in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from openai) (3.8.4)\n",- "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.20->openai) (3.1.0)\n",- "Requirement already satisfied: idna<4,>=2.5 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.20->openai) (3.4)\n",- "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.20->openai) (2.0.3)\n",- "Requirement already satisfied: certifi>=2017.4.17 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.20->openai) (2023.5.7)\n",- "Requirement already satisfied: attrs>=17.3.0 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from aiohttp->openai) (23.1.0)\n",- "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from aiohttp->openai) (6.0.4)\n",- "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from aiohttp->openai) (4.0.2)\n",- "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from aiohttp->openai) (1.9.2)\n",- "Requirement already satisfied: frozenlist>=1.1.1 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from aiohttp->openai) (1.3.3)\n",- "Requirement already satisfied: aiosignal>=1.1.2 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from aiohttp->openai) (1.3.1)\n",- "Requirement already satisfied: requests in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (2.31.0)\n",- "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.1.0)\n",- "Requirement already satisfied: idna<4,>=2.5 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.4)\n",- "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests) (2.0.3)\n",- "Requirement already satisfied: certifi>=2017.4.17 in /Users/joe/.virtualenvs/openai-cookbook/lib/python3.9/site-packages (from requests) (2023.5.7)\n"- ]- }- ],+ "outputs": [],"source": ["!pip install scipy\n","!pip install tenacity\n",@@ -87,7 +50,7 @@},{"cell_type": "code",- "execution_count": 2,+ "execution_count": 1,"id": "dab872c5","metadata": {},"outputs": [],@@ -113,7 +76,7 @@},{"cell_type": "code",- "execution_count": 3,+ "execution_count": 2,"id": "745ceec5","metadata": {},"outputs": [],@@ -144,7 +107,7 @@},{"cell_type": "code",- "execution_count": 4,+ "execution_count": 3,"id": "c4d1c99f","metadata": {},"outputs": [],@@ -189,7 +152,7 @@},{"cell_type": "code",- "execution_count": 5,+ "execution_count": 4,"id": "d2e25069","metadata": {},"outputs": [],@@ -250,7 +213,7 @@},{"cell_type": "code",- "execution_count": 6,+ "execution_count": 5,"id": "518d6827","metadata": {},"outputs": [@@ -258,10 +221,10 @@"data": {"text/plain": ["{'role': 'assistant',\n",- " 'content': \"Sure, could you please tell me the city and state for which you'd like to know the weather?\"}"+ " 'content': 'In which city and state would you like to know the current weather?'}"]},- "execution_count": 6,+ "execution_count": 5,"metadata": {},"output_type": "execute_result"}@@ -288,7 +251,7 @@},{"cell_type": "code",- "execution_count": 7,+ "execution_count": 6,"id": "23c42a6e","metadata": {},"outputs": [@@ -301,7 +264,7 @@" 'arguments': '{\\n \"location\": \"Glasgow, Scotland\",\\n \"format\": \"celsius\"\\n}'}}"]},- "execution_count": 7,+ "execution_count": 6,"metadata": {},"output_type": "execute_result"}@@ -326,7 +289,7 @@},{"cell_type": "code",- "execution_count": 8,+ "execution_count": 7,"id": "fa232e54","metadata": {},"outputs": [@@ -334,10 +297,10 @@"data": {"text/plain": ["{'role': 'assistant',\n",- " 'content': 'Sure, I can help you with that. Just tell me how many days you would like to forecast for.'}"+ " 'content': 'Sure, I can help you with that. Please provide me with the number of days you want to forecast for.'}"]},- "execution_count": 8,+ "execution_count": 7,"metadata": {},"output_type": "execute_result"}@@ -364,7 +327,7 @@},{"cell_type": "code",- "execution_count": 9,+ "execution_count": 8,"id": "c7d8a543","metadata": {},"outputs": [@@ -379,7 +342,7 @@" 'finish_reason': 'function_call'}"]},- "execution_count": 9,+ "execution_count": 8,"metadata": {},"output_type": "execute_result"}@@ -410,7 +373,7 @@},{"cell_type": "code",- "execution_count": 10,+ "execution_count": 9,"id": "559371b7","metadata": {},"outputs": [@@ -423,7 +386,7 @@" 'arguments': '{\\n \"location\": \"Toronto, Canada\",\\n \"format\": \"celsius\",\\n \"num_days\": 1\\n}'}}"]},- "execution_count": 10,+ "execution_count": 9,"metadata": {},"output_type": "execute_result"}@@ -441,7 +404,7 @@},{"cell_type": "code",- "execution_count": 11,+ "execution_count": 10,"id": "a7ab0f58","metadata": {},"outputs": [@@ -454,7 +417,7 @@" 'arguments': '{\\n \"location\": \"Toronto, Canada\",\\n \"format\": \"celsius\"\\n}'}}"]},- "execution_count": 11,+ "execution_count": 10,"metadata": {},"output_type": "execute_result"}@@ -480,18 +443,17 @@},{"cell_type": "code",- "execution_count": 12,+ "execution_count": 11,"id": "acfe54e6","metadata": {},"outputs": [{"data": {"text/plain": [- "{'role': 'assistant',\n",- " 'content': '{\\n \"location\": \"Toronto, Canada\",\\n \"format\": \"celsius\"\\n}'}"+ "{'role': 'assistant', 'content': 'Sure, let me get that information for you.'}"]},- "execution_count": 12,+ "execution_count": 11,"metadata": {},"output_type": "execute_result"}@@ -530,7 +492,7 @@},{"cell_type": "code",- "execution_count": 13,+ "execution_count": 12,"id": "30f6b60e","metadata": {},"outputs": [@@ -551,7 +513,7 @@},{"cell_type": "code",- "execution_count": 14,+ "execution_count": 13,"id": "abec0214","metadata": {},"outputs": [],@@ -593,7 +555,7 @@},{"cell_type": "code",- "execution_count": 15,+ "execution_count": 14,"id": "0c0104cd","metadata": {},"outputs": [],@@ -617,7 +579,7 @@},{"cell_type": "code",- "execution_count": 16,+ "execution_count": 15,"id": "0258813a","metadata": {},"outputs": [],@@ -657,7 +619,7 @@},{"cell_type": "code",- "execution_count": 17,+ "execution_count": 16,"id": "65585e74","metadata": {},"outputs": [],@@ -672,7 +634,7 @@"\n","def execute_function_call(message):\n"," if message[\"function_call\"][\"name\"] == \"ask_database\":\n",- " query = eval(message[\"function_call\"][\"arguments\"])[\"query\"]\n",+ " query = json.loads(message[\"function_call\"][\"arguments\"])[\"query\"]\n"," results = ask_database(conn, query)\n"," else:\n"," results = f\"Error: function {message['function_call']['name']} does not exist\"\n",@@ -681,7 +643,7 @@},{"cell_type": "code",- "execution_count": 18,+ "execution_count": 17,"id": "38c55083","metadata": {},"outputs": [@@ -689,14 +651,14 @@"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: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT Artist.Name, COUNT(Track.TrackId) AS TrackCount FROM Artist INNER JOIN Album ON Artist.ArtistId = Album.ArtistId INNER JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Artist.Name ORDER BY TrackCount DESC LIMIT 5;\"\\n}'}\n",- "\u001B[0m\n",- "\u001B[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n",- "\u001B[0m\n"+ "system: Answer user questions by generating SQL queries against the Chinook Music Database.\n",+ "\n",+ "user: Hi, who are the top 5 artists by number of tracks?\n",+ "\n",+ "assistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT ar.Name, COUNT(t.TrackId) AS NumTracks FROM Artist ar INNER JOIN Album al ON ar.ArtistId = al.ArtistId INNER JOIN Track t ON al.AlbumId = t.AlbumId GROUP BY ar.ArtistId ORDER BY NumTracks DESC LIMIT 5\"\\n}'}\n",+ "\n",+ "function (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n",+ "\n"]}],@@ -715,7 +677,7 @@},{"cell_type": "code",- "execution_count": 19,+ "execution_count": 18,"id": "710481dc","metadata": {"scrolled": true@@ -725,20 +687,20 @@"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: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT Artist.Name, COUNT(Track.TrackId) AS TrackCount FROM Artist INNER JOIN Album ON Artist.ArtistId = Album.ArtistId INNER JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Artist.Name ORDER BY TrackCount DESC LIMIT 5;\"\\n}'}\n",- "\u001B[0m\n",- "\u001B[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n",- "\u001B[0m\n",- "\u001B[32muser: What is the name of the album with the most tracks?\n",- "\u001B[0m\n",- "\u001B[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"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;\"\\n}'}\n",- "\u001B[0m\n",- "\u001B[35mfunction (ask_database): [('Greatest Hits', 57)]\n",- "\u001B[0m\n"+ "system: Answer user questions by generating SQL queries against the Chinook Music Database.\n",+ "\n",+ "user: Hi, who are the top 5 artists by number of tracks?\n",+ "\n",+ "assistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT ar.Name, COUNT(t.TrackId) AS NumTracks FROM Artist ar INNER JOIN Album al ON ar.ArtistId = al.ArtistId INNER JOIN Track t ON al.AlbumId = t.AlbumId GROUP BY ar.ArtistId ORDER BY NumTracks DESC LIMIT 5\"\\n}'}\n",+ "\n",+ "function (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n",+ "\n",+ "user: What is the name of the album with the most tracks?\n",+ "\n",+ "assistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT al.Title, COUNT(t.TrackId) AS NumTracks FROM Album al INNER JOIN Track t ON al.AlbumId = t.AlbumId GROUP BY al.AlbumId ORDER BY NumTracks DESC LIMIT 1\"\\n}'}\n",+ "\n",+ "function (ask_database): [('Greatest Hits', 57)]\n",+ "\n"]}],@@ -754,13 +716,14 @@]},{+ "attachments": {},"cell_type": "markdown","id": "2d89073c","metadata": {},"source": ["## Next Steps\n","\n",- "See our other [notebook](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_for_knowledge_retrieval.ipynb) that demonstrates how to use the Chat Completions API and functions for knowledge retrieval to interact conversationally with a knowledge base."+ "See our other [notebook](How_to_call_functions_for_knowledge_retrieval.ipynb) that demonstrates how to use the Chat Completions API and functions for knowledge retrieval to interact conversationally with a knowledge base."]},{