diff --git a/examples/Function_calling_with_spec.ipynb b/examples/Function_calling_with_spec.ipynb new file mode 100644 index 00000000..f542fe84 --- /dev/null +++ b/examples/Function_calling_with_spec.ipynb @@ -0,0 +1 @@ +{"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":"\nCreated in deepnote.com \nCreated in Deepnote","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":[]}} \ No newline at end of file