How to Connect Langchain Database Agent with SqlServer – Langchain

Liam Thompson
langchain llama-cpp-python py-langchain sql-server

Quick Fix: Utilize the SQLDatabase.from_uri() method to establish a connection to the SQLServer database, specifying the appropriate URI.

The Problem:

It is necessary to connect Langchain’s Database Agent with a Microsoft SQL Server database. However, the documentation solely focuses on establishing a connection with SQLite, and no guidance is provided for SQL Server integration. Attempts to replace the SQLite connection code with the appropriate SQL Server connection string resulted in an error: ‘sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) (‘IM002’, ‘[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)’) ‘. Provide a comprehensive solution that addresses this error and demonstrates how to successfully connect Langchain’s Database Agent with a SQL Server database.

The Solutions:

Solution 1: Create connection with SQL Server Database

To connect your Langchain Database Agent with a SQL Server, you can use the following steps:

  1. Obtain Database Credentials:

    Gather the necessary credentials, including the database server name, database name, username, and password, to establish a connection to your SQL Server database.
  2. Construct Connection URI:

    Construct the connection URI using the mssql+pyodbc format. The general syntax is as follows:

    uri = "mssql+pyodbc://DBUsername:DBPassword@ServerName:Port/DBName?driver=ODBC+Driver+17+for+SQL+Server"

    Replace DBUsername, DBPassword, ServerName, Port, and DBName with your actual values. Ensure that the ODBC driver is properly installed on your system.
  3. Instantiate SQLDatabase Object:

    Instantiate the SQLDatabase object using the SQLDatabase.from_uri() method. Pass the constructed connection URI as an argument to establish the connection.

    db = SQLDatabase.from_uri(uri)
  4. Utilize SQLDatabaseToolkit:

    Instantiate the SQLDatabaseToolkit with the connected SQLDatabase object and the desired Language Model (LLM). This toolkit provides the necessary functionalities to interact with the SQL Server database.

    toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))
  5. Integrate with Agent:

    Create an instance of the AgentExecutor and incorporate the SQLDatabaseToolkit. This enables seamless data retrieval and manipulation from the SQL Server database within your Langchain Agent.

    agent_executor = AgentExecutor(agent_type=AgentType.CHAT, toolkit=toolkit)

By following these steps, you can successfully connect your Langchain Database Agent to a SQL Server database, enabling data access and manipulation from within your agent.


How to configure connection parameters to SQLServer

Configure connection parameters as mssql+pyodbc with username, password, server, port and database name.

Where to find the ODBC driver for SQLServer?

What is the correct naming for the connection string

The correct naming should be mssql+pyodbc

Video Explanation:

The following video, titled "LangChain SQL Database agent with OpenAI to query PostgreSQL ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

In this video we discover how to set up the LangChain SQL Database Agent with PostgreSQL and OpenAI using LangChain.