seansullivan/Langchain-Docs huggingface dataset:
## sql-llamacpp

This template enables a user to interact with a SQL database using natural language.

It uses [Mistral-7b](https://mistral.ai/news/announcing-mistral-7b/) via [llama.cpp](https://github.com/ggerganov/llama.cpp) to run inference locally on a Mac laptop.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

To set up the environment, use the following steps:

```
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.shbash Miniforge3-MacOSX-arm64.shconda create -n llama python=3.9.16conda activate /Users/rlm/miniforge3/envs/llamaCMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 pip install -U llama-cpp-python --no-cache-dir
```

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package sql-llamacpp
```

If you want to add this to an existing project, you can just run:

```
langchain app add sql-llamacpp
```

And add the following code to your `server.py` file:

```
from sql_llamacpp import chain as sql_llamacpp_chainadd_routes(app, sql_llamacpp_chain, path="/sql-llamacpp")
```

The package will download the Mistral-7b model from [here](https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF). You can select other files and specify their download path (browse [here](https://huggingface.co/TheBloke)).

This package includes an example DB of 2023 NBA rosters. You can see instructions to build this DB [here](https://github.com/facebookresearch/llama-recipes/blob/main/demo_apps/StructuredLlama.ipynb).

(Optional) Configure LangSmith for tracing, monitoring and debugging LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server running locally at [http://localhost:8000](http://localhost:8000/)

You can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) You can access the playground at [http://127.0.0.1:8000/sql-llamacpp/playground](http://127.0.0.1:8000/sql-llamacpp/playground)

You can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/sql-llamacpp")
```

* * *

#### Help us out by providing feedback on this documentation page:
## summarize-anthropic

This template uses Anthropic's `claude-3-sonnet-20240229` to summarize long documents.

It leverages a large context window of 100k tokens, allowing for summarization of documents over 100 pages.

You can see the summarization prompt in `chain.py`.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Set the `ANTHROPIC_API_KEY` environment variable to access the Anthropic models.

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package summarize-anthropic
```

If you want to add this to an existing project, you can just run:

```
langchain app add summarize-anthropic
```

And add the following code to your `server.py` file:

```
from summarize_anthropic import chain as summarize_anthropic_chainadd_routes(app, summarize_anthropic_chain, path="/summarize-anthropic")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/summarize-anthropic/playground](http://127.0.0.1:8000/summarize-anthropic/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/summarize-anthropic")
```

* * *

#### Help us out by providing feedback on this documentation page:
## sql-ollama

This template enables a user to interact with a SQL database using natural language.

It uses [Zephyr-7b](https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha) via [Ollama](https://ollama.ai/library/zephyr) to run inference locally on a Mac laptop.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Before using this template, you need to set up Ollama and SQL database.

1.  Follow instructions [here](https://python.langchain.com/docs/integrations/chat/ollama) to download Ollama.
    
2.  Download your LLM of interest:
    
    *   This package uses `zephyr`: `ollama pull zephyr`
    *   You can choose from many LLMs [here](https://ollama.ai/library)
3.  This package includes an example DB of 2023 NBA rosters. You can see instructions to build this DB [here](https://github.com/facebookresearch/llama-recipes/blob/main/demo_apps/StructuredLlama.ipynb).
    

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package sql-ollama
```

If you want to add this to an existing project, you can just run:

```
langchain app add sql-ollama
```

And add the following code to your `server.py` file:

```
from sql_ollama import chain as sql_ollama_chainadd_routes(app, sql_ollama_chain, path="/sql-ollama")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/sql-ollama/playground](http://127.0.0.1:8000/sql-ollama/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/sql-ollama")
```

* * *

#### Help us out by providing feedback on this documentation page:
## xml-agent

This package creates an agent that uses XML syntax to communicate its decisions of what actions to take. It uses Anthropic's Claude models for writing XML syntax and can optionally look up things on the internet using DuckDuckGo.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Two environment variables need to be set:

*   `ANTHROPIC_API_KEY`: Required for using Anthropic

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package xml-agent
```

If you want to add this to an existing project, you can just run:

```
langchain app add xml-agent
```

And add the following code to your `server.py` file:

```
from xml_agent import agent_executor as xml_agent_chainadd_routes(app, xml_agent_chain, path="/xml-agent")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/xml-agent/playground](http://127.0.0.1:8000/xml-agent/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/xml-agent")
```

* * *

#### Help us out by providing feedback on this documentation page:
## vertexai-chuck-norris

This template makes jokes about Chuck Norris using Vertex AI PaLM2.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

First, make sure you have a Google Cloud project with an active billing account, and have the [gcloud CLI installed](https://cloud.google.com/sdk/docs/install).

Configure [application default credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc):

```
gcloud auth application-default login
```

To set a default Google Cloud project to use, run this command and set [the project ID](https://support.google.com/googleapi/answer/7014113?hl=en) of the project you want to use:

```
gcloud config set project [PROJECT-ID]
```

Enable the [Vertex AI API](https://console.cloud.google.com/apis/library/aiplatform.googleapis.com) for the project:

```
gcloud services enable aiplatform.googleapis.com
```

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package pirate-speak
```

If you want to add this to an existing project, you can just run:

```
langchain app add vertexai-chuck-norris
```

And add the following code to your `server.py` file:

```
from vertexai_chuck_norris.chain import chain as vertexai_chuck_norris_chainadd_routes(app, vertexai_chuck_norris_chain, path="/vertexai-chuck-norris")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/vertexai-chuck-norris/playground](http://127.0.0.1:8000/vertexai-chuck-norris/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/vertexai-chuck-norris")
```
## stepback-qa-prompting

This template replicates the "Step-Back" prompting technique that improves performance on complex questions by first asking a "step back" question.

This technique can be combined with regular question-answering applications by doing retrieval on both the original and step-back question.

Read more about this in the paper [here](https://arxiv.org/abs/2310.06117) and an excellent blog post by Cobus Greyling [here](https://cobusgreyling.medium.com/a-new-prompt-engineering-technique-has-been-introduced-called-step-back-prompting-b00e8954cacb)

We will modify the prompts slightly to work better with chat models in this template.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package stepback-qa-prompting
```

If you want to add this to an existing project, you can just run:

```
langchain app add stepback-qa-prompting
```

And add the following code to your `server.py` file:

```
from stepback_qa_prompting.chain import chain as stepback_qa_prompting_chainadd_routes(app, stepback_qa_prompting_chain, path="/stepback-qa-prompting")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/stepback-qa-prompting/playground](http://127.0.0.1:8000/stepback-qa-prompting/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/stepback-qa-prompting")
```
## Use cases

This section contains walkthroughs and techniques for common end-to-end use tasks.

If you're looking to build something specific or are more of a hands-on learner, try one out! While they reference building blocks that are explained in greater detail in other sections, we absolutely encourage folks to get started by going through them and picking apart the code in a real-world context.

Or, if you prefer to look at the fundamentals first, you can check out the sections on [Expression Language](https://python.langchain.com/docs/expression_language/get_started/) and the various [components](https://python.langchain.com/docs/modules/) LangChain provides for more background knowledge.

[

## 🗃️ Q&A with RAG

6 items

](https://python.langchain.com/docs/use_cases/question_answering/)

[

## 🗃️ Extracting structured output

4 items

](https://python.langchain.com/docs/use_cases/extraction/)

[

## 🗃️ Chatbots

5 items

](https://python.langchain.com/docs/use_cases/chatbots/)

[

## 🗃️ Tool use and agents

8 items

](https://python.langchain.com/docs/use_cases/tool_use/)

[

## 🗃️ Query analysis

4 items

](https://python.langchain.com/docs/use_cases/query_analysis/)

[

## 🗃️ Q&A over SQL + CSV

7 items

](https://python.langchain.com/docs/use_cases/sql/)

[

## 🗃️ More

6 items

](https://python.langchain.com/docs/use_cases/graph/)
## sql-research-assistant

This package does research over a SQL database

## Usage[​](#usage "Direct link to Usage")

This package relies on multiple models, which have the following dependencies:

*   OpenAI: set the `OPENAI_API_KEY` environment variables
*   Ollama: [install and run Ollama](https://python.langchain.com/docs/integrations/chat/ollama)
*   llama2 (on Ollama): `ollama pull llama2` (otherwise you will get 404 errors from Ollama)

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package sql-research-assistant
```

If you want to add this to an existing project, you can just run:

```
langchain app add sql-research-assistant
```

And add the following code to your `server.py` file:

```
from sql_research_assistant import chain as sql_research_assistant_chainadd_routes(app, sql_research_assistant_chain, path="/sql-research-assistant")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/sql-research-assistant/playground](http://127.0.0.1:8000/sql-research-assistant/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/sql-research-assistant")
```

* * *

#### Help us out by providing feedback on this documentation page:
## Chatbots

## Overview[​](#overview "Direct link to Overview")

Chatbots are one of the most popular use-cases for LLMs. The core features of chatbots are that they can have long-running, stateful conversations and can answer user questions using relevant information.

## Architectures[​](#architectures "Direct link to Architectures")

Designing a chatbot involves considering various techniques with different benefits and tradeoffs depending on what sorts of questions you expect it to handle.

For example, chatbots commonly use [retrieval-augmented generation](https://python.langchain.com/docs/use_cases/question_answering/), or RAG, over private data to better answer domain-specific questions. You also might choose to route between multiple data sources to ensure it only uses the most topical context for final question answering, or choose to use a more specialized type of chat history or memory than just passing messages back and forth.

![Image description](https://python.langchain.com/assets/images/chat_use_case-eb8a4883931d726e9f23628a0d22e315.png)

Optimizations like this can make your chatbot more powerful, but add latency and complexity. The aim of this guide is to give you an overview of how to implement various features and help you tailor your chatbot to your particular use-case.

## Table of contents[​](#table-of-contents "Direct link to Table of contents")

*   [Quickstart](https://python.langchain.com/docs/use_cases/chatbots/quickstart/): We recommend starting here. Many of the following guides assume you fully understand the architecture shown in the Quickstart.
*   [Memory management](https://python.langchain.com/docs/use_cases/chatbots/memory_management/): This section covers various strategies your chatbot can use to handle information from previous conversation turns.
*   [Retrieval](https://python.langchain.com/docs/use_cases/chatbots/retrieval/): This section covers how to enable your chatbot to use outside data sources as context.
*   [Tool usage](https://python.langchain.com/docs/use_cases/chatbots/tool_usage/): This section covers how to turn your chatbot into a conversational agent by adding the ability to interact with other systems and APIs using tools.
## Guidelines

The quality of extraction results depends on many factors.

Here is a set of guidelines to help you squeeze out the best performance from your models:

*   Set the model temperature to `0`.
*   Improve the prompt. The prompt should be precise and to the point.
*   Document the schema: Make sure the schema is documented to provide more information to the LLM.
*   Provide reference examples! Diverse examples can help, including examples where nothing should be extracted.
*   If you have a lot of examples, use a retriever to retrieve the most relevant examples.
*   Benchmark with the best available LLM/Chat Model (e.g., gpt-4, claude-3, etc) – check with the model provider which one is the latest and greatest!
*   If the schema is very large, try breaking it into multiple smaller schemas, run separate extractions and merge the results.
*   Make sure that the schema allows the model to REJECT extracting information. If it doesn’t, the model will be forced to make up information!
*   Add verification/correction steps (ask an LLM to correct or verify the results of the extraction).

## Benchmark[​](#benchmark "Direct link to Benchmark")

*   Create and benchmark data for your use case using [LangSmith 🦜️🛠️](https://docs.smith.langchain.com/).
*   Is your LLM good enough? Use [langchain-benchmarks 🦜💯](https://github.com/langchain-ai/langchain-benchmarks) to test out your LLM using existing datasets.

## Keep in mind! 😶‍🌫️[​](#keep-in-mind "Direct link to Keep in mind! 😶‍🌫️")

*   LLMs are great, but are not required for all cases! If you’re extracting information from a single structured source (e.g., linkedin), using an LLM is not a good idea – traditional web-scraping will be much cheaper and reliable.
    
*   **human in the loop** If you need **perfect quality**, you’ll likely need to plan on having a human in the loop – even the best LLMs will make mistakes when dealing with complex extraction tasks.
    

* * *

#### Help us out by providing feedback on this documentation page:
Besides raw text data, you may wish to extract information from other file types such as PowerPoint presentations or PDFs.

You can use LangChain [document loaders](https://python.langchain.com/docs/modules/data_connection/document_loaders/) to parse files into a text format that can be fed into LLMs.

Here, we’ll be looking at MIME-type based parsing which is often useful for extraction based applications if you’re writing server code that accepts user uploaded files.

In this case, it’s best to assume that the file extension of the file provided by the user is wrong and instead infer the mimetype from the binary content of the file.

Let’s download some content. This will be an HTML file, but the code below will work with other file types.

```
import magicfrom langchain.document_loaders.parsers import BS4HTMLParser, PDFMinerParserfrom langchain.document_loaders.parsers.generic import MimeTypeBasedParserfrom langchain.document_loaders.parsers.txt import TextParserfrom langchain_community.document_loaders import Blob# Configure the parsers that you want to use per mime-type!HANDLERS = {    "application/pdf": PDFMinerParser(),    "text/plain": TextParser(),    "text/html": BS4HTMLParser(),}# Instantiate a mimetype based parser with the given parsersMIMETYPE_BASED_PARSER = MimeTypeBasedParser(    handlers=HANDLERS,    fallback_parser=None,)mime = magic.Magic(mime=True)mime_type = mime.from_buffer(data)# A blob represents binary data by either reference (path on file system)# or value (bytes in memory).blob = Blob.from_data(    data=data,    mime_type=mime_type,)parser = HANDLERS[mime_type]documents = parser.parse(blob=blob)
```
## Graphs

One of the common types of databases that we can build Q&A systems for are graph databases. LangChain comes with a number of built-in chains and agents that are compatible with graph query language dialects like Cypher, SparQL, and others (e.g., Neo4j, MemGraph, Amazon Neptune, Kùzu, OntoText, Tigergraph). They enable use cases such as:

*   Generating queries that will be run based on natural language questions,
*   Creating chatbots that can answer questions based on database data,
*   Building custom dashboards based on insights a user wants to analyze,

and much more.

## ⚠️ Security note ⚠️[​](#security-note "Direct link to ⚠️ Security note ⚠️")

Building Q&A systems of graph databases might require executing model-generated database queries. There are inherent risks in doing this. Make sure that your database connection permissions are always scoped as narrowly as possible for your chain/agent’s needs. This will mitigate though not eliminate the risks of building a model-driven system. For more on general security best practices, [see here](https://python.langchain.com/docs/security/).

![graphgrag_usecase.png](https://python.langchain.com/assets/images/graph_usecase-34d891523e6284bb6230b38c5f8392e5.png)

> Employing database query templates within a semantic layer provides the advantage of bypassing the need for database query generation. This approach effectively eradicates security vulnerabilities linked to the generation of database queries.

## Quickstart[​](#quickstart "Direct link to Quickstart")

Head to the **[Quickstart](https://python.langchain.com/docs/use_cases/graph/quickstart/)** page to get started.

## Advanced[​](#advanced "Direct link to Advanced")

Once you’ve familiarized yourself with the basics, you can head to the advanced guides:

*   [Prompting strategies](https://python.langchain.com/docs/use_cases/graph/prompting/): Advanced prompt engineering techniques.
*   [Mapping values](https://python.langchain.com/docs/use_cases/graph/mapping/): Techniques for mapping values from questions to database.
*   [Semantic layer](https://python.langchain.com/docs/use_cases/graph/semantic/): Techniques for implementing semantic layers.
*   [Constructing graphs](https://python.langchain.com/docs/use_cases/graph/constructing/): Techniques for constructing knowledge graphs.
## Construct Filters

We may want to do query analysis to extract filters to pass into retrievers. One way we ask the LLM to represent these filters is as a Pydantic model. There is then the issue of converting that Pydantic model into a filter that can be passed into a retriever.

This can be done manually, but LangChain also provides some “Translators” that are able to translate from a common syntax into filters specific to each retriever. Here, we will cover how to use those translators.

```
from typing import Optionalfrom langchain.chains.query_constructor.ir import (    Comparator,    Comparison,    Operation,    Operator,    StructuredQuery,)from langchain.retrievers.self_query.chroma import ChromaTranslatorfrom langchain.retrievers.self_query.elasticsearch import ElasticsearchTranslatorfrom langchain_core.pydantic_v1 import BaseModel
```

In this example, `year` and `author` are both attributes to filter on.

```
class Search(BaseModel):    query: str    start_year: Optional[int]    author: Optional[str]
```

```
search_query = Search(query="RAG", start_year=2022, author="LangChain")
```

```
def construct_comparisons(query: Search):    comparisons = []    if query.start_year is not None:        comparisons.append(            Comparison(                comparator=Comparator.GT,                attribute="start_year",                value=query.start_year,            )        )    if query.author is not None:        comparisons.append(            Comparison(                comparator=Comparator.EQ,                attribute="author",                value=query.author,            )        )    return comparisons
```

```
comparisons = construct_comparisons(search_query)
```

```
_filter = Operation(operator=Operator.AND, arguments=comparisons)
```

```
ElasticsearchTranslator().visit_operation(_filter)
```

```
{'bool': {'must': [{'range': {'metadata.start_year': {'gt': 2022}}},   {'term': {'metadata.author.keyword': 'LangChain'}}]}}
```

```
ChromaTranslator().visit_operation(_filter)
```

```
{'$and': [{'start_year': {'$gt': 2022}}, {'author': {'$eq': 'LangChain'}}]}
```
## SQL

One of the most common types of databases that we can build Q&A systems for are SQL databases. LangChain comes with a number of built-in chains and agents that are compatible with any SQL dialect supported by SQLAlchemy (e.g., MySQL, PostgreSQL, Oracle SQL, Databricks, SQLite). They enable use cases such as:

*   Generating queries that will be run based on natural language questions,
*   Creating chatbots that can answer questions based on database data,
*   Building custom dashboards based on insights a user wants to analyze,

and much more.

## ⚠️ Security note ⚠️[​](#security-note "Direct link to ⚠️ Security note ⚠️")

Building Q&A systems of SQL databases requires executing model-generated SQL queries. There are inherent risks in doing this. Make sure that your database connection permissions are always scoped as narrowly as possible for your chain/agent’s needs. This will mitigate though not eliminate the risks of building a model-driven system. For more on general security best practices, [see here](https://python.langchain.com/docs/security/).

![sql_usecase.png](https://python.langchain.com/assets/images/sql_usecase-d432701261f05ab69b38576093718cf3.png)

## Quickstart[​](#quickstart "Direct link to Quickstart")

Head to the **[Quickstart](https://python.langchain.com/docs/use_cases/sql/quickstart/)** page to get started.

## Advanced[​](#advanced "Direct link to Advanced")

Once you’ve familiarized yourself with the basics, you can head to the advanced guides:

*   [Agents](https://python.langchain.com/docs/use_cases/sql/agents/): Building agents that can interact with SQL DBs.
*   [Prompting strategies](https://python.langchain.com/docs/use_cases/sql/prompting/): Strategies for improving SQL query generation.
*   [Query validation](https://python.langchain.com/docs/use_cases/sql/query_checking/): How to validate SQL queries.
*   [Large databases](https://python.langchain.com/docs/use_cases/sql/large_db/): How to interact with DBs with many tables and high-cardinality columns.
## Tool use and agents

An exciting use case for LLMs is building natural language interfaces for other “tools”, whether those are APIs, functions, databases, etc. LangChain is great for building such interfaces because it has:

*   Good model output parsing, which makes it easy to extract JSON, XML, OpenAI function-calls, etc. from model outputs.
*   A large collection of built-in [Tools](https://python.langchain.com/docs/integrations/tools/).
*   Provides a lot of flexibility in how you call these tools.

There are two main ways to use tools: [chains](https://python.langchain.com/docs/modules/chains/) and [agents](https://python.langchain.com/docs/modules/agents/).

Chains lets you create a pre-defined sequence of tool usage(s).

![chain](https://python.langchain.com/assets/images/tool_chain-3571e7fbc481d648aff93a2630f812ab.svg)

Agents let the model use tools in a loop, so that it can decide how many times to use tools.

![agent](https://python.langchain.com/assets/images/tool_agent-d25fafc271da3ee950ac1fba59cdf490.svg)

To get started with both approaches, head to the [Quickstart](https://python.langchain.com/docs/use_cases/tool_use/quickstart/) page.
In the [Chains with multiple tools](https://python.langchain.com/docs/use_cases/tool_use/multiple_tools/) guide we saw how to build function-calling chains that select between multiple tools. Some models, like the OpenAI models released in Fall 2023, also support parallel function calling, which allows you to invoke multiple functions (or the same function multiple times) in a single model call. Our previous chain from the multiple tools guides actually already supports this.

If you’d like to trace your runs in [LangSmith](https://python.langchain.com/docs/langsmith/) uncomment and set the following environment variables:

```
from operator import itemgetterfrom typing import Dict, List, Unionfrom langchain_core.messages import AIMessagefrom langchain_core.runnables import (    Runnable,    RunnableLambda,    RunnableMap,    RunnablePassthrough,)tools = [multiply, exponentiate, add]llm_with_tools = llm.bind_tools(tools)tool_map = {tool.name: tool for tool in tools}def call_tools(msg: AIMessage) -> Runnable:    """Simple sequential tool calling helper."""    tool_map = {tool.name: tool for tool in tools}    tool_calls = msg.tool_calls.copy()    for tool_call in tool_calls:        tool_call["output"] = tool_map[tool_call["name"]].invoke(tool_call["args"])    return tool_callschain = llm_with_tools | call_tools
```

```
[{'name': 'multiply',  'args': {'first_int': 23, 'second_int': 7},  'id': 'call_22tgOrsVLyLMsl2RLbUhtycw',  'output': 161}, {'name': 'multiply',  'args': {'first_int': 5, 'second_int': 18},  'id': 'call_EbKHEG3TjqBhEwb7aoxUtgzf',  'output': 90}, {'name': 'add',  'args': {'first_int': 1000000, 'second_int': 1000000000},  'id': 'call_LUhu2IT3vINxlTc5fCVY6Nhi',  'output': 1001000000}, {'name': 'exponentiate',  'args': {'base': 37, 'exponent': 3},  'id': 'call_bnCZIXelOKkmcyd4uGXId9Ct',  'output': 50653}]
```