oracle 23c free DB + langchain sql query chain #19299
neerajsingh4736
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am having issues with working with create sql query chain and Oracle 23c free DB . Please help , below is my code.
from langchain.chains import create_sql_query_chain
from langchain_openai import ChatOpenAI
from sqlalchemy import create_engine, MetaData
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
Get database credentials
username = 'HR'
hostname = 'localhost'
port = '1521'
service_name = 'freepdb1'
password = getpass.getpass(f'Enter password for {username}@{hostname}/{service_name}: ')
Construct connection string with service name
connection_string = f'oracle+cx_oracle://{username}:{password}@{hostname}:{port}/?service_name={service_name}'
Create SQLAlchemy engine
engine = create_engine(connection_string)
Create SQLAlchemy connection
connection = engine.connect()
Create metadata object and bind it to the engine
metadata = MetaData(bind=engine)
Reflect database tables
metadata.reflect()
Extract table names
table_names = metadata.tables.keys()
Create SQL query chain
chain = create_sql_query_chain(llm, connection)
Invoke the chain with the question
response = chain.invoke({"question": "How many employees are there"})
print(response)
Beta Was this translation helpful? Give feedback.
All reactions