seansullivan/Langchain-Docs huggingface dataset:
## AI21Embeddings

This notebook covers how to get started with AI21 embedding 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 AI21Embeddingsembeddings = AI21Embeddings()
```

```
embeddings.embed_query("My query to look up")
```

```
embeddings.embed_documents(    ["This is a content of the document", "This is another document"])
```

* * *

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

There are two possible ways to use Aleph Alpha’s semantic embeddings. If you have texts with a dissimilar structure (e.g. a Document and a Query) you would want to use asymmetric embeddings. Conversely, for texts with comparable structures, symmetric embeddings are the suggested approach.

## Asymmetric[​](#asymmetric "Direct link to Asymmetric")

```
from langchain_community.embeddings import AlephAlphaAsymmetricSemanticEmbedding
```

```
document = "This is a content of the document"query = "What is the content of the document?"
```

```
embeddings = AlephAlphaAsymmetricSemanticEmbedding(normalize=True, compress_to_size=128)
```

```
doc_result = embeddings.embed_documents([document])
```

```
query_result = embeddings.embed_query(query)
```

## Symmetric[​](#symmetric "Direct link to Symmetric")

```
from langchain_community.embeddings import AlephAlphaSymmetricSemanticEmbedding
```

```
text = "This is a test text"
```

```
embeddings = AlephAlphaSymmetricSemanticEmbedding(normalize=True, compress_to_size=128)
```

```
doc_result = embeddings.embed_documents([text])
```

```
query_result = embeddings.embed_query(text)
```

* * *

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

> [AwaDB](https://github.com/awa-ai/awadb) is an AI Native database for the search and storage of embedding vectors used by LLM Applications.

This notebook explains how to use `AwaEmbeddings` in LangChain.

## import the library[​](#import-the-library "Direct link to import the library")

```
from langchain_community.embeddings import AwaEmbeddings
```

```
Embedding = AwaEmbeddings()
```

## Set embedding model

Users can use `Embedding.set_model()` to specify the embedding model.  
The input of this function is a string which represents the model’s name.  
The list of currently supported models can be obtained [here](https://github.com/awa-ai/awadb)   

The **default model** is `all-mpnet-base-v2`, it can be used without setting.

```
text = "our embedding test"Embedding.set_model("all-mpnet-base-v2")
```

```
res_query = Embedding.embed_query("The test information")res_document = Embedding.embed_documents(["test1", "another test"])
```

* * *

#### Help us out by providing feedback on this documentation page:
The `LocalFileStore` is a persistent implementation of `ByteStore` that stores everything in a folder of your choosing.

```
from pathlib import Pathfrom langchain.storage import LocalFileStoreroot_path = Path.cwd() / "data"  # can also be a path set by a stringstore = LocalFileStore(root_path)store.mset([("k1", b"v1"), ("k2", b"v2")])print(store.mget(["k1", "k2"]))
```
## Baichuan Text Embeddings

As of today (Jan 25th, 2024) BaichuanTextEmbeddings ranks #1 in C-MTEB (Chinese Multi-Task Embedding Benchmark) leaderboard.

Leaderboard (Under Overall -\> Chinese section): [https://huggingface.co/spaces/mteb/leaderboard](https://huggingface.co/spaces/mteb/leaderboard)

Official Website: [https://platform.baichuan-ai.com/docs/text-Embedding](https://platform.baichuan-ai.com/docs/text-Embedding)

An API key is required to use this embedding model. You can get one by registering at [https://platform.baichuan-ai.com/docs/text-Embedding](https://platform.baichuan-ai.com/docs/text-Embedding).

BaichuanTextEmbeddings support 512 token window and preduces vectors with 1024 dimensions.

Please NOTE that BaichuanTextEmbeddings only supports Chinese text embedding. Multi-language support is coming soon.

```
from langchain_community.embeddings import BaichuanTextEmbeddingsembeddings = BaichuanTextEmbeddings(baichuan_api_key="sk-*")
```

Alternatively, you can set API key this way:

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

```
text_1 = "今天天气不错"text_2 = "今天阳光很好"query_result = embeddings.embed_query(text_1)query_result
```

```
doc_result = embeddings.embed_documents([text_1, text_2])doc_result
```
## Azure OpenAI

Let’s load the Azure OpenAI Embedding class with environment variables set to indicate to use Azure endpoints.

```
import osos.environ["AZURE_OPENAI_API_KEY"] = "..."os.environ["AZURE_OPENAI_ENDPOINT"] = "https://<your-endpoint>.openai.azure.com/"
```

```
from langchain_openai import AzureOpenAIEmbeddingsembeddings = AzureOpenAIEmbeddings(    azure_deployment="<your-embeddings-deployment-name>",    openai_api_version="2023-05-15",)
```

```
text = "this is a test document"
```

```
query_result = embeddings.embed_query(text)
```

```
doc_result = embeddings.embed_documents([text])
```

```
[-0.012222584727053133, 0.0072103982392216145, -0.014818063280923775, -0.026444746872933557, -0.0034330499700826883]
```

## \[Legacy\] When using `openai<1`[​](#legacy-when-using-openai1 "Direct link to legacy-when-using-openai1")

```
# set the environment variables needed for openai package to know to reach out to azureimport osos.environ["OPENAI_API_TYPE"] = "azure"os.environ["OPENAI_API_BASE"] = "https://<your-endpoint.openai.azure.com/"os.environ["OPENAI_API_KEY"] = "your AzureOpenAI key"os.environ["OPENAI_API_VERSION"] = "2023-05-15"
```

```
from langchain_openai import OpenAIEmbeddingsembeddings = OpenAIEmbeddings(deployment="your-embeddings-deployment-name")
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)
```

```
doc_result = embeddings.embed_documents([text])
```

* * *

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

> [Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like `AI21 Labs`, `Anthropic`, `Cohere`, `Meta`, `Stability AI`, and `Amazon` via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. Using `Amazon Bedrock`, you can easily experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and `Retrieval Augmented Generation` (`RAG`), and build agents that execute tasks using your enterprise systems and data sources. Since `Amazon Bedrock` is serverless, you don’t have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with.

```
%pip install --upgrade --quiet  boto3
```

```
from langchain_community.embeddings import BedrockEmbeddingsembeddings = BedrockEmbeddings(    credentials_profile_name="bedrock-admin", region_name="us-east-1")
```

```
embeddings.embed_query("This is a content of the document")
```

```
embeddings.embed_documents(    ["This is a content of the document", "This is another document"])
```

```
# async embed queryawait embeddings.aembed_query("This is a content of the document")
```

```
# async embed documentsawait embeddings.aembed_documents(    ["This is a content of the document", "This is another document"])
```

* * *

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

Let’s load the Bookend AI Embeddings class.

```
from langchain_community.embeddings import BookendEmbeddings
```

```
embeddings = BookendEmbeddings(    domain="your_domain",    api_token="your_api_token",    model_id="your_embeddings_model_id",)
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)
```

```
doc_result = embeddings.embed_documents([text])
```

* * *

#### Help us out by providing feedback on this documentation page:
> [LASER](https://github.com/facebookresearch/LASER/) is a Python library developed by the Meta AI Research team and used for creating multilingual sentence embeddings for over 147 languages as of 2/25/2024 - List of supported languages at [https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200)

## Dependencies[​](#dependencies "Direct link to Dependencies")

To use LaserEmbed with LangChain, install the `laser_encoders` Python package.

```
%pip install laser_encoders
```

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

```
from langchain_community.embeddings.laser import LaserEmbeddings
```

## Instantiating Laser[​](#instantiating-laser "Direct link to Instantiating Laser")

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

*   `lang: Optional[str]` \>If empty will default to using a multilingual LASER encoder model (called “laser2”). You can find the list of supported languages and lang\_codes [here](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) and [here](https://github.com/facebookresearch/LASER/blob/main/laser_encoders/language_list.py) .

```
# Ex Instantiationzembeddings = LaserEmbeddings(lang="eng_Latn")
```

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

### Generating document embeddings[​](#generating-document-embeddings "Direct link to Generating document embeddings")

```
document_embeddings = embeddings.embed_documents(    ["This is a sentence", "This is some other sentence"])
```

### Generating query embeddings[​](#generating-query-embeddings "Direct link to Generating query embeddings")

```
query_embeddings = embeddings.embed_query("This is a query")
```

* * *

#### Help us out by providing feedback on this documentation page:
```
from langchain_community.embeddings import HuggingFaceBgeEmbeddingsmodel_name = "BAAI/bge-small-en"model_kwargs = {"device": "cpu"}encode_kwargs = {"normalize_embeddings": True}hf = HuggingFaceBgeEmbeddings(    model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs)
```
## Clarifai

> [Clarifai](https://www.clarifai.com/) is an AI Platform that provides the full AI lifecycle ranging from data exploration, data labeling, model training, evaluation, and inference.

This example goes over how to use LangChain to interact with `Clarifai` [models](https://clarifai.com/explore/models). Text embedding models in particular can be found [here](https://clarifai.com/explore/models?page=1&perPage=24&filterData=%5B%7B%22field%22%3A%22model_type_id%22%2C%22value%22%3A%5B%22text-embedder%22%5D%7D%5D).

To use Clarifai, you must have an account and a Personal Access Token (PAT) key. [Check here](https://clarifai.com/settings/security) to get or create a PAT.

## Dependencies

```
# Install required dependencies%pip install --upgrade --quiet  clarifai
```

## Imports

Here we will be setting the personal access token. You can find your PAT under [settings/security](https://clarifai.com/settings/security) in your Clarifai account.

```
# Please login and get your API key from  https://clarifai.com/settings/securityfrom getpass import getpassCLARIFAI_PAT = getpass()
```

```
# Import the required modulesfrom langchain.chains import LLMChainfrom langchain_community.embeddings import ClarifaiEmbeddingsfrom langchain_core.prompts import PromptTemplate
```

## Input

Create a prompt template to be used with the LLM Chain:

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

## Setup

Set the user id and app id to the application in which the model resides. You can find a list of public models on [https://clarifai.com/explore/models](https://clarifai.com/explore/models)

You will have to also initialize the model id and if needed, the model version id. Some models have many versions, you can choose the one appropriate for your task.

```
USER_ID = "clarifai"APP_ID = "main"MODEL_ID = "BAAI-bge-base-en-v15"MODEL_URL = "https://clarifai.com/clarifai/main/models/BAAI-bge-base-en-v15"# Further you can also provide a specific model version as the model_version_id arg.# MODEL_VERSION_ID = "MODEL_VERSION_ID"
```

```
# Initialize a Clarifai embedding modelembeddings = ClarifaiEmbeddings(user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)# Initialize a clarifai embedding model using model URLembeddings = ClarifaiEmbeddings(model_url=MODEL_URL)# Alternatively you can initialize clarifai class with pat argument.
```

```
text = "roses are red violets are blue."text2 = "Make hay while the sun shines."
```

You can embed single line of your text using embed\_query function !

```
query_result = embeddings.embed_query(text)
```

Further to embed list of texts/documents use embed\_documents function.

```
doc_result = embeddings.embed_documents([text, text2])
```
## Cloudflare Workers AI

> [Cloudflare, Inc. (Wikipedia)](https://en.wikipedia.org/wiki/Cloudflare) is an American company that provides content delivery network services, cloud cybersecurity, DDoS mitigation, and ICANN-accredited domain registration services.

> [Cloudflare Workers AI](https://developers.cloudflare.com/workers-ai/) allows you to run machine learning models, on the `Cloudflare` network, from your code via REST API.

> [Cloudflare AI document](https://developers.cloudflare.com/workers-ai/models/text-embeddings/) listed all text embeddings models available.

## Setting up[​](#setting-up "Direct link to Setting up")

Both Cloudflare account ID and API token are required. Find how to obtain them from [this document](https://developers.cloudflare.com/workers-ai/get-started/rest-api/).

```
import getpassmy_account_id = getpass.getpass("Enter your Cloudflare account ID:\n\n")my_api_token = getpass.getpass("Enter your Cloudflare API token:\n\n")
```

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

```
from langchain_community.embeddings.cloudflare_workersai import (    CloudflareWorkersAIEmbeddings,)
```

```
embeddings = CloudflareWorkersAIEmbeddings(    account_id=my_account_id,    api_token=my_api_token,    model_name="@cf/baai/bge-small-en-v1.5",)# single string embeddingsquery_result = embeddings.embed_query("test")len(query_result), query_result[:3]
```

```
(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])
```

```
# string embeddings in batchesbatch_query_result = embeddings.embed_documents(["test1", "test2", "test3"])len(batch_query_result), len(batch_query_result[0])
```

* * *

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

Let’s load the [llamafile](https://github.com/Mozilla-Ocho/llamafile) Embeddings class.

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

First, the are 3 setup steps:

1.  Download a llamafile. In this notebook, we use `TinyLlama-1.1B-Chat-v1.0.Q5_K_M` but there are many others available on [HuggingFace](https://huggingface.co/models?other=llamafile).
2.  Make the llamafile executable.
3.  Start the llamafile in server mode.

You can run the following bash script to do all this:

```
%%bash# llamafile setup# Step 1: Download a llamafile. The download may take several minutes.wget -nv -nc https://huggingface.co/jartine/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile# Step 2: Make the llamafile executable. Note: if you're on Windows, just append '.exe' to the filename.chmod +x TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile# Step 3: Start llamafile server in background. All the server logs will be written to 'tinyllama.log'.# Alternatively, you can just open a separate terminal outside this notebook and run: #   ./TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile --server --nobrowser --embedding./TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile --server --nobrowser --embedding > tinyllama.log 2>&1 &pid=$!echo "${pid}" > .llamafile_pid  # write the process pid to a file so we can terminate the server later
```

## Embedding texts using LlamafileEmbeddings[​](#embedding-texts-using-llamafileembeddings "Direct link to Embedding texts using LlamafileEmbeddings")

Now, we can use the `LlamafileEmbeddings` class to interact with the llamafile server that’s currently serving our TinyLlama model at http://localhost:8080.

```
from langchain_community.embeddings import LlamafileEmbeddings
```

```
embedder = LlamafileEmbeddings()
```

```
text = "This is a test document."
```

To generate embeddings, you can either query an invidivual text, or you can query a list of texts.

```
query_result = embedder.embed_query(text)query_result[:5]
```

```
doc_result = embedder.embed_documents([text])doc_result[0][:5]
```

```
%%bash# cleanup: kill the llamafile server processkill $(cat .llamafile_pid)rm .llamafile_pid
```
## LLMRails

Let’s load the LLMRails Embeddings class.

To use LLMRails embedding you need to pass api key by argument or set it in environment with `LLM_RAILS_API_KEY` key. To gey API Key you need to sign up in [https://console.llmrails.com/signup](https://console.llmrails.com/signup) and then go to [https://console.llmrails.com/api-keys](https://console.llmrails.com/api-keys) and copy key from there after creating one key in platform.

```
from langchain_community.embeddings import LLMRailsEmbeddings
```

```
embeddings = LLMRailsEmbeddings(model="embedding-english-v1")  # or embedding-multi-v1
```

```
text = "This is a test document."
```

To generate embeddings, you can either query an invidivual text, or you can query a list of texts.

```
query_result = embeddings.embed_query(text)query_result[:5]
```

```
[-0.09996652603149414, 0.015568195842206478, 0.17670190334320068, 0.16521021723747253, 0.21193109452724457]
```

```
doc_result = embeddings.embed_documents([text])doc_result[0][:5]
```

```
[-0.04242777079343796, 0.016536075621843338, 0.10052520781755447, 0.18272875249385834, 0.2079043835401535]
```
## DeepInfra

[DeepInfra](https://deepinfra.com/?utm_source=langchain) is a serverless inference as a service that provides access to a [variety of LLMs](https://deepinfra.com/models?utm_source=langchain) and [embeddings models](https://deepinfra.com/models?type=embeddings&utm_source=langchain). This notebook goes over how to use LangChain with DeepInfra for text embeddings.

```
# sign up for an account: https://deepinfra.com/login?utm_source=langchainfrom getpass import getpassDEEPINFRA_API_TOKEN = getpass()
```

```
import osos.environ["DEEPINFRA_API_TOKEN"] = DEEPINFRA_API_TOKEN
```

```
from langchain_community.embeddings import DeepInfraEmbeddings
```

```
embeddings = DeepInfraEmbeddings(    model_id="sentence-transformers/clip-ViT-B-32",    query_instruction="",    embed_instruction="",)
```

```
docs = ["Dog is not a cat", "Beta is the second letter of Greek alphabet"]document_result = embeddings.embed_documents(docs)
```

```
query = "What is the first letter of Greek alphabet"query_result = embeddings.embed_query(query)
```

```
import numpy as npquery_numpy = np.array(query_result)for doc_res, doc in zip(document_result, docs):    document_numpy = np.array(doc_res)    similarity = np.dot(query_numpy, document_numpy) / (        np.linalg.norm(query_numpy) * np.linalg.norm(document_numpy)    )    print(f'Cosine similarity between "{doc}" and query: {similarity}')
```

```
Cosine similarity between "Dog is not a cat" and query: 0.7489097144129355Cosine similarity between "Beta is the second letter of Greek alphabet" and query: 0.9519380640702013
```

* * *

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

Let’s load the DashScope Embedding class.

```
from langchain_community.embeddings import DashScopeEmbeddings
```

```
embeddings = DashScopeEmbeddings(    model="text-embedding-v1", dashscope_api_key="your-dashscope-api-key")
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)print(query_result)
```

```
doc_results = embeddings.embed_documents(["foo"])print(doc_results)
```

* * *

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

This notebook goes over how to use Llama-cpp embeddings within LangChain

```
%pip install --upgrade --quiet  llama-cpp-python
```

```
from langchain_community.embeddings import LlamaCppEmbeddings
```

```
llama = LlamaCppEmbeddings(model_path="/path/to/model/ggml-model-q4_0.bin")
```

```
text = "This is a test document."
```

```
query_result = llama.embed_query(text)
```

```
doc_result = llama.embed_documents([text])
```

* * *

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

Let’s load the LocalAI Embedding class. In order to use the LocalAI Embedding class, you need to have the LocalAI service hosted somewhere and configure the embedding models. See the documentation at [https://localai.io/basics/getting\_started/index.html](https://localai.io/basics/getting_started/index.html) and [https://localai.io/features/embeddings/index.html](https://localai.io/features/embeddings/index.html).

```
from langchain_community.embeddings import LocalAIEmbeddings
```

```
embeddings = LocalAIEmbeddings(    openai_api_base="http://localhost:8080", model="embedding-model-name")
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)
```

```
doc_result = embeddings.embed_documents([text])
```

Let’s load the LocalAI Embedding class with first generation models (e.g. text-search-ada-doc-001/text-search-ada-query-001). Note: These are not recommended models - see [here](https://platform.openai.com/docs/guides/embeddings/what-are-embeddings)

```
from langchain_community.embeddings import LocalAIEmbeddings
```

```
embeddings = LocalAIEmbeddings(    openai_api_base="http://localhost:8080", model="embedding-model-name")
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)
```

```
doc_result = embeddings.embed_documents([text])
```

```
import os# if you are behind an explicit proxy, you can use the OPENAI_PROXY environment variable to pass throughos.environ["OPENAI_PROXY"] = "http://proxy.yourcompany.com:8080"
```

* * *

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

Eden AI is revolutionizing the AI landscape by uniting the best AI providers, empowering users to unlock limitless possibilities and tap into the true potential of artificial intelligence. With an all-in-one comprehensive and hassle-free platform, it allows users to deploy AI features to production lightning fast, enabling effortless access to the full breadth of AI capabilities via a single API. (website: [https://edenai.co/](https://edenai.co/))

This example goes over how to use LangChain to interact with Eden AI embedding models

* * *

Accessing the EDENAI’s API requires an API key,

which you can get by creating an account [https://app.edenai.run/user/register](https://app.edenai.run/user/register) and heading here [https://app.edenai.run/admin/account/settings](https://app.edenai.run/admin/account/settings)

Once we have a key we’ll want to set it as an environment variable by running:

```
export EDENAI_API_KEY="..."
```

If you’d prefer not to set an environment variable you can pass the key in directly via the edenai\_api\_key named parameter

when initiating the EdenAI embedding class:

```
from langchain_community.embeddings.edenai import EdenAiEmbeddings
```

```
embeddings = EdenAiEmbeddings(edenai_api_key="...", provider="...")
```

## Calling a model[​](#calling-a-model "Direct link to Calling a model")

The EdenAI API brings together various providers.

To access a specific model, you can simply use the “provider” when calling.

```
embeddings = EdenAiEmbeddings(provider="openai")
```

```
docs = ["It's raining right now", "cats are cute"]document_result = embeddings.embed_documents(docs)
```

```
query = "my umbrella is broken"query_result = embeddings.embed_query(query)
```

```
import numpy as npquery_numpy = np.array(query_result)for doc_res, doc in zip(document_result, docs):    document_numpy = np.array(doc_res)    similarity = np.dot(query_numpy, document_numpy) / (        np.linalg.norm(query_numpy) * np.linalg.norm(document_numpy)    )    print(f'Cosine similarity between "{doc}" and query: {similarity}')
```

```
Cosine similarity between "It's raining right now" and query: 0.849261496107252Cosine similarity between "cats are cute" and query: 0.7525900655705218
```
## MistralAI

This notebook explains how to use MistralAIEmbeddings, which is included in the langchain\_mistralai package, to embed texts in langchain.

```
# pip install -U langchain-mistralai
```

## import the library[​](#import-the-library "Direct link to import the library")

```
from langchain_mistralai import MistralAIEmbeddings
```

```
embedding = MistralAIEmbeddings(api_key="your-api-key")
```

## Using the Embedding Model

With `MistralAIEmbeddings`, you can directly use the default model ‘mistral-embed’, or set a different one if available.

```
embedding.model = "mistral-embed"  # or your preferred model if available
```

```
res_query = embedding.embed_query("The test information")res_document = embedding.embed_documents(["test1", "another test"])
```

* * *

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

> [ModelScope](https://www.modelscope.cn/home) is big repository of the models and datasets.

Let’s load the ModelScope Embedding class.

```
from langchain_community.embeddings import ModelScopeEmbeddings
```

```
model_id = "damo/nlp_corom_sentence-embedding_english-base"
```

```
embeddings = ModelScopeEmbeddings(model_id=model_id)
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)
```

```
doc_results = embeddings.embed_documents(["foo"])
```

* * *

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

Walkthrough of how to generate embeddings using a hosted embedding model in Elasticsearch

The easiest way to instantiate the `ElasticsearchEmbeddings` class it either - using the `from_credentials` constructor if you are using Elastic Cloud - or using the `from_es_connection` constructor with any Elasticsearch cluster

```
!pip -q install langchain-elasticsearch
```

```
from langchain_elasticsearch import ElasticsearchEmbeddings
```

```
# Define the model IDmodel_id = "your_model_id"
```

## Testing with `from_credentials`[​](#testing-with-from_credentials "Direct link to testing-with-from_credentials")

This required an Elastic Cloud `cloud_id`

```
# Instantiate ElasticsearchEmbeddings using credentialsembeddings = ElasticsearchEmbeddings.from_credentials(    model_id,    es_cloud_id="your_cloud_id",    es_user="your_user",    es_password="your_password",)
```

```
# Create embeddings for multiple documentsdocuments = [    "This is an example document.",    "Another example document to generate embeddings for.",]document_embeddings = embeddings.embed_documents(documents)
```

```
# Print document embeddingsfor i, embedding in enumerate(document_embeddings):    print(f"Embedding for document {i+1}: {embedding}")
```

```
# Create an embedding for a single queryquery = "This is a single query."query_embedding = embeddings.embed_query(query)
```

```
# Print query embeddingprint(f"Embedding for query: {query_embedding}")
```

## Testing with Existing Elasticsearch client connection[​](#testing-with-existing-elasticsearch-client-connection "Direct link to Testing with Existing Elasticsearch client connection")

This can be used with any Elasticsearch deployment

```
# Create Elasticsearch connectionfrom elasticsearch import Elasticsearches_connection = Elasticsearch(    hosts=["https://es_cluster_url:port"], basic_auth=("user", "password"))
```

```
# Instantiate ElasticsearchEmbeddings using es_connectionembeddings = ElasticsearchEmbeddings.from_es_connection(    model_id,    es_connection,)
```

```
# Create embeddings for multiple documentsdocuments = [    "This is an example document.",    "Another example document to generate embeddings for.",]document_embeddings = embeddings.embed_documents(documents)
```

```
# Print document embeddingsfor i, embedding in enumerate(document_embeddings):    print(f"Embedding for document {i+1}: {embedding}")
```

```
# Create an embedding for a single queryquery = "This is a single query."query_embedding = embeddings.embed_query(query)
```

```
# Print query embeddingprint(f"Embedding for query: {query_embedding}")
```
## Embaas

[embaas](https://embaas.io/) is a fully managed NLP API service that offers features like embedding generation, document text extraction, document to embeddings and more. You can choose a [variety of pre-trained models](https://embaas.io/docs/models/embeddings).

In this tutorial, we will show you how to use the embaas Embeddings API to generate embeddings for a given text.

### Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Create your free embaas account at [https://embaas.io/register](https://embaas.io/register) and generate an [API key](https://embaas.io/dashboard/api-keys).

```
import os# Set API keyembaas_api_key = "YOUR_API_KEY"# or set environment variableos.environ["EMBAAS_API_KEY"] = "YOUR_API_KEY"
```

```
from langchain_community.embeddings import EmbaasEmbeddings
```

```
embeddings = EmbaasEmbeddings()
```

```
# Create embeddings for a single documentdoc_text = "This is a test document."doc_text_embedding = embeddings.embed_query(doc_text)
```

```
# Print created embeddingprint(doc_text_embedding)
```

```
# Create embeddings for multiple documentsdoc_texts = ["This is a test document.", "This is another test document."]doc_texts_embeddings = embeddings.embed_documents(doc_texts)
```

```
# Print created embeddingsfor i, doc_text_embedding in enumerate(doc_texts_embeddings):    print(f"Embedding for document {i + 1}: {doc_text_embedding}")
```

```
# Using a different model and/or custom instructionembeddings = EmbaasEmbeddings(    model="instructor-large",    instruction="Represent the Wikipedia document for retrieval",)
```

For more detailed information about the embaas Embeddings API, please refer to [the official embaas API documentation](https://embaas.io/api-reference).
## MiniMax

[MiniMax](https://api.minimax.chat/document/guides/embeddings?id=6464722084cdc277dfaa966a) offers an embeddings service.

This example goes over how to use LangChain to interact with MiniMax Inference for text embedding.

```
import osos.environ["MINIMAX_GROUP_ID"] = "MINIMAX_GROUP_ID"os.environ["MINIMAX_API_KEY"] = "MINIMAX_API_KEY"
```

```
from langchain_community.embeddings import MiniMaxEmbeddings
```

```
embeddings = MiniMaxEmbeddings()
```

```
query_text = "This is a test query."query_result = embeddings.embed_query(query_text)
```

```
document_text = "This is a test document."document_result = embeddings.embed_documents([document_text])
```

```
import numpy as npquery_numpy = np.array(query_result)document_numpy = np.array(document_result[0])similarity = np.dot(query_numpy, document_numpy) / (    np.linalg.norm(query_numpy) * np.linalg.norm(document_numpy))print(f"Cosine similarity between document and query: {similarity}")
```

```
Cosine similarity between document and query: 0.1573236279277012
```

* * *

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

[ERNIE Embedding-V1](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/alj562vvu) is a text representation model based on `Baidu Wenxin` large-scale model technology, which converts text into a vector form represented by numerical values, and is used in text retrieval, information recommendation, knowledge mining and other scenarios.

**Deprecated Warning**

We recommend users using `langchain_community.embeddings.ErnieEmbeddings` to use `langchain_community.embeddings.QianfanEmbeddingsEndpoint` instead.

documentation for `QianfanEmbeddingsEndpoint` is [here](https://python.langchain.com/docs/integrations/text_embedding/baidu_qianfan_endpoint/).

they are 2 why we recommend users to use `QianfanEmbeddingsEndpoint`:

1.  `QianfanEmbeddingsEndpoint` support more embedding model in the Qianfan platform.
2.  `ErnieEmbeddings` is lack of maintenance and deprecated.

Some tips for migration:

```
from langchain_community.embeddings import QianfanEmbeddingsEndpointembeddings = QianfanEmbeddingsEndpoint(    qianfan_ak="your qianfan ak",    qianfan_sk="your qianfan sk",)
```

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

```
from langchain_community.embeddings import ErnieEmbeddings
```

```
embeddings = ErnieEmbeddings()
```

```
query_result = embeddings.embed_query("foo")
```

```
doc_results = embeddings.embed_documents(["foo"])
```
## MosaicML

> [MosaicML](https://docs.mosaicml.com/en/latest/inference.html) offers a managed inference service. You can either use a variety of open-source models, or deploy your own.

This example goes over how to use LangChain to interact with `MosaicML` Inference for text embedding.

```
# sign up for an account: https://forms.mosaicml.com/demo?utm_source=langchainfrom getpass import getpassMOSAICML_API_TOKEN = getpass()
```

```
import osos.environ["MOSAICML_API_TOKEN"] = MOSAICML_API_TOKEN
```

```
from langchain_community.embeddings import MosaicMLInstructorEmbeddings
```

```
embeddings = MosaicMLInstructorEmbeddings(    query_instruction="Represent the query for retrieval: ")
```

```
query_text = "This is a test query."query_result = embeddings.embed_query(query_text)
```

```
document_text = "This is a test document."document_result = embeddings.embed_documents([document_text])
```

```
import numpy as npquery_numpy = np.array(query_result)document_numpy = np.array(document_result[0])similarity = np.dot(query_numpy, document_numpy) / (    np.linalg.norm(query_numpy) * np.linalg.norm(document_numpy))print(f"Cosine similarity between document and query: {similarity}")
```

* * *

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

LangChain also provides a fake embedding class. You can use this to test your pipelines.

```
from langchain_community.embeddings import FakeEmbeddings
```

```
embeddings = FakeEmbeddings(size=1352)
```

```
query_result = embeddings.embed_query("foo")
```

```
doc_results = embeddings.embed_documents(["foo"])
```

* * *

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

This notebook shows how to use LangChain with [GigaChat embeddings](https://developers.sber.ru/portal/products/gigachat). To use you need to install `gigachat` python package.

```
%pip install --upgrade --quiet  gigachat
```

To get GigaChat credentials you need to [create account](https://developers.sber.ru/studio/login) and [get access to API](https://developers.sber.ru/docs/ru/gigachat/individuals-quickstart)

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

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

```
from langchain_community.embeddings import GigaChatEmbeddingsembeddings = GigaChatEmbeddings(verify_ssl_certs=False, scope="GIGACHAT_API_PERS")
```

```
query_result = embeddings.embed_query("The quick brown fox jumps over the lazy dog")
```

```
[0.8398333191871643, -0.14180311560630798, -0.6161925792694092, -0.17103666067123413, 1.2884578704833984]
```

* * *

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

> [FastEmbed](https://qdrant.github.io/fastembed/) from [Qdrant](https://qdrant.tech/) is a lightweight, fast, Python library built for embedding generation.
> 
> *   Quantized model weights
> *   ONNX Runtime, no PyTorch dependency
> *   CPU-first design
> *   Data-parallelism for encoding of large datasets.

## Dependencies[​](#dependencies "Direct link to Dependencies")

To use FastEmbed with LangChain, install the `fastembed` Python package.

```
%pip install --upgrade --quiet  fastembed
```

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

```
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
```

## Instantiating FastEmbed[​](#instantiating-fastembed "Direct link to Instantiating FastEmbed")

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

*   `model_name: str` (default: “BAAI/bge-small-en-v1.5”) \> Name of the FastEmbedding model to use. You can find the list of supported models [here](https://qdrant.github.io/fastembed/examples/Supported_Models/).
    
*   `max_length: int` (default: 512) \> The maximum number of tokens. Unknown behavior for values \> 512.
    
*   `cache_dir: Optional[str]` \> The path to the cache directory. Defaults to `local_cache` in the parent directory.
    
*   `threads: Optional[int]` \> The number of threads a single onnxruntime session can use. Defaults to None.
    
*   `doc_embed_type: Literal["default", "passage"]` (default: “default”) \> “default”: Uses FastEmbed’s default embedding method.
    
    > “passage”: Prefixes the text with “passage” before embedding.
    

```
embeddings = FastEmbedEmbeddings()
```

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

### Generating document embeddings[​](#generating-document-embeddings "Direct link to Generating document embeddings")

```
document_embeddings = embeddings.embed_documents(    ["This is a document", "This is some other document"])
```

### Generating query embeddings[​](#generating-query-embeddings "Direct link to Generating query embeddings")

```
query_embeddings = embeddings.embed_query("This is a query")
```

* * *

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

This notebook explains how to use Fireworks Embeddings, which is included in the langchain\_fireworks package, to embed texts in langchain. We use the default nomic-ai v1.5 model in this example.

```
%pip install -qU langchain-fireworks
```

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

```
from langchain_fireworks import FireworksEmbeddings
```

```
import getpassimport osif "FIREWORKS_API_KEY" not in os.environ:    os.environ["FIREWORKS_API_KEY"] = getpass.getpass("Fireworks API Key:")
```

## Using the Embedding Model

With `FireworksEmbeddings`, you can directly use the default model ‘nomic-ai/nomic-embed-text-v1.5’, or set a different one if available.

```
embedding = FireworksEmbeddings(model="nomic-ai/nomic-embed-text-v1.5")
```

```
res_query = embedding.embed_query("The test information")res_document = embedding.embed_documents(["test1", "another test"])print(res_query[:5])print(res_document[1][:5])
```

```
[0.01367950439453125, 0.0103607177734375, -0.157958984375, -0.003070831298828125, 0.05926513671875][0.0369873046875, 0.00545501708984375, -0.179931640625, -0.018707275390625, 0.0552978515625]
```

* * *

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

> [NLP Cloud](https://docs.nlpcloud.com/#introduction) is an artificial intelligence platform that allows you to use the most advanced AI engines, and even train your own engines with your own data.

The [embeddings](https://docs.nlpcloud.com/#embeddings) endpoint offers the following model:

*   `paraphrase-multilingual-mpnet-base-v2`: Paraphrase Multilingual MPNet Base V2 is a very fast model based on Sentence Transformers that is perfectly suited for embeddings extraction in more than 50 languages (see the full list here).

```
%pip install --upgrade --quiet  nlpcloud
```

```
from langchain_community.embeddings import NLPCloudEmbeddings
```

```
import osos.environ["NLPCLOUD_API_KEY"] = "xxx"nlpcloud_embd = NLPCloudEmbeddings()
```

```
text = "This is a test document."
```

```
query_result = nlpcloud_embd.embed_query(text)
```

```
doc_result = nlpcloud_embd.embed_documents([text])
```

* * *

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

This notebook covers how to get started with Nomic embedding models.

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

```
# install package!pip install -U langchain-nomic
```

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

Make sure to set the following environment variables:

*   `NOMIC_API_KEY`

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

```
from langchain_nomic.embeddings import NomicEmbeddingsembeddings = NomicEmbeddings(model="nomic-embed-text-v1.5")
```

```
embeddings.embed_query("My query to look up")
```

```
embeddings.embed_documents(    ["This is a content of the document", "This is another document"])
```

```
# async embed queryawait embeddings.aembed_query("My query to look up")
```

```
# async embed documentsawait embeddings.aembed_documents(    ["This is a content of the document", "This is another document"])
```

### Custom Dimensionality[​](#custom-dimensionality "Direct link to Custom Dimensionality")

Nomic’s `nomic-embed-text-v1.5` model was [trained with Matryoshka learning](https://blog.nomic.ai/posts/nomic-embed-matryoshka) to enable variable-length embeddings with a single model. This means that you can specify the dimensionality of the embeddings at inference time. The model supports dimensionality from 64 to 768.

```
embeddings = NomicEmbeddings(model="nomic-embed-text-v1.5", dimensionality=256)embeddings.embed_query("My query to look up")
```

* * *

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

Connect to NVIDIA’s embedding service using the `NeMoEmbeddings` class.

The NeMo Retriever Embedding Microservice (NREM) brings the power of state-of-the-art text embedding to your applications, providing unmatched natural language processing and understanding capabilities. Whether you’re developing semantic search, Retrieval Augmented Generation (RAG) pipelines—or any application that needs to use text embeddings—NREM has you covered. Built on the NVIDIA software platform incorporating CUDA, TensorRT, and Triton, NREM brings state of the art GPU accelerated Text Embedding model serving.

NREM uses NVIDIA’s TensorRT built on top of the Triton Inference Server for optimized inference of text embedding models.

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

```
from langchain_community.embeddings import NeMoEmbeddings
```

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

```
batch_size = 16model = "NV-Embed-QA-003"api_endpoint_url = "http://localhost:8080/v1/embeddings"
```

```
embedding_model = NeMoEmbeddings(    batch_size=batch_size, model=model, api_endpoint_url=api_endpoint_url)
```

```
Checking if endpoint is live: http://localhost:8080/v1/embeddings
```

```
embedding_model.embed_query("This is a test.")
```
Oracle Cloud Infrastructure (OCI) Generative AI is a fully managed service that provides a set of state-of-the-art, customizable large language models (LLMs), that cover a wide range of use cases, and which are available through a single API. Using the OCI Generative AI service you can access ready-to-use pretrained models, or create and host your own fine-tuned custom models based on your own data on dedicated AI clusters. Detailed documentation of the service and API is available **[here](https://docs.oracle.com/en-us/iaas/Content/generative-ai/home.htm)** and **[here](https://docs.oracle.com/en-us/iaas/api/#/en/generative-ai/20231130/)**.

This notebook explains how to use OCI’s Genrative AI models with LangChain.

```
from langchain_community.embeddings import OCIGenAIEmbeddings# use default authN method API-keyembeddings = OCIGenAIEmbeddings(    model_id="MY_EMBEDDING_MODEL",    service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",    compartment_id="MY_OCID",)query = "This is a query in English."response = embeddings.embed_query(query)print(response)documents = ["This is a sample document", "and here is another one"]response = embeddings.embed_documents(documents)print(response)
```

```
# Use Session Token to authNembeddings = OCIGenAIEmbeddings(    model_id="MY_EMBEDDING_MODEL",    service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",    compartment_id="MY_OCID",    auth_type="SECURITY_TOKEN",    auth_profile="MY_PROFILE",  # replace with your profile name)query = "This is a sample query"response = embeddings.embed_query(query)print(response)documents = ["This is a sample document", "and here is another one"]response = embeddings.embed_documents(documents)print(response)
```
## TensorFlow Hub

> [TensorFlow Hub](https://www.tensorflow.org/hub) is a repository of trained machine learning models ready for fine-tuning and deployable anywhere. Reuse trained models like `BERT` and `Faster R-CNN` with just a few lines of code.
> 
> Let’s load the TensorflowHub Embedding class.

```
from langchain_community.embeddings import TensorflowHubEmbeddings
```

```
embeddings = TensorflowHubEmbeddings()
```

```
2023-01-30 23:53:01.652176: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMATo enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.2023-01-30 23:53:34.362802: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMATo enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
```

```
text = "This is a test document."
```

```
query_result = embeddings.embed_query(text)
```

```
doc_results = embeddings.embed_documents(["foo"])
```

* * *

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

> [Hugging Face Text Embeddings Inference (TEI)](https://huggingface.co/docs/text-embeddings-inference/index) is a toolkit for deploying and serving open-source text embeddings and sequence classification models. `TEI` enables high-performance extraction for the most popular models, including `FlagEmbedding`, `Ember`, `GTE` and `E5`.

To use it within langchain, first install `huggingface-hub`.

```
%pip install --upgrade huggingface-hub
```

Then expose an embedding model using TEI. For instance, using Docker, you can serve `BAAI/bge-large-en-v1.5` as follows:

```
model=BAAI/bge-large-en-v1.5revision=refs/pr/5volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every rundocker run --gpus all -p 8080:80 -v $volume:/data --pull always ghcr.io/huggingface/text-embeddings-inference:0.6 --model-id $model --revision $revision
```

Finally, instantiate the client and embed your texts.

```
from langchain_community.embeddings import HuggingFaceHubEmbeddings
```

```
embeddings = HuggingFaceHubEmbeddings(model="http://localhost:8080")
```

```
text = "What is deep learning?"
```

```
query_result = embeddings.embed_query(text)query_result[:3]
```

```
[0.018113142, 0.00302585, -0.049911194]
```

```
doc_result = embeddings.embed_documents([text])
```

```
[0.018113142, 0.00302585, -0.049911194]
```