Mutasim Al-Mualimi
Mutasim Al-Mualimi

Reputation: 71

How to Connect Langchain Database Agent with SqlServer

I need to connect my langchain database agent with my sqlserver database so that my agent can access the data, yet in the documention it does not explain how to do so, it only shows how to connect to sqlite like the following code:

from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI


db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

I have tried to replace this code:

db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")

with this:

db = SQLDatabase.from_uri("mssql+pyodbc://{server}/{database}?driver{driver}")

but the following error showed up:

sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: https://sqlalche.me/e/20/rvf5)**

Upvotes: 1

Views: 7029

Answers (1)

Blu
Blu

Reputation: 1

I've tested this and it seems to be working.

uri = 'mssql+pyodbc://DBUsername:DBPassword@ServerName:Port/DBName?driver=ODBC+Driver+17+for+SQL+Server'
db = SQLDatabase.from_uri(uri)

Ref : https://github.com/langchain-ai/langchain/issues/9804

Upvotes: 0

Related Questions