configurable agent tool at runtime #17555
Replies: 5 comments 5 replies
-
🤖 Hello again, @rere950303! It's great to see you diving into another interesting challenge with LangChain. How's everything going? To configure the arguments used by the agent tool in LangChain at runtime, you can modify the Here is the relevant code snippet: @classmethod
def create_prompt(
cls,
tools: Sequence[BaseTool],
prefix: str = PREFIX,
suffix: str = SUFFIX,
human_message_template: str = HUMAN_MESSAGE_TEMPLATE,
format_instructions: str = FORMAT_INSTRUCTIONS,
input_variables: Optional[List[str]] = None,
memory_prompts: Optional[List[BasePromptTemplate]] = None,
) -> BasePromptTemplate:
tool_strings = []
for tool in tools:
args_schema = re.sub("}", "}}", re.sub("{", "{{", str(tool.args)))
tool_strings.append(f"{tool.name}: {tool.description}, args: {args_schema}")
... In this method, The As for the As for configuring arguments at runtime, the LangChain framework provides several utilities for this purpose. The I hope this helps! If you have any more questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
-
any updates on how to use ConfigurableField with agent? |
Beta Was this translation helpful? Give feedback.
-
Hi all, To configure a tool behavior at runtime, you can create your own version of a from langchain_core.runnables import ConfigurableField, RunnableSerializable
from langchain_core.runnables.config import RunnableConfig
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain.prompts import ChatPromptTemplate
class MyCustomRunnable(RunnableSerializable[list[str], str]):
name: str
def invoke(self, input: list[str], config: RunnableConfig | None = None) -> str:
#Do your tool computing using input[0] to get the query parameter and self.name the runtime configured field
return f"{self.name} - {input[0]}"
@tool
def my_tool(
query: str
):
"""
Tool description
"""
return (
MyCustomRunnable()
.configurable_fields(
name=ConfigurableField(
id="name",
name="name",
description="Name configured at runtime.",
)
)
.invoke(input=[query])
)
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant.",
),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
]
)
# Construct the Tools agent
agent = create_tool_calling_agent(llm, [my_tool], prompt)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "what is LangChain?", "config": {"configurable" : {"name": "MY RUNTIME FIELD"}}}) If you are using async def per_req_config_modifier(config: Dict, request: Request) -> Dict:
"""Modify the config for each request."""
req = await request.json()
if "name" in req["config"]["configurable"]:
config["configurable"]["name] = req["config"]["configurable"]["name"]
return config
add_routes(
app,
agent_executor,
path="/chat",
per_req_config_modifier=per_req_config_modifier,
) Let me know if you have any questions ! |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
-
Hi! Any updates on this? I believe that the "None" you are recieving is because the agent_executer doesn´t propagate the config to the tools, therefore it uses the default name: str = None defined in your class The work around I found is to use a wrapper on the agent executor, that do propagate the tools: `
` One extra problem I found is that using with_config on the tools change their type from StructuredTool to RunnableBinding, which doesnt have _run implemented. Therefore the agent executor fails the validation of the tools. The work around for this is to define the funcion with_config on the tool definition: ` class EmptyInput(BaseModel): class CustomSearchTool(BaseTool):
` However, It would be nice that tools natively accepted configurations without changing their type to runnable, and that agentExecutor propagated configuration to the tools. Or maybe I am missing something |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
pass
Description
How can I make the tool used by the agent to configure the argument in runtime as in lcel?
System Info
pass
Beta Was this translation helpful? Give feedback.
All reactions