You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openai-cookbook/examples/Function_calling_with_spec....

1 line
9.5 KiB
Plaintext

{"cells":[{"cell_type":"markdown","metadata":{"formattedRanges":[],"cell_id":"8317718bcd8b475e9b336514241d679e","deepnote_cell_type":"text-cell-h1"},"source":"# Function calling with OpenAPI Spec","block_group":"915440d3c8d2439bb021e661c89cc652"},{"cell_type":"code","metadata":{"source_hash":"d6b9a6d3","execution_start":1697176493606,"execution_millis":4812,"deepnote_to_be_reexecuted":false,"cell_id":"bf983b3e199d4ea6a2718e58a141bd88","deepnote_cell_type":"code"},"source":"!pip install -q jsonref\n!pip install -q openai","block_group":"f8d6b1292af340c4872f7de9a8403f82","execution_count":2,"outputs":[{"name":"stdout","text":"\n\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n\n\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n","output_type":"stream"}]},{"cell_type":"code","metadata":{"source_hash":"3957e668","execution_start":1697176503695,"execution_millis":111,"deepnote_to_be_reexecuted":false,"cell_id":"12fb12583cc74b7c842a1b4656b94f47","deepnote_cell_type":"code"},"source":"import uuid\nimport os\nimport json\nfrom typing import Optional\nfrom pydantic import BaseModel, Extra\nimport requests\nimport jsonref\nimport re\nimport openai\n\nopenai.api_key = os.environ[\"OPENAI_API_KEY\"]","block_group":"12fb12583cc74b7c842a1b4656b94f47","execution_count":3,"outputs":[]},{"cell_type":"code","metadata":{"source_hash":"d360bbb7","execution_start":1697178508954,"execution_millis":10,"deepnote_to_be_reexecuted":false,"cell_id":"adbb17ca8a2a4fa2aa3f0213a0e211b6","deepnote_cell_type":"code"},"source":"def parse_functions(openapi_spec):\n \n name_map = {}\n \n # List to hold the details of the generated functions\n functions = []\n\n # Iterate through the paths and methods specified in the OpenAPI spec\n for path, methods in openapi_spec[\"paths\"].items():\n for method, spec_with_ref in methods.items():\n spec = jsonref.replace_refs(spec_with_ref)\n function_name = spec.get(\"operationId\")\n name_map[function_name] = {\"method\": method, \"path\": path}\n \n # Gather the description, request body and parameters from the spec\n desc = spec.get(\"description\") or spec.get(\"summary\", \"\")\n req_body = spec.get(\"requestBody\", {}).get(\"content\", {}).get(\"application/json\", {}).get(\"schema\")\n params = spec.get(\"parameters\", [])\n \n schema = {\"type\": \"object\", \"properties\": {}}\n if req_body:\n schema[\"properties\"][\"requestBody\"] = req_body\n \n if params:\n param_properties = {param[\"name\"]: param[\"schema\"] for param in params}\n schema[\"properties\"][\"parameters\"] = {\"type\": \"object\", \"properties\": param_properties}\n \n functions.append({\"name\": function_name, \"description\": desc, \"parameters\": schema})\n\n return functions, name_map","block_group":"227ed653d07f476c94ed3808ffccad57","execution_count":42,"outputs":[]},{"cell_type":"code","metadata":{"source_hash":"71999fa0","execution_start":1697178509840,"execution_millis":21,"deepnote_to_be_reexecuted":false,"cell_id":"e6ec88ccaeaa4215a40713f3a6eb8604","deepnote_cell_type":"code"},"source":"url = \"https://gist.githubusercontent.com/shyamal-anadkat/d44674a87778796222bdb8fa9158ad47/raw/030d173d53c55d806a93976705cf1c5f9e9c5240/frogeapi.json\"\nopenapi_spec = jsonref.loads(requests.get(url).content)\n\nfunctions, names = parse_functions(openapi_spec)","block_group":"03da0f1171ec46a6ae3c1b0dff4e6b18","execution_count":43,"outputs":[]},{"cell_type":"code","metadata":{"source_hash":"f18c5931","execution_start":1697179060000,"execution_millis":6205,"deepnote_to_be_reexecuted":false,"cell_id":"b8f7d0f157264694b958008f93aabf3f","deepnote_cell_type":"code"},"source":"FROGE_PROMPT = \"\"\"\nYou are a froge. Respond to the following prompt using a function_call:\n\nGet all the froges. Then create a new 1-year old froge named dalle3. Then look for a froge named davinci. Then delete the davinci froge.\n\"\"\"\n\nmessages = [\n {\"role\": \"user\", \"content\": FROGE_PROMPT}\n]\n\nMAX_CHAINED_CALLS = 5\n\ndef get_openai_response(functions, messages):\n return openai.ChatCompletion.create(\n model='gpt-3.5-turbo-16k-0613',\n functions=functions,\n function_call=\"auto\",\n temperature=0,\n messages=messages\n )\n\ndef process_chained_calls(functions, messages):\n for stack in range(MAX_CHAINED_CALLS):\n response = get_openai_response(functions, messages)\n message = response[\"choices\"][0][\"message\"]\n print(message)\n \n if message.get(\"function_call\"):\n messages.append(message)\n else:\n break\n else:\n print(f\"Reached max chained function calls: {MAX_CHAINED_CALLS}\")\n\n\nprocess_chained_calls(functions, messages)","block_group":"a9b683e7b54c43838e747b4d660581f1","execution_count":61,"outputs":[{"name":"stdout","text":"{\n \"role\": \"assistant\",\n \"content\": null,\n \"function_call\": {\n \"name\": \"listFroges\",\n \"arguments\": \"{}\"\n }\n}\n{\n \"role\": \"assistant\",\n \"content\": null,\n \"function_call\": {\n \"name\": \"createFroge\",\n \"arguments\": \"{\\n \\\"requestBody\\\": {\\n \\\"id\\\": \\\"dalle3\\\",\\n \\\"name\\\": \\\"dalle3\\\",\\n \\\"age\\\": 1\\n }\\n}\"\n }\n}\n{\n \"role\": \"assistant\",\n \"content\": null,\n \"function_call\": {\n \"name\": \"getFrogeByName\",\n \"arguments\": \"{\\n \\\"parameters\\\": {\\n \\\"name\\\": \\\"davinci\\\"\\n }\\n}\"\n }\n}\n{\n \"role\": \"assistant\",\n \"content\": null,\n \"function_call\": {\n \"name\": \"deleteFroge\",\n \"arguments\": \"{\\n \\\"parameters\\\": {\\n \\\"id\\\": \\\"davinci\\\"\\n }\\n}\"\n }\n}\n{\n \"role\": \"assistant\",\n \"content\": \"Here are the steps to perform the requested actions:\\n\\n1. Get all the froges.\\n2. Create a new 1-year old froge named dalle3.\\n3. Look for a froge named davinci.\\n4. Delete the davinci froge.\\n\\nPlease note that the response for each step will depend on the current state of the froges.\"\n}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"<a style='text-decoration:none;line-height:16px;display:flex;color:#5B5B62;padding:10px;justify-content:end;' href='https://deepnote.com?utm_source=created-in-deepnote-cell&projectId=baca1bec-71a4-4a27-a9d1-210d39960b44' target=\"_blank\">\n<img alt='Created in deepnote.com' style='display:inline;max-height:16px;margin:0px;margin-right:7.5px;' src='data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iODBweCIgaGVpZ2h0PSI4MHB4IiB2aWV3Qm94PSIwIDAgODAgODAiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDU0LjEgKDc2NDkwKSAtIGh0dHBzOi8vc2tldGNoYXBwLmNvbSAtLT4KICAgIDx0aXRsZT5Hcm91cCAzPC90aXRsZT4KICAgIDxkZXNjPkNyZWF0ZWQgd2l0aCBTa2V0Y2guPC9kZXNjPgogICAgPGcgaWQ9IkxhbmRpbmciIHN0cm9rZT0ibm9uZSIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiPgogICAgICAgIDxnIGlkPSJBcnRib2FyZCIgdHJhbnNmb3JtPSJ0cmFuc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gwIEM0Ny4wNTUzNDg5LDc3LjU2NzA4MDggNTMuNDE4MjY3Nyw3MC4zMTM2MTAzIDUzLjQxODI2NzcsNTguMjM5NTg4NSBDNTMuNDE4MjY3Nyw0MC4xMjg1NTU3IDM2LjMwMzk1NDQsMzcuNzMwNDQwNSAyNS4yMjc0MTcsMzcuNzMwNDQwNSBDMTcuODQzMDU4NiwzNy43MzA0NDA1IDkuNDMzOTE5NjYsMzcuNzMwNDQwNSAwLDM3LjczMDQ0MDUgWiIgaWQ9IlBhdGgtMTkiIGZpbGw9IiMzNzkzRUYiPjwvcGF0aD4KICAgICAgICAgICAgPC9nPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+' > </img>\nCreated in <span style='font-weight:600;margin-left:4px;'>Deepnote</span></a>","metadata":{"created_in_deepnote_cell":true,"deepnote_cell_type":"markdown"}}],"nbformat":4,"nbformat_minor":0,"metadata":{"deepnote":{},"orig_nbformat":2,"deepnote_notebook_id":"84d101406ec34e36a9cf96d0c0c25a7d","deepnote_persisted_session":{"createdAt":"2023-10-12T23:01:45.778Z"},"deepnote_execution_queue":[]}}