在langchain实现中,SQLDatabaseToolkit出错:SQLDatabase的数据库实例预期。这是连接问题吗?

In langchain implementation SQLDatabaseToolkit erroring: db Instance of SQLDatabase Expected. Is this a connection issue?

提问人:Jimmy Sgrazzutti 提问时间:10/10/2023 更新时间:10/12/2023 访问量:92

问:

我只是试图将我的数据库连接到我的 OpenAI API 实例,以便从我的信息中得出结论;但是,我在让我的连接正常工作并返回正确的数据库时遇到了很大的麻烦。目前我的连接正常,但我收到以下警告: 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")

错误:

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)
python sql-server sqlalchemy ssms langchain

评论


答:

1赞 Chris Risager 10/10/2023 #1

也许这会有所帮助

from urllib.parse import quote_plus
from sqlalchemy import create_engine

conn =  "DRIVER={ODBC Driver 17 for SQL Server};Server=tmwsql01;Database=DBJimmyTest;trusted_connection=yes;"
quoted = quote_plus(conn)
target_connection = 'mssql+pyodbc:///?odbc_connect={}'.format(quoted)
engine = create_engine(target_connection)

我至少在另一个项目中成功地连接了这样。希望它也对你有用。

编辑: 你也必须改变

db = engine.connect()

db = SQLDatabase(engine)

因为 SQLDatabaseToolkit 需要一个 SQLDatabase 对象,但为您提供了一个来自 sqlalchemy 的 Connection 对象。engine.Connect()

评论

0赞 Jimmy Sgrazzutti 10/10/2023
嗯,它解决了列出的警告;但是,数据库不是 SQLDatabase 的实例错误仍然存在。也许这表明这可能不是连接问题?
1赞 Chris Risager 10/12/2023
问题出在您的对象类型上。当你这样做时,你会得到一个 Connection 对象,但你不希望这样做。相反,您应该像这样使用 langchain 的 SQLDatabase。这应该有效。我将更新原始答案以包含此信息。db = engine.connect()db = SQLDatabase(engine)