langchain JsonOutputParser有时会在LLM将输出的JSON包含在json ...中时失败,

s2j5cfk0  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(131)

检查其他资源

  • 我为这个问题添加了一个非常描述性的标题。
  • 我使用集成搜索在LangChain文档中进行了搜索。
  • 我使用GitHub搜索找到了一个类似的问题,但没有找到。
  • 我确信这是LangChain中的一个bug,而不是我的代码。
  • 该bug无法通过更新到LangChain的最新稳定版本(或特定集成包)来解决。

示例代码

from langchain_core.output_parsers import JsonOutputParser
 
msg = "what queries must i run?"

class Step(BaseModel):
    step_name: str = Field(
        description="...")
    tool_to_use: str = Field(description="...")
    tool_input: str = Field(description="...")
    depends_on: List[str] = Field(
        description="...")

class PlanOutput(BaseModel):
    task: str = Field(description="...")
    steps: List[Step] = Field(description="...")

parser = JsonOutputParser(pydantic_object=PlanOutput)

llm = ChatOpenAI(...)
chain = ChatPromptTemplate.from_messages([('user': '...{input} Your output must follow this format: {format}' | llm | parser

chain.invoke({'format': plan_parser.get_format_instructions(), "input": msg})

错误信息和堆栈跟踪(如果适用)

2024-06-22 11:21:03,116 - agent.py - 90 - ERROR - Traceback (most recent call last):
File "/root/classifier/.venv/lib/python3.9/site-packages/langchain_core/output_parsers/json.py", line 66, in parse_result
return parse_json_markdown(text)
File "/root/classifier/.venv/lib/python3.9/site-packages/langchain_core/utils/json.py", line 147, in parse_json_markdown
return _parse_json(json_str, parser=parser)
File "/root/classifier/.venv/lib/python3.9/site-packages/langchain_core/utils/json.py", line 160, in _parse_json
return parser(json_str)
File "/root/classifier/.venv/lib/python3.9/site-packages/langchain_core/utils/json.py", line 120, in parse_partial_json
return json.loads(s, strict=strict)
File "/usr/lib/python3.9/json/init.py", line 359, in loads
return cls(**kw).decode(s)
File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 6 column 22 (char 109)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/mnt/d/python_projects/azure-openai-qa-bot/nat-sql/src/agent.py", line 69, in talk
for s in ap.app.stream({"task": inp, 'session_id': sid}, config=args):
File "/root/classifier/.venv/lib/python3.9/site-packages/langgraph/pregel/init.py", line 963, in _panic_or_proceed
_panic_or_proceed(done, inflight, step)
File "/root/classifier/.venv/lib/python3.9/site-packages/langgraph/pregel/init.py", line 1489, in _panic_or_proceed
raise exc
File "/usr/lib/python3.9/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/root/classifier/.venv/lib/python3.9/site-packages/langgraph/pregel/retry.py", line 66, in run_with_retry
task.proc.invoke(task.input, task.config)
File "/root/classifier/.venv/lib/python3.9/site-packages/langchain_core/runnables/base.py", line 2399, in invoke
input = step.invoke(
File "/root/classifier/.venv/lib/python3.9/site-packages/langchain_core

gk7wooem

gk7wooem1#

JsonOutputParser 的工作完全正常,它可以检测到有效的json。

{
{
    "task": "what Queries tmust i run",
    "steps": [
        {
            "step_name": "Step#1",
            "tool_to_use": Document_Search_Tool, #not valid, should be "Document_Search_Tool" 
            "tool_input": "What queries must I run?",
            "depends_on": []
        }
    ]
}

这不是一个有效的json输出。请尝试提供一些示例以及提示来生成一致且有效的结果。

相关问题