seansullivan/Langchain-Docs huggingface dataset:
## Baidu Cloud ElasticSearch VectorSearch

> [Baidu Cloud VectorSearch](https://cloud.baidu.com/doc/BES/index.html?from=productToDoc) is a fully managed, enterprise-level distributed search and analysis service which is 100% compatible to open source. Baidu Cloud VectorSearch provides low-cost, high-performance, and reliable retrieval and analysis platform level product services for structured/unstructured data. As a vector database , it supports multiple index types and similarity distance methods.

> `Baidu Cloud ElasticSearch` provides a privilege management mechanism, for you to configure the cluster privileges freely, so as to further ensure data security.

This notebook shows how to use functionality related to the `Baidu Cloud ElasticSearch VectorStore`. To run, you should have an [Baidu Cloud ElasticSearch](https://cloud.baidu.com/product/bes.html) instance up and running:

Read the [help document](https://cloud.baidu.com/doc/BES/s/8llyn0hh4) to quickly familiarize and configure Baidu Cloud ElasticSearch instance.

After the instance is up and running, follow these steps to split documents, get embeddings, connect to the baidu cloud elasticsearch instance, index documents, and perform vector retrieval.

We need to install the following Python packages first.

```
%pip install --upgrade --quiet  elasticsearch == 7.11.0
```

First, we want to use `QianfanEmbeddings` so we have to get the Qianfan AK and SK. Details for QianFan is related to [Baidu Qianfan Workshop](https://cloud.baidu.com/product/wenxinworkshop)

```
import getpassimport osos.environ["QIANFAN_AK"] = getpass.getpass("Your Qianfan AK:")os.environ["QIANFAN_SK"] = getpass.getpass("Your Qianfan SK:")
```

Secondly, split documents and get embeddings.

```
from langchain_community.document_loaders import TextLoaderfrom langchain_text_splitters import CharacterTextSplitterloader = TextLoader("../../../state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)from langchain_community.embeddings import QianfanEmbeddingsEndpointembeddings = QianfanEmbeddingsEndpoint()
```

Then, create a Baidu ElasticeSearch accessable instance.

```
# Create a bes instance and index docs.from langchain_community.vectorstores import BESVectorStorebes = BESVectorStore.from_documents(    documents=docs,    embedding=embeddings,    bes_url="your bes cluster url",    index_name="your vector index",)bes.client.indices.refresh(index="your vector index")
```

Finally, Query and retrive data

```
query = "What did the president say about Ketanji Brown Jackson"docs = bes.similarity_search(query)print(docs[0].page_content)
```

Please feel free to contact [liuboyao@baidu.com](mailto:liuboyao@baidu.com) or [chenweixu01@baidu.com](mailto:chenweixu01@baidu.com) if you encounter any problems during use, and we will do our best to support you.
## Baidu VectorDB

> [Baidu VectorDB](https://cloud.baidu.com/product/vdb.html) is a robust, enterprise-level distributed database service, meticulously developed and fully managed by Baidu Intelligent Cloud. It stands out for its exceptional ability to store, retrieve, and analyze multi-dimensional vector data. At its core, VectorDB operates on Baidu’s proprietary “Mochow” vector database kernel, which ensures high performance, availability, and security, alongside remarkable scalability and user-friendliness.

> This database service supports a diverse range of index types and similarity calculation methods, catering to various use cases. A standout feature of VectorDB is its capacity to manage an immense vector scale of up to 10 billion, while maintaining impressive query performance, supporting millions of queries per second (QPS) with millisecond-level query latency.

This notebook shows how to use functionality related to the Baidu VectorDB.

To run, you should have a [Database instance.](https://cloud.baidu.com/doc/VDB/s/hlrsoazuf).

```
from langchain.text_splitter import CharacterTextSplitterfrom langchain_community.document_loaders import TextLoaderfrom langchain_community.embeddings.fake import FakeEmbeddingsfrom langchain_community.vectorstores import BaiduVectorDBfrom langchain_community.vectorstores.baiduvectordb import ConnectionParams
```

```
loader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = FakeEmbeddings(size=128)
```

```
conn_params = ConnectionParams(    endpoint="http://192.168.xx.xx:xxxx", account="root", api_key="****")vector_db = BaiduVectorDB.from_documents(    docs, embeddings, connection_params=conn_params, drop_old=True)
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = vector_db.similarity_search(query)docs[0].page_content
```

```
vector_db = BaiduVectorDB(embeddings, conn_params)vector_db.add_texts(["Ankush went to Princeton"])query = "Where did Ankush go to college?"docs = vector_db.max_marginal_relevance_search(query)docs[0].page_content
```

* * *

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

This notebook shows how to use IFTTT Webhooks.

From [https://github.com/SidU/teams-langchain-js/wiki/Connecting-IFTTT-Services](https://github.com/SidU/teams-langchain-js/wiki/Connecting-IFTTT-Services).

## Creating a webhook[​](#creating-a-webhook "Direct link to Creating a webhook")

*   Go to [https://ifttt.com/create](https://ifttt.com/create)

## Configuring the “If This”[​](#configuring-the-if-this "Direct link to Configuring the “If This”")

*   Click on the “If This” button in the IFTTT interface.
*   Search for “Webhooks” in the search bar.
*   Choose the first option for “Receive a web request with a JSON payload.”
*   Choose an Event Name that is specific to the service you plan to connect to. This will make it easier for you to manage the webhook URL. For example, if you’re connecting to Spotify, you could use “Spotify” as your Event Name.
*   Click the “Create Trigger” button to save your settings and create your webhook.

## Configuring the “Then That”[​](#configuring-the-then-that "Direct link to Configuring the “Then That”")

*   Tap on the “Then That” button in the IFTTT interface.
*   Search for the service you want to connect, such as Spotify.
*   Choose an action from the service, such as “Add track to a playlist”.
*   Configure the action by specifying the necessary details, such as the playlist name, e.g., “Songs from AI”.
*   Reference the JSON Payload received by the Webhook in your action. For the Spotify scenario, choose “{{JsonPayload}}” as your search query.
*   Tap the “Create Action” button to save your action settings.
*   Once you have finished configuring your action, click the “Finish” button to complete the setup.
*   Congratulations! You have successfully connected the Webhook to the desired service, and you’re ready to start receiving data and triggering actions 🎉

## Finishing up[​](#finishing-up "Direct link to Finishing up")

*   To get your webhook URL go to [https://ifttt.com/maker\_webhooks/settings](https://ifttt.com/maker_webhooks/settings)
*   Copy the IFTTT key value from there. The URL is of the form [https://maker.ifttt.com/use/YOUR\_IFTTT\_KEY](https://maker.ifttt.com/use/YOUR_IFTTT_KEY). Grab the YOUR\_IFTTT\_KEY value.

```
from langchain_community.tools.ifttt import IFTTTWebhook
```

```
import oskey = os.environ["IFTTTKey"]url = f"https://maker.ifttt.com/trigger/spotify/json/with/key/{key}"tool = IFTTTWebhook(    name="Spotify", description="Add a song to spotify playlist", url=url)
```

```
"Congratulations! You've fired the spotify JSON event"
```

* * *

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

> [Huggingface Tools](https://huggingface.co/docs/transformers/v4.29.0/en/custom_tools) that supporting text I/O can be loaded directly using the `load_huggingface_tool` function.

```
# Requires transformers>=4.29.0 and huggingface_hub>=0.14.1%pip install --upgrade --quiet  transformers huggingface_hub > /dev/null
```

```
from langchain.agents import load_huggingface_tooltool = load_huggingface_tool("lysandre/hf-model-downloads")print(f"{tool.name}: {tool.description}")
```

```
model_download_counter: This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. It takes the name of the category (such as text-classification, depth-estimation, etc), and returns the name of the checkpoint
```

```
tool.run("text-classification")
```

```
'facebook/bart-large-mnli'
```

* * *

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

[Ionic](https://www.ioniccommerce.com/) is a plug and play ecommerce marketplace for AI Assistants. By including the [Ionic Tool](https://github.com/ioniccommerce/ionic_langchain) in your agent, you are effortlessly providing your users with the ability to shop and transact directly within your agent, and you’ll get a cut of the transaction.

This is a basic jupyter notebook demonstrating how to integrate the Ionic Tool into your agent. For more information on setting up your Agent with Ionic, see the Ionic [documentation](https://docs.ioniccommerce.com/introduction).

This Jupyter Notebook demonstrates how to use the Ionic tool with an Agent.

**Note: The ionic-langchain package is maintained by the Ionic Commerce team, not the LangChain maintainers.**

* * *

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

```
pip install langchain langchain_openai langchainhub
```

```
pip install ionic-langchain
```

## Setup Agent[​](#setup-agent "Direct link to Setup Agent")

```
from ionic_langchain.tool import Ionic, IonicToolfrom langchain import hubfrom langchain.agents import AgentExecutor, Tool, create_react_agentfrom langchain_openai import OpenAI# Based on ReAct Agent# https://python.langchain.com/docs/modules/agents/agent_types/react# Please reach out to support@ionicapi.com for help with add'l agent types.open_ai_key = "YOUR KEY HERE"model = "gpt-3.5-turbo-instruct"temperature = 0.6llm = OpenAI(openai_api_key=open_ai_key, model_name=model, temperature=temperature)ionic_tool = IonicTool().tool()# The tool comes with its own prompt,# but you may also update it directly via the description attribute:ionic_tool.description = str(    """Ionic is an e-commerce shopping tool. Assistant uses the Ionic Commerce Shopping Tool to find, discover, and compare products from thousands of online retailers. Assistant should use the tool when the user is looking for a product recommendation or trying to find a specific product.The user may specify the number of results, minimum price, and maximum price for which they want to see results.Ionic Tool input is a comma-separated string of values:  - query string (required, must not include commas)  - number of results (default to 4, no more than 10)  - minimum price in cents ($5 becomes 500)  - maximum price in centsFor example, if looking for coffee beans between 5 and 10 dollars, the tool input would be `coffee beans, 5, 500, 1000`.Return them as a markdown formatted list with each recommendation from tool results, being sure to include the full PDP URL. For example:1. Product 1: [Price] -- link2. Product 2: [Price] -- link3. Product 3: [Price] -- link4. Product 4: [Price] -- link""")tools = [ionic_tool]# default prompt for create_react_agentprompt = hub.pull("hwchase17/react")agent = create_react_agent(    llm,    tools,    prompt=prompt,)agent_executor = AgentExecutor(    agent=agent, tools=tools, handle_parsing_errors=True, verbose=True, max_iterations=5)
```

## Run[​](#run "Direct link to Run")

```
input = (    "I'm looking for a new 4k monitor can you find me some options for less than $1000")agent_executor.invoke({"input": input})
```
## Memorize

Fine-tuning LLM itself to memorize information using unsupervised learning.

This tool requires LLMs that support fine-tuning. Currently, only `langchain.llms import GradientLLM` is supported.

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

```
import osfrom langchain.agents import AgentExecutor, AgentType, initialize_agent, load_toolsfrom langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryfrom langchain_community.llms import GradientLLM
```

## 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 Gradient AI. You are given \\$10 in free credits to test and fine-tune different models.

```
from getpass import getpassif not os.environ.get("GRADIENT_ACCESS_TOKEN", None):    # Access token under https://auth.gradient.ai/select-workspace    os.environ["GRADIENT_ACCESS_TOKEN"] = getpass("gradient.ai access token:")if not os.environ.get("GRADIENT_WORKSPACE_ID", None):    # `ID` listed in `$ gradient workspace list`    # also displayed after login at at https://auth.gradient.ai/select-workspace    os.environ["GRADIENT_WORKSPACE_ID"] = getpass("gradient.ai workspace id:")if not os.environ.get("GRADIENT_MODEL_ADAPTER_ID", None):    # `ID` listed in `$ gradient model list --workspace-id "$GRADIENT_WORKSPACE_ID"`    os.environ["GRADIENT_MODEL_ID"] = getpass("gradient.ai model id:")
```

Optional: Validate your Environment variables `GRADIENT_ACCESS_TOKEN` and `GRADIENT_WORKSPACE_ID` to get currently deployed models.

## Create the `GradientLLM` instance[​](#create-the-gradientllm-instance "Direct link to create-the-gradientllm-instance")

You can specify different parameters such as the model name, max tokens generated, temperature, etc.

```
llm = GradientLLM(    model_id=os.environ["GRADIENT_MODEL_ID"],    # # optional: set new credentials, they default to environment variables    # gradient_workspace_id=os.environ["GRADIENT_WORKSPACE_ID"],    # gradient_access_token=os.environ["GRADIENT_ACCESS_TOKEN"],)
```

```
tools = load_tools(["memorize"], llm=llm)
```

## Initiate the Agent[​](#initiate-the-agent "Direct link to Initiate the Agent")

```
agent = initialize_agent(    tools,    llm,    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,    verbose=True,    # memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),)
```

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

Ask the agent to memorize a piece of text.

```
agent.run(    "Please remember the fact in detail:\nWith astonishing dexterity, Zara Tubikova set a world record by solving a 4x4 Rubik's Cube variation blindfolded in under 20 seconds, employing only their feet.")
```

```
> Entering new AgentExecutor chain...I should memorize this fact.Action: MemorizeAction Input: Zara TObservation: Train complete. Loss: 1.6853971333333335Thought:I now know the final answer.Final Answer: Zara Tubikova set a world> Finished chain.
```

```
'Zara Tubikova set a world'
```
## Python REPL

Sometimes, for complex calculations, rather than have an LLM generate the answer directly, it can be better to have the LLM generate code to calculate the answer, and then run that code to get the answer. In order to easily do that, we provide a simple Python REPL to execute commands in.

This interface will only return things that are printed - therefore, if you want to use it to calculate an answer, make sure to have it print out the answer.

```
from langchain.agents import Toolfrom langchain_experimental.utilities import PythonREPL
```

```
python_repl = PythonREPL()
```

```
python_repl.run("print(1+1)")
```

```
Python REPL can execute arbitrary code. Use with caution.
```

```
# You can create the tool to pass to an agentrepl_tool = Tool(    name="python_repl",    description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",    func=python_repl.run,)
```
This notebook goes over how to use `PubMed` as a tool.

```
'Published: 2024-02-10\nTitle: circEPB41L2 blocks the progression and metastasis in non-small cell lung cancer by promoting TRIP12-triggered PTBP1 ubiquitylation.\nCopyright Information: © 2024. The Author(s).\nSummary::\nThe metastasis of non-small cell lung cancer (NSCLC) is the leading death cause of NSCLC patients, which requires new biomarkers for precise diagnosis and treatment. Circular RNAs (circRNAs), the novel noncoding RNA, participate in the progression of various cancers as microRNA or protein sponges. We revealed the mechanism by which circEPB41L2 (hsa_circ_0077837) blocks the aerobic glycolysis, progression and metastasis of NSCLC through modulating protein metabolism of PTBP1 by the E3 ubiquitin ligase TRIP12. With ribosomal RNA-depleted RNA seq, 57 upregulated and 327 downregulated circRNAs were identified in LUAD tissues. circEPB41L2 was selected due to its dramatically reduced levels in NSCLC tissues and NSCLC cells. Interestingly, circEPB41L2 blocked glucose uptake, lactate production, NSCLC cell proliferation, migration and invasion in vitro and in vivo. Mechanistically, acting as a scaffold, circEPB41L2 bound to the RRM1 domain of the PTBP1 and the E3 ubiquitin ligase TRIP12 to promote TRIP12-mediated PTBP1 polyubiquitylation and degradation, which could be reversed by the HECT domain mutation of TRIP12 and circEPB41L2 depletion. As a result, circEPB41L2-induced PTBP1 inhibition led to PTBP1-induced PKM2 and Vimentin activation but PKM1 and E-cadherin inactivation. These findings highlight the circEPB41L2-dependent mechanism that modulates the "Warburg Effect" and EMT to inhibit NSCLC development and metastasis, offering an inhibitory target for NSCLC treatment.\n\nPublished: 2024-01-17\nTitle: The safety of seasonal influenza vaccination among adults prescribed immune checkpoint inhibitors: A self-controlled case series study using administrative data.\nCopyright Information: Copyright © 2024 The Author(s). Published by Elsevier Ltd.. All rights reserv'
```
## OpenWeatherMap

This notebook goes over how to use the `OpenWeatherMap` component to fetch weather information.

First, you need to sign up for an `OpenWeatherMap API` key:

1.  Go to OpenWeatherMap and sign up for an API key [here](https://openweathermap.org/api/)
2.  pip install pyowm

Then we will need to set some environment variables: 1. Save your API KEY into OPENWEATHERMAP\_API\_KEY env variable

## Use the wrapper[​](#use-the-wrapper "Direct link to Use the wrapper")

```
import osfrom langchain_community.utilities import OpenWeatherMapAPIWrapperos.environ["OPENWEATHERMAP_API_KEY"] = ""weather = OpenWeatherMapAPIWrapper()
```

```
weather_data = weather.run("London,GB")print(weather_data)
```

```
In London,GB, the current weather is as follows:Detailed status: broken cloudsWind speed: 2.57 m/s, direction: 240°Humidity: 55%Temperature:   - Current: 20.12°C  - High: 21.75°C  - Low: 18.68°C  - Feels like: 19.62°CRain: {}Heat index: NoneCloud cover: 75%
```

```
import osfrom langchain.agents import AgentType, initialize_agent, load_toolsfrom langchain_openai import OpenAIos.environ["OPENAI_API_KEY"] = ""os.environ["OPENWEATHERMAP_API_KEY"] = ""llm = OpenAI(temperature=0)tools = load_tools(["openweathermap-api"], llm)agent_chain = initialize_agent(    tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
```

```
agent_chain.run("What's the weather like in London?")
```

```
> Entering new AgentExecutor chain... I need to find out the current weather in London.Action: OpenWeatherMapAction Input: London,GBObservation: In London,GB, the current weather is as follows:Detailed status: broken cloudsWind speed: 2.57 m/s, direction: 240°Humidity: 56%Temperature:   - Current: 20.11°C  - High: 21.75°C  - Low: 18.68°C  - Feels like: 19.64°CRain: {}Heat index: NoneCloud cover: 75%Thought: I now know the current weather in London.Final Answer: The current weather in London is broken clouds, with a wind speed of 2.57 m/s, direction 240°, humidity of 56%, temperature of 20.11°C, high of 21.75°C, low of 18.68°C, and a heat index of None.> Finished chain.
```

```
'The current weather in London is broken clouds, with a wind speed of 2.57 m/s, direction 240°, humidity of 56%, temperature of 20.11°C, high of 21.75°C, low of 18.68°C, and a heat index of None.'
```

* * *

#### Help us out by providing feedback on this documentation page:
[SceneXplain](https://scenex.jina.ai/) is an ImageCaptioning service accessible through the SceneXplain Tool.

To use this tool, you’ll need to make an account and fetch your API Token [from the website](https://scenex.jina.ai/api). Then you can instantiate the tool.

Or directly instantiate the tool.

```
from langchain.agents import initialize_agentfrom langchain.memory import ConversationBufferMemoryfrom langchain_openai import OpenAIllm = OpenAI(temperature=0)memory = ConversationBufferMemory(memory_key="chat_history")agent = initialize_agent(    tools, llm, memory=memory, agent="conversational-react-description", verbose=True)output = agent.run(    input=(        "What is in this image https://storage.googleapis.com/causal-diffusion.appspot.com/imagePrompts%2F0rw369i5h9t%2Foriginal.png. "        "Is it movie or a game? If it is a movie, what is the name of the movie?"    ))print(output)
```

```
> Entering new AgentExecutor chain...Thought: Do I need to use a tool? YesAction: Image ExplainerAction Input: https://storage.googleapis.com/causal-diffusion.appspot.com/imagePrompts%2F0rw369i5h9t%2Foriginal.pngObservation: In a charmingly whimsical scene, a young girl is seen braving the rain alongside her furry companion, the lovable Totoro. The two are depicted standing on a bustling street corner, where they are sheltered from the rain by a bright yellow umbrella. The girl, dressed in a cheerful yellow frock, holds onto the umbrella with both hands while gazing up at Totoro with an expression of wonder and delight.Totoro, meanwhile, stands tall and proud beside his young friend, holding his own umbrella aloft to protect them both from the downpour. His furry body is rendered in rich shades of grey and white, while his large ears and wide eyes lend him an endearing charm.In the background of the scene, a street sign can be seen jutting out from the pavement amidst a flurry of raindrops. A sign with Chinese characters adorns its surface, adding to the sense of cultural diversity and intrigue. Despite the dreary weather, there is an undeniable sense of joy and camaraderie in this heartwarming image.Thought: Do I need to use a tool? NoAI: This image appears to be a still from the 1988 Japanese animated fantasy film My Neighbor Totoro. The film follows two young girls, Satsuki and Mei, as they explore the countryside and befriend the magical forest spirits, including the titular character Totoro.> Finished chain.This image appears to be a still from the 1988 Japanese animated fantasy film My Neighbor Totoro. The film follows two young girls, Satsuki and Mei, as they explore the countryside and befriend the magical forest spirits, including the titular character Totoro.
```
## SerpAPI

This notebook goes over how to use the SerpAPI component to search the web.

```
from langchain_community.utilities import SerpAPIWrapper
```

```
search = SerpAPIWrapper()
```

```
search.run("Obama's first name?")
```

```
'Barack Hussein Obama II'
```

## Custom Parameters[​](#custom-parameters "Direct link to Custom Parameters")

You can also customize the SerpAPI wrapper with arbitrary parameters. For example, in the below example we will use `bing` instead of `google`.

```
params = {    "engine": "bing",    "gl": "us",    "hl": "en",}search = SerpAPIWrapper(params=params)
```

```
search.run("Obama's first name?")
```

```
'Barack Hussein Obama II is an American politician who served as the 44th president of the United States from 2009 to 2017. A member of the Democratic Party, Obama was the first African-American presi…New content will be added above the current area of focus upon selectionBarack Hussein Obama II is an American politician who served as the 44th president of the United States from 2009 to 2017. A member of the Democratic Party, Obama was the first African-American president of the United States. He previously served as a U.S. senator from Illinois from 2005 to 2008 and as an Illinois state senator from 1997 to 2004, and previously worked as a civil rights lawyer before entering politics.Wikipediabarackobama.com'
```

```
from langchain.agents import Tool# You can create the tool to pass to an agentrepl_tool = Tool(    name="python_repl",    description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",    func=search.run,)
```

* * *

#### Help us out by providing feedback on this documentation page:
[Stack Exchange](https://stackexchange.com/) is a network of question-and-answer (Q&A) websites on topics in diverse fields, each site covering a specific topic, where questions, answers, and users are subject to a reputation award process. The reputation system allows the sites to be self-moderating.

The `StackExchange` component integrates the StackExchange API into LangChain allowing access to the [StackOverflow](https://stackoverflow.com/) site of the Stack Excchange network. Stack Overflow focuses on computer programming.

This notebook goes over how to use the `StackExchange` component.

We first have to install the python package stackapi which implements the Stack Exchange API.

```
from langchain_community.utilities import StackExchangeAPIWrapperstackexchange = StackExchangeAPIWrapper()stackexchange.run("zsh: command not found: python")
```
## DashVector

> [DashVector](https://help.aliyun.com/document_detail/2510225.html) is a fully-managed vectorDB service that supports high-dimension dense and sparse vectors, real-time insertion and filtered search. It is built to scale automatically and can adapt to different application requirements.

This notebook shows how to use functionality related to the `DashVector` vector database.

To use DashVector, you must have an API key. Here are the [installation instructions](https://help.aliyun.com/document_detail/2510223.html).

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

```
%pip install --upgrade --quiet  dashvector dashscope
```

We want to use `DashScopeEmbeddings` so we also have to get the Dashscope API Key.

```
import getpassimport osos.environ["DASHVECTOR_API_KEY"] = getpass.getpass("DashVector API Key:")os.environ["DASHSCOPE_API_KEY"] = getpass.getpass("DashScope API Key:")
```

## Example[​](#example "Direct link to Example")

```
from langchain_community.embeddings.dashscope import DashScopeEmbeddingsfrom langchain_community.vectorstores import DashVectorfrom langchain_text_splitters import CharacterTextSplitter
```

```
from langchain_community.document_loaders import TextLoaderloader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = DashScopeEmbeddings()
```

We can create DashVector from documents.

```
dashvector = DashVector.from_documents(docs, embeddings)query = "What did the president say about Ketanji Brown Jackson"docs = dashvector.similarity_search(query)print(docs)
```

We can add texts with meta datas and ids, and search with meta filter.

```
texts = ["foo", "bar", "baz"]metadatas = [{"key": i} for i in range(len(texts))]ids = ["0", "1", "2"]dashvector.add_texts(texts, metadatas=metadatas, ids=ids)docs = dashvector.similarity_search("foo", filter="key = 2")print(docs)
```

```
[Document(page_content='baz', metadata={'key': 2})]
```

### Operating band `partition` parameters[​](#operating-band-partition-parameters "Direct link to operating-band-partition-parameters")

The `partition` parameter defaults to default, and if a non-existent `partition` parameter is passed in, the `partition` will be created automatically.

```
texts = ["foo", "bar", "baz"]metadatas = [{"key": i} for i in range(len(texts))]ids = ["0", "1", "2"]partition = "langchain"# add textsdashvector.add_texts(texts, metadatas=metadatas, ids=ids, partition=partition)# similarity searchquery = "What did the president say about Ketanji Brown Jackson"docs = dashvector.similarity_search(query, partition=partition)# deletedashvector.delete(ids=ids, partition=partition)
```
## Tair

> [Tair](https://www.alibabacloud.com/help/en/tair/latest/what-is-tair) is a cloud native in-memory database service developed by `Alibaba Cloud`. It provides rich data models and enterprise-grade capabilities to support your real-time online scenarios while maintaining full compatibility with open-source `Redis`. `Tair` also introduces persistent memory-optimized instances that are based on the new non-volatile memory (NVM) storage medium.

This notebook shows how to use functionality related to the `Tair` vector database.

To run, you should have a `Tair` instance up and running.

```
from langchain_community.embeddings.fake import FakeEmbeddingsfrom langchain_community.vectorstores import Tairfrom langchain_text_splitters import CharacterTextSplitter
```

```
from langchain_community.document_loaders import TextLoaderloader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = FakeEmbeddings(size=128)
```

Connect to Tair using the `TAIR_URL` environment variable

```
export TAIR_URL="redis://{username}:{password}@{tair_address}:{tair_port}"
```

or the keyword argument `tair_url`.

Then store documents and embeddings into Tair.

```
tair_url = "redis://localhost:6379"# drop first if index already existsTair.drop_index(tair_url=tair_url)vector_store = Tair.from_documents(docs, embeddings, tair_url=tair_url)
```

Query similar documents.

```
query = "What did the president say about Ketanji Brown Jackson"docs = vector_store.similarity_search(query)docs[0]
```

Tair Hybrid Search Index build

```
# drop first if index already existsTair.drop_index(tair_url=tair_url)vector_store = Tair.from_documents(    docs, embeddings, tair_url=tair_url, index_params={"lexical_algorithm": "bm25"})
```

Tair Hybrid Search

```
query = "What did the president say about Ketanji Brown Jackson"# hybrid_ratio: 0.5 hybrid search, 0.9999 vector search, 0.0001 text searchkwargs = {"TEXT": query, "hybrid_ratio": 0.5}docs = vector_store.similarity_search(query, **kwargs)docs[0]
```
## NucliaDB

You can use a local NucliaDB instance or use [Nuclia Cloud](https://nuclia.cloud/).

When using a local instance, you need a Nuclia Understanding API key, so your texts are properly vectorized and indexed. You can get a key by creating a free account at [https://nuclia.cloud](https://nuclia.cloud/), and then [create a NUA key](https://docs.nuclia.dev/docs/docs/using/understanding/intro).

```
%pip install --upgrade --quiet  langchain nuclia
```

## Usage with nuclia.cloud[​](#usage-with-nuclia.cloud "Direct link to Usage with nuclia.cloud")

```
from langchain_community.vectorstores.nucliadb import NucliaDBAPI_KEY = "YOUR_API_KEY"ndb = NucliaDB(knowledge_box="YOUR_KB_ID", local=False, api_key=API_KEY)
```

## Usage with a local instance[​](#usage-with-a-local-instance "Direct link to Usage with a local instance")

Note: By default `backend` is set to `http://localhost:8080`.

```
from langchain_community.vectorstores.nucliadb import NucliaDBndb = NucliaDB(knowledge_box="YOUR_KB_ID", local=True, backend="http://my-local-server")
```

## Add and delete texts to your Knowledge Box[​](#add-and-delete-texts-to-your-knowledge-box "Direct link to Add and delete texts to your Knowledge Box")

```
ids = ndb.add_texts(["This is a new test", "This is a second test"])
```

## Search in your Knowledge Box[​](#search-in-your-knowledge-box "Direct link to Search in your Knowledge Box")

```
results = ndb.similarity_search("Who was inspired by Ada Lovelace?")print(results[0].page_content)
```
## Tigris

> [Tigris](https://tigrisdata.com/) is an open-source Serverless NoSQL Database and Search Platform designed to simplify building high-performance vector search applications. `Tigris` eliminates the infrastructure complexity of managing, operating, and synchronizing multiple tools, allowing you to focus on building great applications instead.

This notebook guides you how to use Tigris as your VectorStore

**Pre requisites** 1. An OpenAI account. You can sign up for an account [here](https://platform.openai.com/) 2. [Sign up for a free Tigris account](https://console.preview.tigrisdata.cloud/). Once you have signed up for the Tigris account, create a new project called `vectordemo`. Next, make a note of the _Uri_ for the region you’ve created your project in, the **clientId** and **clientSecret**. You can get all this information from the **Application Keys** section of the project.

Let’s first install our dependencies:

```
%pip install --upgrade --quiet  tigrisdb openapi-schema-pydantic langchain-openai tiktoken
```

We will load the `OpenAI` api key and `Tigris` credentials in our environment

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")os.environ["TIGRIS_PROJECT"] = getpass.getpass("Tigris Project Name:")os.environ["TIGRIS_CLIENT_ID"] = getpass.getpass("Tigris Client Id:")os.environ["TIGRIS_CLIENT_SECRET"] = getpass.getpass("Tigris Client Secret:")
```

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.vectorstores import Tigrisfrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import CharacterTextSplitter
```

### Initialize Tigris vector store[​](#initialize-tigris-vector-store "Direct link to Initialize Tigris vector store")

Let’s import our test dataset:

```
loader = TextLoader("../../../state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()
```

```
vector_store = Tigris.from_documents(docs, embeddings, index_name="my_embeddings")
```

### Similarity Search[​](#similarity-search "Direct link to Similarity Search")

```
query = "What did the president say about Ketanji Brown Jackson"found_docs = vector_store.similarity_search(query)print(found_docs)
```

### Similarity Search with score (vector distance)[​](#similarity-search-with-score-vector-distance "Direct link to Similarity Search with score (vector distance)")

```
query = "What did the president say about Ketanji Brown Jackson"result = vector_store.similarity_search_with_score(query)for doc, score in result:    print(f"document={doc}, score={score}")
```
## DuckDB

This notebook shows how to use `DuckDB` as a vector store.

We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

```
from langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores import DuckDB
```

```
from langchain.document_loaders import TextLoaderfrom langchain_text_splitters import CharacterTextSplitterloader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()documents = CharacterTextSplitter().split_documents(documents)embeddings = OpenAIEmbeddings()
```

```
docsearch = DuckDB.from_documents(documents, embeddings)query = "What did the president say about Ketanji Brown Jackson"docs = docsearch.similarity_search(query)
```

```
print(docs[0].page_content)
```

* * *

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

> [TileDB](https://github.com/TileDB-Inc/TileDB) is a powerful engine for indexing and querying dense and sparse multi-dimensional arrays.

> TileDB offers ANN search capabilities using the [TileDB-Vector-Search](https://github.com/TileDB-Inc/TileDB-Vector-Search) module. It provides serverless execution of ANN queries and storage of vector indexes both on local disk and cloud object stores (i.e. AWS S3).

More details in: - [Why TileDB as a Vector Database](https://tiledb.com/blog/why-tiledb-as-a-vector-database) - [TileDB 101: Vector Search](https://tiledb.com/blog/tiledb-101-vector-search)

This notebook shows how to use the `TileDB` vector database.

```
%pip install --upgrade --quiet  tiledb-vector-search
```

## Basic Example[​](#basic-example "Direct link to Basic Example")

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import TileDBfrom langchain_text_splitters import CharacterTextSplitterraw_documents = TextLoader("../../modules/state_of_the_union.txt").load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(raw_documents)embeddings = HuggingFaceEmbeddings()db = TileDB.from_documents(    documents, embeddings, index_uri="/tmp/tiledb_index", index_type="FLAT")
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = db.similarity_search(query)docs[0].page_content
```

### Similarity search by vector[​](#similarity-search-by-vector "Direct link to Similarity search by vector")

```
embedding_vector = embeddings.embed_query(query)docs = db.similarity_search_by_vector(embedding_vector)docs[0].page_content
```

### Similarity search with score[​](#similarity-search-with-score "Direct link to Similarity search with score")

```
docs_and_scores = db.similarity_search_with_score(query)docs_and_scores[0]
```

## Maximal Marginal Relevance Search (MMR)[​](#maximal-marginal-relevance-search-mmr "Direct link to Maximal Marginal Relevance Search (MMR)")

In addition to using similarity search in the retriever object, you can also use `mmr` as retriever.

```
retriever = db.as_retriever(search_type="mmr")retriever.get_relevant_documents(query)
```

Or use `max_marginal_relevance_search` directly:

```
db.max_marginal_relevance_search(query, k=2, fetch_k=10)
```
## Zilliz

> [Zilliz Cloud](https://zilliz.com/doc/quick_start) is a fully managed service on cloud for `LF AI Milvus®`,

This notebook shows how to use functionality related to the Zilliz Cloud managed vector database.

To run, you should have a `Zilliz Cloud` instance up and running. Here are the [installation instructions](https://zilliz.com/cloud)

```
%pip install --upgrade --quiet  pymilvus
```

We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key.

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

```
# replaceZILLIZ_CLOUD_URI = ""  # example: "https://in01-17f69c292d4a5sa.aws-us-west-2.vectordb.zillizcloud.com:19536"ZILLIZ_CLOUD_USERNAME = ""  # example: "username"ZILLIZ_CLOUD_PASSWORD = ""  # example: "*********"ZILLIZ_CLOUD_API_KEY = ""  # example: "*********" (for serverless clusters which can be used as replacements for user and password)
```

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.vectorstores import Milvusfrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import CharacterTextSplitter
```

```
from langchain_community.document_loaders import TextLoaderloader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()
```

```
vector_db = Milvus.from_documents(    docs,    embeddings,    connection_args={        "uri": ZILLIZ_CLOUD_URI,        "user": ZILLIZ_CLOUD_USERNAME,        "password": ZILLIZ_CLOUD_PASSWORD,        # "token": ZILLIZ_CLOUD_API_KEY,  # API key, for serverless clusters which can be used as replacements for user and password        "secure": True,    },)
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = vector_db.similarity_search(query)
```

```
'Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.'
```

* * *

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

> [Typesense](https://typesense.org/) is an open-source, in-memory search engine, that you can either [self-host](https://typesense.org/docs/guide/install-typesense#option-2-local-machine-self-hosting) or run on [Typesense Cloud](https://cloud.typesense.org/).
> 
> Typesense focuses on performance by storing the entire index in RAM (with a backup on disk) and also focuses on providing an out-of-the-box developer experience by simplifying available options and setting good defaults.
> 
> It also lets you combine attribute-based filtering together with vector queries, to fetch the most relevant documents.

This notebook shows you how to use Typesense as your VectorStore.

Let’s first install our dependencies:

```
%pip install --upgrade --quiet  typesense openapi-schema-pydantic langchain-openai tiktoken
```

We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key.

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

```
from langchain_community.document_loaders import TextLoaderfrom langchain_community.vectorstores import Typesensefrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import CharacterTextSplitter
```

Let’s import our test dataset:

```
loader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()
```

```
docsearch = Typesense.from_documents(    docs,    embeddings,    typesense_client_params={        "host": "localhost",  # Use xxx.a1.typesense.net for Typesense Cloud        "port": "8108",  # Use 443 for Typesense Cloud        "protocol": "http",  # Use https for Typesense Cloud        "typesense_api_key": "xyz",        "typesense_collection_name": "lang-chain",    },)
```

## Similarity Search[​](#similarity-search "Direct link to Similarity Search")

```
query = "What did the president say about Ketanji Brown Jackson"found_docs = docsearch.similarity_search(query)
```

```
print(found_docs[0].page_content)
```

## Typesense as a Retriever[​](#typesense-as-a-retriever "Direct link to Typesense as a Retriever")

Typesense, as all the other vector stores, is a LangChain Retriever, by using cosine similarity.

```
retriever = docsearch.as_retriever()retriever
```

```
query = "What did the president say about Ketanji Brown Jackson"retriever.get_relevant_documents(query)[0]
```
## Epsilla

> [Epsilla](https://www.epsilla.com/) is an open-source vector database that leverages the advanced parallel graph traversal techniques for vector indexing. Epsilla is licensed under GPL-3.0.

This notebook shows how to use the functionalities related to the `Epsilla` vector database.

As a prerequisite, you need to have a running Epsilla vector database (for example, through our docker image), and install the `pyepsilla` package. View full docs at [docs](https://epsilla-inc.gitbook.io/epsilladb/quick-start).

```
!pip/pip3 install pyepsilla
```

We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

OpenAI API Key: ········

```
from langchain_community.vectorstores import Epsillafrom langchain_openai import OpenAIEmbeddings
```

```
from langchain_community.document_loaders import TextLoaderfrom langchain_text_splitters import CharacterTextSplitterloader = TextLoader("../../modules/state_of_the_union.txt")documents = loader.load()documents = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0).split_documents(    documents)embeddings = OpenAIEmbeddings()
```

Epsilla vectordb is running with default host “localhost” and port “8888”. We have a custom db path, db name and collection name instead of the default ones.

```
from pyepsilla import vectordbclient = vectordb.Client()vector_store = Epsilla.from_documents(    documents,    embeddings,    client,    db_path="/tmp/mypath",    db_name="MyDB",    collection_name="MyCollection",)
```

```
query = "What did the president say about Ketanji Brown Jackson"docs = vector_store.similarity_search(query)print(docs[0].page_content)
```

In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections.

We cannot let this happen.

Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections.

Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
You can create a custom handler to set on the object as well. In the example below, we’ll implement streaming with a custom handler.

```
from langchain_core.callbacks import BaseCallbackHandlerfrom langchain_core.messages import HumanMessagefrom langchain_openai import ChatOpenAIclass MyCustomHandler(BaseCallbackHandler):    def on_llm_new_token(self, token: str, **kwargs) -> None:        print(f"My custom handler, token: {token}")# To enable streaming, we pass in `streaming=True` to the ChatModel constructor# Additionally, we pass in a list with our custom handlerchat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[MyCustomHandler()])chat.invoke([HumanMessage(content="Tell me a joke")])
```

```
My custom handler, token: My custom handler, token: WhyMy custom handler, token:  donMy custom handler, token: 'tMy custom handler, token:  scientistsMy custom handler, token:  trustMy custom handler, token:  atomsMy custom handler, token: ?My custom handler, token:  My custom handler, token: BecauseMy custom handler, token:  theyMy custom handler, token:  makeMy custom handler, token:  upMy custom handler, token:  everythingMy custom handler, token: .My custom handler, token: 
```

```
AIMessage(content="Why don't scientists trust atoms? \n\nBecause they make up everything.", additional_kwargs={}, example=False)
```