agentscope.agents.dict_dialog_agent module

An agent that replies in a dictionary format.

class agentscope.agents.dict_dialog_agent.DictDialogAgent(*args: tuple, **kwargs: dict)[源代码]

基类:AgentBase

An agent that generates response in a dict format, where user can specify the required fields in the response via specifying the parser

About parser, please refer to our [tutorial](https://modelscope.github.io/agentscope/en/tutorial/203-parser.html)

For usage example, please refer to the example of werewolf in examples/game_werewolf

__init__(name: str, sys_prompt: str, model_config_name: str, use_memory: bool = True, max_retries: int | None = 3) None[源代码]

Initialize the dict dialog agent.

参数:
  • name (str) – The name of the agent.

  • sys_prompt (Optional[str], defaults to None) – The system prompt of the agent, which can be passed by args or hard-coded in the agent.

  • model_config_name (str, defaults to None) – The name of the model config, which is used to load model from configuration.

  • use_memory (bool, defaults to True) – Whether the agent has memory.

  • max_retries (Optional[int], defaults to None) – The maximum number of retries when failed to parse the model output.

set_parser(parser: ParserBase) None[源代码]

Set response parser, which will provide 1) format instruction; 2) response parsing; 3) filtering fields when returning message, storing message in memory. So developers only need to change the parser, and the agent will work as expected.

reply(x: Sequence[Msg] | Msg | None = None) Msg[源代码]

Reply function of the agent. Processes the input data, generates a prompt using the current dialogue memory and system prompt, and invokes the language model to produce a response. The response is then formatted and added to the dialogue memory.

参数:

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.

返回:

The output message generated by the agent.

返回类型:

Msg

抛出:

json.decoder.JSONDecodeError – If the response from the language model is not valid JSON, it defaults to treating the response as plain text.