Troubleshooting GPT-4 Integration with SQLDatabaseToolkit and create_sql_agent for Prompt Passing Error

216 views Asked by At

I was previously using SQLDatabaseChain to connect LLM (Language Model) with my database, and it was functioning correctly with GPT-3.5. However, when attempting the same process with GPT-4, I encountered an error stating "incorrect syntax near 's"

To address this issue, I opted to use SQLDatabaseToolkit and the create_sql_agent function. However, I encountered a problem with this approach as I was unable to pass a prompt. When attempting to include a PromptTemplate in the create_sql_agent argument, it resulted in errors.

ValueError: Prompt missing required variables: {'tool_names', 'agent_scratchpad', 'tools'}

Below is my code:

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True,
    prompt=MSSQL_PROMPT,
)
1

There are 1 answers

0
PJR On BEST ANSWER

I found solution for it.

agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=False)

will worked for me. And it also work with prompt based approach. So if you want to add prompt in it then it should be like

agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=False, prompt=MSSQL_PROMPT)

where

    MSSQL_PROMPT = """You are an MS SQL expert. Given an input question, first create a syntactically correct MS SQL query to run, then look at the results of the query and return the answer to the input question.

Use the following Domain Knowledge about Database: One Order can have multiple shipments & shipment containers. 

Use the following format:

Question: Question here
SQLQuery: SQL Query to run
SQLResult: Result of the SQLQuery
Answer: Final answer here

Only use the following tables:
{table_info}

Thought: I should look at the tables in the database to see what I can query. Then I should query the schema of the most relevant tables.
{agent_scratchpad}

Question: {input}"""

You can add more data in prompt as per your use cases.