解析错误发生时如何重试
虽然在有些情况下,仅通过查看输出就可以修复任何解析错误,但在其他情况下则不行。一个例子是当输出不仅格式不正确,而且部分完成时。考虑以下示例。
from langchain.output_parsers import OutputFixingParser
from langchain_core.exceptions import OutputParserException
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI, OpenAI
from pydantic import BaseModel, Field
API Reference:OutputFixingParser | OutputParserException | PydanticOutputParser | PromptTemplate | ChatOpenAI | OpenAI
template = """Based on the user question, provide an Action and Action Input for what step should be taken.
{format_instructions}
Question: {query}
Response:"""
class Action(BaseModel):
action: str = Field(description="action to take")
action_input: str = Field(description="input to the action")
parser = PydanticOutputParser(pydantic_object=Action)
prompt = PromptTemplate(
template="Answer the user query.\n{format_instructions}\n{query}\n",
input_variables=["query"],
partial_variables={"format_instructions": parser.get_format_instructions()},
)
prompt_value = prompt.format_prompt(query="who is leo di caprios gf?")
bad_response = '{"action": "search"}'
如果我们尝试直接解析这个响应,我们会得到一个错误:
try:
parser.parse(bad_response)
except OutputParserException as e:
print(e)
Failed to parse Action from completion {"action": "search"}. Got: 1 validation error for Action
action_input
Field required [type=missing, input_value={'action': 'search'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.9/v/missing
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/OUTPUT_PARSING_FAILURE
如果我们尝试使用OutputFixingParser
来修复这个错误,它会感到困惑——也就是说,它不知道实际要为操作输入什么。
fix_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())
fix_parser.parse(bad_response)
Action(action='search', action_input='input')
相反,我们可以使用RetryOutputParser,它将提示(以及原始输出)传递进去,以再次尝试获得更好的响应。
from langchain.output_parsers import RetryOutputParser
API Reference:RetryOutputParser
retry_parser = RetryOutputParser.from_llm(parser=parser, llm=OpenAI(temperature=0))
retry_parser.parse_with_prompt(bad_response, prompt_value)
Action(action='search', action_input='leo di caprio girlfriend')
我们还可以轻松地添加RetryOutputParser,通过一个自定义链将原始的LLM/ChatModel输出转换为更可用的格式。
from langchain_core.runnables import RunnableLambda, RunnableParallel
completion_chain = prompt | OpenAI(temperature=0)
main_chain = RunnableParallel(
completion=completion_chain, prompt_value=prompt
) | RunnableLambda(lambda x: retry_parser.parse_with_prompt(**x))
main_chain.invoke({"query": "who is leo di caprios gf?"})
API Reference:RunnableLambda | RunnableParallel
Action(action='search', action_input='leo di caprio girlfriend')
查找RetryOutputParser的API文档。