Langchain sql agent examples. Action: list_tables_sql_db Action Input: "" .
- Langchain sql agent examples. example as a template. Be careful running it on sensitive data! This uses the example Chinook database. Agents LangChain has a SQL Agent which provides a more flexible way of interacting with SQL Databases than a chain. env. Let’s select a chat model for our application: Make sure the create an . from_uri("sqlite:///Chinook. By the end of this tutorial, you’ll have a functional SQL agent that can answer questions about your data using natural language. guide/2-sample-databases-sqlite/, placing the . env using . Mar 10, 2025 · We will explain how to implement an SQL Agent using LangChain, OpenAI API, and DuckDB , and how to turn it into an application with Morph . db"). test-2. Note that this approach is lightweight, but ephemeral and not thread-safe. sql. db and instantiate the database via db = SQLDatabase. We'll largely focus on methods for getting relevant database-specific information in your prompt. In this tutorial we In this guide we'll go over prompting strategies to improve SQL query generation using createsqlquerychain. Below we will use the requests library to pull the . We will cover implementations using both chains and agents. This is often achieved via tool-calling. Build resilient language agents as graphs. Aug 21, 2023 · In this tutorial, we will walk through step-by-step, the creation of a LangChain enabled, large language model (LLM) driven, agent that can use a SQL database to answer questions. After executing actions, the results can be fed back into the LLM to determine whether more actions are needed, or whether it is okay to finish. extra_tools (Sequence[BaseTool]) – Additional tools to give to agent on top of the ones that come with SQLDatabaseToolkit. In this first example we will use slightly different type of agent - SQL Agent which can be instantiated with it's own method create_sql_agent. This app will generate SQL queries using an LLM, execute them in DuckDB, and use the results to answer user questions. db (Optional[SQLDatabase]) – SQLDatabase from which to create a SQLDatabaseToolkit. agent_toolkits. You can read more about them in the documentation. > Entering new AgentExecutor chain Action: list_tables_sql_db Action Input: "" . create_sql_agent # langchain_community. This repository demonstrates the use of LangChain and LangGraph for SQL query generation, execution and validation. base. 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). Toolkit is created using ‘db’ and Jun 17, 2025 · Build an Agent LangChain supports the creation of agents, or systems that use LLMs as reasoning engines to determine which actions to take and the inputs necessary to perform the action. It can recover from errors by running a generated query, catching the traceback and regenerating it Jun 21, 2023 · In our last blog post we discussed the topic of connecting a PostGres database to Large Language Model (LLM) and provided an example of how to use LangChain SQLChain to connect and ask questions 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. create_sql_agent( llm: BaseLanguageModel, toolkit: SQLDatabaseToolkit | None = None, agent_type: AgentType | Literal['openai-tools', 'tool-calling'] | None = None, callback_manager: BaseCallbackManager | None = None, prefix: str | None = None, suffix: str | None = None, format_instructions: str | None = None, input_variables: List Check out some other full examples of apps that utilize LangChain + Streamlit: Auto-graph - Build knowledge graphs from user-input text (Source code) Web Explorer - Retrieve and summarize insights from the web (Source code) LangChain Teacher - Learn LangChain from an LLM tutor (Source code) Text Splitter Playground - Play with various types of text splitting for RAG (Source code) Tweet . This method uses toolkit instead of simple list of tools. py: Basic sample to store vectors, content and metadata into SQL Server or Azure SQL and then do simple similarity searches. To set it up follow the instructions on https://database. Contribute to langchain-ai/langgraph development by creating an account on GitHub. These systems will allow us to ask a question about the data in a database and get back a natural language answer. This setup allows you to interact with complex databases using natural language, making data analysis more accessible to everyone, regardless of their SQL expertise. Convert question to SQL query The first step is to take the user input and convert it to a SQL query. If you'd prefer, you can follow the instructions to save the file locally as Chinook. In this guide we'll go over the basic ways to create a Q&A system over tabular data in databases. sql file and create an in-memory SQLite database. To reliably obtain SQL queries (absent markdown formatting and explanations or clarifications), we will make use of LangChain’s structured output abstraction. Dec 13, 2024 · In this post, we’ll walk you through creating a LangChain agent that can understand questions in natural language (NLP), dynamically generate SQL queries based on your input, fetch results from agent_executor_kwargs (Optional[Dict[str, Any]]) – Arbitrary additional AgentExecutor args. Samples on how to use the langchain_sqlserver library with SQL Server or Azure SQL as a vector store are: test-1. db file in a notebooks folder at the root of this repository. py: Read books reviews from a file, store it in SQL Server or Azure SQL, and then do Aug 30, 2024 · Using LangChain and OpenAI in conjunction with an SQL database can simplify the process of querying and analyzing data. Other agents will be instantiated in more generic way as we will see below in other examples. noys regx ackacypx ircqyk qlqomy fczlzpyl uotx xbot iqb dkxbok