is there a way to set default tool when no tool is being selected? #25227
Replies: 2 comments 7 replies
-
Hey there, @InsectScholar! I'm here to help you with any bugs, questions, or contributions. Let's tackle this issue together! Yes, there is a way to set a default tool for the LangChain agent such that even if it does not invoke any tool, it should use that tool by default. This can be achieved by using the Here is an example of how you can implement this: from langchain.tools import BaseTool
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chat_models import ChatOpenAI
from typing import Optional
class ExceptionTool(BaseTool):
"""Tool that just returns the query."""
name: str = "_Exception"
description: str = "Exception tool"
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
return query
async def _arun(self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None) -> str:
return query
# Define your tools
tools = [retriever_tool2, retriever_tool]
# Create the prompt
prompt = ChatPromptTemplate.from_messages([
("system", SystemPrompt),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
])
# Create the memory
memory = ConversationBufferMemory(
memory_key="chat_history",
max_len=4,
tools=tools,
return_messages=True,
k=1
)
# Create the agent
llm = ChatOpenAI() # Assuming you have defined your LLM
agent = create_openai_tools_agent(llm, tools, prompt)
# Create the agent executor with the ExceptionTool as the default tool
agent_executor = AgentExecutor(agent=agent, tools=tools + [ExceptionTool()], verbose=True, memory=memory) In this setup, the |
Beta Was this translation helpful? Give feedback.
-
did you find a solution for this ? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Langchain agent donot choose the tool every time I want to set a default tool for langchain agent such that even if it do not invoke any tool it should take that tool by default. Is there any way I can get it , by any way i mean any other agent, changes in original code or any thing i want it to be done
@DosuBot
System Info
Name: langchain-core
Version: 0.1.45
Beta Was this translation helpful? Give feedback.
All reactions