From e4f15e4eac472dd9233abbe393ecccb000d16a6c Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:27:48 -0700 Subject: [PATCH] Add support for YAML Spec Plugins (#2054) It's common to use `yaml` for an OpenAPI spec used in the GPT plugins. For example: https://www.joinmilo.com/openapi.yaml or https://api.slack.com/specs/openapi/ai-plugin.yaml (from [Wong2's ChatGPT Plugins List](https://github.com/wong2/chatgpt-plugins)) --- langchain/tools/plugin.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/langchain/tools/plugin.py b/langchain/tools/plugin.py index efac2faf..34f93404 100644 --- a/langchain/tools/plugin.py +++ b/langchain/tools/plugin.py @@ -1,10 +1,21 @@ from __future__ import annotations +import json + import requests +import yaml from langchain.tools.base import BaseTool +def marshal_spec(txt: str) -> dict: + """Convert the yaml or json serialized spec to a dict.""" + try: + return json.loads(txt) + except json.JSONDecodeError: + return yaml.safe_load(txt) + + class AIPluginTool(BaseTool): api_spec: str @@ -17,9 +28,12 @@ class AIPluginTool(BaseTool): f"You should only call this ONCE! What is the " f"{response['name_for_human']} API useful for? " ) + response["description_for_human"] + url = response["api"]["url"] + open_api_spec_str = requests.get(url).text + open_api_spec = marshal_spec(open_api_spec_str) api_spec = ( f"Usage Guide: {response['description_for_model']}\n\n" - f"OpenAPI Spec: {requests.get(response['api']['url']).json()}" + f"OpenAPI Spec: {open_api_spec}" ) return cls( name=response["name_for_model"], description=description, api_spec=api_spec