agentscope.parsers.json_object_parser module

The parser for JSON object in the model response.

class agentscope.parsers.json_object_parser.MarkdownJsonObjectParser(content_hint: Any | None = None)[源代码]

基类:ParserBase

A parser to parse the response text to a json object.

name: str = 'json block'

The name of the parser.

tag_begin: str = '```json'

Opening tag for a code block.

tag_end: str = '```'

Closing end for a code block.

__init__(content_hint: Any | None = None) None[源代码]

Initialize the parser with the content hint.

参数:

content_hint (Optional[Any], defaults to None) – The hint used to remind LLM what should be fill between the tags. If it is a string, it will be used as the content hint directly. If it is a dict, it will be converted to a json string and used as the content hint.

content_hint: str = '{your_json_object}'

The hint of the content.

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

Parse the response text to a json object, and fill it in the parsed field in the response object.

property format_instruction: str

Get the format instruction for the json object, if the format_example is provided, it will be used as the example.

class agentscope.parsers.json_object_parser.MarkdownJsonDictParser(content_hint: Any | None = None, required_keys: List[str] | None = None, keys_to_memory: str | bool | Sequence[str] = True, keys_to_content: str | bool | Sequence[str] = True, keys_to_metadata: str | bool | Sequence[str] = False)[源代码]

基类:MarkdownJsonObjectParser, DictFilterMixin

A class used to parse a JSON dictionary object in a markdown fenced code

name: str = 'json block'

The name of the parser.

tag_begin: str = '```json'

Opening tag for a code block.

tag_end: str = '```'

Closing end for a code block.

__init__(content_hint: Any | None = None, required_keys: List[str] | None = None, keys_to_memory: str | bool | Sequence[str] = True, keys_to_content: str | bool | Sequence[str] = True, keys_to_metadata: str | bool | Sequence[str] = False) None[源代码]

Initialize the parser with the content hint.

参数:
  • content_hint (Optional[Any], defaults to None) – The hint used to remind LLM what should be fill between the tags. If it is a string, it will be used as the content hint directly. If it is a dict, it will be converted to a json string and used as the content hint. If it’s a Pydantic model, the schema will be displayed in the instruction.

  • required_keys (List[str], defaults to []) – A list of required keys in the JSON dictionary object. If the response misses any of the required keys, it will raise a RequiredFieldNotFoundError.

  • (`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

content_hint: str = '{your_json_dictionary}'

The hint of the content.

required_keys: List[str]

A list of required keys in the JSON dictionary object. If the response misses any of the required keys, it will raise a RequiredFieldNotFoundError.

property format_instruction: str

Get the format instruction for the json object, if the format_example is provided, it will be used as the example.

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

Parse the text field of the response to a JSON dictionary object, store it in the parsed field of the response object, and check if the required keys exists.