0

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)**

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Have you contacted the vendor to see if they even support SQL Server? That might be the reason you don't see an example in the documentation. – Aaron Bertrand Aug 26 '23 at 18:00
  • @AaronBertrand Indeed they do – Mutasim Al-Mualimi Aug 26 '23 at 18:06
  • 1
    Ok, so did you ask their support team _how to do it_? I've never heard of this product before and I doubt many others here have either. So I would still start with the vendor. – Aaron Bertrand Aug 26 '23 at 18:09
  • 1
    Some basic guidance is here [https://docs.sqlalchemy.org/en/20/core/engines.html#microsoft-sql-server] (`SQLDatabase is a wrapper around a SQLAlchemy engine, as the error message suggests). – snakecharmerb Aug 26 '23 at 18:49
  • Have you tried `driver={driver}`? And what's stored in the `driver` variable? e.g.: if you have ODBC Driver 17 for SQL Server installed then it should end up with `driver=ODBC Driver 17 for SQL Server` encoded in the URI as `driver=ODBC+Driver+17+for+SQL+Server` Ref: [SQLAlchemy # Microsoft SQL Server](https://docs.sqlalchemy.org/en/20/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pyodbc) – AlwaysLearning Aug 27 '23 at 01:18

0 Answers0