Search code examples
azure-sql-databaseopenai-apilangchainchatgpt-apipy-langchain

Langchain's SQLDatabaseSequentialChain to query database


I am trying to create a chatbot with langchain and openAI that can query the database with large number of tables based on user query. I have used SQLDatabaseSequentialChain which is said to be best if you have large number of tables in the database.

The problem is when I run this code, it takes forever to establish the connection and at the end I get this error:

 raise self.handle_error_response(
openai.error.APIError: internal error {
        "message": "internal error",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
 500 {'error': {'message': 'internal error', 'type': 'invalid_request_error', 'param': None, 'code': None}} {'Date': 'Wed, 21 Jun 2023 14:49:42 GMT', 'Content-Type': 
'application/json; charset=utf-8', 'Content-Length': '147', 'Connection': 'keep-alive', 'vary': 'Origin', 'x-request-id': '37d9d00a37ce69e68166317740bad7da', 'strict-transport-security': 'max-age=15724800; includeSubDomains', 'CF-Cache-Status': 'DYNAMIC', 'Server': 'cloudflare', 'CF-RAY': '7dad0f24fa9c6ec5-BOM', 'alt-svc': 'h3=":443"; ma=86400'}

Below is the code I found on the internet:

from langchain import OpenAI, SQLDatabase
from langchain.chains import SQLDatabaseSequentialChain
import pyodbc

server = 'XYZ'
database = 'XYZ'
username = 'XYZ'
password = 'XYZ'
driver = 'ODBC Driver 17 for SQL Server'

conn_str = f"mssql+pyodbc://{username}:{password}@{server}/{database}?driver={driver}"

try:
    # Establish a connection to the database
    conn = SQLDatabase.from_uri(conn_str)

except pyodbc.Error as e:
    # Handle any errors that occur during the connection or query execution
    print(f"Error connecting to Azure SQL Database: {str(e)}")

OPENAI_API_KEY = "XYZ key"

llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name='text-davinci-003 ')

PROMPT = """ 
Given an input question, first create a syntactically correct SQL query to run,  
then look at the results of the query and return the answer.  
The question: {question}
"""

db_chain = SQLDatabaseSequentialChain.from_llm(llm, conn, verbose=True, top_k=3)

question = "What is the property code of Ambassador, 821?"

db_chain.run(PROMPT.format(question=question))

I have confirmed that my openAI API key is up and running.

Please help me out with this.

Also if you have suggestions for any other method that I should consider, please let me know. I am currently doing RnD on this project but didn't found any satisfactory solution.

Thank you

I tried to check if my openAI API key is available and yes, it is. Expected to get a response from GPT model.


Solution

  • The SQLDatabase class has a metadata reflect function call in its init. Irrespective of what you specify in the "include_tables", it inspects the metadata of every table in the database. It doesn't assign the inspection results to anything. When I commented out these lines, the code runs now in <0.5 sec where it was taking more than few min.

    self._max_string_length = max_string_length
    self._metadata = metadata or MetaData()
    # including view support if view_support = true
    # self._metadata.reflect(
    #     views=view_support,
    #     bind=self._engine,
    #     only=list(self._usable_tables),
    #     schema=self._schema,
    # )