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 = jsonr