From 0e3b0c827e930e5e7784b403b93649e893e98adf Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Mon, 27 Mar 2023 23:31:53 -0700 Subject: [PATCH] Harrison/ai plugin (#2084) Co-authored-by: Xupeng (Tony) Tong --- .../tools/examples/chatgpt_plugins.ipynb | 12 +++--- langchain/tools/plugin.py | 40 +++++++++++++++---- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/docs/modules/agents/tools/examples/chatgpt_plugins.ipynb b/docs/modules/agents/tools/examples/chatgpt_plugins.ipynb index d407e66b..e1dbfa08 100644 --- a/docs/modules/agents/tools/examples/chatgpt_plugins.ipynb +++ b/docs/modules/agents/tools/examples/chatgpt_plugins.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "edc0ea0e", "metadata": { "scrolled": false @@ -60,9 +60,9 @@ "Thought:\u001b[32;1m\u001b[1;3mI need to use the Klarna Shopping API to search for t shirts.\n", "Action: requests_get\n", "Action Input: https://www.klarna.com/us/shopping/public/openai/v0/products?q=t%20shirts\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m{\"products\":[{\"name\":\"Lacoste Men's Pack of Plain T-Shirts\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3202043025/Clothing/Lacoste-Men-s-Pack-of-Plain-T-Shirts/?source=openai\",\"price\":\"$28.02\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White,Black\"]},{\"name\":\"Hanes Men's Ultimate 6pk. Crewneck T-Shirts\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3201808270/Clothing/Hanes-Men-s-Ultimate-6pk.-Crewneck-T-Shirts/?source=openai\",\"price\":\"$13.82\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White\"]},{\"name\":\"Nike Boy's Jordan Stretch T-shirts\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl359/3201863202/Children-s-Clothing/Nike-Boy-s-Jordan-Stretch-T-shirts/?source=openai\",\"price\":\"$14.99\",\"attributes\":[\"Color:White,Green\",\"Model:Boy\",\"Pattern:Solid Color\",\"Size (Small-Large):S,XL,L,M\"]},{\"name\":\"Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3203028500/Clothing/Polo-Classic-Fit-Cotton-V-Neck-T-Shirts-3-Pack/?source=openai\",\"price\":\"$29.95\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White,Blue,Black\"]},{\"name\":\"adidas Comfort T-shirts Men's 3-pack\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3202640533/Clothing/adidas-Comfort-T-shirts-Men-s-3-pack/?source=openai\",\"price\":\"$14.99\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White,Black\",\"Pattern:Solid Color\",\"Neckline:Round\"]}]}\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3mThese are the available t shirts on Klarna: Lacoste Men's Pack of Plain T-Shirts, Hanes Men's Ultimate 6pk. Crewneck T-Shirts, Nike Boy's Jordan Stretch T-shirts, Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack, and adidas Comfort T-shirts Men's 3-pack.\n", - "Final Answer: The available t shirts on Klarna are Lacoste Men's Pack of Plain T-Shirts, Hanes Men's Ultimate 6pk. Crewneck T-Shirts, Nike Boy's Jordan Stretch T-shirts, Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack, and adidas Comfort T-shirts Men's 3-pack.\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m{\"products\":[{\"name\":\"Lacoste Men's Pack of Plain T-Shirts\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3202043025/Clothing/Lacoste-Men-s-Pack-of-Plain-T-Shirts/?utm_source=openai\",\"price\":\"$26.60\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White,Black\"]},{\"name\":\"Hanes Men's Ultimate 6pk. Crewneck T-Shirts\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3201808270/Clothing/Hanes-Men-s-Ultimate-6pk.-Crewneck-T-Shirts/?utm_source=openai\",\"price\":\"$13.82\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White\"]},{\"name\":\"Nike Boy's Jordan Stretch T-shirts\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl359/3201863202/Children-s-Clothing/Nike-Boy-s-Jordan-Stretch-T-shirts/?utm_source=openai\",\"price\":\"$14.99\",\"attributes\":[\"Material:Cotton\",\"Color:White,Green\",\"Model:Boy\",\"Size (Small-Large):S,XL,L,M\"]},{\"name\":\"Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3203028500/Clothing/Polo-Classic-Fit-Cotton-V-Neck-T-Shirts-3-Pack/?utm_source=openai\",\"price\":\"$29.95\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White,Blue,Black\"]},{\"name\":\"adidas Comfort T-shirts Men's 3-pack\",\"url\":\"https://www.klarna.com/us/shopping/pl/cl10001/3202640533/Clothing/adidas-Comfort-T-shirts-Men-s-3-pack/?utm_source=openai\",\"price\":\"$14.99\",\"attributes\":[\"Material:Cotton\",\"Target Group:Man\",\"Color:White,Black\",\"Neckline:Round\"]}]}\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3mThe available t shirts in Klarna are Lacoste Men's Pack of Plain T-Shirts, Hanes Men's Ultimate 6pk. Crewneck T-Shirts, Nike Boy's Jordan Stretch T-shirts, Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack, and adidas Comfort T-shirts Men's 3-pack.\n", + "Final Answer: The available t shirts in Klarna are Lacoste Men's Pack of Plain T-Shirts, Hanes Men's Ultimate 6pk. Crewneck T-Shirts, Nike Boy's Jordan Stretch T-shirts, Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack, and adidas Comfort T-shirts Men's 3-pack.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -70,10 +70,10 @@ { "data": { "text/plain": [ - "\"The available t shirts on Klarna are Lacoste Men's Pack of Plain T-Shirts, Hanes Men's Ultimate 6pk. Crewneck T-Shirts, Nike Boy's Jordan Stretch T-shirts, Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack, and adidas Comfort T-shirts Men's 3-pack.\"" + "\"The available t shirts in Klarna are Lacoste Men's Pack of Plain T-Shirts, Hanes Men's Ultimate 6pk. Crewneck T-Shirts, Nike Boy's Jordan Stretch T-shirts, Polo Classic Fit Cotton V-Neck T-Shirts 3-Pack, and adidas Comfort T-shirts Men's 3-pack.\"" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } diff --git a/langchain/tools/plugin.py b/langchain/tools/plugin.py index 34f93404..e6f6e3e8 100644 --- a/langchain/tools/plugin.py +++ b/langchain/tools/plugin.py @@ -1,13 +1,34 @@ from __future__ import annotations import json +from typing import Optional import requests import yaml +from pydantic import BaseModel from langchain.tools.base import BaseTool +class ApiConfig(BaseModel): + type: str + url: str + has_user_authentication: Optional[bool] = False + + +class AIPlugin(BaseModel): + schema_version: str + name_for_model: str + name_for_human: str + description_for_model: str + description_for_human: str + auth: Optional[dict] = None + api: ApiConfig + logo_url: Optional[str] + contact_email: Optional[str] + legal_info_url: Optional[str] + + def marshal_spec(txt: str) -> dict: """Convert the yaml or json serialized spec to a dict.""" try: @@ -17,26 +38,31 @@ def marshal_spec(txt: str) -> dict: class AIPluginTool(BaseTool): + plugin: AIPlugin api_spec: str @classmethod def from_plugin_url(cls, url: str) -> AIPluginTool: response = requests.get(url).json() + plugin = AIPlugin(**response) description = ( f"Call this tool to get the OpenAPI spec (and usage guide) " - f"for interacting with the {response['name_for_human']} API. " + f"for interacting with the {plugin.name_for_human} API. " 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 + f"{plugin.name_for_human} API useful for? " + ) + plugin.description_for_human + open_api_spec_str = requests.get(plugin.api.url).text open_api_spec = marshal_spec(open_api_spec_str) api_spec = ( - f"Usage Guide: {response['description_for_model']}\n\n" + f"Usage Guide: {plugin.description_for_model}\n\n" f"OpenAPI Spec: {open_api_spec}" ) + return cls( - name=response["name_for_model"], description=description, api_spec=api_spec + name=plugin.name_for_model, + description=description, + plugin=plugin, + api_spec=api_spec, ) def _run(self, tool_input: str) -> str: