ahvn.utils.basic.parser_utils module¶
- ahvn.utils.basic.parser_utils.parse_keys(response, keys=None, mode='dict')[源代码]¶
Parse keys from an LLM response based on the provided mode. The LLM response is expected to be formatted as "<key>: <value>" pairs.
- 参数:
response (str) -- The LLM response containing key-value pairs.
keys (list, optional) -- A list of keys to parse from the response. If None, all keys will be parsed.
mode (Literal['list', 'dict'], optional) -- The mode of parsing. 'list' returns a list of key-value pairs, while 'dict' returns a dictionary with keys and their corresponding values.
- 返回:
Parsed key-value pairs in the specified mode.
- 返回类型:
示例
>>> parse_keys("name: John Doe\nage: 30", keys=["name", "age", "height"], mode="list") [{'key': 'name', 'value': 'John Doe'}, {'key': 'age', 'value': '30'}] >>> parse_keys("name: John Doe\nage: 30", keys=["name", "age", "height"], mode="dict") {'name': 'John Doe', 'age': '30', 'height': None}
- ahvn.utils.basic.parser_utils.parse_md(response, recurse=False, mode='dict')[源代码]¶
Parses a markdown-like string into structured blocks.
This function extracts blocks from the input string that are either:
XML-like tags (e.g., <tag>...</tag>)
Fenced code blocks (e.g.,
`python ... `,``sql ... ``), languages are optional and case-sensitive. Supports variable-length backtick fences (3+ backticks). Missing language defaults to "markdown".Plain text between blocks
This parser is streaming-compatible: incomplete/unfinished input will never raise errors and will produce the best possible parse result given the available data.
- 参数:
response (str) -- The input string to parse.
recurse (bool, optional) -- If True, recursively parses nested blocks. Defaults to False.
mode (Literal["list", "dict"], optional) --
"list": Returns a list of blocks, each as a dict with 'key' and 'value'.
"dict": Returns a flattened dictionary with dot-separated keys for nested blocks. Notice that duplicate keys will be overwritten.
Defaults to "dict".
- 返回:
The parsed structure, as a list or dict depending on
mode.- 返回类型:
示例
>>> parse_md("<think>Hello!</think>\nSome textual output.\n```sql\nSELECT *\nFROM table;\n```\n<rating>\n```json\n{\"rating\": 5}\n```</rating>") {'think': 'Hello!', 'text': 'Some textual output.', 'sql': 'SELECT *\nFROM table;', 'rating': '```json\n{"rating": 5}\n```'}
>>> parse_md("<think>Hello!</think>\nSome textual output.\n```sql\nSELECT *\nFROM table;\n```\n<rating>\n```json\n{\"rating\": 5}\n```</rating>", recurse=True) {'think.text': 'Hello!', 'text': 'Some textual output.', 'sql': 'SELECT *\nFROM table;', 'rating.json': '{"rating": 5}'}
>>> parse_md("<think>Hello!</think>\nSome textual output.\n```sql\nSELECT *\nFROM table;\n```\n<rating>\n```json\n{\"rating\": 5}\n```</rating>", mode="list") [{'key': 'think', 'value': 'Hello!'}, {'key': 'text', 'value': 'Some textual output.'}, {'key': 'sql', 'value': 'SELECT *\nFROM table;'}, {'key': 'rating', 'value': '```json\n{"rating": 5}\n```'}]
- ahvn.utils.basic.parser_utils.parse_fc(call, tools_args=None)[源代码]¶
Parse a simple function call string into name, positional and keyword arguments.
Supported syntax mirrors typical Python-style calls with both positional and keyword arguments. Examples:
"fibonacci(32)"->{"name": "fibonacci", "positional_args": [32], "keyword_args": {}}"fibonacci(n=32)"->{"name": "fibonacci", "positional_args": [], "keyword_args": {"n": 32}}"foo(1, 'baz', qux=true, nada=None)"-> booleans andNone/nullare normalized."foo(1, bar='baz', 2)"-> mixed positional and keyword arguments.Empty argument lists like
"ping()"yield empty positional and keyword arg collections.
- 参数:
- 返回:
{"name": <function_name>, "positional_args": [val1, ...], "keyword_args": {<key>: <parsed_value>, ...}}- 返回类型:
- 抛出:
ValueError -- If the call string cannot be parsed.