agent

Module Contents

Classes

Agent

Agent is the basic unit of the system. It is responsible for

AsyncAgentMixin

AsyncAgent

Asynchronous variant of the Agent class

StreamingAgentMixin

Component that makes agent calling output a streaming response.

AsyncStreamingAgentMixin

Component that makes asynchronous agent calling output a streaming response.

StreamingAgent

Streaming variant of the Agent class

AsyncStreamingAgent

Streaming variant of the AsyncAgent class

Sequential

Sequential is an agent container that forwards messages to each agent

AsyncSequential

Sequential is an agent container that forwards messages to each agent

StreamingSequential

Streaming variant of the Sequential class

AsyncStreamingSequential

Streaming variant of the AsyncSequential class

AgentContainerMixin

AgentList

Agent is the basic unit of the system. It is responsible for

AgentDict

Agent is the basic unit of the system. It is responsible for

class lagent.agents.agent.Agent(llm=None, template=None, memory=dict(type=Memory), output_format=None, aggregator=dict(type=DefaultAggregator), name=None, description=None, hooks=None)

Agent is the basic unit of the system. It is responsible for communicating with the LLM, managing the memory, and handling the message aggregation and parsing. It can also be extended with hooks

参数:
  • llm (Union[BaseLLM, Dict]) – The language model used by the agent.

  • template (Union[PromptTemplate, str]) – The template used to format the messages.

  • memory (Dict) – The memory used by the agent.

  • output_format (Dict) – The output format used by the agent.

  • aggregator (Dict) – The aggregator used by the agent.

  • name (Optional[str]) – The name of the agent.

  • description (Optional[str]) – The description of the agent.

  • hooks (Optional[Union[List[Dict], Dict]]) – The hooks used by the agent.

返回:

The response message.

返回类型:

AgentMessage

name = 'Agent'
llm: lagent.llms.BaseLLM = None
memory: lagent.memory.MemoryManager
output_format: lagent.prompts.parsers.StrParser = None
template = None
description = None
aggregator: lagent.agents.aggregator.DefaultAggregator
update_memory(message, session_id=0)
forward(*message, session_id=0, **kwargs)
参数:

message (lagent.schema.AgentMessage) –

返回类型:

Union[lagent.schema.AgentMessage, str]

state_dict(session_id=None, prefix='', destination=None)
返回类型:

Dict

load_state_dict(state_dict, session_id=0)
参数:

state_dict (Dict) –

register_hook(hook)
参数:

hook (Callable) –

reset(session_id=0, keypath=None, recursive=False)
参数:
  • keypath (Optional[str]) –

  • recursive (bool) –

get_messages(session_id=0, keypath=None)

Get OpenAI format messages from memory.

参数:
  • session_id (int) – The session id of the memory.

  • keypath (Optional[str]) – The keypath of the sub-agent to get messages from. Default is None.

返回:

The messages from the memory including the sub-agent’s system prompt.

返回类型:

List[dict]

class lagent.agents.agent.AsyncAgentMixin
async forward(*message, session_id=0, **kwargs)
参数:

message (lagent.schema.AgentMessage) –

返回类型:

Union[lagent.schema.AgentMessage, str]

class lagent.agents.agent.AsyncAgent(llm=None, template=None, memory=dict(type=Memory), output_format=None, aggregator=dict(type=DefaultAggregator), name=None, description=None, hooks=None)

Bases: AsyncAgentMixin, Agent

Asynchronous variant of the Agent class

参数:
  • llm (Union[lagent.llms.BaseLLM, Dict]) –

  • template (Union[lagent.prompts.prompt_template.PromptTemplate, str, dict, List[dict]]) –

  • memory (Dict) –

  • output_format (Optional[Dict]) –

  • aggregator (Dict) –

  • name (Optional[str]) –

  • description (Optional[str]) –

  • hooks (Optional[Union[List[Dict], Dict]]) –

class lagent.agents.agent.StreamingAgentMixin

Component that makes agent calling output a streaming response.

forward(*message, session_id=0, **kwargs)
参数:

message (lagent.schema.AgentMessage) –

返回类型:

Generator[Union[lagent.schema.AgentMessage, Tuple[lagent.schema.ModelStatusCode, str]], None, None]

class lagent.agents.agent.AsyncStreamingAgentMixin

Component that makes asynchronous agent calling output a streaming response.

async forward(*message, session_id=0, **kwargs)
参数:

message (lagent.schema.AgentMessage) –

返回类型:

AsyncGenerator[Union[lagent.schema.AgentMessage, Tuple[lagent.schema.ModelStatusCode, str]], None]

