Skip to content

The agent engineering platform.

Add tags in agent initialization (#6559)

Zander Chase9c0986194633
diff --git a/langchain/agents/agent.py b/langchain/agents/agent.py
index 0056d10fbc..1f26ff2920 100644
--- a/langchain/agents/agent.py
+++ b/langchain/agents/agent.py
@@ -621,14 +621,44 @@ class AgentExecutor(Chain):
"""Consists of an agent using tools."""
agent: Union[BaseSingleActionAgent, BaseMultiActionAgent]
+ """The agent to run for creating a plan and determining actions
+ to take at each step of the execution loop."""
tools: Sequence[BaseTool]
+ """The valid tools the agent can call."""
return_intermediate_steps: bool = False
+ """Whether to return the agent's trajectory of intermediate steps
+ at the end in addition to the final output."""
max_iterations: Optional[int] = 15
+ """The maximum number of steps to take before ending the execution
+ loop.
+
+ Setting to 'None' could lead to an infinite loop."""
max_execution_time: Optional[float] = None
+ """The maximum amount of wall clock time to spend in the execution
+ loop.
+ """
early_stopping_method: str = "force"
+ """The method to use for early stopping if the agent never
+ returns `AgentFinish`. Either 'force' or 'generate'.
+
+ `"force"` returns a string saying that it stopped because it met a
+ time or iteration limit.
+
+ `"generate"` calls the agent's LLM Chain one final time to generate
+ a final answer based on the previous steps.
+ """
handle_parsing_errors: Union[
bool, str, Callable[[OutputParserException], str]
] = False
+ """How to handle errors raised by the agent's output parser.
+ Defaults to `False`, which raises the error.
+s
+ If `true`, the error will be sent back to the LLM as an observation.
+ If a string, the string itself will be sent to the LLM as an observation.
+ If a callable function, the function will be called with the exception
+ as an argument, and the result of that function will be passed to the agent
+ as an observation.
+ """
@classmethod
def from_agent_and_tools(
diff --git a/langchain/agents/initialize.py b/langchain/agents/initialize.py
index 9a52b15196..cb26fb630a 100644
--- a/langchain/agents/initialize.py
+++ b/langchain/agents/initialize.py
@@ -16,6 +16,8 @@ def initialize_agent(
callback_manager: Optional[BaseCallbackManager] = None,
agent_path: Optional[str] = None,
agent_kwargs: Optional[dict] = None,
+ *,
+ tags: Optional[Sequence[str]] = None,
**kwargs: Any,
) -> AgentExecutor:
"""Load an agent executor given tools and LLM.
@@ -29,11 +31,13 @@ def initialize_agent(
not provided. Defaults to None.
agent_path: Path to serialized agent to use.
agent_kwargs: Additional key word arguments to pass to the underlying agent
+ tags: Tags to apply to the traced runs.
**kwargs: Additional key word arguments passed to the agent executor
Returns:
An agent executor
"""
+ tags_ = list(tags) if tags else []
if agent is None and agent_path is None:
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
if agent is not None and agent_path is not None:
@@ -47,6 +51,7 @@ def initialize_agent(
f"Got unknown agent type: {agent}. "
f"Valid types are: {AGENT_TO_CLASS.keys()}."
)
+ tags_.append(agent.value)
agent_cls = AGENT_TO_CLASS[agent]
agent_kwargs = agent_kwargs or {}
agent_obj = agent_cls.from_llm_and_tools(
@@ -56,6 +61,11 @@ def initialize_agent(
agent_obj = load_agent(
agent_path, llm=llm, tools=tools, callback_manager=callback_manager
)
+ try:
+ # TODO: Add tags from the serialized object directly.
+ tags_.append(agent_obj._agent_type)
+ except NotImplementedError:
+ pass
else:
raise ValueError(
"Somehow both `agent` and `agent_path` are None, "
@@ -65,5 +75,6 @@ def initialize_agent(
agent=agent_obj,
tools=tools,
callback_manager=callback_manager,
+ tags=tags_,
**kwargs,
)