Sql database langchain documentation. This is documentation for LangChain v0.

Sql database langchain documentation. 15 # Main entrypoint into package. prompt import DECIDER_PROMPT, PROMPT, SQL_PROMPTS from langchain. We will be using LangChain for our framework and will be writing in Example from langchain_experimental. Based on those tables, call the normal SQL database chain. SQLDatabaseSequentialChain # class langchain_experimental. ClassesFunctions How to: pass runtime secrets to a runnable LangGraph LangGraph is an extension of LangChain aimed at building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph. sql # SQL Chain interacts with SQL Database. Initialize with a SQLChatMessageHistory instance. """ """Vector SQL Database Chain Retriever""" from typing import Any, Dict, List from langchain_core. schema import Returns: SQLDatabase: An instance of SQLDatabase configured with the provided CnosDB connection details. ⚠️ Security note ⚠️ sql # SQL Chain interacts with SQL Database. 27 # Main entrypoint into package. In this guide we'll go over prompting strategies to improve SQL query generation. , use an LLM to write a summary of the document) for indexing while retaining linkage to the source document. Optionally, use the includesTables or LangChain has two different retrievers that can be used to address this challenge. This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. Each document represents one row of the result. A message can be a question, an answer, a statement, a greeting or any other piece of text that the user or application gives during the conversation. Give the LLM access to a Python environment where it can use libraries like Pandas to interact with the data. These systems will allow us to ask a question about the data in a SQL database and get back a natural language answer. For example, you can build a retriever for a SQL database using text-to-SQL conversion. SQL Database This notebook showcases an agent designed to interact with a SQL databases. engine import Result from pydantic import BaseModel, Field, root_validator, model_validator, ConfigDict from langchain_core. You keep all the strengths of Postgres—transactions, joins, security—while unlocking modern RAG patterns that bring external knowledge into your LLM applications. It offers MySQL, PostgreSQL, and SQL Server database engines. sql_database import SQLDatabase # Initialize SQLDatabase with the engine db = SQLDatabase(engine) How to better prompt when doing SQL question-answering In this guide we'll go over prompting strategies to improve SQL query generation using create_sql_query_chain. from_uri(database_uri=uri) except ImportError: raise ImportError( "cnos-connector package not SQLDatabase # class langchain_community. " "Be sure that the tables actually exist by calling " f"{list_sql_database_tool. tools. For talking to the database, the document loader uses the SQLDatabase utility from the LangChain integration toolkit. from_llm(OpenAI(), db) Security note: Make sure that the database connection uses credentials that are narrowly-scoped to only include the permissions this chain needs. Sep 5, 2024 · In this post I want to explore how one might go about prompt engineering to retrieve more accurate results incorporating a local SQL database, return a SQL how to use LangChain to talk to a local database and return not just a SQL query, but also get the model to run the SQL query and return the answer in text form. Instead, we libSQL Turso is a SQLite-compatible database built on libSQL, the Open Contribution fork of SQLite. LangChain. It is PostgreSQL-compatible, based on Lucene, and inheriting from Elasticsearch. llm import LLMChain from langchain. Instead, we must find ways to dynamically insert into the prompt In order to write valid queries against a database, we need to feed the model the table names, table schemas, and feature values for it to query over. Additionally, it is not guaranteed that the agent won’t perform DML statements on your database given certain questions. utilities. May 30, 2024 · Checked other resources I added a very descriptive title to this question. LangChain is a popular framework for working with AI, Vectors, and embeddings. InfoSQLDatabaseTool [source] # Bases: BaseSQLDatabaseTool, BaseTool Tool for getting metadata about a SQL database. How to deal with large databases when doing SQL question-answering In order to write valid queries against a database, we need to feed the model the table names, table schemas, and feature values for it to query over. These are applications that can answer questions about specific source information. Parameters catalog (str) – The catalog name in the Databricks database. The SQLDatabase class provides a getTableInfo method that can be used to get column information as well as sample data from the table. If not installed, it can be added using pip install databricks-sql-connector. 3. SQLDatabase(engine: Engine, schema: str | None = None, metadata: MetaData | None = None, ignore_tables Example from langchain_experimental. Jun 12, 2025 · Combining LangChain and pgvector gives you a battle-tested, SQL-native vector store with the ergonomic developer experience of LangChain. Each tool has a description. Documentation for LangChain. jsSecurity Security Notice This class generates SQL queries for the given database. Dec 9, 2024 · Load documents by querying database tables supported by SQLAlchemy. They enable use cases such as: Generating queries that will be run based on natural language questions, Creating Convert question to SQL query The first step is to take the user input and convert it to a SQL query. Parameters: session_id (str) – Indicates the id of the same session. Cloud SQL for PostgreSQL is a fully-managed database service that helps you set up, maintain, manage, and administer your PostgreSQL relational databases on Google Cloud Platform. The SQLDatabaseChain can therefore be used with any SQL dialect supported by SQLAlchemy, such as MS SQL, MySQL, MariaDB, PostgreSQL, Oracle SQL, and SQLite. You can also find the documentation for the Python equivalent here. You can use them with the simple invoke method: [docs] def create_sql_query_chain( llm: BaseLanguageModel, db: SQLDatabase, prompt: Optional[BasePromptTemplate] = None, k: int = 5, ) -> Runnable[Union[SQLInput, SQLInputWithTables, Dict[str, Any]], str]: """Create a chain that generates SQL queries. Azure Cosmos DB is the database that powers LangChain Agent: Created with create_sql_agent, which handles the logic of converting natural language into SQL queries. Apr 28, 2025 · This utility enables LangChain agents and chains to query SQL databases, retrieve table information, and execute SQL commands with proper error handling. ListSQLDatabaseTool # class langchain_community. For a detailed walkthrough of LangChain's conversation memory abstractions, visit the How to add message history (memory) LCEL page. db) info_sql_database_tool_description = ( "Input to this tool is a comma-separated list of tables, output is the " "schema and sample rows for those tables. Since Azure Database for PostgreSQL is open-source Postgres, you can use the LangChain's Postgres support to connect to Azure Database for PostgreSQL. base. You can find the FAISS documentation at this page. If not provided, it attempts to fetch from the environment variable ‘DATABRICKS_HOST’. Setup: Install langchain-community. Toolkit: Uses SQLDatabaseToolkit for database interactions. ClassesFunctions sql # SQL Chain interacts with SQL Database. SQL In this guide we'll go over the basic ways to create a Q&A chain and agent over a SQL database. callout-note} The SQLDatabase adapter utility is a wrapper around a database connection. One of the most powerful applications enabled by LLMs is sophisticated question-answering (Q&A) chatbots. from_uri() method to connect to your MS-SQL database. See The FAISS Library paper. I used the GitHub search to find a similar question and For example, you can build a retriever for a SQL database using text-to-SQL conversion. Parameters: llm (BaseLanguageModel) – Language model to use for the agent. Get started with the langchain_sqlserver library with the following tutorials. Jun 15, 2023 · This article will demonstrate how to use a LLM with a SQL database by connecting OpenAI’s GPT-3. The tutorial below is a great way to get Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability. callbacks QuerySQLDataBaseTool # class langchain_community. from langchain_core. Vector Similiarity Search is built into Turso and libSQL as a native datatype, enabling you to store and query vectors directly in the database. Azure SQL provides a dedicated Vector data type that simplifies the creation, storage, and querying of vector embeddings directly within a relational database. The Multi-Vector retriever allows the user to use any document transformation (e. , MySQL, PostgreSQL, Oracle SQL, Databricks, SQLite). A vector store stores embedded data and performs similarity search. This database was created for learning purposes and comes with a decently involved structure and some mock data and we thought it Mar 11, 2024 · Unlock the full potential of database interactions with our guide on Natural Language to SQL using LangChain and LLM. Parameters: query (str | Select) – The query to execute. Please refer to the SQLAlchemy documentation for May 9, 2023 · Applications of LangChain These are some of the applications of LangChain. To mitigate risk of leaking sensitive data, limit permissions to read and scope to the tables that are needed. This will help you get started with the SQL Database toolkit. How to do Text-to-SQL in LangChain?. The main difference between the two is that our agent can query the database in a loop as many time as it needs to answer the question. If agent_type is “tool-calling” then llm is expected to support tool calling. Productionization """Chain for interacting with SQL Database. custom_message_converter (BaseMessageConverter | None LangChain has two different retrievers that can be used to address this challenge. llms import OpenAI, SQLDatabase db = SQLDatabase() db_chain = SQLDatabaseChain. This notebook This is documentation for LangChain v0. callbacks. pydantic_v1 import BaseModel, Field, root_validator from langchain_core. Note that, as this agent is in active development, all answers might not be correct. from_uri(database_uri=uri) except ImportError: raise ImportError( "cnos-connector package not SQLDatabaseSequentialChain # class langchain_experimental. This toolkit contains a the following tools: Input to this tool is a detailed and correct SQL query, output is a result from the database. RECOMMENDED: Load the CSV (s) into a SQL database, and use the approaches outlined in the SQL use case docs. Example from langchain_experimental. You can peruse LangGraph how-to guides here. custom_message_converter (BaseMessageConverter | None Cloud SQL for PostgreSQL is a fully-managed database service that helps you set up, maintain, manage, and administer your PostgreSQL relational databases on Google Cloud Platform. Additionally, it is not guaranteed that the agent won’t perform DML In this guide we'll go over prompting strategies to improve SQL query generation. toolkit. Initializing your database Prepare you database with the relevant tables: [docs] def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit. This guide Feb 3, 2025 · Whether retrieving structured data from SQL, querying live APIs, leveraging vector databases for semantic search, or tapping into knowledge graphs and blockchain data, LangChain provides an # flake8: noqa """Tools for interacting with a SQL database. Querying Datasets with Natural Language LLMs can write SQL queries using natural language. language_models import BaseLanguageModel from langchain_core. The Chain interface makes it easy to create apps that are: Class that represents a SQL database chain in the LangChain framework. LangSmith chains # Chains are easily reusable components linked together. agent_toolkits. We’re excited to announce LangChain integration with Azure SQL Database and SQL database in Microsoft Fabric! LangChain, a powerful tool for building solutions with language models, can be effectively combined with these services to build AI-ready […] RECOMMENDED: Load the CSV (s) into a SQL database, and use the approaches outlined in the SQL use case docs. ⚠️ Security note ⚠️ Building Q&A SQLDatabaseToolkit # class langchain_community. 2. For the current stable version, see this version (Latest). VectorSQLDatabaseChain [source] # Bases: SQLDatabaseChain Chain for interacting with Vector SQL Database. You can access that version of the documentation in the v0. Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability. chains # Chains are easily reusable components linked together. SQLDatabaseToolkit [source] # Bases: BaseToolkit SQLDatabaseToolkit for interacting with SQL databases. It offers MySQL, PostgreSQL, and SQL Server database Apr 27, 2024 · Output for Azure SQL Studio Conclusion By integrating RAG with SQL databases using the combined capabilities of Azure, OpenAI, and LangChain, this approach not only simplifies the data querying Head to Integrations for documentation on built-in integrations with vectorstore providers. Agent uses the description to choose the right tool for the job. Example The LangChain ChatMessageHistory class lets the application save messages to a database and retrieve them when needed to formulate further answers. It extends the BaseChain class and implements the functionality specific to a SQL database chain. The main advantages of using the SQL Agent are: It can answer questions based on the databases' schema as well as on the databases' content (like describing a specific table). host (Optional[str]) – The Databricks workspace hostname, excluding ‘ https:// ’ part. LangChain comes with a number of built-in chains and agents that are compatible with any SQL dialect supported by SQLAlchemy (e. For detailed documentation of all SQLDatabaseToolkit features and configurations head to the API reference. We will be using LangChain for our framework and will be writing in Python. This example shows how to load and use an agent with a SQL toolkit. Aug 1, 2023 · The SQL Analyst is connected to a MySQL database called sakila. ClassesFunctions A Streamlit app that allows users to query SQLite or MySQL databases using LangChain agents powered by Groq's LLM for natural language processing. Dec 9, 2024 · Return type Runnable [Union [SQLInput, SQLInputWithTables, Dict [str, Any]], str] Examples using create_sql_query_chain ¶ Build a Question/Answering system over SQL data How to better prompt when doing SQL question-answering How to deal with large databases when doing SQL question-answering How to do query validation as part of SQL question Aug 21, 2023 · A step-by-step guide to building a LangChain enabled SQL database question answering agent. Jun 24, 2024 · LangChain offers an SQL Agent that allows for more flexible interactions with SQL databases. At a high-level Oct 12, 2023 · In this article, we will look at how to use LangChain to connect to our SQL database, retrieve contextual information, pass the User Query along with the Context to the LLM and generate accurate Faiss Facebook AI Similarity Search (FAISS) is a library for efficient similarity search and clustering of dense vectors. deprecation import deprecated from langchain_core. session_id_field_name (str) – The name of field of session_id. Initialize the tool. ListSQLDatabaseTool [source] # Bases: BaseSQLDatabaseTool, BaseTool Tool for getting tables names. chains. js supports using a local libSQL, or remote Turso database as a vector store, and provides a simple API to interact with it. To learn more about agents, head to the Agents Modules. tool. """ try: from cnosdb_connector import make_cnosdb_langchain_uri uri = make_cnosdb_langchain_uri(url, user, password, tenant, database) return cls. 1, which is no longer actively maintained. 🌐 SQL Database - Databricks SQL is integrated with SQLDatabase in LangChain, allowing you to access the auto-optimizing, exceptionally performant data warehouse. LangGraph documentation is currently hosted on a separate site. *Security Note*: This chain generates SQL queries for the given database. Chains encode a sequence of calls to components like models, document retrievers, other Chains, etc. langchain: 0. We will cover: How the dialect of the LangChain SQLDatabase impacts the prompt of the chain; How to format schema information into Example code for building applications with LangChain, with an emphasis on more applied and end-to-end examples than contained in the main documentation. It also includes supporting code for evaluation and parameter tuning. Aug 16, 2023 · This blog delves into the intriguing synergy between LangChain, an innovative language interface, and a robust language model, to effortlessly query the Oracle Database. It uses the example Chinook Database, and demonstrates those features: Query using SQL Query using SQLAlchemy selectable Fetch modes cursor, all Load documents by querying database tables supported by SQLAlchemy. The agent builds off of SQLDatabaseChain and is designed to answer more general questions about a database, as well as recover from errors. Class that represents a SQL database chain in the LangChain framework. Sep 12, 2023 · Under the hood, the LangChain SQL Agent uses a MRKL (pronounced Miracle)-based approach, and queries the database schema and example rows and uses these to generate SQL queries, which it then executes to pull back the results you're asking for. sql import SQLDatabaseChain from langchain_community. We'll largely focus on methods for getting relevant database-specific information in your prompt. Extend your database application to build AI-powered experiences leveraging Cloud SQL's Langchain integrations. LangChain supports using Supabase as a vector store, using the pgvector extension. QuerySQLDataBaseTool [source] # Bases: BaseSQLDatabaseTool, BaseTool Tool for querying a SQL database. Quickstart In this guide we'll go over the basic ways to create a Q&A chain and agent over a SQL database. This project is a Streamlit-based web application that allows users to interact with SQL databases (SQLite or MySQL) using the LangChain framework and Mar 5, 2024 · The LangChain Vector stores integration is available for Google Cloud databases with vector support, including AlloyDB, Cloud SQL for PostgreSQL, Memorystore for Redis, and Spanner. VectorSQLDatabaseChain # class langchain_experimental. SQL Database ::: {. documents import Document from langchain_core. You can use them with the simple invoke method: 4 days ago · The LangChain ChatMessageHistory class lets the application save messages to a database and retrieve them when needed to formulate further answers. Evaluation LangSmith helps you evaluate the performance of your LLM applications. Nov 19, 2024 · In today’s data-driven world, the ability to seamlessly integrate various technologies is crucial for efficient data management and analysis. Construct a SQL agent from an LLM and toolkit or database. It is designed to answer more general questions about a database, as well as recover from errors. The chain is as follows: 1. LangSmith documentation is hosted on a separate site. The function create_sql_agent you've used in your code is designed to construct a SQL agent from a language model and a toolkit or database. Feb 20, 2024 · Based on the information you've provided and the similar issue I found in the LangChain repository, it seems like you're on the right track with using the SQLDatabase. May 16, 2024 · Let’s talk about ways Q&A chain can work on SQL database. Azure Cosmos DB No SQL This notebook shows you how to leverage this integrated vector database to store documents in collections, create indicies and perform vector search queries using approximate nearest neighbor algorithms such as COS (cosine distance), L2 (Euclidean distance), and IP (inner product) to locate documents close to the query vectors. SQL Database Agent # This notebook showcases an agent designed to interact with a sql databases. vector_sql. Azure SQL Database tools # Tools are classes that an Agent uses to interact with the world. callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun Large databases In order to write valid queries against a database, we need to feed the model the table names, table schemas, and feature values for it to query over. LangChain’s document loaders, index-related chains, and output parser help load and parse the data to generate results. You can peruse LangSmith tutorials here. engine import Result from langchain_core. 5 to a postgres database. base import Chain from langchain. LangChain simplifies every stage of the LLM application lifecycle: Development: Build your applications using LangChain's open-source components and third-party integrations. Apr 2, 2025 · Learn about the LangChain integrations that facilitate the development and deployment of large language models (LLMs) on Databricks. The main advantages of using SQL Agents are: It can answer questions based on the databases schema as well as on the databases content (like describing a specific table). schema (str) – The schema name in the catalog. Oracle autonomous database is a cloud database that uses machine learning to automate database tuning, security, backups, updates, and other routine management tasks traditionally performed by DBAs. Dec 9, 2024 · [docs] def create_sql_query_chain( llm: BaseLanguageModel, db: SQLDatabase, prompt: Optional[BasePromptTemplate] = None, k: int = 5, ) -> Runnable[Union[SQLInput, SQLInputWithTables, Dict[str, Any]], str]: """Create a chain that generates SQL queries. I searched the LangChain documentation with the integrated search. Parameters: catalog (str) – The catalog name in the Databricks database. It contains algorithms that search in sets of vectors of any size, up to ones that possibly do not fit in RAM. g. QuerySQLDatabaseTool [source] # Bases: BaseSQLDatabaseTool, BaseTool Tool for querying a SQL database. retrievers import BaseRetriever from langchain_experimental. Agents LangChain offers a number of tools and functions that allow you to create SQL Agents which can provide a more flexible way of interacting with SQL databases. For talking to SQL databases, it uses the SQLAlchemy Core API . table_name (str) – Table name used to save data. Let's select a chat model for our application: SQLDatabaseToolkit # class langchain_community. It offers PostgreSQL, PostgreSQL, and SQL Server database engines. 2 docs. Regardless of the underlying retrieval system, all retrievers in LangChain share a common interface. 2. This guide provides explanations of the key concepts behind the LangChain framework and AI applications more broadly. These applications use a technique known as Retrieval Augmented Generation, or RAG. ClassesFunctions Agents LangChain has a SQL Agent which provides a more flexible way of interacting with SQL Databases than a chain. SurrealDB is a unified, multi-model database purpose-built for AI systems. Extend your database application to build AI-powered experiences leveraging Cloud SQL for PostgreSQL's Langchain integrations. May 15, 2024 · Here, we offer a step-by-step guide on how to use LangChain to implement text-to-SQL, and how to handle any challenges that come your way. Load documents by querying database tables supported by SQLAlchemy. It seamlessly integrates with LangChain, and you can use it to inspect and debug individual steps of your chains as you build. Feb 19, 2024 · I hope all's been well on your side! Yes, it is indeed possible to create an SQL agent in the latest version of LangChain to query tables on Google BigQuery. _api. prompts import PromptTemplate template = '''Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. toolkit (Optional[SQLDatabaseToolkit]) – SQLDatabaseToolkit for the agent to use. This method requires the ‘databricks-sql-connector’ package. This system will allow us to ask a question about the data in an SQL database and get back a natural language answer. InfoSQLDatabaseTool # class langchain_community. # flake8: noqa """Tools for interacting with a SQL database. For detailed documentation of all SqlToolkit features and configurations head to the API reference. SQLDatabaseSequentialChain [source] # Bases: Chain Chain for querying SQL database that is a sequential chain. Use LangGraph to build stateful agents with first-class streaming and human-in-the-loop support. To reliably obtain SQL queries (absent markdown formatting and explanations or clarifications), we will make use of LangChain's structured output abstraction. Class hierarchy: This tutorial previously used the RunnableWithMessageHistory abstraction. This eliminates the need for separate vector databases and related integrations, increasing the security of your solutions while reducing the overall complexity. To set up this agent, we use the create_sql_agent function, which includes the SQLDatabaseToolkit. sql_database. sql. SQL This example demonstrates the use of Runnables with questions and more on a SQL database. Updated to use the langchain_sqlserver (0. """ list_sql_database_tool = ListSQLDatabaseTool(db=self. Introduction LangChain is a framework for developing applications powered by large language models (LLMs). How to: pass runtime secrets to a runnable LangGraph LangGraph is an extension of LangChain aimed at building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph. Returns: SQLDatabase: An instance of SQLDatabase configured with the provided CnosDB connection details. It can recover from errors by running a generated query QuerySQLDatabaseTool # class langchain_community. This allows a natural language query (string) to be transformed into a SQL query behind the scenes. For talking to the database, the document loader uses the `SQLDatabase` utility from the LangChain integration toolkit. SQL One of the most common types of databases that we can build Q&A systems for are SQL databases. 1. """ from __future__ import annotations import warnings from typing import Any, Dict, List, Optional from langchain. Under the hood, LangChain uses SQLAlchemy to connect to SQL databases. This is useful in cases where the number of tables in the database is large. All the tutorials works with Azure SQL or SQL Server 2025, using the newly introduced Vector type. ) library. When there are many tables, columns, and/or high-cardinality columns, it becomes impossible for us to dump the full information about our database in every prompt. Generating synthetic tabular data Before diving into the example, let's talk about synthetic data. The SQLDatabase class provides a get_table_info method that can be 3 days ago · The LangChain ChatMessageHistory class lets the application save messages to a database and retrieve them when needed to formulate further answers. manager import ( AsyncCallbackManagerForRetrieverRun, CallbackManagerForRetrieverRun, ) from langchain_core. It can recover from errors by running a generated query, catching the traceback and regenerating it Jun 15, 2023 · This article will demonstrate how to use a LLM with a SQL database by connecting OpenAI’s GPT-3. Dec 9, 2024 · Class method to create an SQLDatabase instance from a Databricks connection. MLflow's LangChain Integration streamlines the process of developing and operating modern compound ML systems. , and provide a simple interface to this sequence. name} first SQL Chain example # This example demonstrates the use of the SQLDatabaseChain for answering questions over a database. Based on the query, determine which tables to use. vector_sql import langchain: 0. """ from typing import Any, Dict, Optional, Sequence, Type, Union from sqlalchemy. connection_string (str | None) – String parameter configuration for connecting to the database. The Chain interface makes it easy to create apps that are: Sep 28, 2024 · Initialize SQLDatabase: In Langchain, you need to create an instance of SQLDatabase: from langchain. 📄️ Google SQL for PostgreSQL Google Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability. The SQLDatabase class provides a get_table_info method that can be Contribute to ritigit7/SQLDatabase-Q-A-with-LangChain development by creating an account on GitHub. It combines structured and unstructured data (including vector search, graph traversal, relational queries, full-text search, document storage, and time-series data) into a single ACID-compliant engine, scaling from a 3 MB edge binary to petabyte-scale clusters in the cloud. [docs] class SQLDatabaseLoader(BaseLoader): """ Load documents by querying database tables supported by SQLAlchemy. ::: This notebook shows how to use the utility to access an SQLite database. Parameters query (Union[str, Select]) – The query to execute. SQLDatabaseToolkit # class langchain_community. CrateDB is a distributed and scalable SQL database for storing and analyzing massive amounts of data in near real-time, even with complex queries. hosylrn jzuclwz xhec vqvgu cjhuqh fldbh zyjd muihbv mcjg smnfu