class lagent.agents.agent.StreamingAgent(llm=None, template=None, memory=dict(type=Memory), output_format=None, aggregator=dict(type=DefaultAggregator), name=None, description=None, hooks=None)

Bases: StreamingAgentMixin, Agent

Streaming variant of the Agent class

参数:
  • llm (Union[lagent.llms.BaseLLM, Dict]) –

  • template (Union[lagent.prompts.prompt_template.PromptTemplate, str, dict, List[dict]]) –

  • memory (Dict) –

  • output_format (Optional[Dict]) –

  • aggregator (Dict) –

  • name (Optional[str]) –

  • description (Optional[str]) –

  • hooks (Optional[Union[List[Dict], Dict]]) –

class lagent.agents.agent.AsyncStreamingAgent(llm=None, template=None, memory=dict(type=Memory), output_format=None, aggregator=dict(type=DefaultAggregator), name=None, description=None, hooks=None)

Bases: AsyncStreamingAgentMixin, Agent

Streaming variant of the AsyncAgent class

参数:
  • llm (Union[lagent.llms.BaseLLM, Dict]) –

  • template (Union[lagent.prompts.prompt_template.PromptTemplate, str, dict, List[dict]]) –

  • memory (Dict) –

  • output_format (Optional[Dict]) –

  • aggregator (Dict) –

  • name (Optional[str]) –

  • description (Optional[str]) –

  • hooks (Optional[Union[List[Dict], Dict]]) –

class lagent.agents.agent.Sequential(*agents, **kwargs)

Bases: Agent

Sequential is an agent container that forwards messages to each agent in the order they are added.

参数:

agents (Union[Agent, Iterable]) –

add_agent(name, agent)
参数:
  • name (str) –

  • agent (Agent) –

forward(*message, session_id=0, exit_at=None, **kwargs)
参数:
返回类型:

lagent.schema.AgentMessage

class lagent.agents.agent.AsyncSequential(*agents, **kwargs)

Bases: AsyncAgentMixin, Sequential

Sequential is an agent container that forwards messages to each agent in the order they are added.

参数:

agents (Union[Agent, Iterable]) –

async forward(*message, session_id=0, exit_at=None, **kwargs)
参数:
返回类型:

lagent.schema.AgentMessage

class lagent.agents.agent.StreamingSequential(*agents, **kwargs)

Bases: StreamingAgentMixin, Sequential

Streaming variant of the Sequential class

参数:

agents (Union[Agent, Iterable]) –

forward(*message, session_id=0, exit_at=None, **kwargs)
参数:
class lagent.agents.agent.AsyncStreamingSequential(*agents, **kwargs)

Bases: AsyncStreamingAgentMixin, Sequential

Streaming variant of the AsyncSequential class

参数:

agents (Union[Agent, Iterable]) –

async forward(*message, session_id=0, exit_at=None, **kwargs)
参数:
class lagent.agents.agent.AgentContainerMixin
class lagent.agents.agent.AgentList(agents=None)

Bases: Agent, collections.UserList, AgentContainerMixin

Agent is the basic unit of the system. It is responsible for communicating with the LLM, managing the memory, and handling the message aggregation and parsing. It can also be extended with hooks

参数:
  • llm (Union[BaseLLM, Dict]) – The language model used by the agent.

  • template (Union[PromptTemplate, str]) – The template used to format the messages.

  • memory (Dict) – The memory used by the agent.

  • output_format (Dict) – The output format used by the agent.

  • aggregator (Dict) – The aggregator used by the agent.

  • name (Optional[str]) – The name of the agent.

  • description (Optional[str]) – The description of the agent.

  • hooks (Optional[Union[List[Dict], Dict]]) – The hooks used by the agent.

  • agents (Optional[Iterable[Agent]]) –

返回:

The response message.

返回类型:

AgentMessage

name = None
class lagent.agents.agent.AgentDict(agents=None)

Bases: Agent, collections.UserDict, AgentContainerMixin

Agent is the basic unit of the system. It is responsible for communicating with the LLM, managing the memory, and handling the message aggregation and parsing. It can also be extended with hooks

参数:
  • llm (Union[BaseLLM, Dict]) – The language model used by the agent.

  • template (Union[PromptTemplate, str]) – The template used to format the messages.

  • memory (Dict) – The memory used by the agent.

  • output_format (Dict) – The output format used by the agent.

  • aggregator (Dict) – The aggregator used by the agent.

  • name (Optional[str]) – The name of the agent.

  • description (Optional[str]) – The description of the agent.

  • hooks (Optional[Union[List[Dict], Dict]]) – The hooks used by the agent.

  • agents (Optional[Mapping[str, Agent]]) –

返回:

The response message.

返回类型:

AgentMessage

name = None