agentscope.agents.rag_agent module

This example shows how to build an agent with RAG with LlamaIndex.

Notice, this is a Beta version of RAG agent.

class agentscope.agents.rag_agent.LlamaIndexAgent(*args: tuple, **kwargs: dict)[source]

Bases: AgentBase

A LlamaIndex agent build on LlamaIndex.

__init__(name: str, sys_prompt: str, model_config_name: str, knowledge_list: list[Knowledge] | None = None, knowledge_id_list: list[str] | None = None, similarity_top_k: int | None = None, log_retrieval: bool = True, recent_n_mem_for_retrieve: int = 1, **kwargs: Any) None[source]

Initialize the RAG LlamaIndexAgent :param name: the name for the agent :type name: str :param sys_prompt: system prompt for the RAG agent :type sys_prompt: str :param model_config_name: language model for the agent :type model_config_name: str :param knowledge_list: a list of knowledge.

User can choose to pass a list knowledge object directly when initializing the RAG agent. Another choice can be passing a list of knowledge ids and obtain the knowledge with the equip function of a knowledge bank.

Parameters:
  • knowledge_id_list (list[Knowledge]) – a list of id of the knowledge. This is designed for easy setting up multiple RAG agents with a config file. To obtain the knowledge objects, users can pass this agent to the equip function in a knowledge bank to add corresponding knowledge to agent’s self.knowledge_list.

  • similarity_top_k (int) – the number of most similar data blocks retrieved from each of the knowledge

  • log_retrieval (bool) – whether to print the retrieved content

  • recent_n_mem_for_retrieve (int) – the number of pieces of memory used as part of retrival query

reply(x: Sequence[Msg] | Msg | None = None) Msg[source]

Reply function of the RAG agent. Processes the input data, 1) use the input data to retrieve with RAG function; 2) generates a prompt using the current memory and system prompt; 3) invokes the language model to produce a response. The response is then formatted and added to the dialogue memory.

Parameters:

x (Optional[Union[Msg, Sequence[Msg]]], defaults to None) – The input message(s) to the agent, which also can be omitted if the agent doesn’t need any input.

Returns:

The output message generated by the agent.

Return type:

Msg

to_dist(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_expire_time: int = 7200, max_timeout_seconds: int = 5, local_mode: bool = True, retry_strategy: ~agentscope.rpc.retry_strategy.RetryBase = <agentscope.rpc.retry_strategy.RetryFixedTimes object>) Any

Convert current object into its distributed version.

Parameters:
  • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

  • port (int, defaults to None) – Port of the rpc agent server.

  • max_pool_size (int, defaults to 8192) – Only takes effect when host and port are not filled in. The max number of agent reply messages that the started agent server can accommodate. Note that the oldest message will be deleted after exceeding the pool size.

  • max_expire_time (int, defaults to 7200) – Only takes effect when host and port are not filled in. Maximum time for reply messages to be cached in the launched agent server. Note that expired messages will be deleted.

  • max_timeout_seconds (int, defaults to 5) – Max timeout seconds for the rpc call.

  • local_mode (bool, defaults to True) – Only takes effect when host and port are not filled in. Whether the started agent server only listens to local requests.

  • retry_strategy (RetryBase, defaults to _DEAFULT_RETRY_STRATEGY) – The retry strategy for the async rpc call.

Returns:

the wrapped agent instance with distributed functionality

Return type:

RpcObject