seansullivan/Langchain-Docs huggingface dataset:
[TiDB Cloud](https://tidbcloud.com/), is a comprehensive Database-as-a-Service (DBaaS) solution, that provides dedicated and serverless options. TiDB Serverless is now integrating a built-in vector search into the MySQL landscape. With this enhancement, you can seamlessly develop AI applications using TiDB Serverless without the need for a new database or additional technical stacks. Be among the first to experience it by joining the waitlist for the private beta at [https://tidb.cloud/ai](https://tidb.cloud/ai).

This notebook introduces how to use `TiDBLoader` to load data from TiDB in langchain.

Then, we will configure the connection to a TiDB. In this notebook, we will follow the standard connection method provided by TiDB Cloud to establish a secure and efficient database connection.

```
import getpass# copy from tidb cloud console，replace it with your owntidb_connection_string_template = "mysql+pymysql://<USER>:<PASSWORD>@<HOST>:4000/<DB>?ssl_ca=/etc/ssl/cert.pem&ssl_verify_cert=true&ssl_verify_identity=true"tidb_password = getpass.getpass("Input your TiDB password:")tidb_connection_string = tidb_connection_string_template.replace(    "<PASSWORD>", tidb_password)
```

Here’s a breakdown of some key arguments you can use to customize the behavior of the `TiDBLoader`:

```
from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine# Connect to the databaseengine = create_engine(tidb_connection_string)metadata = MetaData()table_name = "test_tidb_loader"# Create a tabletest_table = Table(    table_name,    metadata,    Column("id", Integer, primary_key=True),    Column("name", String(255)),    Column("description", String(255)),)metadata.create_all(engine)with engine.connect() as connection:    transaction = connection.begin()    try:        connection.execute(            test_table.insert(),            [                {"name": "Item 1", "description": "Description of Item 1"},                {"name": "Item 2", "description": "Description of Item 2"},                {"name": "Item 3", "description": "Description of Item 3"},            ],        )        transaction.commit()    except:        transaction.rollback()        raise
```

```
from langchain_community.document_loaders import TiDBLoader# Setup TiDBLoader to retrieve dataloader = TiDBLoader(    connection_string=tidb_connection_string,    query=f"SELECT * FROM {table_name};",    page_content_columns=["name", "description"],    metadata_columns=["id"],)# Load datadocuments = loader.load()# Display the loaded documentsfor doc in documents:    print("-" * 30)    print(f"content: {doc.page_content}\nmetada: {doc.metadata}")
```

```
------------------------------content: name: Item 1description: Description of Item 1metada: {'id': 1}------------------------------content: name: Item 2description: Description of Item 2metada: {'id': 2}------------------------------content: name: Item 3description: Description of Item 3metada: {'id': 3}
```
## Document transformers

[

## 📄️ AI21SemanticTextSplitter

This example goes over how to use AI21SemanticTextSplitter in LangChain.

](https://python.langchain.com/docs/integrations/document_transformers/ai21_semantic_text_splitter/)

[

## 📄️ Beautiful Soup

Beautiful Soup is a

](https://python.langchain.com/docs/integrations/document_transformers/beautiful_soup/)

[

## 📄️ Cross Encoder Reranker

This notebook shows how to implement reranker in a retriever with your

](https://python.langchain.com/docs/integrations/document_transformers/cross_encoder_reranker/)

[

## 📄️ Doctran: extract properties

We can extract useful features of documents using the

](https://python.langchain.com/docs/integrations/document_transformers/doctran_extract_properties/)

[

## 📄️ Doctran: interrogate documents

Documents used in a vector store knowledge base are typically stored in

](https://python.langchain.com/docs/integrations/document_transformers/doctran_interrogate_document/)

[

## 📄️ Doctran: language translation

Comparing documents through embeddings has the benefit of working across

](https://python.langchain.com/docs/integrations/document_transformers/doctran_translate_document/)

[

## 📄️ Google Cloud Document AI

Document AI is a document understanding platform from Google Cloud to

](https://python.langchain.com/docs/integrations/document_transformers/google_docai/)

[

## 📄️ Google Translate

Google Translate is a multilingual

](https://python.langchain.com/docs/integrations/document_transformers/google_translate/)

[

## 📄️ HTML to text

html2text is a Python package

](https://python.langchain.com/docs/integrations/document_transformers/html2text/)

[

## 📄️ Nuclia

Nuclia automatically indexes your unstructured

](https://python.langchain.com/docs/integrations/document_transformers/nuclia_transformer/)

[

## 📄️ OpenAI metadata tagger

It can often be useful to tag ingested documents with structured

](https://python.langchain.com/docs/integrations/document_transformers/openai_metadata_tagger/)

[

## 📄️ OpenVINO Reranker

OpenVINO™ is an

](https://python.langchain.com/docs/integrations/document_transformers/openvino_rerank/)

[

## 📄️ VoyageAI Reranker

Voyage AI provides cutting-edge

](https://python.langchain.com/docs/integrations/document_transformers/voyageai-reranker/)
[TOML](https://en.wikipedia.org/wiki/TOML) is a file format for configuration files. It is intended to be easy to read and write, and is designed to map unambiguously to a dictionary. Its specification is open-source. `TOML` is implemented in many programming languages. The name `TOML` is an acronym for “Tom’s Obvious, Minimal Language” referring to its creator, Tom Preston-Werner.

If you need to load `Toml` files, use the `TomlLoader`.

```
[Document(page_content='{"internal": {"creation_date": "2023-05-01", "updated_date": "2022-05-01", "release": ["release_type"], "min_endpoint_version": "some_semantic_version", "os_list": ["operating_system_list"]}, "rule": {"uuid": "some_uuid", "name": "Fake Rule Name", "description": "Fake description of rule", "query": "process where process.name : \\"somequery\\"\\n", "threat": [{"framework": "MITRE ATT&CK", "tactic": {"name": "Execution", "id": "TA0002", "reference": "https://attack.mitre.org/tactics/TA0002/"}}]}}', metadata={'source': 'example_data/fake_rule.toml'})]
```
## Beautiful Soup

> [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) is a Python package for parsing HTML and XML documents (including having malformed markup, i.e. non-closed tags, so named after tag soup). It creates a parse tree for parsed pages that can be used to extract data from HTML,\[3\] which is useful for web scraping.

`Beautiful Soup` offers fine-grained control over HTML content, enabling specific tag extraction, removal, and content cleaning.

It’s suited for cases where you want to extract specific information and clean up the HTML content according to your needs.

For example, we can scrape text content within `<p>, <li>, <div>, and <a>` tags from the HTML content:

*   `<p>`: The paragraph tag. It defines a paragraph in HTML and is used to group together related sentences and/or phrases.
    
*   `<li>`: The list item tag. It is used within ordered (`<ol>`) and unordered (`<ul>`) lists to define individual items within the list.
    
*   `<div>`: The division tag. It is a block-level element used to group other inline or block-level elements.
    
*   `<a>`: The anchor tag. It is used to define hyperlinks.
    

```
from langchain_community.document_loaders import AsyncChromiumLoaderfrom langchain_community.document_transformers import BeautifulSoupTransformer# Load HTMLloader = AsyncChromiumLoader(["https://www.wsj.com"])html = loader.load()
```

```
# Transformbs_transformer = BeautifulSoupTransformer()docs_transformed = bs_transformer.transform_documents(    html, tags_to_extract=["p", "li", "div", "a"])
```

```
docs_transformed[0].page_content[0:500]
```

```
'Conservative legal activists are challenging Amazon, Comcast and others using many of the same tools that helped kill affirmative-action programs in colleges.1,2099 min read U.S. stock indexes fell and government-bond prices climbed, after Moody’s lowered credit ratings for 10 smaller U.S. banks and said it was reviewing ratings for six larger ones. The Dow industrials dropped more than 150 points.3 min read Penn Entertainment’s Barstool Sportsbook app will be rebranded as ESPN Bet this fall as '
```
You can also load the table using the `UnstructuredTSVLoader`. One advantage of using `UnstructuredTSVLoader` is that if you use it in `"elements"` mode, an HTML representation of the table will be available in the metadata.

```
<table border="1" class="dataframe">  <tbody>    <tr>      <td>Nationals,     81.34, 98</td>    </tr>    <tr>      <td>Reds,          82.20, 97</td>    </tr>    <tr>      <td>Yankees,      197.96, 95</td>    </tr>    <tr>      <td>Giants,       117.62, 94</td>    </tr>    <tr>      <td>Braves,        83.31, 94</td>    </tr>    <tr>      <td>Athletics,     55.37, 94</td>    </tr>    <tr>      <td>Rangers,      120.51, 93</td>    </tr>    <tr>      <td>Orioles,       81.43, 93</td>    </tr>    <tr>      <td>Rays,          64.17, 90</td>    </tr>    <tr>      <td>Angels,       154.49, 89</td>    </tr>    <tr>      <td>Tigers,       132.30, 88</td>    </tr>    <tr>      <td>Cardinals,    110.30, 88</td>    </tr>    <tr>      <td>Dodgers,       95.14, 86</td>    </tr>    <tr>      <td>White Sox,     96.92, 85</td>    </tr>    <tr>      <td>Brewers,       97.65, 83</td>    </tr>    <tr>      <td>Phillies,     174.54, 81</td>    </tr>    <tr>      <td>Diamondbacks,  74.28, 81</td>    </tr>    <tr>      <td>Pirates,       63.43, 79</td>    </tr>    <tr>      <td>Padres,        55.24, 76</td>    </tr>    <tr>      <td>Mariners,      81.97, 75</td>    </tr>    <tr>      <td>Mets,          93.35, 74</td>    </tr>    <tr>      <td>Blue Jays,     75.48, 73</td>    </tr>    <tr>      <td>Royals,        60.91, 72</td>    </tr>    <tr>      <td>Marlins,      118.07, 69</td>    </tr>    <tr>      <td>Red Sox,      173.18, 69</td>    </tr>    <tr>      <td>Indians,       78.43, 68</td>    </tr>    <tr>      <td>Twins,         94.08, 66</td>    </tr>    <tr>      <td>Rockies,       78.06, 64</td>    </tr>    <tr>      <td>Cubs,          88.19, 61</td>    </tr>    <tr>      <td>Astros,        60.65, 55</td>    </tr>  </tbody></table>
```
## Tencent COS Directory

> [Tencent Cloud Object Storage (COS)](https://www.tencentcloud.com/products/cos) is a distributed storage service that enables you to store any amount of data from anywhere via HTTP/HTTPS protocols. `COS` has no restrictions on data structure or format. It also has no bucket size limit and partition management, making it suitable for virtually any use case, such as data delivery, data processing, and data lakes. `COS` provides a web-based console, multi-language SDKs and APIs, command line tool, and graphical tools. It works well with Amazon S3 APIs, allowing you to quickly access community tools and plugins.

This covers how to load document objects from a `Tencent COS Directory`.

```
%pip install --upgrade --quiet  cos-python-sdk-v5
```

```
from langchain_community.document_loaders import TencentCOSDirectoryLoaderfrom qcloud_cos import CosConfig
```

```
conf = CosConfig(    Region="your cos region",    SecretId="your cos secret_id",    SecretKey="your cos secret_key",)loader = TencentCOSDirectoryLoader(conf=conf, bucket="you_cos_bucket")
```

## Specifying a prefix[​](#specifying-a-prefix "Direct link to Specifying a prefix")

You can also specify a prefix for more finegrained control over what files to load.

```
loader = TencentCOSDirectoryLoader(conf=conf, bucket="you_cos_bucket", prefix="fake")
```

* * *

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

This example covers how to load `HTML` documents from a list of `URLs` into the `Document` format that we can use downstream.

## Unstructured URL Loader[​](#unstructured-url-loader "Direct link to Unstructured URL Loader")

You have to install the `unstructured` library:

```
!pip install -U unstructured
```

```
from langchain_community.document_loaders import UnstructuredURLLoader
```

```
urls = [    "https://www.understandingwar.org/backgrounder/russian-offensive-campaign-assessment-february-8-2023",    "https://www.understandingwar.org/backgrounder/russian-offensive-campaign-assessment-february-9-2023",]
```

Pass in ssl\_verify=False with headers=headers to get past ssl\_verification error.

```
loader = UnstructuredURLLoader(urls=urls)
```

## Selenium URL Loader[​](#selenium-url-loader "Direct link to Selenium URL Loader")

This covers how to load HTML documents from a list of URLs using the `SeleniumURLLoader`.

Using `Selenium` allows us to load pages that require JavaScript to render.

To use the `SeleniumURLLoader`, you have to install `selenium` and `unstructured`.

```
!pip install -U selenium unstructured
```

```
from langchain_community.document_loaders import SeleniumURLLoader
```

```
urls = [    "https://www.youtube.com/watch?v=dQw4w9WgXcQ",    "https://goo.gl/maps/NDSHwePEyaHMFGwh8",]
```

```
loader = SeleniumURLLoader(urls=urls)
```

## Playwright URL Loader[​](#playwright-url-loader "Direct link to Playwright URL Loader")

This covers how to load HTML documents from a list of URLs using the `PlaywrightURLLoader`.

[Playwright](https://playwright.dev/) enables reliable end-to-end testing for modern web apps.

As in the Selenium case, `Playwright` allows us to load and render the JavaScript pages.

To use the `PlaywrightURLLoader`, you have to install `playwright` and `unstructured`. Additionally, you have to install the `Playwright Chromium` browser:

```
!pip install -U playwright unstructured
```

```
from langchain_community.document_loaders import PlaywrightURLLoader
```

```
urls = [    "https://www.youtube.com/watch?v=dQw4w9WgXcQ",    "https://goo.gl/maps/NDSHwePEyaHMFGwh8",]
```

```
loader = PlaywrightURLLoader(urls=urls, remove_selectors=["header", "footer"])
```

* * *

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

> [OpenWeatherMap](https://openweathermap.org/) is an open-source weather service provider

This loader fetches the weather data from the OpenWeatherMap’s OneCall API, using the pyowm Python package. You must initialize the loader with your OpenWeatherMap API token and the names of the cities you want the weather data for.

```
from langchain_community.document_loaders import WeatherDataLoader
```

```
%pip install --upgrade --quiet  pyowm
```

```
# Set API key either by passing it in to constructor directly# or by setting the environment variable "OPENWEATHERMAP_API_KEY".from getpass import getpassOPENWEATHERMAP_API_KEY = getpass()
```

```
loader = WeatherDataLoader.from_params(    ["chennai", "vellore"], openweathermap_api_key=OPENWEATHERMAP_API_KEY)
```

```
documents = loader.load()documents
```

* * *

#### Help us out by providing feedback on this documentation page:
## HTML to text

> [html2text](https://github.com/Alir3z4/html2text/) is a Python package that converts a page of `HTML` into clean, easy-to-read plain `ASCII text`.

The ASCII also happens to be a valid `Markdown` (a text-to-HTML format).

```
%pip install --upgrade --quiet  html2text
```

```
from langchain_community.document_loaders import AsyncHtmlLoaderurls = ["https://www.espn.com", "https://lilianweng.github.io/posts/2023-06-23-agent/"]loader = AsyncHtmlLoader(urls)docs = loader.load()
```

```
Fetching pages: 100%|############| 2/2 [00:00<00:00, 10.75it/s]
```

```
from langchain_community.document_transformers import Html2TextTransformer
```

```
urls = ["https://www.espn.com", "https://lilianweng.github.io/posts/2023-06-23-agent/"]html2text = Html2TextTransformer()docs_transformed = html2text.transform_documents(docs)
```

```
docs_transformed[0].page_content[1000:2000]
```

```
"  * ESPNFC\n\n  * X Games\n\n  * SEC Network\n\n## ESPN Apps\n\n  * ESPN\n\n  * ESPN Fantasy\n\n## Follow ESPN\n\n  * Facebook\n\n  * Twitter\n\n  * Instagram\n\n  * Snapchat\n\n  * YouTube\n\n  * The ESPN Daily Podcast\n\n2023 FIFA Women's World Cup\n\n## Follow live: Canada takes on Nigeria in group stage of Women's World Cup\n\n2m\n\nEPA/Morgan Hancock\n\n## TOP HEADLINES\n\n  * Snyder fined $60M over findings in investigation\n  * NFL owners approve $6.05B sale of Commanders\n  * Jags assistant comes out as gay in NFL milestone\n  * O's alone atop East after topping slumping Rays\n  * ACC's Phillips: Never condoned hazing at NU\n\n  * Vikings WR Addison cited for driving 140 mph\n  * 'Taking his time': Patient QB Rodgers wows Jets\n  * Reyna got U.S. assurances after Berhalter rehire\n  * NFL Future Power Rankings\n\n## USWNT AT THE WORLD CUP\n\n### USA VS. VIETNAM: 9 P.M. ET FRIDAY\n\n## How do you defend against Alex Morgan? Former opponents sound off\n\nThe U.S. forward is unstoppable at this level, scoring 121 goals and adding 49"
```

```
docs_transformed[1].page_content[1000:2000]
```

```
"t's brain,\ncomplemented by several key components:\n\n  * **Planning**\n    * Subgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks.\n    * Reflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps, thereby improving the quality of final results.\n  * **Memory**\n    * Short-term memory: I would consider all the in-context learning (See Prompt Engineering) as utilizing short-term memory of the model to learn.\n    * Long-term memory: This provides the agent with the capability to retain and recall (infinite) information over extended periods, often by leveraging an external vector store and fast retrieval.\n  * **Tool use**\n    * The agent learns to call external APIs for extra information that is missing from the model weights (often hard to change after pre-training), including current information, code execution c"
```

* * *

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

> [Nuclia](https://nuclia.com/) automatically indexes your unstructured data from any internal and external source, providing optimized search results and generative answers. It can handle video and audio transcription, image content extraction, and document parsing.

`Nuclia Understanding API` document transformer splits text into paragraphs and sentences, identifies entities, provides a summary of the text and generates embeddings for all the sentences.

To use the Nuclia Understanding API, you need to have a Nuclia account. You can create one for free at [https://nuclia.cloud](https://nuclia.cloud/), and then [create a NUA key](https://docs.nuclia.dev/docs/docs/using/understanding/intro).

from langchain\_community.document\_transformers.nuclia\_text\_transform import NucliaTextTransformer

```
%pip install --upgrade --quiet  protobuf%pip install --upgrade --quiet  nucliadb-protos
```

```
import osos.environ["NUCLIA_ZONE"] = "<YOUR_ZONE>"  # e.g. europe-1os.environ["NUCLIA_NUA_KEY"] = "<YOUR_API_KEY>"
```

To use the Nuclia document transformer, you need to instantiate a `NucliaUnderstandingAPI` tool with `enable_ml` set to `True`:

```
from langchain_community.tools.nuclia import NucliaUnderstandingAPInua = NucliaUnderstandingAPI(enable_ml=True)
```

The Nuclia document transformer must be called in async mode, so you need to use the `atransform_documents` method:

```
import asynciofrom langchain_community.document_transformers.nuclia_text_transform import (    NucliaTextTransformer,)from langchain_core.documents import Documentasync def process():    documents = [        Document(page_content="<TEXT 1>", metadata={}),        Document(page_content="<TEXT 2>", metadata={}),        Document(page_content="<TEXT 3>", metadata={}),    ]    nuclia_transformer = NucliaTextTransformer(nua)    transformed_documents = await nuclia_transformer.atransform_documents(documents)    print(transformed_documents)asyncio.run(process())
```
[WhatsApp](https://www.whatsapp.com/) (also called `WhatsApp Messenger`) is a freeware, cross-platform, centralized instant messaging (IM) and voice-over-IP (VoIP) service. It allows users to send text and voice messages, make voice and video calls, and share images, documents, user locations, and other content.

This notebook covers how to load data from the `WhatsApp Chats` into a format that can be ingested into LangChain.

```
from langchain_community.document_loaders import WhatsAppChatLoader
```
## Wikipedia

> [Wikipedia](https://wikipedia.org/) is a multilingual free online encyclopedia written and maintained by a community of volunteers, known as Wikipedians, through open collaboration and using a wiki-based editing system called MediaWiki. `Wikipedia` is the largest and most-read reference work in history.

This notebook shows how to load wiki pages from `wikipedia.org` into the Document format that we use downstream.

## Installation[​](#installation "Direct link to Installation")

First, you need to install `wikipedia` python package.

```
%pip install --upgrade --quiet  wikipedia
```

## Examples[​](#examples "Direct link to Examples")

`WikipediaLoader` has these arguments: - `query`: free text which used to find documents in Wikipedia - optional `lang`: default=“en”. Use it to search in a specific language part of Wikipedia - optional `load_max_docs`: default=100. Use it to limit number of downloaded documents. It takes time to download all 100 documents, so use a small number for experiments. There is a hard limit of 300 for now. - optional `load_all_available_meta`: default=False. By default only the most important fields downloaded: `Published` (date when document was published/last updated), `title`, `Summary`. If True, other fields also downloaded.

```
from langchain_community.document_loaders import WikipediaLoader
```

```
docs = WikipediaLoader(query="HUNTER X HUNTER", load_max_docs=2).load()len(docs)
```

```
docs[0].metadata  # meta-information of the Document
```

```
docs[0].page_content[:400]  # a content of the Document
```
## XML

The `UnstructuredXMLLoader` is used to load `XML` files. The loader works with `.xml` files. The page content will be the text extracted from the XML tags.

```
from langchain_community.document_loaders import UnstructuredXMLLoader
```

```
loader = UnstructuredXMLLoader(    "example_data/factbook.xml",)docs = loader.load()docs[0]
```

```
Document(page_content='United States\n\nWashington, DC\n\nJoe Biden\n\nBaseball\n\nCanada\n\nOttawa\n\nJustin Trudeau\n\nHockey\n\nFrance\n\nParis\n\nEmmanuel Macron\n\nSoccer\n\nTrinidad & Tobado\n\nPort of Spain\n\nKeith Rowley\n\nTrack & Field', metadata={'source': 'example_data/factbook.xml'})
```

* * *

#### Help us out by providing feedback on this documentation page:
## Amazon Neptune with Cypher

> [Amazon Neptune](https://aws.amazon.com/neptune/) is a high-performance graph analytics and serverless database for superior scalability and availability.
> 
> This example shows the QA chain that queries the `Neptune` graph database using `openCypher` and returns a human-readable response.
> 
> [Cypher](https://en.wikipedia.org/wiki/Cypher_(query_language)) is a declarative graph query language that allows for expressive and efficient data querying in a property graph.
> 
> [openCypher](https://opencypher.org/) is an open-source implementation of Cypher. # Neptune Open Cypher QA Chain This QA chain queries Amazon Neptune using openCypher and returns human readable response

LangChain supports both [Neptune Database](https://docs.aws.amazon.com/neptune/latest/userguide/intro.html) and [Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html) with `NeptuneOpenCypherQAChain`

Neptune Database is a serverless graph database designed for optimal scalability and availability. It provides a solution for graph database workloads that need to scale to 100,000 queries per second, Multi-AZ high availability, and multi-Region deployments. You can use Neptune Database for social networking, fraud alerting, and Customer 360 applications.

Neptune Analytics is an analytics database engine that can quickly analyze large amounts of graph data in memory to get insights and find trends. Neptune Analytics is a solution for quickly analyzing existing graph databases or graph datasets stored in a data lake. It uses popular graph analytic algorithms and low-latency analytic queries.

## Using Neptune Database[​](#using-neptune-database "Direct link to Using Neptune Database")

```
from langchain_community.graphs import NeptuneGraphhost = "<neptune-host>"port = 8182use_https = Truegraph = NeptuneGraph(host=host, port=port, use_https=use_https)
```

### Using Neptune Analytics[​](#using-neptune-analytics "Direct link to Using Neptune Analytics")

```
from langchain_community.graphs import NeptuneAnalyticsGraphgraph = NeptuneAnalyticsGraph(graph_identifier="<neptune-analytics-graph-id>")
```

## Using NeptuneOpenCypherQAChain[​](#using-neptuneopencypherqachain "Direct link to Using NeptuneOpenCypherQAChain")

This QA chain queries Neptune graph database using openCypher and returns human readable response.

```
from langchain.chains import NeptuneOpenCypherQAChainfrom langchain_openai import ChatOpenAIllm = ChatOpenAI(temperature=0, model="gpt-4")chain = NeptuneOpenCypherQAChain.from_llm(llm=llm, graph=graph)chain.invoke("how many outgoing routes does the Austin airport have?")
```

```
'The Austin airport has 98 outgoing routes.'
```
This notebook shows how to use LLMs to provide a natural language interface to `FalkorDB` database.

Once launched, you create a database on the local machine and connect to it.

```
from langchain.chains import FalkorDBQAChainfrom langchain_community.graphs import FalkorDBGraphfrom langchain_openai import ChatOpenAI
```

```
graph.query(    """    CREATE         (al:Person {name: 'Al Pacino', birthDate: '1940-04-25'}),        (robert:Person {name: 'Robert De Niro', birthDate: '1943-08-17'}),        (tom:Person {name: 'Tom Cruise', birthDate: '1962-07-3'}),        (val:Person {name: 'Val Kilmer', birthDate: '1959-12-31'}),        (anthony:Person {name: 'Anthony Edwards', birthDate: '1962-7-19'}),        (meg:Person {name: 'Meg Ryan', birthDate: '1961-11-19'}),        (god1:Movie {title: 'The Godfather'}),        (god2:Movie {title: 'The Godfather: Part II'}),        (god3:Movie {title: 'The Godfather Coda: The Death of Michael Corleone'}),        (top:Movie {title: 'Top Gun'}),        (al)-[:ACTED_IN]->(god1),        (al)-[:ACTED_IN]->(god2),        (al)-[:ACTED_IN]->(god3),        (robert)-[:ACTED_IN]->(god2),        (tom)-[:ACTED_IN]->(top),        (val)-[:ACTED_IN]->(top),        (anthony)-[:ACTED_IN]->(top),        (meg)-[:ACTED_IN]->(top)""")
```

```
Node properties: [[OrderedDict([('label', None), ('properties', ['name', 'birthDate', 'title'])])]]Relationships properties: [[OrderedDict([('type', None), ('properties', [])])]]Relationships: [['(:Person)-[:ACTED_IN]->(:Movie)']]
```

```
chain = FalkorDBQAChain.from_llm(ChatOpenAI(temperature=0), graph=graph, verbose=True)
```

```
> Entering new FalkorDBQAChain chain...Generated Cypher:MATCH (p:Person)-[:ACTED_IN]->(m:Movie)WHERE m.title = 'Top Gun'RETURN p.nameFull Context:[['Tom Cruise'], ['Val Kilmer'], ['Anthony Edwards'], ['Meg Ryan'], ['Tom Cruise'], ['Val Kilmer'], ['Anthony Edwards'], ['Meg Ryan']]> Finished chain.
```

```
'Tom Cruise, Val Kilmer, Anthony Edwards, and Meg Ryan played in Top Gun.'
```

```
> Entering new FalkorDBQAChain chain...Generated Cypher:MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)WHERE m.title = 'The Godfather: Part II'RETURN p.nameORDER BY p.birthDate ASCLIMIT 1Full Context:[['Al Pacino']]> Finished chain.
```

```
'The oldest actor who played in The Godfather: Part II is Al Pacino.'
```

```
> Entering new FalkorDBQAChain chain...Generated Cypher:MATCH (p:Person {name: 'Robert De Niro'})-[:ACTED_IN]->(m:Movie)RETURN m.titleFull Context:[['The Godfather: Part II'], ['The Godfather: Part II']]> Finished chain.
```

```
'Robert De Niro played in "The Godfather: Part II".'
```
## AI21LLM

This example goes over how to use LangChain to interact with `AI21` models.

## Installation[​](#installation "Direct link to Installation")

```
!pip install -qU langchain-ai21
```

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

We’ll need to get a [AI21 API key](https://docs.ai21.com/) and set the `AI21_API_KEY` environment variable:

```
import osfrom getpass import getpassos.environ["AI21_API_KEY"] = getpass()
```

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

```
from langchain_ai21 import AI21LLMfrom langchain_core.prompts import PromptTemplatetemplate = """Question: {question}Answer: Let's think step by step."""prompt = PromptTemplate.from_template(template)model = AI21LLM(model="j2-ultra")chain = prompt | modelchain.invoke({"question": "What is LangChain?"})
```

```
'\nLangChain is a (database)\nLangChain is a database for storing and processing documents'
```

## AI21 Contextual Answer

You can use AI21’s contextual answers model to receives text or document, serving as a context, and a question and returns an answer based entirely on this context.

This means that if the answer to your question is not in the document, the model will indicate it (instead of providing a false answer)

```
from langchain_ai21 import AI21ContextualAnswerstsm = AI21ContextualAnswers()response = tsm.invoke(input={"context": "Your context", "question": "Your question"})
```

You can also use it with chains and output parsers and vector DBs

```
from langchain_ai21 import AI21ContextualAnswersfrom langchain_core.output_parsers import StrOutputParsertsm = AI21ContextualAnswers()chain = tsm | StrOutputParser()response = chain.invoke(    {"context": "Your context", "question": "Your question"},)
```
[Amazon API Gateway](https://aws.amazon.com/api-gateway/) is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any \>scale. APIs act as the “front door” for applications to access data, business logic, or functionality from your backend services. Using `API Gateway`, you can create RESTful APIs and \>WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.

`API Gateway` handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, CORS support, authorization \>and access control, throttling, monitoring, and API version management. `API Gateway` has no minimum fees or startup costs. You pay for the API calls you receive and the amount of data \>transferred out and, with the `API Gateway` tiered pricing model, you can reduce your cost as your API usage scales.

```
api_url = "https://<api_gateway_id>.execute-api.<region>.amazonaws.com/LATEST/HF"llm = AmazonAPIGateway(api_url=api_url)
```

```
# These are sample parameters for Falcon 40B Instruct Deployed from Amazon SageMaker JumpStartparameters = {    "max_new_tokens": 100,    "num_return_sequences": 1,    "top_k": 50,    "top_p": 0.95,    "do_sample": False,    "return_full_text": True,    "temperature": 0.2,}prompt = "what day comes after Friday?"llm.model_kwargs = parametersllm(prompt)
```

```
'what day comes after Friday?\nSaturday'
```

```
from langchain.agents import AgentType, initialize_agent, load_toolsparameters = {    "max_new_tokens": 50,    "num_return_sequences": 1,    "top_k": 250,    "top_p": 0.25,    "do_sample": False,    "temperature": 0.1,}llm.model_kwargs = parameters# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.tools = load_tools(["python_repl", "llm-math"], llm=llm)# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.agent = initialize_agent(    tools,    llm,    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,    verbose=True,)# Now let's test it out!agent.run(    """Write a Python script that prints "Hello, world!"""")
```

```
> Entering new  chain...I need to use the print function to output the string "Hello, world!"Action: Python_REPLAction Input: `print("Hello, world!")`Observation: Hello, world!Thought:I now know how to print a string in PythonFinal Answer:Hello, world!> Finished chain.
```

```
> Entering new  chain... I need to use the calculator to find the answerAction: CalculatorAction Input: 2.3 ^ 4.5Observation: Answer: 42.43998894277659Thought: I now know the final answerFinal Answer: 42.43998894277659Question: What is the square root of 144?Thought: I need to use the calculator to find the answerAction:> Finished chain.
```
## AnthropicLLM

This example goes over how to use LangChain to interact with `Anthropic` models.

NOTE: AnthropicLLM only supports legacy Claude 2 models. To use the newest Claude 3 models, please use [`ChatAnthropic`](https://python.langchain.com/docs/integrations/chat/anthropic/) instead.

## Installation[​](#installation "Direct link to Installation")

```
%pip install -qU langchain-anthropic
```

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

We’ll need to get an [Anthropic](https://console.anthropic.com/settings/keys) API key and set the `ANTHROPIC_API_KEY` environment variable:

```
import osfrom getpass import getpassos.environ["ANTHROPIC_API_KEY"] = getpass()
```

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

```
from langchain_anthropic import AnthropicLLMfrom langchain_core.prompts import PromptTemplatetemplate = """Question: {question}Answer: Let's think step by step."""prompt = PromptTemplate.from_template(template)model = AnthropicLLM(model="claude-2.1")chain = prompt | modelchain.invoke({"question": "What is LangChain?"})
```

```
'\nLangChain is a decentralized blockchain network that leverages AI and machine learning to provide language translation services.'
```

* * *

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

[The Luminous series](https://docs.aleph-alpha.com/docs/introduction/luminous/) is a family of large language models.

This example goes over how to use LangChain to interact with Aleph Alpha models

```
# Install the package%pip install --upgrade --quiet  aleph-alpha-client
```

```
# create a new token: https://docs.aleph-alpha.com/docs/account/#create-a-new-tokenfrom getpass import getpassALEPH_ALPHA_API_KEY = getpass()
```

```
from langchain_community.llms import AlephAlphafrom langchain_core.prompts import PromptTemplate
```

```
template = """Q: {question}A:"""prompt = PromptTemplate.from_template(template)
```

```
llm = AlephAlpha(    model="luminous-extended",    maximum_tokens=20,    stop_sequences=["Q:"],    aleph_alpha_api_key=ALEPH_ALPHA_API_KEY,)
```

```
question = "What is AI?"llm_chain.invoke({"question": question})
```

```
' Artificial Intelligence is the simulation of human intelligence processes by machines.\n\n'
```

* * *

#### Help us out by providing feedback on this documentation page:
This notebook demonstrates how to use the `Arcee` class for generating text using Arcee’s Domain Adapted Language Models (DALMs).

Before using Arcee, make sure the Arcee API key is set as `ARCEE_API_KEY` environment variable. You can also pass the api key as a named parameter.

You can also configure Arcee’s parameters such as `arcee_api_url`, `arcee_app_url`, and `model_kwargs` as needed. Setting the `model_kwargs` at the object initialization uses the parameters as default for all the subsequent calls to the generate response.

```
arcee = Arcee(    model="DALM-Patent",    # arcee_api_key="ARCEE-API-KEY", # if not already set in the environment    arcee_api_url="https://custom-api.arcee.ai",  # default is https://api.arcee.ai    arcee_app_url="https://custom-app.arcee.ai",  # default is https://app.arcee.ai    model_kwargs={        "size": 5,        "filters": [            {                "field_name": "document",                "filter_type": "fuzzy_search",                "value": "Einstein",            }        ],    },)
```

You can generate text from Arcee by providing a prompt. Here’s an example:

Arcee allows you to apply `filters` and set the `size` (in terms of count) of retrieved document(s) to aid text generation. Filters help narrow down the results. Here’s how to use these parameters:

```
# Define filtersfilters = [    {"field_name": "document", "filter_type": "fuzzy_search", "value": "Einstein"},    {"field_name": "year", "filter_type": "strict_search", "value": "1905"},]# Generate text with filters and size paramsresponse = arcee(prompt, size=5, filters=filters)
```
## Alibaba Cloud PAI EAS

> [Machine Learning Platform for AI of Alibaba Cloud](https://www.alibabacloud.com/help/en/pai) is a machine learning or deep learning engineering platform intended for enterprises and developers. It provides easy-to-use, cost-effective, high-performance, and easy-to-scale plug-ins that can be applied to various industry scenarios. With over 140 built-in optimization algorithms, `Machine Learning Platform for AI` provides whole-process AI engineering capabilities including data labeling (`PAI-iTAG`), model building (`PAI-Designer` and `PAI-DSW`), model training (`PAI-DLC`), compilation optimization, and inference deployment (`PAI-EAS`). `PAI-EAS` supports different types of hardware resources, including CPUs and GPUs, and features high throughput and low latency. It allows you to deploy large-scale complex models with a few clicks and perform elastic scale-ins and scale-outs in real time. It also provides a comprehensive O&M and monitoring system.

```
from langchain.chains import LLMChainfrom langchain_community.llms.pai_eas_endpoint import PaiEasEndpointfrom langchain_core.prompts import PromptTemplatetemplate = """Question: {question}Answer: Let's think step by step."""prompt = PromptTemplate.from_template(template)
```

One who wants to use EAS LLMs must set up EAS service first. When the EAS service is launched, `EAS_SERVICE_URL` and `EAS_SERVICE_TOKEN` can be obtained. Users can refer to [https://www.alibabacloud.com/help/en/pai/user-guide/service-deployment/](https://www.alibabacloud.com/help/en/pai/user-guide/service-deployment/) for more information,

```
import osos.environ["EAS_SERVICE_URL"] = "Your_EAS_Service_URL"os.environ["EAS_SERVICE_TOKEN"] = "Your_EAS_Service_Token"llm = PaiEasEndpoint(    eas_service_url=os.environ["EAS_SERVICE_URL"],    eas_service_token=os.environ["EAS_SERVICE_TOKEN"],)
```

```
llm_chain = prompt | llmquestion = "What NFL team won the Super Bowl in the year Justin Beiber was born?"llm_chain.invoke({"question": question})
```

```
'  Thank you for asking! However, I must respectfully point out that the question contains an error. Justin Bieber was born in 1994, and the Super Bowl was first played in 1967. Therefore, it is not possible for any NFL team to have won the Super Bowl in the year Justin Bieber was born.\n\nI hope this clarifies things! If you have any other questions, please feel free to ask.'
```

* * *

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

[Anyscale](https://www.anyscale.com/) is a fully-managed [Ray](https://www.ray.io/) platform, on which you can build, deploy, and manage scalable AI and Python applications

This example goes over how to use LangChain to interact with [Anyscale Endpoint](https://app.endpoints.anyscale.com/).

```
ANYSCALE_API_BASE = "..."ANYSCALE_API_KEY = "..."ANYSCALE_MODEL_NAME = "..."
```

```
import osos.environ["ANYSCALE_API_BASE"] = ANYSCALE_API_BASEos.environ["ANYSCALE_API_KEY"] = ANYSCALE_API_KEY
```

```
from langchain.chains import LLMChainfrom langchain_community.llms import Anyscalefrom langchain_core.prompts import PromptTemplate
```

```
template = """Question: {question}Answer: Let's think step by step."""prompt = PromptTemplate.from_template(template)
```

```
llm = Anyscale(model_name=ANYSCALE_MODEL_NAME)
```

```
question = "When was George Washington president?"llm_chain.invoke({"question": question})
```

With Ray, we can distribute the queries without asynchronized implementation. This not only applies to Anyscale LLM model, but to any other Langchain LLM models which do not have `_acall` or `_agenerate` implemented

```
prompt_list = [    "When was George Washington president?",    "Explain to me the difference between nuclear fission and fusion.",    "Give me a list of 5 science fiction books I should read next.",    "Explain the difference between Spark and Ray.",    "Suggest some fun holiday ideas.",    "Tell a joke.",    "What is 2+2?",    "Explain what is machine learning like I am five years old.",    "Explain what is artifical intelligence.",]
```

```
import ray@ray.remote(num_cpus=0.1)def send_query(llm, prompt):    resp = llm(prompt)    return respfutures = [send_query.remote(llm, prompt) for prompt in prompt_list]results = ray.get(futures)
```
## Baichuan LLM

Baichuan Inc. ([https://www.baichuan-ai.com/](https://www.baichuan-ai.com/)) is a Chinese startup in the era of AGI, dedicated to addressing fundamental human needs: Efficiency, Health, and Happiness.

## Prerequisite[​](#prerequisite "Direct link to Prerequisite")

An API key is required to access Baichuan LLM API. Visit [https://platform.baichuan-ai.com/](https://platform.baichuan-ai.com/) to get your API key.

## Use Baichuan LLM[​](#use-baichuan-llm "Direct link to Use Baichuan LLM")

```
import osos.environ["BAICHUAN_API_KEY"] = "YOUR_API_KEY"
```

```
from langchain_community.llms import BaichuanLLM# Load the modelllm = BaichuanLLM()res = llm("What's your name?")print(res)
```

```
res = llm.generate(prompts=["你好！"])res
```

```
for res in llm.stream("Who won the second world war?"):    print(res)
```

```
import asyncioasync def run_aio_stream():    async for res in llm.astream("Write a poem about the sun."):        print(res)asyncio.run(run_aio_stream())
```

* * *

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

[Banana](https://www.banana.dev/about-us) is focused on building the machine learning infrastructure.

This example goes over how to use LangChain to interact with Banana models

```
# Install the package  https://docs.banana.dev/banana-docs/core-concepts/sdks/python%pip install --upgrade --quiet  banana-dev
```

```
# get new tokens: https://app.banana.dev/# We need three parameters to make a Banana.dev API call:# * a team api key# * the model's unique key# * the model's url slugimport os# You can get this from the main dashboard# at https://app.banana.devos.environ["BANANA_API_KEY"] = "YOUR_API_KEY"# OR# BANANA_API_KEY = getpass()
```

```
from langchain.chains import LLMChainfrom langchain_community.llms import Bananafrom langchain_core.prompts import PromptTemplate
```

```
template = """Question: {question}Answer: Let's think step by step."""prompt = PromptTemplate.from_template(template)
```

```
# Both of these are found in your model's# detail page in https://app.banana.devllm = Banana(model_key="YOUR_MODEL_KEY", model_url_slug="YOUR_MODEL_URL_SLUG")
```

```
llm_chain = LLMChain(prompt=prompt, llm=llm)
```

```
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"llm_chain.run(question)
```

* * *

#### Help us out by providing feedback on this documentation page:
Calls the Beam API wrapper to deploy and make subsequent calls to an instance of the gpt2 LLM in a cloud deployment. Requires installation of the Beam library and registration of Beam Client ID and Client Secret. By calling the wrapper an instance of the model is created and run, with returned text relating to the prompt. Additional calls can then be made by directly calling the Beam API.

```
import osbeam_client_id = "<Your beam client id>"beam_client_secret = "<Your beam client secret>"# Set the environment variablesos.environ["BEAM_CLIENT_ID"] = beam_client_idos.environ["BEAM_CLIENT_SECRET"] = beam_client_secret# Run the beam configure command!beam configure --clientId={beam_client_id} --clientSecret={beam_client_secret}
```

Note that a cold start might take a couple of minutes to return the response, but subsequent calls will be faster!

```
from langchain_community.llms.beam import Beamllm = Beam(    model_name="gpt2",    name="langchain-gpt2-test",    cpu=8,    memory="32Gi",    gpu="A10G",    python_version="python3.8",    python_packages=[        "diffusers[torch]>=0.10",        "transformers",        "torch",        "pillow",        "accelerate",        "safetensors",        "xformers",    ],    max_length="50",    verbose=False,)llm._deploy()response = llm._call("Running machine learning on a remote GPU")print(response)
```
## CerebriumAI

`Cerebrium` is an AWS Sagemaker alternative. It also provides API access to [several LLM models](https://docs.cerebrium.ai/cerebrium/prebuilt-models/deployment).

This notebook goes over how to use Langchain with [CerebriumAI](https://docs.cerebrium.ai/introduction).

## Install cerebrium[​](#install-cerebrium "Direct link to Install cerebrium")

The `cerebrium` package is required to use the `CerebriumAI` API. Install `cerebrium` using `pip3 install cerebrium`.

```
# Install the package!pip3 install cerebrium
```

## Imports[​](#imports "Direct link to Imports")

```
import osfrom langchain.chains import LLMChainfrom langchain_community.llms import CerebriumAIfrom langchain_core.prompts import PromptTemplate
```

## Set the Environment API Key[​](#set-the-environment-api-key "Direct link to Set the Environment API Key")

Make sure to get your API key from CerebriumAI. See [here](https://dashboard.cerebrium.ai/login). You are given a 1 hour free of serverless GPU compute to test different models.

```
os.environ["CEREBRIUMAI_API_KEY"] = "YOUR_KEY_HERE"
```

## Create the CerebriumAI instance[​](#create-the-cerebriumai-instance "Direct link to Create the CerebriumAI instance")

You can specify different parameters such as the model endpoint url, max length, temperature, etc. You must provide an endpoint url.

```
llm = CerebriumAI(endpoint_url="YOUR ENDPOINT URL HERE")
```

## Create a Prompt Template[​](#create-a-prompt-template "Direct link to Create a Prompt Template")

We will create a prompt template for Question and Answer.

```
template = """Question: {question}Answer: Let's think step by step."""prompt = PromptTemplate.from_template(template)
```

## Initiate the LLMChain[​](#initiate-the-llmchain "Direct link to Initiate the LLMChain")

```
llm_chain = LLMChain(prompt=prompt, llm=llm)
```

## Run the LLMChain[​](#run-the-llmchain "Direct link to Run the LLMChain")

Provide a question and run the LLMChain.

```
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"llm_chain.run(question)
```

* * *

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