agentscope.parsers.tagged_content_parser module

The parser for tagged content in the model response.

class 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)[source]

Bases: 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.

parse(response: ModelResponse) ModelResponse[source]

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.

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.

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.

class TaggedContent(name: str, tag_begin: str, content_hint: str, tag_end: str, parse_json: bool = False)[source]

Bases: object

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

content_hint: str

The hint of the content.

name: str

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

parse_json: bool

Whether to parse the content as a json object.

tag_begin: str

The beginning tag.

tag_end: str

The ending tag.