/langchain_experimental/llms/ollama_functions.py", line 400, in _generate raise ValueError( ValueError: 'llama3' did not respond with valid JSON.

djp7away  于 3个月前  发布在  其他
关注(0)|答案(2)|浏览(55)

检查其他资源

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

示例代码

from langchain_core.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_experimental.llms.ollama_functions import OllamaFunctions
from typing import Optional
import json

# Schema for structured response
class AuditorOpinion(BaseModel):
    opinion: Optional[str] = Field(
        None,
        description="The auditor's opinion on the financial statements. Values are: 'Unqualified Opinion', "
                    "'Qualified Opinion', 'Adverse Opinion', 'Disclaimer of Opinion'."
    )

def load_markdown_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

path = "data/auditor_opinion_1.md"
markdown_text = load_markdown_file(path)

# Prompt template
prompt = PromptTemplate.from_template(
"""
what is the auditor's opinion
    
Human: {question}
AI: """
)

# Chain
llm = OllamaFunctions(model="llama3", format="json", temperature=0)
structured_llm = llm.with_structured_output(AuditorOpinion)
chain = prompt | structured_llm
alex = chain.invoke(markdown_text)

response_dict = alex.dict()

# Serialize the dictionary to a JSON string with indentation for readability
readable_json = json.dumps(response_dict, indent=2, ensure_ascii=False)

# Print the readable JSON
print(readable_json)

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

langchain_experimental/llms/ollama_functions.py", line 400, in _generate
    raise ValueError(
ValueError: 'llama3' did not respond with valid JSON.

描述

尝试使用with_structured_output从markdown文本获取结构化输出

系统信息

系统信息

操作系统:Darwin
操作系统版本:Darwin Kernel Version 23.5.0: Wed May 1 20:14:38 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6020
Python版本:3.9.6(默认,2024年2月3日15:58:27)
[Clang 15.0.0 (clang-1500.3.9.4)]

软件包信息

langchain_core: 0.2.9
langchain: 0.2.5
langchain_community: 0.2.5
langsmith: 0.1.79
langchain_experimental: 0.0.61
langchain_google_genai: 1.0.5
langchain_google_vertexai: 1.0.4
langchain_mistralai: 0.1.8
langchain_openai: 0.1.8
langchain_text_splitters: 0.2.0
langchainhub: 0.1.16

没有安装的软件包(不一定是个问题)

以下软件包未找到:
langgraph
langserve

omvjsjqw

omvjsjqw2#

在这里也是一样。
我刚刚进行了一些调试。_generate方法似乎总是返回一个空字符串。
以下代码块来自langchain_experimental/llms/ollama_functions.py的第398行左右:

system_message = system_message_prompt_template.format(
                    tools=json.dumps(functions, indent=2)
                )
        print(f"{messages=}")
        response_message = super()._generate(
            [system_message] + messages, stop=stop, run_manager=run_manager, **kwargs
        )
        print(f"{response_message=}")
        chat_generation_content = response_message.generations[0].text
        print(f"{chat_generation_content=}")
messages=[HumanMessage(content='what is the weather in shenzhen', id='1a5b3d30-aa81-4fe4-acf4-4dec3c7b60f7'), AIMessage(content='', id='run-3d3484d6-f615-42a1-83c3-57da16dde58f-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in shenzhen'}, 'id': 'call_b858f03795424bee96f12ac819572eb2'}]), ToolMessage(content='[{"url": "https://world-weather.info/forecast/china/shenzhen/june-2024/", "content": "Detailed \\u26a1 Shenzhen Weather Forecast for June 2024 - day/night \\ud83c\\udf21\\ufe0f temperatures, precipitations - World-Weather.info. Add the current city. Search. Weather; Archive; Widgets \\u00b0F. World; China; Guangdong; Weather in Shenzhen; Weather in Shenzhen in June 2024. ... 24 +88\\u00b0 +82\\u00b0 25 +86\\u00b0 +82\\u00b0 26 ..."}]', name='tavily_search_results_json', id='1c92f82d-4e5d-4880-993c-c7c0967c85e4', tool_call_id='call_b858f03795424bee96f12ac819572eb2')]
response_message=ChatResult(generations=[ChatGeneration(generation_info={'model': 'qwen2:7b-instruct', 'created_at': '2024-06-24T05:57:25.530385494Z', 'message': {'role': 'assistant', 'content': ''}, 'done': True, 'total_duration': 10381721517, 'load_duration': 884395, 'prompt_eval_count': 164, 'prompt_eval_duration': 10253678000, 'eval_count': 1, 'eval_duration': 49000}, message=AIMessage(content=''))], llm_output=None)
chat_generation_content=''

错误:
ValueError: 'qwen2:7b-instruct' did not respond with valid JSON. Please try again. Response:

相关问题