agentscope.parsers.tagged_content_parser module

The parser for tagged content in the model response.

class agentscope.parsers.tagged_content_parser.TaggedContent(name: str, tag_begin: str, content_hint: str, tag_end: str, parse_json: bool = False)[源代码]

基类:object

A tagged content object to store the tag name, tag begin, content hint and tag end.

__init__(name: str, tag_begin: str, content_hint: str, tag_end: str, parse_json: bool = False) None[源代码]

Initialize the tagged content object.

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

  • tag_begin (str) – The beginning tag.

  • content_hint (str) – The hint of the content.

  • tag_end (str) – The ending tag.

  • parse_json (bool, defaults to False) – Whether to parse the content as a json object.

name: str

The name of the tagged content, which will be used as the key in extracted dictionary.

tag_begin: str

The beginning tag.

content_hint: str

The hint of the content.

tag_end: str

The ending tag.

parse_json: bool

Whether to parse the content as a json object.

class agentscope.parsers.tagged_content_parser.MultiTaggedContentParser(*tagged_contents: TaggedContent, keys_to_memory: str | bool | Sequence[str] | None = True, keys_to_content: str | bool | Sequence[str] | None = True, keys_to_metadata: str | bool | Sequence[str] | None = False, keys_allow_missing: List[str] | None = None)[源代码]

基类:ParserBase, DictFilterMixin

Parse response text by multiple tags, and return a dict of their content. Asking llm to generate JSON dictionary object directly maybe not a good idea due to involving escape characters and other issues. So we can ask llm to generate text with tags, and then parse the text to get the final JSON dictionary object.

json_required_hint = ', and the content between {} MUST be a JSON object:'

If a tagged content is required to be a JSON object by parse_json equals to True, this instruction will be used to remind the model to generate JSON object.

__init__(*tagged_contents: TaggedContent, keys_to_memory: str | bool | Sequence[str] | None = True, keys_to_content: str | bool | Sequence[str] | None = True, keys_to_metadata: str | bool | Sequence[str] | None = False, keys_allow_missing: List[str] | None = None) None[源代码]

Initialize the parser with tags.

参数:
  • *tagged_contents (dict[str, Tuple[str, str]]) – Multiple TaggedContent objects, each object contains the tag name, tag begin, content hint and tag end. The name will be used as the key in the extracted dictionary.

  • required_keys (Optional[List[str]], defaults to None) – A list of required

  • (`Optional[Union[str (keys_to_memory) –

  • bool

  • Sequence[str]]]`

:param : :param defaults to True): The key or keys to be filtered in to_memory method. If

it’s - False, None will be returned in the to_memory method - str, the corresponding value will be returned - List[str], a filtered dictionary will be returned - True, the whole dictionary will be returned

参数:
  • (`Optional[Union[str (keys_to_content) –

  • bool

  • Sequence[str]]`

:param : :param defaults to True): The key or keys to be filtered in to_content method. If

it’s - False, None will be returned in the to_content method - str, the corresponding value will be returned - List[str], a filtered dictionary will be returned - True, the whole dictionary will be returned

参数:
  • (`Optional[Union[str (keys_to_metadata) –

  • bool

  • Sequence[str]]]`

:param : :param defaults to False): The key or keys to be filtered in to_metadata method. If

it’s - False, None will be returned in the to_metadata method - str, the corresponding value will be returned - List[str], a filtered dictionary will be returned - True, the whole dictionary will be returned

参数:

keys_allow_missing (Optional[List[str]], defaults to None) – A list of keys that are allowed to be missing in the response.

format_instruction = 'Respond with specific tags as outlined below{json_required_hint}\n{tag_lines_format}'

The instruction for the format of the tagged content.

parse(response: ModelResponse) ModelResponse[源代码]

Parse the response text by tags, and return a dict of their content in the parsed field of the model response object. If the tagged content requires to parse as a JSON object by parse_json equals to True, it will be parsed as a JSON object by json.loads.