OutputParserException: Could not parse LLM output on Jupyter Notebook

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

问题背景:

I am facing the following error on conda notebook

我在 Conda Notebook 上遇到了以下错误:

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 action = 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")`

I also checked the   我也检查了https://python.langchain.com/docs/modules/agents/how_to/handle_parsing_errors

I was trying with ConversationalAgent vs initialize_agent which has some limitation for my purpose.

我尝试了使用 `ConversationalAgent` 和 `initialize_agent`,但后者对于我的目的有一些限制。

This is the code I tried `        这是我尝试的代码

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 `

Would really appreciate. Any suggestion thanks!

非常感谢!任何建议都不胜感激!

问题解决:

Temporary work around would be as below

临时解决方法如下:

     try:
             response= agent_chain.run("how many unique statuses are there?")
     except Exception as e:
             response = str(e)
             if response.startswith("Could not parse LLM output: `"):
                 response = response.removeprefix("Could not parse LLM output: `").removesuffix("`")
                 print(response)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值