mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
c2a3021bb0
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
111 lines
2.8 KiB
Python
111 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Optional, Type
|
|
|
|
import requests
|
|
import yaml
|
|
from langchain_core.callbacks import (
|
|
AsyncCallbackManagerForToolRun,
|
|
CallbackManagerForToolRun,
|
|
)
|
|
from langchain_core.tools import BaseTool
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class ApiConfig(BaseModel):
|
|
"""API Configuration."""
|
|
|
|
type: str
|
|
url: str
|
|
has_user_authentication: Optional[bool] = False
|
|
|
|
|
|
class AIPlugin(BaseModel):
|
|
"""AI Plugin Definition."""
|
|
|
|
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]
|
|
|
|
@classmethod
|
|
def from_url(cls, url: str) -> AIPlugin:
|
|
"""Instantiate AIPlugin from a URL."""
|
|
response = requests.get(url).json()
|
|
return cls(**response)
|
|
|
|
|
|
def marshal_spec(txt: str) -> dict:
|
|
"""Convert the yaml or json serialized spec to a dict.
|
|
|
|
Args:
|
|
txt: The yaml or json serialized spec.
|
|
|
|
Returns:
|
|
dict: The spec as a dict.
|
|
"""
|
|
try:
|
|
return json.loads(txt)
|
|
except json.JSONDecodeError:
|
|
return yaml.safe_load(txt)
|
|
|
|
|
|
class AIPluginToolSchema(BaseModel):
|
|
"""Schema for AIPluginTool."""
|
|
|
|
tool_input: Optional[str] = ""
|
|
|
|
|
|
class AIPluginTool(BaseTool):
|
|
"""Tool for getting the OpenAPI spec for an AI Plugin."""
|
|
|
|
plugin: AIPlugin
|
|
api_spec: str
|
|
args_schema: Type[AIPluginToolSchema] = AIPluginToolSchema
|
|
|
|
@classmethod
|
|
def from_plugin_url(cls, url: str) -> AIPluginTool:
|
|
plugin = AIPlugin.from_url(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
|
|
open_api_spec = marshal_spec(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,
|
|
)
|
|
|
|
def _run(
|
|
self,
|
|
tool_input: Optional[str] = "",
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the tool."""
|
|
return self.api_spec
|
|
|
|
async def _arun(
|
|
self,
|
|
tool_input: Optional[str] = None,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the tool asynchronously."""
|
|
return self.api_spec
|