首页 > 编程问答 >OutputParserException:无法解析 Jupyter Notebook 上的 LLM 输出

OutputParserException:无法解析 Jupyter Notebook 上的 LLM 输出

时间:2024-07-26 06:17:12浏览次数:22  
标签:python langchain

我在 conda 笔记本上遇到以下错误

File ~\.conda\envs\LLMS\lib\site-packages\langchain\agents\conversational\output_parser.py:26, in ConvoOutputParser.parse(self, text) 24 match = re.search(regex, text) 25 if not match: ---> 26 raise OutputParserException(f"Could not parse LLM output: {text}`") 27 动作 = match.group(1) 28 action_input = match.group(2)

OutputParserException: Could not parse LLM output: `
Answer: "Hello, good morning. I am a helpful assistant. 
Have a normal`

morning")`

我还检查了 https://python.langchain.com/docs/modules/agents/how_to/handle_parsing_errors

我是尝试使用ConversationalAgent与initialize_agent,这对我的目的有一些限制。

这是我尝试过的代码 `

import os
from langchain.llms.huggingface_endpoint import HuggingFaceEndpoint
from langchain.llms import LlamaCpp
from langchain import PromptTemplate, LLMChain
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
import pandas as pd
from utils import *
llm_hf = HuggingFaceEndpoint(
            endpoint_url="https://xxx",
            huggingfacehub_api_token="xxx", task="text-generation"
        )
from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI
# Connect to the SQLite database (it will create a new one if it doesn't exist)
conn = sqlite3.connect('doctors.db')

# Replace 'table_name' with the name of the table you want to create in the database
table_name = 'Doctors'

# Use the `to_sql` method to save the DataFrame to the database
clean_df.to_sql(table_name, conn, if_exists='replace', index=False)


llm = llm_hf



db = SQLDatabase.from_uri("sqlite:///doctors.db")
db.get_table_names()
toolkit = SQLDatabaseToolkit(db=db,
                             llm=llm,
                            )
sql_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    top_k = 10000,
    #agent_executor_kwargs={"return_intermediate_steps": True},
)

from langchain.agents import initialize_agent, Tool
tools = [
    Tool(
        name="Doctors Database System",
        func=sql_executor.run,
        return_direct=True,
        description="useful to give information about doctors names, specialities and locations. Input should be a fully formed question.",
    )
]
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor, ConversationalAgent
from langchain.memory import ConversationBufferWindowMemory
from langchain.llms import OpenAI
from langchain.chains import LLMChain

prefix = """You are a helpful assistant. 
Have a normal conversation with a human. 
You can offer to answer questions about a database with doctor information.
You have access to the following tools:"""
suffix = """Begin!"

{chat_history}
Question: {input}
{agent_scratchpad}"""

prompt = ConversationalAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"],
)
memory = ConversationBufferWindowMemory(memory_key="chat_history", k = 5)

llm_chain = LLMChain(llm=llm, prompt=prompt)

agent = ConversationalAgent(llm_chain=llm_chain,
                            #output_parser= lambda **kwargs : dict(kwargs),
                            verbose=True)

agent_chain = AgentExecutor.from_agent_and_tools(
    agent=agent, tools=tools, verbose=True, memory=memory
)
agent_chain.run("Hello, good `

非常感激。任何建议谢谢!


The error you're encountering, "OutputParserException: Could not parse LLM output", stems from the Conversational Agent's inability to find the expected structure in the LLM's response. Let's break down why this happens and how to fix it:

Understanding the Problem

  • Conversational Agent Expectations: Conversational Agents in LangChain are designed to engage in back-and-forth dialogue. They rely on a specific format in the LLM's output to determine the next action (e.g., call a tool, provide information).
  • Output Parser: The OutputParser is responsible for looking for patterns in the LLM's response to extract this action and any relevant data.
  • Your Error: The error message means the regular expression defined within the default ConvoOutputParser couldn't find a match in the LLM's output ( "Hello, good morning. I am a helpful assistant. Have a normal morning" ). This likely means your LLM isn't structuring its response in a way the agent expects.

Solution: Guiding the LLM's Output

The key is to provide clearer instructions within your prompt to guide the LLM into generating responses that the Conversational Agent can understand. Here's a modified approach:

import os
import sqlite3
from langchain.llms.huggingface_endpoint import HuggingFaceEndpoint
from langchain.agents import create_sql_agent, Tool, AgentExecutor, ConversationalAgent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import LLMChain
from langchain.agents import AgentType

# ... (Your existing code for database setup, llm_hf, clean_df, etc.)

# Modified Prompt for Structured Output
prefix = """You are a helpful assistant. 
Have a normal conversation with a human. 
You can offer to answer questions about a database with doctor information.

If the user asks a question that requires accessing the Doctors Database System, respond using the following format:

```tool_code
{{
"tool": "Doctors Database System",
"query": "YOUR_SQL_QUERY_HERE"
}}

Otherwise, respond naturally.

You have access to the following tools:"""

suffix = """Begin!

{chat_history} Question: {input} {agent_scratchpad}"""

prompt = ConversationalAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) memory = ConversationBufferWindowMemory(memory_key="chat_history", k=5)

llm_chain = LLMChain(llm=llm, prompt=prompt)

agent = ConversationalAgent( llm_chain=llm_chain, verbose=True )

agent_chain = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True, memory=memory )

agent_chain.run("Hello, good morning! Can you tell me about doctors specializing in cardiology?") ```

Explanation of Changes:

  1. Structured Output Instructions: The updated prompt explicitly instructs the LLM to format its responses in a specific way when the user asks a database-related question. The LLM should now output a JSON-like structure with tool and query keys.

  2. Tool Code Block: Using backticks ( ``) around tool_code` can further help the LLM distinguish the structured output from its regular responses.

How it Works:

  • When the user asks a database-related question, the LLM will now, ideally, generate a response similar to: json { "tool": "Doctors Database System", "query": "SELECT * FROM Doctors WHERE speciality='Cardiology'" }

  • The Conversational Agent's OutputParser can then easily extract the "tool" and "query" values to execute the desired action.

Important Notes:

  • LLM Behavior: It's crucial to remember that LLMs can be unpredictable. While the updated prompt provides strong guidance, you may need to refine it further if the LLM doesn't consistently follow the desired output format.
  • Debugging: Use the verbose=True setting in your agent and agent chain to closely examine the LLM's output and understand how it interacts with the parsing logic.

Let me know if you have any more questions or if you encounter further issues!

标签:python,langchain
From: 77190232

相关文章

  • 84 我正在使用 Python 开发 selenium 自动化项目。我收到错误 .NoSuchElementExceptio
    场景是这样的,我将打开一个网页,在使用selenium单击该网页后,它会要求位置访问权限,屏幕上会出现一堆按钮,我正在尝试定位其中一个按钮,但即使正确给出了Xpath地址,我得到.NoSuchElementException:错误能够单击目标按钮你正在使用Selenium在Python中开发自动化项目,并遇到......
  • 在Python 3中删除两个指定字符串之间的字符串
    我正在从事一个NLP项目,该项目要求我从一段文本中删除计算机代码。代码包含在标签<pre><code>和</code></pre>之间。现在我可以做一个简单的正则表达式匹配,但我想概括这个函数,以便它可以删除任何两个指定字符串之间的文本,即使它们是嵌套的。例如,如果我有一个......
  • Azure Open AI - Python 和 Java API 之间 gpt4o 的结果截然不同
    我使用Java和PythonAPI对AzureOpenAI进行相同的调用,但收到截然不同的结果:相同的系统提示相同的用户提示适用于Java和Python的azureai包的相同(最新)版本尽管输入的用户和系统提示完全相同,但响应却非常不同-python提示是“正确的”并......
  • leetcode 输出错误? (Python)
    我的VSCode/本地终端给出了[1,4,1,5,1,6]的正确输出,但不知何故leetcode给了我完全不同的输出。我在这里错过了什么吗?这怎么可能?顺便说一下,这是wigglesort2将我的本地代码复制粘贴到leetcode中给出了不同的输出数组很难在没有看到你的代码的情况下......
  • 当 python 窗口的一部分不在屏幕上时,如何让它自己被记录?
    在Windows10中,大多数应用程序窗口都可以使用OBS等程序进行记录。当窗口被拖动以致其部分内容在显示屏上不可见时,通常OBS仍会接收窗口的内容,即使它在屏幕上不可见。但是,在编写python应用程序时,这似乎不以相同的方式工作。我尝试了几种不同的类似GUI的模块......
  • 使用 aws cdk 设置用户池客户端属性以具有读/写访问权限 - Python
    我试图根据属性给予一些自定义属性特定的读/写访问权限。我收到此错误。资源处理程序返回消息:“无效写入创建客户端时指定的属性(服务:CognitoIdentityProvider,状态代码:400,请求ID:<request_id>)”(RequestToken:<request_token>,HandlerErrorCode:InvalidRequest)任何人都可以为......
  • 试图找出此页面的逻辑:存储了大约 ++ 100 个结果 - 并使用 Python 和 BS4 进行了解析
    试图找出此页面背后的逻辑:我们已将一些结果存储在以下数据库中:https://www.raiffeisen.ch/rch/de/ueber-uns/raiffeisen-gruppe/Organization/raiffeisenbanken/deutsche-schweiz.html#accordionitem_18104049731620873397从a到z大约:120个结果或更多:......
  • 如何在 Numpy Python 中将 4 维数组的下三角形复制到上三角形?
    目标是将下三角形复制到上三角形。根据OP中提出的建议,起草了以下代码。importnumpyasnplw_up_pair=np.tril_indices(4,-1)arr=np.zeros((4,4,1,1))arr[1,:1,:,0]=1arr[2,:2,0,0]=2arr[3,:3,0,0]=3arr=arr+arr.T-np.diag(np.diag(arr))但是,它......
  • 如何在 Python 中对多行使用单个 INSERT INTO 语句?
    我目前正在开发一个DiscordPython机器人,我在其中循环遍历ForumTags列表,并为每个对象生成INSERTINTOSQL语句以将数据插入MySQL数据库。但是,我想要通过将所有这些单独的INSERTINTO语句组合到单个查询中来优化我的代码,如下所示:INSERTINTO......
  • 双 for 循环的 Pythonic 方式
    我有以下代码:importnumpyasnpepsilon=np.array([[0.,0.00172667,0.00071437,0.00091779,0.00154501],[0.00128983,0.,0.00028139,0.00215905,0.00094862],[0.00035811,0.00018714,0.,0.00029365,0.00036993......