BaseAgent¶
Base Agent Class¶
The foundation class for all agents in AgentFly:
BaseAgent(model_name_or_path, template: str = None, system_prompt: str = None, tools: List = [], max_model_len: int = None, backend_config: Optional[Dict[str, Any]] = None, reward_fn: Callable = None, streaming: str = 'console', debug: bool = False, monitors: List[str] = ['wandb'], wandb_project_name: str = None, wandb_run_name: str = None, local_cache_dir: str = None, tool_parser: Optional[Any] = None, tool_parser_name: Optional[str] = None, **kwargs)
¶
Bases: ChainRollout, ABC
Base class for all agents. All agent should subclass this class. A customized agent can implement the following methods:
-
generate_async: generate responses asynchronously.
-
parse: parse the tool call from the generated response.
template: The template to use for the agent.
system_prompt: The system prompt to use for the agent.
tools: The tools to use for the agent.
debug: Whether to enable debug mode.
backend_config: Dict specifying the backend and its parameters. Must include "backend" (e.g. "async_vllm", "client").
Other keys are passed as kwargs to that backend (e.g. "gpu_memory_utilization" for async_vllm).
Defaults to {"backend": "async_vllm"}.
tool_parser: Optional tool parser instance from vLLM. If provided, will be used for parsing tool calls.
tool_parser_name: Optional name of the tool parser to use (e.g., "hermes", "pythonic"). If provided and tool_parser is None, will create a parser using this name.
Functions¶
run(messages: Union[List[dict], np.ndarray, Dict], max_turns: int, generation_config: Optional[Dict[str, Any]] = {}, context_config: Optional[ContextConfig] = None, **kwargs) -> RunResult
async
¶
Run the agent on a batch of messages and return the rollout result.
This is the main interface for running the agent. It is a wrapper of different rollout methods, which must be asynchronous. Currently we only support chain-based rollout.
Parameters:
-
messages(Union[List[dict], ndarray, Dict]) –List of messages to generate responses for.
-
max_turns(int) –The maximum number of turns to generate.
-
generation_config(Optional[Dict[str, Any]], default:{}) –The generation configuration.
-
context_config(Optional[ContextConfig], default:None) –Optional settings for :class:
~agentfly.core.context.Context(resource backend). -
**kwargs–Additional keyword arguments for generation (passed to
run_async).
Returns:
-
RunResult–class:
~agentfly.agents.types.RunResultcontaining the trajectories -
RunResult–produced by this run. The same result is also cached on the agent so
-
that(RunResult) –meth:
get_verl_data_protocan be called afterwards without -
RunResult–re-running.
generate_async(messages_list_or_inputs: List[List[Dict]], **kwargs)
async
¶
Generate responses asynchronously. This method is used to generate responses for a list of messages. In a customized agent, this method can be overridden to implement more complex generation logic. For example, retrieve some relevant context from the database.
Parameters:
-
messages_list_or_inputs(List[List[Dict]]) –List of messages to generate responses for.
-
**args–Additional arguments for generation.
Returns:
-
–
List of responses.
generate_streaming(messages_list_or_inputs: List[List[Dict]], **kwargs)
async
¶
Generate responses with streaming support. This method yields response chunks as they are generated.
Parameters:
-
messages_list_or_inputs(List[List[Dict]]) –List of messages to generate responses for.
-
**args–Additional arguments for generation.
Yields:
-
str–Response chunks as they are generated.
postprocess_trajectories(trajectories: List[Trajectory]) -> List[Trajectory]
¶
Hook for subclasses to post-process trajectories before they're
wrapped in a :class:RunResult. Default implementation is identity.
extract_final_response(messages: List[Dict[str, Any]]) -> str
¶
Extract the final response text from a trajectory.
We scan messages in reverse order and take the last assistant/tool message as the final response.
parse(responses: List[str], context: Optional[Context] = None, **kwargs) -> List[Dict]
¶
This method is used to define the interaction logic of the agent. It can be used to parse the tool call from the response. If tool_parser is provided, it will use the vLLM tool parser by default. Otherwise, subclasses should override this method.
Parameters:
-
responses(List[str]) –List of responses to parse.
-
context(Optional[Context], default:None) –Optional rollout context carrying trajectory and metadata.
-
**args–Additional arguments for parsing.
Returns:
-
messages(List[Dict]) –Assistant messages in the following format: