输出解析器类。
源代码位于 llama-index-core/llama_index/core/types.py
解析 抽象方法
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 | class BaseOutputParser(DispatcherSpanMixin, ABC):
"""Output parser class."""
@abstractmethod
def parse(self, output: str) -> Any:
"""Parse, validate, and correct errors programmatically."""
def format(self, query: str) -> str:
"""Format a query with structured output formatting instructions."""
return query
def _format_message(self, message: ChatMessage) -> ChatMessage:
text_blocks: list[tuple[int, TextBlock]] = [
(idx, block)
for idx, block in enumerate(message.blocks)
if isinstance(block, TextBlock)
]
# add text to the last text block, or add a new text block
format_text = ""
if text_blocks:
format_idx = text_blocks[-1][0]
format_text = text_blocks[-1][1].text
if format_idx != -1:
# this should always be a text block
assert isinstance(message.blocks[format_idx], TextBlock)
message.blocks[format_idx].text = self.format(format_text) # type: ignore
else:
message.blocks.append(TextBlock(text=self.format(format_text)))
return message
def format_messages(self, messages: List[ChatMessage]) -> List[ChatMessage]:
"""Format a list of messages with structured output formatting instructions."""
# NOTE: apply output parser to either the first message if it's a system message
# or the last message
if messages:
if messages[0].role == MessageRole.SYSTEM:
# get text from the last text blocks
messages[0] = self._format_message(messages[0])
else:
messages[-1] = self._format_message(messages[-1])
return messages
@classmethod
def __get_pydantic_core_schema__(
cls, source: Type[Any], handler: GetCoreSchemaHandler
) -> CoreSchema:
return core_schema.any_schema()
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler
) -> Dict[str, Any]:
json_schema = handler(core_schema)
return handler.resolve_ref_schema(json_schema)
|
程序化地解析、验证和纠正错误。
parse(output: str) -> Any
格式化
解析 抽象方法
| @abstractmethod
def parse(self, output: str) -> Any:
"""Parse, validate, and correct errors programmatically."""
|
format(query: str) -> str
格式化消息
解析 抽象方法
| def format(self, query: str) -> str:
"""Format a query with structured output formatting instructions."""
return query
|
回到顶部
解析 抽象方法
76
77
78
79
80
81
82
83
84
85
86
87 | def format_messages(self, messages: List[ChatMessage]) -> List[ChatMessage]:
"""Format a list of messages with structured output formatting instructions."""
# NOTE: apply output parser to either the first message if it's a system message
# or the last message
if messages:
if messages[0].role == MessageRole.SYSTEM:
# get text from the last text blocks
messages[0] = self._format_message(messages[0])
else:
messages[-1] = self._format_message(messages[-1])
return messages
|