Skip to content

Instantly share code, notes, and snippets.

@goldengrape
Last active May 23, 2023 03:36
Show Gist options
  • Save goldengrape/1e3e342240cc6903e61e590f43569c93 to your computer and use it in GitHub Desktop.
Save goldengrape/1e3e342240cc6903e61e590f43569c93 to your computer and use it in GitHub Desktop.
from langchain.chat_models import ChatOpenAI
from langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType
from langchain.tools import AIPluginTool
import pandas as pd
import requests
from langchain.tools.plugin import AIPlugin
import json
import yaml
from langchain.llms import OpenAI
def plugin_csv_to_dict(filename):
plugin_df = pd.read_csv(filename)
plugin_list=[]
for i in range(len(plugin_df)):
plugin={}
plugin["schema_version"]="v1"
plugin["logo_url"]=plugin_df["图标"][i]
plugin["name_for_human"]=plugin_df["名称"][i]
plugin["description_for_human"]=plugin_df["description"][i]
plugin['description_for_model']=plugin_df["intro"][i]
plugin['legal_info_url']=plugin_df['网站'][i]
plugin['contact_email']=plugin_df['邮箱'][i]
plugin['name_for_model']=plugin_df['namespace'][i]
plugin["api"]={
"type": "openapi",
"url": plugin_df['API'][i],
"has_user_authentication": False}
plugin["auth"]={"type": "none"}
plugin_list.append(plugin)
return plugin_list
class AIPluginToolExtended(AIPluginTool):
@classmethod
def from_plugin_dict(cls, plugin_dict: dict) -> 'AIPluginToolExtended':
plugin = AIPlugin(
schema_version=plugin_dict["schema_version"],
name_for_model=plugin_dict["name_for_model"],
name_for_human=plugin_dict["name_for_human"],
description_for_human=plugin_dict["description_for_human"],
description_for_model=plugin_dict["description_for_model"],
api=plugin_dict["api"],
auth=plugin_dict["auth"],
logo_url=plugin_dict.get("logo_url"),
contact_email=plugin_dict.get("contact_email"),
legal_info_url=plugin_dict.get("legal_info_url"),
)
description = (
f"Call this tool to get the OpenAPI spec (and usage guide) "
f"for interacting with the {plugin.name_for_human} API. "
f"You should only call this ONCE! What is the "
f"{plugin.name_for_human} API useful for? "
) + plugin.description_for_human
open_api_spec_str = requests.get(plugin.api.url).text
try:
open_api_spec= json.loads(open_api_spec_str)
except json.JSONDecodeError:
open_api_spec= yaml.safe_load(open_api_spec_str)
api_spec = (
f"Usage Guide: {plugin.description_for_model}\n\n"
f"OpenAPI Spec: {open_api_spec}"
)
return cls(
name=plugin.name_for_model,
description=description,
plugin=plugin,
api_spec=api_spec,
)
llm = OpenAI(temperature=0)
# csv can export from https://www.notion.so/df51bba48da344af968311846e46465e?v=0798673974714e2da05a8f7711633c3d
plugin_csv_file = "ChatGPT_Plugins.csv"
plugin_dict_list=plugin_csv_to_dict(plugin_csv_file)
tools = load_tools(["requests_all"] )
for plugin_dict in plugin_dict_list:
try:
tool = AIPluginToolExtended.from_plugin_dict(plugin_dict)
tools+=[tool]
except:
print(f"Failed to load tool: {plugin_dict['name_for_human']}")
# Failed to load tool: ChatWithPDF
# Failed to load tool: Cloudflare Radar
# Failed to load tool: GameBase
# Failed to load tool: Hubbub
# Failed to load tool: Open Trivia
# Failed to load tool: Shimmer: Nutrition Coach
# Failed to load tool: Tutory
# Failed to load tool: Zillow
# test
agent_chain = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent_chain.run("请用中文总结这个链接的内容 https://www.klarna.com/.well-known/ai-plugin.json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment