seansullivan/Langchain-Docs huggingface dataset:
## Google Vertex AI PaLM

> [Vertex AI PaLM API](https://cloud.google.com/vertex-ai/docs/generative-ai/learn/overview) is a service on Google Cloud exposing the embedding models.

Note: This integration is separate from the Google PaLM integration.

By default, Google Cloud [does not use](https://cloud.google.com/vertex-ai/docs/generative-ai/data-governance#foundation_model_development) Customer Data to train its foundation models as part of Google Cloud\`s AI/ML Privacy Commitment. More details about how Google processes data can also be found in [Google’s Customer Data Processing Addendum (CDPA)](https://cloud.google.com/terms/data-processing-addendum).

To use Vertex AI PaLM you must have the `langchain-google-vertexai` Python package installed and either: - Have credentials configured for your environment (gcloud, workload identity, etc…) - Store the path to a service account JSON file as the GOOGLE\_APPLICATION\_CREDENTIALS environment variable

This codebase uses the `google.auth` library which first looks for the application credentials variable mentioned above, and then looks for system-level auth.

For more information, see: - [https://cloud.google.com/docs/authentication/application-default-credentials#GAC](https://cloud.google.com/docs/authentication/application-default-credentials#GAC) - [https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth](https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth)

```
%pip install --upgrade --quiet langchain langchain-google-vertexai
```

```
from langchain_google_vertexai import VertexAIEmbeddings
```

```
embeddings = VertexAIEmbeddings()
```

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

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

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

Let’s load the Ollama Embeddings class.

```
from langchain_community.embeddings import OllamaEmbeddings
```

```
embeddings = OllamaEmbeddings()
```

```
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]
```

Let’s load the Ollama Embeddings class with smaller model (e.g. llama:7b). Note: See other supported models [https://ollama.ai/library](https://ollama.ai/library)

```
embeddings = OllamaEmbeddings(model="llama2:7b")
```

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

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

```
[-0.09996627271175385, 0.015567859634757042, 0.17670205235481262, 0.16521376371383667, 0.21193283796310425]
```

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

```
[-0.042427532374858856, 0.01653730869293213, 0.10052604228258133, 0.18272635340690613, 0.20790338516235352]
```

* * *

#### Help us out by providing feedback on this documentation page:
## Google Generative AI Embeddings

Connect to Google’s generative AI embeddings service using the `GoogleGenerativeAIEmbeddings` class, found in the [langchain-google-genai](https://pypi.org/project/langchain-google-genai/) package.

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

```
%pip install --upgrade --quiet  langchain-google-genai
```

## Credentials[​](#credentials "Direct link to Credentials")

```
import getpassimport osif "GOOGLE_API_KEY" not in os.environ:    os.environ["GOOGLE_API_KEY"] = getpass("Provide your Google API key here")
```

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

```
from langchain_google_genai import GoogleGenerativeAIEmbeddingsembeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")vector = embeddings.embed_query("hello, world!")vector[:5]
```

```
[0.05636945, 0.0048285457, -0.0762591, -0.023642512, 0.05329321]
```

## Batch[​](#batch "Direct link to Batch")

You can also embed multiple strings at once for a processing speedup:

```
vectors = embeddings.embed_documents(    [        "Today is Monday",        "Today is Tuesday",        "Today is April Fools day",    ])len(vectors), len(vectors[0])
```

## Task type[​](#task-type "Direct link to Task type")

`GoogleGenerativeAIEmbeddings` optionally support a `task_type`, which currently must be one of:

*   task\_type\_unspecified
*   retrieval\_query
*   retrieval\_document
*   semantic\_similarity
*   classification
*   clustering

By default, we use `retrieval_document` in the `embed_documents` method and `retrieval_query` in the `embed_query` method. If you provide a task type, we will use that for all methods.

```
%pip install --upgrade --quiet  matplotlib scikit-learn
```

```
Note: you may need to restart the kernel to use updated packages.
```

```
query_embeddings = GoogleGenerativeAIEmbeddings(    model="models/embedding-001", task_type="retrieval_query")doc_embeddings = GoogleGenerativeAIEmbeddings(    model="models/embedding-001", task_type="retrieval_document")
```

All of these will be embedded with the ‘retrieval\_query’ task set

```
query_vecs = [query_embeddings.embed_query(q) for q in [query, query_2, answer_1]]
```

All of these will be embedded with the ‘retrieval\_document’ task set

```
doc_vecs = [doc_embeddings.embed_query(q) for q in [query, query_2, answer_1]]
```

In retrieval, relative distance matters. In the image above, you can see the difference in similarity scores between the “relevant doc” and “simil stronger delta between the similar query and relevant doc on the latter case.

## Additional Configuration[​](#additional-configuration "Direct link to Additional Configuration")

You can pass the following parameters to ChatGoogleGenerativeAI in order to customize the SDK’s behavior:

*   `client_options`: [Client Options](https://googleapis.dev/python/google-api-core/latest/client_options.html#module-google.api_core.client_options) to pass to the Google API Client, such as a custom `client_options["api_endpoint"]`
*   `transport`: The transport method to use, such as `rest`, `grpc`, or `grpc_asyncio`.

* * *

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

[GPT4All](https://gpt4all.io/index.html) is a free-to-use, locally running, privacy-aware chatbot. There is no GPU or internet required. It features popular models and its own models such as GPT4All Falcon, Wizard, etc.

This notebook explains how to use [GPT4All embeddings](https://docs.gpt4all.io/gpt4all_python_embedding.html#gpt4all.gpt4all.Embed4All) with LangChain.

## Install GPT4All’s Python Bindings[​](#install-gpt4alls-python-bindings "Direct link to Install GPT4All’s Python Bindings")

```
%pip install --upgrade --quiet  gpt4all > /dev/null
```

Note: you may need to restart the kernel to use updated packages.

```
from langchain_community.embeddings import GPT4AllEmbeddings
```

```
gpt4all_embd = GPT4AllEmbeddings()
```

```
100%|████████████████████████| 45.5M/45.5M [00:02<00:00, 18.5MiB/s]objc[45711]: Class GGMLMetalClass is implemented in both /Users/rlm/anaconda3/envs/lcn2/lib/python3.9/site-packages/gpt4all/llmodel_DO_NOT_MODIFY/build/libreplit-mainline-metal.dylib (0x29fe18208) and /Users/rlm/anaconda3/envs/lcn2/lib/python3.9/site-packages/gpt4all/llmodel_DO_NOT_MODIFY/build/libllamamodel-mainline-metal.dylib (0x2a0244208). One of the two will be used. Which one is undefined.
```

```
Model downloaded at:  /Users/rlm/.cache/gpt4all/ggml-all-MiniLM-L6-v2-f16.bin
```

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

## Embed the Textual Data[​](#embed-the-textual-data "Direct link to Embed the Textual Data")

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

With embed\_documents you can embed multiple pieces of text. You can also map these embeddings with [Nomic’s Atlas](https://docs.nomic.ai/index.html) to see a visual representation of your data.

```
doc_result = gpt4all_embd.embed_documents([text])
```
## Titan Takeoff

`TitanML` helps businesses build and deploy better, smaller, cheaper, and faster NLP models through our training, compression, and inference optimization platform.

Our inference server, [Titan Takeoff](https://docs.titanml.co/docs/intro) enables deployment of LLMs locally on your hardware in a single command. Most embedding models are supported out of the box, if you experience trouble with a specific model, please let us know at [hello@titanml.co](mailto:hello@titanml.co).

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

Here are some helpful examples to get started using Titan Takeoff Server. You need to make sure Takeoff Server has been started in the background before running these commands. For more information see [docs page for launching Takeoff](https://docs.titanml.co/docs/Docs/launching/).

```
import timefrom langchain_community.embeddings import TitanTakeoffEmbed
```

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

Basic use assuming Takeoff is running on your machine using its default ports (ie localhost:3000).

```
embed = TitanTakeoffEmbed()output = embed.embed_query(    "What is the weather in London in August?", consumer_group="embed")print(output)
```

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

Starting readers using TitanTakeoffEmbed Python Wrapper. If you haven’t created any readers with first launching Takeoff, or you want to add another you can do so when you initialize the TitanTakeoffEmbed object. Just pass a list of models you want to start as the `models` parameter.

You can use `embed.query_documents` to embed multiple documents at once. The expected input is a list of strings, rather than just a string expected for the `embed_query` method.

```
# Model config for the embedding model, where you can specify the following parameters:#   model_name (str): The name of the model to use#   device: (str): The device to use for inference, cuda or cpu#   consumer_group (str): The consumer group to place the reader intoembedding_model = {    "model_name": "BAAI/bge-large-en-v1.5",    "device": "cpu",    "consumer_group": "embed",}embed = TitanTakeoffEmbed(models=[embedding_model])# The model needs time to spin up, length of time need will depend on the size of model and your network connection speedtime.sleep(60)prompt = "What is the capital of France?"# We specified "embed" consumer group so need to send request to the same consumer group so it hits our embedding model and not othersoutput = embed.embed_query(prompt, consumer_group="embed")print(output)
```

* * *

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

Let’s load the OpenAI Embedding class.

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

First we install langchain-openai and set the required env vars

```
%pip install -qU langchain-openai
```

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass()
```

```
from langchain_openai import OpenAIEmbeddings
```

```
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
```

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

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

### Embed query[​](#embed-query "Direct link to Embed query")

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

```
Warning: model not found. Using cl100k_base encoding.
```

```
[-0.014380056377383358, -0.027191711627651764, -0.020042716111860304, 0.057301379620345545, -0.022267658631828974]
```

## Embed documents[​](#embed-documents "Direct link to Embed documents")

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

```
Warning: model not found. Using cl100k_base encoding.
```

```
[-0.014380056377383358, -0.027191711627651764, -0.020042716111860304, 0.057301379620345545, -0.022267658631828974]
```

## Specify dimensions[​](#specify-dimensions "Direct link to Specify dimensions")

With the `text-embedding-3` class of models, you can specify the size of the embeddings you want returned. For example by default `text-embedding-3-large` returned embeddings of dimension 3072:

But by passing in `dimensions=1024` we can reduce the size of our embeddings to 1024:

```
embeddings_1024 = OpenAIEmbeddings(model="text-embedding-3-large", dimensions=1024)
```

```
len(embeddings_1024.embed_documents([text])[0])
```

```
Warning: model not found. Using cl100k_base encoding.
```
## OpenVINO

[OpenVINO™](https://github.com/openvinotoolkit/openvino) is an open-source toolkit for optimizing and deploying AI inference. The OpenVINO™ Runtime supports various hardware [devices](https://github.com/openvinotoolkit/openvino?tab=readme-ov-file#supported-hardware-matrix) including x86 and ARM CPUs, and Intel GPUs. It can help to boost deep learning performance in Computer Vision, Automatic Speech Recognition, Natural Language Processing and other common tasks.

Hugging Face embedding model can be supported by OpenVINO through `OpenVINOEmbeddings` class. If you have an Intel GPU, you can specify `model_kwargs={"device": "GPU"}` to run inference on it.

```
%pip install --upgrade-strategy eager "optimum[openvino,nncf]" --quiet
```

```
Note: you may need to restart the kernel to use updated packages.
```

```
from langchain_community.embeddings import OpenVINOEmbeddings
```

```
model_name = "sentence-transformers/all-mpnet-base-v2"model_kwargs = {"device": "CPU"}encode_kwargs = {"mean_pooling": True, "normalize_embeddings": True}ov_embeddings = OpenVINOEmbeddings(    model_name_or_path=model_name,    model_kwargs=model_kwargs,    encode_kwargs=encode_kwargs,)
```

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

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

```
[-0.048951778560876846, -0.03986183926463127, -0.02156277745962143]
```

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

## Export IR model[​](#export-ir-model "Direct link to Export IR model")

It is possible to export your embedding model to the OpenVINO IR format with `OVModelForFeatureExtraction`, and load the model from local folder.

```
from pathlib import Pathov_model_dir = "all-mpnet-base-v2-ov"if not Path(ov_model_dir).exists():    ov_embeddings.save_model(ov_model_dir)
```

```
ov_embeddings = OpenVINOEmbeddings(    model_name_or_path=ov_model_dir,    model_kwargs=model_kwargs,    encode_kwargs=encode_kwargs,)
```

```
Compiling the model to CPU ...
```

## BGE with OpenVINO[​](#bge-with-openvino "Direct link to BGE with OpenVINO")

We can also access BGE embedding models via the `OpenVINOBgeEmbeddings` class with OpenVINO.

```
from langchain_community.embeddings import OpenVINOBgeEmbeddingsmodel_name = "BAAI/bge-small-en"model_kwargs = {"device": "CPU"}encode_kwargs = {"normalize_embeddings": True}ov_embeddings = OpenVINOBgeEmbeddings(    model_name_or_path=model_name,    model_kwargs=model_kwargs,    encode_kwargs=encode_kwargs,)
```

```
embedding = ov_embeddings.embed_query("hi this is harrison")len(embedding)
```

For more information refer to:

*   [OpenVINO LLM guide](https://docs.openvino.ai/2024/learn-openvino/llm_inference_guide.html).
    
*   [OpenVINO Documentation](https://docs.openvino.ai/2024/home.html).
    
*   [OpenVINO Get Started Guide](https://www.intel.com/content/www/us/en/content-details/819067/openvino-get-started-guide.html).
    
*   [RAG Notebook with LangChain](https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-rag-langchain).
## Gradient

`Gradient` allows to create `Embeddings` as well fine tune and get completions on LLMs with a simple web API.

This notebook goes over how to use Langchain with Embeddings of [Gradient](https://gradient.ai/).

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

```
from langchain_community.embeddings import GradientEmbeddings
```

## 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.

```
import osfrom 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:")
```

Optional: Validate your environment variables `GRADIENT_ACCESS_TOKEN` and `GRADIENT_WORKSPACE_ID` to get currently deployed models. Using the `gradientai` Python package.

```
%pip install --upgrade --quiet  gradientai
```

## Create the Gradient instance[​](#create-the-gradient-instance "Direct link to Create the Gradient instance")

```
documents = [    "Pizza is a dish.",    "Paris is the capital of France",    "numpy is a lib for linear algebra",]query = "Where is Paris?"
```

```
embeddings = GradientEmbeddings(model="bge-large")documents_embedded = embeddings.embed_documents(documents)query_result = embeddings.embed_query(query)
```

```
# (demo) compute similarityimport numpy as npscores = np.array(documents_embedded) @ np.array(query_result).Tdict(zip(documents, scores))
```
## UpstageEmbeddings

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

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

Install `langchain-upstage` package.

```
pip install -U langchain-upstage
```

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

Make sure to set the following environment variables:

*   `UPSTAGE_API_KEY`: Your Upstage API key from [Upstage console](https://console.upstage.ai/).

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

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

Initialize `UpstageEmbeddings` class.

```
from langchain_upstage import UpstageEmbeddingsembeddings = UpstageEmbeddings()
```

Use `embed_documents` to embed list of texts or documents.

```
doc_result = embeddings.embed_documents(    ["Sam is a teacher.", "This is another document"])print(doc_result)
```

Use `embed_query` to embed query string.

```
query_result = embeddings.embed_query("What does Sam do?")print(query_result)
```

Use `aembed_documents` and `aembed_query` for async operations.

```
# 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"])
```

## Using with vector store[​](#using-with-vector-store "Direct link to Using with vector store")

You can use `UpstageEmbeddings` with vector store component. The following demonstrates a simple example.

```
from langchain_community.vectorstores import DocArrayInMemorySearchvectorstore = DocArrayInMemorySearch.from_texts(    ["harrison worked at kensho", "bears like to eat honey"],    embedding=UpstageEmbeddings(),)retriever = vectorstore.as_retriever()docs = retriever.get_relevant_documents("Where did Harrison work?")print(docs)
```
This notebook shows how to use agents to interact with the NASA toolkit. The toolkit provides access to the NASA Image and Video Library API, with potential to expand and include other accessible NASA APIs in future iterations.

**Note: NASA Image and Video Library search queries can result in large responses when the number of desired media results is not specified. Consider this prior to using the agent with LLM token credits.**

```
from langchain.agents import AgentType, initialize_agentfrom langchain_community.agent_toolkits.nasa.toolkit import NasaToolkitfrom langchain_community.utilities.nasa import NasaAPIWrapperfrom langchain_openai import OpenAIllm = OpenAI(temperature=0, openai_api_key="")nasa = NasaAPIWrapper()toolkit = NasaToolkit.from_nasa_api_wrapper(nasa)agent = initialize_agent(    toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
```
## Hugging Face

Let’s load the Hugging Face Embedding class.

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

```
from langchain_community.embeddings import HuggingFaceEmbeddings
```

```
embeddings = HuggingFaceEmbeddings()
```

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

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

```
[-0.04895168915390968, -0.03986193612217903, -0.021562768146395683]
```

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

## Hugging Face Inference API[​](#hugging-face-inference-api "Direct link to Hugging Face Inference API")

We can also access embedding models via the Hugging Face Inference API, which does not require us to install `sentence_transformers` and download models locally.

```
import getpassinference_api_key = getpass.getpass("Enter your HF Inference API Key:\n\n")
```

```
Enter your HF Inference API Key: ········
```

```
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddingsembeddings = HuggingFaceInferenceAPIEmbeddings(    api_key=inference_api_key, model_name="sentence-transformers/all-MiniLM-l6-v2")query_result = embeddings.embed_query(text)query_result[:3]
```

```
[-0.038338541984558105, 0.1234646737575531, -0.028642963618040085]
```

## Hugging Face Hub[​](#hugging-face-hub "Direct link to Hugging Face Hub")

We can also generate embeddings locally via the Hugging Face Hub package, which requires us to install `huggingface_hub`

```
!pip install huggingface_hub
```

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

```
embeddings = HuggingFaceHubEmbeddings()
```

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

```
query_result = embeddings.embed_query(text)
```
## Volc Engine

This notebook provides you with a guide on how to load the Volcano Embedding class.

## API Initialization[​](#api-initialization "Direct link to API Initialization")

To use the LLM services based on [VolcEngine](https://www.volcengine.com/docs/82379/1099455), you have to initialize these parameters:

You could either choose to init the AK,SK in environment variables or init params:

```
export VOLC_ACCESSKEY=XXXexport VOLC_SECRETKEY=XXX
```

```
"""For basic init and call"""import osfrom langchain_community.embeddings import VolcanoEmbeddingsos.environ["VOLC_ACCESSKEY"] = ""os.environ["VOLC_SECRETKEY"] = ""embed = VolcanoEmbeddings(volcano_ak="", volcano_sk="")print("embed_documents result:")res1 = embed.embed_documents(["foo", "bar"])for r in res1:    print("", r[:8])
```

```
embed_documents result: [0.02929673343896866, -0.009310632012784481, -0.060323506593704224, 0.0031018739100545645, -0.002218986628577113, -0.0023125179577618837, -0.04864659160375595, -2.062115163425915e-05] [0.01987231895327568, -0.026041055098176003, -0.08395249396562576, 0.020043574273586273, -0.028862033039331436, 0.004629664588719606, -0.023107370361685753, -0.0342753604054451]
```

```
print("embed_query result:")res2 = embed.embed_query("foo")print("", r[:8])
```

```
embed_query result: [0.01987231895327568, -0.026041055098176003, -0.08395249396562576, 0.020043574273586273, -0.028862033039331436, 0.004629664588719606, -0.023107370361685753, -0.0342753604054451]
```

* * *

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

This notebook covers how to get started with open source embedding models hosted in the Together AI API.

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

```
# install package%pip install --upgrade --quiet  langchain-together
```

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

Make sure to set the following environment variables:

*   `TOGETHER_API_KEY`

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

First, select a supported model from [this list](https://docs.together.ai/docs/embedding-models). In the following example, we will use `togethercomputer/m2-bert-80M-8k-retrieval`.

```
from langchain_together.embeddings import TogetherEmbeddingsembeddings = TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval")
```

```
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"])
```

* * *

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

> [Voyage AI](https://www.voyageai.com/) provides cutting-edge embedding/vectorizations models.

Let’s load the Voyage AI Embedding class. (Install the LangChain partner package with `pip install langchain-voyageai`)

```
from langchain_voyageai import VoyageAIEmbeddings
```

Voyage AI utilizes API keys to monitor usage and manage permissions. To obtain your key, create an account on our [homepage](https://www.voyageai.com/). Then, create a VoyageEmbeddings model with your API key. Please refer to the documentation for further details on the available models: [https://docs.voyageai.com/embeddings/](https://docs.voyageai.com/embeddings/)

```
embeddings = VoyageAIEmbeddings(    voyage_api_key="[ Your Voyage API key ]", model="voyage-2")
```

Prepare the documents and use `embed_documents` to get their embeddings.

```
documents = [    "Caching embeddings enables the storage or temporary caching of embeddings, eliminating the necessity to recompute them each time.",    "An LLMChain is a chain that composes basic LLM functionality. It consists of a PromptTemplate and a language model (either an LLM or chat model). It formats the prompt template using the input key values provided (and also memory key values, if available), passes the formatted string to LLM and returns the LLM output.",    "A Runnable represents a generic unit of work that can be invoked, batched, streamed, and/or transformed.",]
```

```
documents_embds = embeddings.embed_documents(documents)
```

```
[0.0562174916267395, 0.018221192061901093, 0.0025736060924828053, -0.009720131754875183, 0.04108370840549469]
```

Similarly, use `embed_query` to embed the query.

```
query = "What's an LLMChain?"
```

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

```
[-0.0052348352037370205, -0.040072452276945114, 0.0033957737032324076, 0.01763271726667881, -0.019235141575336456]
```

## A minimalist retrieval system[​](#a-minimalist-retrieval-system "Direct link to A minimalist retrieval system")

The main feature of the embeddings is that the cosine similarity between two embeddings captures the semantic relatedness of the corresponding original passages. This allows us to use the embeddings to do semantic retrieval / search.

We can find a few closest embeddings in the documents embeddings based on the cosine similarity, and retrieve the corresponding document using the `KNNRetriever` class from LangChain.

```
from langchain.retrievers import KNNRetrieverretriever = KNNRetriever.from_texts(documents, embeddings)# retrieve the most relevant documentsresult = retriever.get_relevant_documents(query)top1_retrieved_doc = result[0].page_content  # return the top1 retrieved resultprint(top1_retrieved_doc)
```

```
An LLMChain is a chain that composes basic LLM functionality. It consists of a PromptTemplate and a language model (either an LLM or chat model). It formats the prompt template using the input key values provided (and also memory key values, if available), passes the formatted string to LLM and returns the LLM output.
```
Embedding all documents using Quantized Embedders.

The embedders are based on optimized models, created by using [optimum-intel](https://github.com/huggingface/optimum-intel.git) and [IPEX](https://github.com/intel/intel-extension-for-pytorch).

Example text is based on [SBERT](https://www.sbert.net/docs/pretrained_cross-encoders.html).

```
from langchain_community.embeddings import QuantizedBiEncoderEmbeddingsmodel_name = "Intel/bge-small-en-v1.5-rag-int8-static"encode_kwargs = {"normalize_embeddings": True}  # set True to compute cosine similaritymodel = QuantizedBiEncoderEmbeddings(    model_name=model_name,    encode_kwargs=encode_kwargs,    query_instruction="Represent this sentence for searching relevant passages: ",)
```

```
loading configuration file inc_config.json from cache at INCConfig {  "distillation": {},  "neural_compressor_version": "2.4.1",  "optimum_version": "1.16.2",  "pruning": {},  "quantization": {    "dataset_num_samples": 50,    "is_static": true  },  "save_onnx_model": false,  "torch_version": "2.2.0",  "transformers_version": "4.37.2"}Using `INCModel` to load a TorchScript model will be deprecated in v1.15.0, to load your model please use `IPEXModel` instead.
```

Lets ask a question, and compare to 2 documents. The first contains the answer to the question, and the second one does not.

We can check better suits our query.

```
question = "How many people live in Berlin?"
```

```
documents = [    "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.",    "Berlin is well known for its museums.",]
```

```
doc_vecs = model.embed_documents(documents)
```

```
Batches: 100%|██████████| 1/1 [00:00<00:00,  4.18it/s]
```

```
query_vec = model.embed_query(question)
```

```
doc_vecs_torch = torch.tensor(doc_vecs)
```

```
query_vec_torch = torch.tensor(query_vec)
```

```
query_vec_torch @ doc_vecs_torch.T
```

We can see that indeed the first one ranks higher.
> [Hugging Face sentence-transformers](https://huggingface.co/sentence-transformers) is a Python framework for state-of-the-art sentence, text and image embeddings. One of the instruct embedding models is used in the `HuggingFaceInstructEmbeddings` class.

```
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
```

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

```
load INSTRUCTOR_Transformermax_seq_length  512
```

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

```
query_result = embeddings.embed_query(text)
```
## Xorbits inference (Xinference)

This notebook goes over how to use Xinference embeddings within LangChain

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

Install `Xinference` through PyPI:

```
%pip install --upgrade --quiet  "xinference[all]"
```

## Deploy Xinference Locally or in a Distributed Cluster.[​](#deploy-xinference-locally-or-in-a-distributed-cluster. "Direct link to Deploy Xinference Locally or in a Distributed Cluster.")

For local deployment, run `xinference`.

To deploy Xinference in a cluster, first start an Xinference supervisor using the `xinference-supervisor`. You can also use the option -p to specify the port and -H to specify the host. The default port is 9997.

Then, start the Xinference workers using `xinference-worker` on each server you want to run them on.

You can consult the README file from [Xinference](https://github.com/xorbitsai/inference) for more information.

## Wrapper[​](#wrapper "Direct link to Wrapper")

To use Xinference with LangChain, you need to first launch a model. You can use command line interface (CLI) to do so:

```
!xinference launch -n vicuna-v1.3 -f ggmlv3 -q q4_0
```

```
Model uid: 915845ee-2a04-11ee-8ed4-d29396a3f064
```

A model UID is returned for you to use. Now you can use Xinference embeddings with LangChain:

```
from langchain_community.embeddings import XinferenceEmbeddingsxinference = XinferenceEmbeddings(    server_url="http://0.0.0.0:9997", model_uid="915845ee-2a04-11ee-8ed4-d29396a3f064")
```

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

```
doc_result = xinference.embed_documents(["text A", "text B"])
```

Lastly, terminate the model when you do not need to use it:

```
!xinference terminate --model-uid "915845ee-2a04-11ee-8ed4-d29396a3f064"
```

* * *

#### Help us out by providing feedback on this documentation page:
Let’s load the `SageMaker Endpoints Embeddings` class. The class can be used if you host, e.g. your own Hugging Face model on SageMaker.

**Note**: In order to handle batched requests, you will need to adjust the return line in the `predict_fn()` function within the custom `inference.py` script:

`return {"vectors": sentence_embeddings.tolist()}`.

```
import jsonfrom typing import Dict, Listfrom langchain_community.embeddings import SagemakerEndpointEmbeddingsfrom langchain_community.embeddings.sagemaker_endpoint import EmbeddingsContentHandlerclass ContentHandler(EmbeddingsContentHandler):    content_type = "application/json"    accepts = "application/json"    def transform_input(self, inputs: list[str], model_kwargs: Dict) -> bytes:        """        Transforms the input into bytes that can be consumed by SageMaker endpoint.        Args:            inputs: List of input strings.            model_kwargs: Additional keyword arguments to be passed to the endpoint.        Returns:            The transformed bytes input.        """        # Example: inference.py expects a JSON string with a "inputs" key:        input_str = json.dumps({"inputs": inputs, **model_kwargs})        return input_str.encode("utf-8")    def transform_output(self, output: bytes) -> List[List[float]]:        """        Transforms the bytes output from the endpoint into a list of embeddings.        Args:            output: The bytes output from SageMaker endpoint.        Returns:            The transformed output - list of embeddings        Note:            The length of the outer list is the number of input strings.            The length of the inner lists is the embedding dimension.        """        # Example: inference.py returns a JSON string with the list of        # embeddings in a "vectors" key:        response_json = json.loads(output.read().decode("utf-8"))        return response_json["vectors"]content_handler = ContentHandler()embeddings = SagemakerEndpointEmbeddings(    # credentials_profile_name="credentials-profile-name",    endpoint_name="huggingface-pytorch-inference-2023-03-21-16-14-03-834",    region_name="us-east-1",    content_handler=content_handler,)# client = boto3.client(#     "sagemaker-runtime",#     region_name="us-west-2"# )# embeddings = SagemakerEndpointEmbeddings(#     endpoint_name="huggingface-pytorch-inference-2023-03-21-16-14-03-834",#     client=client#     content_handler=content_handler,# )
```
Load quantized BGE embedding models generated by [Intel® Extension for Transformers](https://github.com/intel/intel-extension-for-transformers) (ITREX) and use ITREX [Neural Engine](https://github.com/intel/intel-extension-for-transformers/blob/main/intel_extension_for_transformers/llm/runtime/deprecated/docs/Installation.md), a high-performance NLP backend, to accelerate the inference of models without compromising accuracy.

Refer to our blog of [Efficient Natural Language Embedding Models with Intel Extension for Transformers](https://medium.com/intel-analytics-software/efficient-natural-language-embedding-models-with-intel-extension-for-transformers-2b6fcd0f8f34) and [BGE optimization example](https://github.com/intel/intel-extension-for-transformers/tree/main/examples/huggingface/pytorch/text-embedding/deployment/mteb/bge) for more details.

```
from langchain_community.embeddings import QuantizedBgeEmbeddingsmodel_name = "Intel/bge-small-en-v1.5-sts-int8-static-inc"encode_kwargs = {"normalize_embeddings": True}  # set True to compute cosine similaritymodel = QuantizedBgeEmbeddings(    model_name=model_name,    encode_kwargs=encode_kwargs,    query_instruction="Represent this sentence for searching relevant passages: ",)
```

```
/home/yuwenzho/.conda/envs/bge/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html  from .autonotebook import tqdm as notebook_tqdm2024-03-04 10:17:17 [INFO] Start to extarct onnx model ops...2024-03-04 10:17:17 [INFO] Extract onnxruntime model done...2024-03-04 10:17:17 [INFO] Start to implement Sub-Graph matching and replacing...2024-03-04 10:17:18 [INFO] Sub-Graph match and replace done...
```

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

```
text = "This is a test document."query_result = model.embed_query(text)doc_result = model.embed_documents([text])
```

* * *

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

This notebook goes over how to use Langchain with [YandexGPT](https://cloud.yandex.com/en/services/yandexgpt) embeddings models.

To use, you should have the `yandexcloud` python package installed.

```
%pip install --upgrade --quiet  yandexcloud
```

First, you should [create service account](https://cloud.yandex.com/en/docs/iam/operations/sa/create) with the `ai.languageModels.user` role.

Next, you have two authentication options: - [IAM token](https://cloud.yandex.com/en/docs/iam/operations/iam-token/create-for-sa). You can specify the token in a constructor parameter `iam_token` or in an environment variable `YC_IAM_TOKEN`. - [API key](https://cloud.yandex.com/en/docs/iam/operations/api-key/create) You can specify the key in a constructor parameter `api_key` or in an environment variable `YC_API_KEY`.

To specify the model you can use `model_uri` parameter, see [the documentation](https://cloud.yandex.com/en/docs/yandexgpt/concepts/models#yandexgpt-embeddings) for more details.

By default, the latest version of `text-search-query` is used from the folder specified in the parameter `folder_id` or `YC_FOLDER_ID` environment variable.

```
from langchain_community.embeddings.yandex import YandexGPTEmbeddings
```

```
embeddings = YandexGPTEmbeddings()
```

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

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

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

```
[-0.021392822265625, 0.096435546875, -0.046966552734375, -0.0183258056640625, -0.00555419921875]
```

```
[-0.021392822265625, 0.096435546875, -0.046966552734375, -0.0183258056640625, -0.00555419921875]
```
## Jina

Let’s load the Jina Embedding class.

```
from langchain_community.embeddings import JinaEmbeddings
```

```
embeddings = JinaEmbeddings(    jina_api_key="jina_*", model_name="jina-embeddings-v2-base-en")
```

```
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:
## Self Hosted

Let’s load the `SelfHostedEmbeddings`, `SelfHostedHuggingFaceEmbeddings`, and `SelfHostedHuggingFaceInstructEmbeddings` classes.

```
import runhouse as rhfrom langchain_community.embeddings import (    SelfHostedEmbeddings,    SelfHostedHuggingFaceEmbeddings,    SelfHostedHuggingFaceInstructEmbeddings,)
```

```
# For an on-demand A100 with GCP, Azure, or Lambdagpu = rh.cluster(name="rh-a10x", instance_type="A100:1", use_spot=False)# For an on-demand A10G with AWS (no single A100s on AWS)# gpu = rh.cluster(name='rh-a10x', instance_type='g5.2xlarge', provider='aws')# For an existing cluster# gpu = rh.cluster(ips=['<ip of the cluster>'],#                  ssh_creds={'ssh_user': '...', 'ssh_private_key':'<path_to_key>'},#                  name='my-cluster')
```

```
embeddings = SelfHostedHuggingFaceEmbeddings(hardware=gpu)
```

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

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

And similarly for SelfHostedHuggingFaceInstructEmbeddings:

```
embeddings = SelfHostedHuggingFaceInstructEmbeddings(hardware=gpu)
```

Now let’s load an embedding model with a custom load function:

```
def get_pipeline():    from transformers import (        AutoModelForCausalLM,        AutoTokenizer,        pipeline,    )    model_id = "facebook/bart-base"    tokenizer = AutoTokenizer.from_pretrained(model_id)    model = AutoModelForCausalLM.from_pretrained(model_id)    return pipeline("feature-extraction", model=model, tokenizer=tokenizer)def inference_fn(pipeline, prompt):    # Return last hidden state of the model    if isinstance(prompt, list):        return [emb[0][-1] for emb in pipeline(prompt)]    return pipeline(prompt)[0][-1]
```

```
embeddings = SelfHostedEmbeddings(    model_load_fn=get_pipeline,    hardware=gpu,    model_reqs=["./", "torch", "transformers"],    inference_fn=inference_fn,)
```

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

> [spaCy](https://spacy.io/) is an open-source software library for advanced natural language processing, written in the programming languages Python and Cython.

## Installation and Setup[​](#installation-and-setup "Direct link to Installation and Setup")

```
%pip install --upgrade --quiet  spacy
```

Import the necessary classes

```
from langchain_community.embeddings.spacy_embeddings import SpacyEmbeddings
```

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

Initialize SpacyEmbeddings.This will load the Spacy model into memory.

```
embedder = SpacyEmbeddings(model_name="en_core_web_sm")
```

Define some example texts . These could be any documents that you want to analyze - for example, news articles, social media posts, or product reviews.

```
texts = [    "The quick brown fox jumps over the lazy dog.",    "Pack my box with five dozen liquor jugs.",    "How vexingly quick daft zebras jump!",    "Bright vixens jump; dozy fowl quack.",]
```

Generate and print embeddings for the texts . The SpacyEmbeddings class generates an embedding for each document, which is a numerical representation of the document’s content. These embeddings can be used for various natural language processing tasks, such as document similarity comparison or text classification.

```
embeddings = embedder.embed_documents(texts)for i, embedding in enumerate(embeddings):    print(f"Embedding for document {i+1}: {embedding}")
```

Generate and print an embedding for a single piece of text. You can also generate an embedding for a single piece of text, such as a search query. This can be useful for tasks like information retrieval, where you want to find documents that are similar to a given query.

```
query = "Quick foxes and lazy dogs."query_embedding = embedder.embed_query(query)print(f"Embedding for query: {query_embedding}")
```
> [Hugging Face sentence-transformers](https://huggingface.co/sentence-transformers) is a Python framework for state-of-the-art sentence, text and image embeddings. One of the embedding models is used in the `HuggingFaceEmbeddings` class. We have also added an alias for `SentenceTransformerEmbeddings` for users who are more familiar with directly using that package.

`sentence_transformers` package models are originating from [Sentence-BERT](https://arxiv.org/abs/1908.10084)

```
%pip install --upgrade --quiet  sentence_transformers > /dev/null
```

```
[notice] A new release of pip is available: 23.0.1 -> 23.1.1[notice] To update, run: pip install --upgrade pip
```

```
from langchain_community.embeddings import HuggingFaceEmbeddings
```

```
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")# Equivalent to SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
```

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

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

```
doc_result = embeddings.embed_documents([text, "This is not a test document."])
```

* * *

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

This notebook shows how to use agents to interact with the [Polygon IO](https://polygon.io/) toolkit. The toolkit provides access to Polygon’s Stock Market Data API.

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

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

```
%pip install --upgrade --quiet langchain-community > /dev/null
```

Get your Polygon IO API key [here](https://polygon.io/), and then set it below. Note that the tool used in this example requires a “Stocks Advanced” subscription

```
import getpassimport osos.environ["POLYGON_API_KEY"] = getpass.getpass()
```

It’s also helpful (but not needed) to set up [LangSmith](https://smith.langchain.com/) for best-in-class observability

```
# os.environ["LANGCHAIN_TRACING_V2"] = "true"# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
```

### Initializing the agent[​](#initializing-the-agent "Direct link to Initializing the agent")

```
from langchain import hubfrom langchain.agents import AgentExecutor, create_openai_functions_agentfrom langchain_community.agent_toolkits.polygon.toolkit import PolygonToolkitfrom langchain_community.utilities.polygon import PolygonAPIWrapperfrom langchain_openai import ChatOpenAIllm = ChatOpenAI(temperature=0)instructions = """You are an assistant."""base_prompt = hub.pull("langchain-ai/openai-functions-template")prompt = base_prompt.partial(instructions=instructions)
```

```
polygon = PolygonAPIWrapper()toolkit = PolygonToolkit.from_polygon_api_wrapper(polygon)agent = create_openai_functions_agent(llm, toolkit.get_tools(), prompt)
```

```
agent_executor = AgentExecutor(    agent=agent,    tools=toolkit.get_tools(),    verbose=True,)
```

### Get the last price quote for a stock[​](#get-the-last-price-quote-for-a-stock "Direct link to Get the last price quote for a stock")

```
agent_executor.invoke({"input": "What is the latest stock price for AAPL?"})
```

* * *

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

Official Website: [https://www.xfyun.cn/doc/spark/Embedding\_new\_api.html](https://www.xfyun.cn/doc/spark/Embedding_new_api.html)

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

SparkLLMTextEmbeddings support 2K token window and preduces vectors with 2560 dimensions.

```
from langchain_community.embeddings import SparkLLMTextEmbeddingsembeddings = SparkLLMTextEmbeddings(    spark_app_id="<spark_app_id>",    spark_api_key="<spark_api_key>",    spark_api_secret="<spark_api_secret>",)
```

Alternatively, you can set API key this way:

```
text_q = "Introducing iFlytek"text_1 = "Science and Technology Innovation Company Limited, commonly known as iFlytek, is a leading Chinese technology company specializing in speech recognition, natural language processing, and artificial intelligence. With a rich history and remarkable achievements, iFlytek has emerged as a frontrunner in the field of intelligent speech and language technologies.iFlytek has made significant contributions to the advancement of human-computer interaction through its cutting-edge innovations. Their advanced speech recognition technology has not only improved the accuracy and efficiency of voice input systems but has also enabled seamless integration of voice commands into various applications and devices.The company's commitment to research and development has been instrumental in its success. iFlytek invests heavily in fostering talent and collaboration with academic institutions, resulting in groundbreaking advancements in speech synthesis and machine translation. Their dedication to innovation has not only transformed the way we communicate but has also enhanced accessibility for individuals with disabilities."text_2 = "Moreover, iFlytek's impact extends beyond domestic boundaries, as they actively promote international cooperation and collaboration in the field of artificial intelligence. They have consistently participated in global competitions and contributed to the development of international standards.In recognition of their achievements, iFlytek has received numerous accolades and awards both domestically and internationally. Their contributions have revolutionized the way we interact with technology and have paved the way for a future where voice-based interfaces play a vital role.Overall, iFlytek is a trailblazer in the field of intelligent speech and language technologies, and their commitment to innovation and excellence deserves commendation."query_result = embeddings.embed_query(text_q)query_result[:8]
```

```
[-0.043609619140625, 0.2017822265625, 0.0270843505859375, -0.250244140625, -0.024993896484375, -0.0382080078125, 0.06207275390625, -0.0146331787109375]
```

```
doc_result = embeddings.embed_documents([text_1, text_2])doc_result[0][:8]
```

```
[-0.161865234375, 0.58984375, 0.998046875, 0.365966796875, 0.72900390625, 0.6015625, -0.8408203125, -0.2666015625]
```