I am trying to connect OpenAI LLM/chatbot to Oracle Database using Langchain framework. The problem I am trying to solve is a part of a bigger problem which is to have the enclosed code be usable as a part of a web based POC shat showcases graphs or charts by taking data out of a relational database. I am only interested in making the enclosed code work and would request you to leave the charting capabilities to myself. Any suggestions are welcome.
The installed Oracle Database version is Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
PS C:\Users\HI> pip show oracledb Name: oracledb Version: 2.0.1
PS C:\Users\HI> pip show langchain Name: langchain Version: 0.1.10
PS C:\Users\HI> pip show langchain-openai Name: langchain-openai Version: 0.0.8
PS C:\Users\HI> pip show langchain-experimental Name: langchain-experimental Version: 0.0.53
PS C:\Users\HI> python --version Python 3.12.2
What I tried, the code, is pasted below. I created a key from OpenAI and I am using find_env to locate a file called .env that contains key value pairs of API keys. I am trying to fetch the list of table names in the schema using a conversational natural language style and I expect NLU to understand the natural language,convert it to a SQL query and output atleast two table names in the output. Thank you.
import oracledb
import langchain
import os
print(langchain.__version__)
from dotenv import load_dotenv, find_dotenv
#from langchain.agents import SQLDatabaseChain
from langchain_experimental.sql import SQLDatabaseChain
#from langchain.llms import OpenAI # Or any other LLM you prefer
from langchain_openai import OpenAI
#from langchain.agents import SQLDatabaseWrapper
try:
connection = oracledb.connect(
user="scott",
password="scott",
dsn="192.168.1.113:1521/orclpdb"
)
except oracledb.Error as e:
print(f"Error connecting to database: {e}")
exit() # Exit if connection fails
finally:
if connection:
connection.close()
load_dotenv(find_dotenv(), override=True)
# Choose your preferred LLM (e.g., OpenAI)
llm = OpenAI(temperature=0.7, max_tokens=100, key=os.environ.get('OPENAI_API_KEY'))
# Create a Langchain chain directly connecting the LLM and the Oracle database connection
#chain = SQLDatabaseChain.from_connection(connection, llm=llm) ## from_connection does not exist
db_chain = SQLDatabaseChain.from_llm(llm, connection)
# User interaction loop
while True:
# Prompt the user for their query
user_query = input("Enter your query about the Oracle database (or 'quit' to exit): ")
if user_query.lower() == "quit":
break
# Use the Langchain chain to process the query
response = chain.run(user_query)
print(response)
# Close the database connection (optional, handled by the context manager in some Langchain versions)
# connection.close()
The error is
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[2], line 32
28 llm = OpenAI(temperature=0.7, max_tokens=100, key=os.environ.get('OPENAI_API_KEY'))
30 # Create a Langchain chain directly connecting the LLM and the Oracle database connection
31 #chain = SQLDatabaseChain.from_connection(connection, llm=llm) ## from_connection does not exist
---> 32 db_chain = SQLDatabaseChain.from_llm(llm, connection)
35 # User interaction loop
36 while True:
37 # Prompt the user for their query
File c:\Users\HI\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_experimental\sql\base.py:226, in SQLDatabaseChain.from_llm(cls, llm, db, prompt, **kwargs)
207 @classmethod
208 def from_llm(
209 cls,
(...)
213 **kwargs: Any,
214 ) -> SQLDatabaseChain:
215 """Create a SQLDatabaseChain from an LLM and a database connection.
216
217 *Security note*: Make sure that the database connection uses credentials
(...)
224 https://github.com/langchain-ai/langchain/issues/5923
225 """
--> 226 prompt = prompt or SQL_PROMPTS.get(db.dialect, PROMPT)
227 llm_chain = LLMChain(llm=llm, prompt=prompt)
228 return cls(llm_chain=llm_chain, database=db, **kwargs)
AttributeError: 'Connection' object has no attribute 'dialect'
The warning is
UserWarning: WARNING! key is not default parameter.
key was transferred to model_kwargs.
Please confirm that key is what you intended.
warnings.warn(