-
Notifications
You must be signed in to change notification settings - Fork 20.3k
Description
System Info
Langchain 0.0.209
The recent commit #6518 provided an OpenAIMultiFunctionsAgent class.
This MultiFunctions agent fails often when using Custom Tools that worked fine with the OpenAIFunctionsAgent.
File "/home/gene/endpoints/app/routers/query.py", line 44, in query3
result = await agent.acall(inputs={"input":query.query})
File "/home/gene/endpoints/venv/lib/python3.10/site-packages/langchain/chains/base.py", line 215, in acall
raise e
File "/home/gene/endpoints/venv/lib/python3.10/site-packages/langchain/chains/base.py", line 209, in acall
await self._acall(inputs, run_manager=run_manager)
File "/home/gene/endpoints/venv/lib/python3.10/site-packages/langchain/agents/agent.py", line 1006, in _acall
next_step_output = await self._atake_next_step(
File "/home/gene/endpoints/venv/lib/python3.10/site-packages/langchain/agents/agent.py", line 853, in _atake_next_step
output = await self.agent.aplan(
File "/home/gene/endpoints/venv/lib/python3.10/site-packages/langchain/agents/openai_functions_multi_agent/base.py", line 301, in aplan
agent_decision = _parse_ai_message(predicted_message)
File "/home/gene/endpoints/venv/lib/python3.10/site-packages/langchain/agents/openai_functions_multi_agent/base.py", line 110, in _parse_ai_message
tools = json.loads(function_call["arguments"])["actions"]
KeyError: 'actions'
Example tool that FAILS:
from typing import Optional, Type
from langchain.tools import BaseTool
from pydantic import BaseModel, Field
from langchain.callbacks.manager import AsyncCallbackManagerForToolRun, CallbackManagerForToolRun
class ProductInput(BaseModel):
prod_name: str = Field(description="Product Name or Type of Product")
class CustomProductTool(BaseTool):
name : str = "price_lookup"
description : str = "useful to look up pricing for a specific product or product type and shopping url of products offered by the Company's online website."
args_schema: Type[BaseModel] = ProductInput
def _run(self, prod_name: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> dict:
# custom code here
products = {}
return products
async def _arun(self, prod_name: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None) -> dict:
return self._run(prod_name)
Example tool that WORKS:
from typing import Optional, Type
from langchain.tools import BaseTool
from ..src.OrderStatus import func_get_order_status, afunc_get_order_status
from langchain.callbacks.manager import AsyncCallbackManagerForToolRun, CallbackManagerForToolRun
from pydantic import BaseModel, Field
class OrderInput(BaseModel):
order_num: str = Field(description="order number")
class CustomOrderTool(BaseTool):
name = "order_status"
description = "useful for when you need to look up the shipping status of an order."
args_schema: Type[BaseModel] = OrderInput
def _run(self, order_num: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> dict:
# Your custom logic here
return func_get_order_status(order_num)
async def _arun(self, order_num: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None) -> dict:
return await afunc_get_order_status(order_num)
Who can help?
Information
- The official example notebooks/scripts
- My own modified scripts
Related Components
- LLMs/Chat Models
- Embedding Models
- Prompts / Prompt Templates / Prompt Selectors
- Output Parsers
- Document Loaders
- Vector Stores / Retrievers
- Memory
- Agents / Agent Executors
- Tools / Toolkits
- Chains
- Callbacks/Tracing
- Async
Reproduction
Instantiate a OpenAIMultiFunctionAgent:
agent = initialize_agent(tools,llm,agent=AgentType.OPENAI_MULTI_FUNCTIONS, verbose=True)
Create a custom tool (example above):
tools=[
CustomOrderTool(return_direct=False),
CustomQAToolSources(llm=llm,vectorstore=general_vectorstore),
CustomProductTool(return_direct=False),
CustomEscalateTool(return_direct=False)
]
Call agent:
result = await agent.acall(inputs={"input":query.query})
Expected behavior
Tools are very similar to each other, not sure why one would work and the other fails. Might have something to do with the different description lengths? As far as I can tell the structure of the args_schema are the same between the two tools. Both tools work fine on OpenAIFunctionAgent.
I expected tools would work on OpenAIMultiFunctionAgent. Instead, KeyError: 'actions' results. Somehow the transformation of langchain tools to OpenAI function schema is not working as expected for OpenAIMultiFunctionAgent.