I am just trying to connect my database to my instance of the OpenAI API in order to drawn conclusions from my information; however, I am having significant troubles getting my connection to work and return a correct db. Currently my connection is working, but I am getting this warning: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections engine = create_engine(f"mssql+pyodbc://{SERVER}/{DATABASE}?Trusted_Connection=yes&Driver={DRIVER}")
#import the appropriate capabilities from langchain
from langchain.llms import OpenAI
from langchain.agents import create_sql_agent
from sqlalchemy import create_engine
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI
#record our code in our environment
os.environ['OPENAI_API_KEY'] = key
#Establish connection with sample database (including: 'customers', 'agents', and 'orders' tables)
DRIVER = 'ODBC Driver 17 for SQL Server'
SERVER = 'tmwsql01'
DATABASE = 'DBJimmyTest'
engine = create_engine(f"mssql+pyodbc://{SERVER}/{DATABASE}?Trusted_Connection=yes&Driver={DRIVER}")
db = engine.connect()
llm=OpenAI(temperature=0)
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
#Create a SQL Agent to Process SQL Information
agent_executor = create_sql_agent(
#temperature rating from 0 to 1 determines how creative the ai is (0 = minimum creativity, 1 = maximum)
llm=ChatOpenAI(temperature=0),
toolkit=toolkit,
verbose=True,
agent_type="openai-functions"
)
agent_executor("what is the name of each artist's longest song? Please put only one song per artist")
Error:
Exception has occurred: ValidationError
1 validation error for SQLDatabaseToolkit
db
instance of SQLDatabase expected (type=type_error.arbitrary_type; expected_arbitrary_type=SQLDatabase)
File "C:\Users\jsgrazzutti\AI Project\Test Code\API_Test.py", line 36, in <module>
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic.v1.error_wrappers.ValidationError: 1 validation error for SQLDatabaseToolkit
db
instance of SQLDatabase expected (type=type_error.arbitrary_type; expected_arbitrary_type=SQLDatabase)
1条答案
按热度按时间2mbi3lxu1#
Perhaps this helps
I have at least successfully connected like that in another project. Hope it works for you as well.
Edit: You also have to change
to
Because SQLDatabaseToolkit expects a SQLDatabase object, but
engine.Connect()
gives you a Connection object from sqlalchemy.