MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / _parse_response_value

Method _parse_response_value

examples/server/server.py:6734–6995  ·  view source on GitHub ↗
(
        self,
        text: Any,
        schema: Dict[str, Any],
        *,
        partial: bool,
    )

Source from the content-addressed store, hash-verified

6732 break
6733
6734 def _parse_response_value(
6735 self,
6736 text: Any,
6737 schema: Dict[str, Any],
6738 *,
6739 partial: bool,
6740 ) -> Any:
6741 if "const" in schema:
6742 return schema["const"]
6743 if text is None:
6744 return None
6745 node_content: Any = text
6746 node_regex = schema.get("x-regex")
6747 if node_regex is not None:
6748 if not isinstance(node_content, str):
6749 raise CompletionResponseParsingError(
6750 "response_schema x-regex requires string input"
6751 )
6752 captured_content = self._regex_capture(node_content, node_regex)
6753 if captured_content is None:
6754 if (
6755 partial
6756 and schema.get("type") == "object"
6757 and "x-regex-key-value" in schema
6758 ):
6759 captured_content = node_content
6760 else:
6761 return None
6762 node_content = captured_content
6763 node_regex_iterator = schema.get("x-regex-iterator")
6764 if node_regex_iterator is not None:
6765 if schema.get("type") != "array":
6766 raise CompletionResponseParsingError(
6767 "response_schema x-regex-iterator requires array type"
6768 )
6769 if not isinstance(node_content, str):
6770 raise CompletionResponseParsingError(
6771 "response_schema x-regex-iterator requires string input"
6772 )
6773 array_values = []
6774 matches = list(re.finditer(node_regex_iterator, node_content, re.S))
6775 for match in matches:
6776 item_text = self._regex_capture(match.group(0), node_regex_iterator)
6777 if item_text is not None:
6778 array_values.append(item_text)
6779 if partial and "(.*?)" in node_regex_iterator:
6780 prefix_pattern, suffix_pattern = node_regex_iterator.split("(.*?)", 1)
6781 prefix_matches = list(re.finditer(prefix_pattern, node_content, re.S))
6782 if prefix_matches:
6783 last_prefix_match = prefix_matches[-1]
6784 if not matches or matches[-1].start() != last_prefix_match.start():
6785 tail = node_content[last_prefix_match.end() :]
6786 if re.search(suffix_pattern, tail, re.S) is None:
6787 array_values.append(tail)
6788 if not array_values:
6789 return None
6790 node_content = array_values
6791 node_regex_key_value = schema.get("x-regex-key-value")

Callers 2

parse_chat_responseMethod · 0.95

Calls 6

_regex_captureMethod · 0.95
_has_text_toolsMethod · 0.95
appendMethod · 0.80

Tested by

no test coverage detected