feat: JsonAgent implementation
This commit is contained in:
parent
203b8ff6fe
commit
d93413ee56
1 changed files with 19 additions and 14 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from langchain import LLMChain
|
from langchain import LLMChain
|
||||||
from langchain.agents import AgentExecutor, ZeroShotAgent
|
from langchain.agents import AgentExecutor, ZeroShotAgent
|
||||||
from langchain.agents.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX
|
from langchain.agents.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX
|
||||||
|
|
@ -7,16 +9,19 @@ from langchain.schema import BaseLanguageModel
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class JsonAgent(BaseModel):
|
class JsonAgent(AgentExecutor):
|
||||||
"""Json agent"""
|
"""Json agent"""
|
||||||
|
|
||||||
toolkit: JsonToolkit
|
@classmethod
|
||||||
llm: BaseLanguageModel
|
def initialize(cls, *args, **kwargs):
|
||||||
|
return cls.from_toolkit_and_llm(*args, **kwargs)
|
||||||
|
|
||||||
def __init__(self, toolkit: JsonToolkit, llm: BaseLanguageModel):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(toolkit=toolkit, llm=llm)
|
super().__init__(*args, **kwargs)
|
||||||
self.toolkit = toolkit
|
|
||||||
tools = self.toolkit.get_tools()
|
@classmethod
|
||||||
|
def from_toolkit_and_llm(cls, toolkit: JsonToolkit, llm: BaseLanguageModel):
|
||||||
|
tools = toolkit.get_tools()
|
||||||
tool_names = [tool.name for tool in tools]
|
tool_names = [tool.name for tool in tools]
|
||||||
prompt = ZeroShotAgent.create_prompt(
|
prompt = ZeroShotAgent.create_prompt(
|
||||||
tools,
|
tools,
|
||||||
|
|
@ -30,12 +35,12 @@ class JsonAgent(BaseModel):
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
)
|
)
|
||||||
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
|
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
|
||||||
self.agent_executor = AgentExecutor.from_agent_and_tools(
|
return cls.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
||||||
agent=agent, tools=tools, verbose=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
|
||||||
return self.agent_executor(*args, **kwargs)
|
|
||||||
|
|
||||||
def run(self, *args, **kwargs):
|
def run(self, *args, **kwargs):
|
||||||
return self.agent_executor.run(*args, **kwargs)
|
return super().run(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
CUSTOM_AGENTS = {
|
||||||
|
"JsonAgent": JsonAgent,
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue