Search code examples
pythonlangchain

OutputParserException: Could not parse LLM output on Jupyter Notebook


I am facing the following error on 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 tryong with ConversationalAgent vs initialize_agent which has some limitation for my purpose.

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!


Solution

  • 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)