mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-18 10:43:36 +00:00
Add tags in agent initialization (#6559)
Add better docstrings for agent executor as well Inspo: https://github.com/hwchase17/langchainjs/pull/1722 
This commit is contained in:
parent
6e69bfbb28
commit
9c09861946
@ -621,14 +621,44 @@ class AgentExecutor(Chain):
|
|||||||
"""Consists of an agent using tools."""
|
"""Consists of an agent using tools."""
|
||||||
|
|
||||||
agent: Union[BaseSingleActionAgent, BaseMultiActionAgent]
|
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]
|
tools: Sequence[BaseTool]
|
||||||
|
"""The valid tools the agent can call."""
|
||||||
return_intermediate_steps: bool = False
|
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
|
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
|
max_execution_time: Optional[float] = None
|
||||||
|
"""The maximum amount of wall clock time to spend in the execution
|
||||||
|
loop.
|
||||||
|
"""
|
||||||
early_stopping_method: str = "force"
|
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[
|
handle_parsing_errors: Union[
|
||||||
bool, str, Callable[[OutputParserException], str]
|
bool, str, Callable[[OutputParserException], str]
|
||||||
] = False
|
] = 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
|
@classmethod
|
||||||
def from_agent_and_tools(
|
def from_agent_and_tools(
|
||||||
|
@ -16,6 +16,8 @@ def initialize_agent(
|
|||||||
callback_manager: Optional[BaseCallbackManager] = None,
|
callback_manager: Optional[BaseCallbackManager] = None,
|
||||||
agent_path: Optional[str] = None,
|
agent_path: Optional[str] = None,
|
||||||
agent_kwargs: Optional[dict] = None,
|
agent_kwargs: Optional[dict] = None,
|
||||||
|
*,
|
||||||
|
tags: Optional[Sequence[str]] = None,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> AgentExecutor:
|
) -> AgentExecutor:
|
||||||
"""Load an agent executor given tools and LLM.
|
"""Load an agent executor given tools and LLM.
|
||||||
@ -29,11 +31,13 @@ def initialize_agent(
|
|||||||
not provided. Defaults to None.
|
not provided. Defaults to None.
|
||||||
agent_path: Path to serialized agent to use.
|
agent_path: Path to serialized agent to use.
|
||||||
agent_kwargs: Additional key word arguments to pass to the underlying agent
|
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
|
**kwargs: Additional key word arguments passed to the agent executor
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
An agent executor
|
An agent executor
|
||||||
"""
|
"""
|
||||||
|
tags_ = list(tags) if tags else []
|
||||||
if agent is None and agent_path is None:
|
if agent is None and agent_path is None:
|
||||||
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
||||||
if agent is not None and agent_path is not None:
|
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"Got unknown agent type: {agent}. "
|
||||||
f"Valid types are: {AGENT_TO_CLASS.keys()}."
|
f"Valid types are: {AGENT_TO_CLASS.keys()}."
|
||||||
)
|
)
|
||||||
|
tags_.append(agent.value)
|
||||||
agent_cls = AGENT_TO_CLASS[agent]
|
agent_cls = AGENT_TO_CLASS[agent]
|
||||||
agent_kwargs = agent_kwargs or {}
|
agent_kwargs = agent_kwargs or {}
|
||||||
agent_obj = agent_cls.from_llm_and_tools(
|
agent_obj = agent_cls.from_llm_and_tools(
|
||||||
@ -56,6 +61,11 @@ def initialize_agent(
|
|||||||
agent_obj = load_agent(
|
agent_obj = load_agent(
|
||||||
agent_path, llm=llm, tools=tools, callback_manager=callback_manager
|
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:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Somehow both `agent` and `agent_path` are None, "
|
"Somehow both `agent` and `agent_path` are None, "
|
||||||
@ -65,5 +75,6 @@ def initialize_agent(
|
|||||||
agent=agent_obj,
|
agent=agent_obj,
|
||||||
tools=tools,
|
tools=tools,
|
||||||
callback_manager=callback_manager,
|
callback_manager=callback_manager,
|
||||||
|
tags=tags_,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user