基类: ChainableOutputParser
Langchain 输出解析器。
源代码位于 llama-index-integrations/output_parsers/llama-index-output-parsers-langchain/llama_index/output_parsers/langchain/base.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 | class LangchainOutputParser(ChainableOutputParser):
"""Langchain output parser."""
def __init__(
self, output_parser: "LCOutputParser", format_key: Optional[str] = None
) -> None:
"""Init params."""
self._output_parser = output_parser
self._format_key = format_key
def parse(self, output: str) -> Any:
"""Parse, validate, and correct errors programmatically."""
# TODO: this object may be stringified by our upstream llmpredictor,
# figure out better
# ways to "convert" the object to a proper string format.
return self._output_parser.parse(output)
def format(self, query: str) -> str:
"""Format a query with structured output formatting instructions."""
format_instructions = self._output_parser.get_format_instructions()
# TODO: this is a temporary hack. if there's curly brackets in the format
# instructions (and query is a string template), we need to
# escape the curly brackets in the format instructions to preserve the
# overall template.
query_tmpl_vars = {
v for _, v, _, _ in Formatter().parse(query) if v is not None
}
if len(query_tmpl_vars) > 0:
format_instructions = format_instructions.replace("{", "{{")
format_instructions = format_instructions.replace("}", "}}")
if self._format_key is not None:
fmt_query = query.format(**{self._format_key: format_instructions})
else:
fmt_query = query + "\n\n" + format_instructions
return fmt_query
|
parse
parse(output: str) -> Any
以编程方式解析、验证和纠正错误。
源代码位于 llama-index-integrations/output_parsers/llama-index-output-parsers-langchain/llama_index/output_parsers/langchain/base.py
| def parse(self, output: str) -> Any:
"""Parse, validate, and correct errors programmatically."""
# TODO: this object may be stringified by our upstream llmpredictor,
# figure out better
# ways to "convert" the object to a proper string format.
return self._output_parser.parse(output)
|
format(query: str) -> str
使用结构化输出格式说明格式化查询。
源代码位于 llama-index-integrations/output_parsers/llama-index-output-parsers-langchain/llama_index/output_parsers/langchain/base.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 | def format(self, query: str) -> str:
"""Format a query with structured output formatting instructions."""
format_instructions = self._output_parser.get_format_instructions()
# TODO: this is a temporary hack. if there's curly brackets in the format
# instructions (and query is a string template), we need to
# escape the curly brackets in the format instructions to preserve the
# overall template.
query_tmpl_vars = {
v for _, v, _, _ in Formatter().parse(query) if v is not None
}
if len(query_tmpl_vars) > 0:
format_instructions = format_instructions.replace("{", "{{")
format_instructions = format_instructions.replace("}", "}}")
if self._format_key is not None:
fmt_query = query.format(**{self._format_key: format_instructions})
else:
fmt_query = query + "\n\n" + format_instructions
return fmt_query
|