How to create a Conversational Chatbot using external SQL dataset?

367 views Asked by At

I am trying to create a OpenAI Chatbot using GPT-3 Model by connecting it to air-dialogue dataset. I want to mimic conversations given in the dataset and connect it to the knowledge base given in the dataset. I have tried to use langchain agents with SQL tools, to find appropriate flight for the user under his requirements. However I encountered an error. The code block given below explains what I want to achieve.

I have tried OpenAI api and created an agent. I am trying to mimic conversation between assistant and User. I have converted the flight_metadata into a SQL database stored in the variable db. The entire structure looks like:

db = SQLDatabase(engine)
# Let's create a SQLDatabase Chain to use the llm
sql_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)

from langchain.agents import Tool

sql_tool = Tool(
    name='Flight DB',
    func=sql_chain.run,
    description="Useful when you need information about currently available flight via departure and return airport, dates, class, price and connecting_airports"
)

from langchain.tools import BaseTool

class HasReservation(BaseTool):
    name = "Reservation Checker"
    description = "use this tool when you need to check if user has an active reservation or not"

    def _run(self, name):
        return bool(json.loads(lines2[0])["reservation"])

    def _arun(self, name):
        raise NotImplementedError("This tool does not support async")

tools = [sql_tool, HasReservation()]

agent = initialize_agent(
    agent='chat-conversational-react-description',
    tools=tools,
    llm=llm,
    verbose=True,
    max_iterations=3,
    early_stopping_method='generate',
    memory=conversational_memory
)

sys_msg = """Assistant is a large language model which acts a Chatbot trained to handle human interaction for flights.

Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations for questions from User on their flights. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide help to User over his flights query.

The User can ask for four actions, booking a flight, reschedule, postpone or cancel a flight.

Assistant always ask for User requirements for flight reschedule, booking, postpone and then lookup for appropriate flights.

The queries for reschedule, postpone and cancel always requires the User to have an reservation. If User don't have reservation don't proceed with anything and ask User to book a flight first.

Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
"""

new_prompt = agent.agent.create_prompt(
    system_message=sys_msg,
    tools=tools
)

agent.agent.llm_chain.prompt = new_prompt

agent("Can you help me to change my recent reservation because my trip dates are got postponed?")

The above command is giving the error that OutputParserException: Could not parse LLM output: AI: { "action": "Final Answer", "action_input": "You do not have an active reservation. Please book a flight first." }

0

There are 0 answers