Embeddings
Wrappers around embedding modules.
class langchain.embeddings.OpenAIEmbeddings(*, client=None, model='text-embedding-ada-002', deployment='text-embedding-ada-002', openai_api_version=None, openai_api_base=None, openai_api_type=None, openai_proxy=None, embedding_ctx_length=8191, openai_api_key=None, openai_organization=None, allowed_special={}, disallowed_special='all', chunk_size=1000, max_retries=6, request_timeout=None, headers=None, tiktoken_model_name=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around OpenAI embedding models.
To use, you should have the openai python package installed, and the
environment variable OPENAI_API_KEY set with your API key or pass it
as a named parameter to the constructor.
Example
from langchain.embeddings import OpenAIEmbeddings
openai = OpenAIEmbeddings(openai_api_key="my-api-key")
In order to use the library with Microsoft Azure endpoints, you need to set
the OPENAI_API_TYPE, OPENAI_API_BASE, OPENAI_API_KEY and OPENAI_API_VERSION.
The OPENAI_API_TYPE must be set to ‘azure’ and the others correspond to
the properties of your endpoint.
In addition, the deployment name must be passed as the model parameter.
Example
import os
os.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-03-15-preview"
os.environ["OPENAI_PROXY"] = "http://your-corporate-proxy:8080"
from langchain.embeddings.openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
    deployment="your-embeddings-deployment-name",
    model="your-embeddings-model-name",
    openai_api_base="https://your-endpoint.openai.azure.com/",
    openai_api_type="azure",
)
text = "This is a test query."
query_result = embeddings.embed_query(text)
Parameters
client (Any) – 
model (str) – 
deployment (str) – 
openai_api_version (Optional[str]) – 
openai_api_base (Optional[str]) – 
openai_api_type (Optional[str]) – 
openai_proxy (Optional[str]) – 
embedding_ctx_length (int) – 
openai_api_key (Optional[str]) – 
openai_organization (Optional[str]) – 
allowed_special (Union[Literal['all'], typing.Set[str]]) – 
disallowed_special (Union[Literal['all'], typing.Set[str], typing.Sequence[str]]) – 
chunk_size (int) – 
max_retries (int) – 
request_timeout (Optional[Union[float, Tuple[float, float]]]) – 
headers (Any) – 
tiktoken_model_name (Optional[str]) – 
Return type
None
attribute chunk_size: int = 1000
Maximum number of texts to embed in each batch
attribute max_retries: int = 6
Maximum number of retries to make when generating.
attribute request_timeout: Optional[Union[float, Tuple[float, float]]] = None
Timeout in seconds for the OpenAPI request.
attribute tiktoken_model_name: Optional[str] = None
The model name to pass to tiktoken when using this class.
Tiktoken is used to count the number of tokens in documents to constrain
them to be under a certain limit. By default, when set to None, this will
be the same as the embedding model name. However, there are some cases
where you may want to use this Embedding class with a model name not
supported by tiktoken. This can include when using Azure embeddings or
when using one of the many model providers that expose an OpenAI-like
API but with different models. In those cases, in order to avoid erroring
when tiktoken is called, you can specify a model name to use here.
async aembed_documents(texts, chunk_size=0)[source]
Call out to OpenAI’s embedding endpoint async for embedding search docs.
Parameters
texts (List[str]) – The list of texts to embed.
chunk_size (Optional[int]) – The chunk size of embeddings. If None, will use the chunk size
specified by the class.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
async aembed_query(text)[source]
Call out to OpenAI’s embedding endpoint async for embedding query text.
Parameters
text (str) – The text to embed.
Returns
Embedding for the text.
Return type
List[float]
embed_documents(texts, chunk_size=0)[source]
Call out to OpenAI’s embedding endpoint for embedding search docs.
Parameters
texts (List[str]) – The list of texts to embed.
chunk_size (Optional[int]) – The chunk size of embeddings. If None, will use the chunk size
specified by the class.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Call out to OpenAI’s embedding endpoint for embedding query text.
Parameters
text (str) – The text to embed.
Returns
Embedding for the text.
Return type
List[float]
class langchain.embeddings.HuggingFaceEmbeddings(*, client=None, model_name='sentence-transformers/all-mpnet-base-v2', cache_folder=None, model_kwargs=None, encode_kwargs=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around sentence_transformers embedding models.
To use, you should have the sentence_transformers python package installed.
Example
from langchain.embeddings import HuggingFaceEmbeddings
model_name = "sentence-transformers/all-mpnet-base-v2"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': False}
hf = HuggingFaceEmbeddings(
    model_name=model_name,
    model_kwargs=model_kwargs,
    encode_kwargs=encode_kwargs
)
Parameters
client (Any) – 
model_name (str) – 
cache_folder (Optional[str]) – 
model_kwargs (Dict[str, Any]) – 
encode_kwargs (Dict[str, Any]) – 
Return type
None
attribute cache_folder: Optional[str] = None
Path to store models.
Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.
attribute encode_kwargs: Dict[str, Any] [Optional]
Key word arguments to pass when calling the encode method of the model.
attribute model_kwargs: Dict[str, Any] [Optional]
Key word arguments to pass to the model.
attribute model_name: str = 'sentence-transformers/all-mpnet-base-v2'
Model name to use.
embed_documents(texts)[source]
Compute doc embeddings using a HuggingFace transformer model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a HuggingFace transformer model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.CohereEmbeddings(*, client=None, model='embed-english-v2.0', truncate=None, cohere_api_key=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around Cohere embedding models.
To use, you should have the cohere python package installed, and the
environment variable COHERE_API_KEY set with your API key or pass it
as a named parameter to the constructor.
Example
from langchain.embeddings import CohereEmbeddings
cohere = CohereEmbeddings(
    model="embed-english-light-v2.0", cohere_api_key="my-api-key"
)
Parameters
client (Any) – 
model (str) – 
truncate (Optional[str]) – 
cohere_api_key (Optional[str]) – 
Return type
None
attribute model: str = 'embed-english-v2.0'
Model name to use.
attribute truncate: Optional[str] = None
Truncate embeddings that are too long from start or end (“NONE”|”START”|”END”)
embed_documents(texts)[source]
Call out to Cohere’s embedding endpoint.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Call out to Cohere’s embedding endpoint.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.ElasticsearchEmbeddings(client, model_id, *, input_field='text_field')[source]
Bases: langchain.embeddings.base.Embeddings
Wrapper around Elasticsearch embedding models.
This class provides an interface to generate embeddings using a model deployed
in an Elasticsearch cluster. It requires an Elasticsearch connection object
and the model_id of the model deployed in the cluster.
In Elasticsearch you need to have an embedding model loaded and deployed.
- https://www.elastic.co/guide/en/elasticsearch/reference/current/infer-trained-model.html
- https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-deploy-models.html
Parameters
client (MlClient) – 
model_id (str) – 
input_field (str) – 
classmethod from_credentials(model_id, *, es_cloud_id=None, es_user=None, es_password=None, input_field='text_field')[source]
Instantiate embeddings from Elasticsearch credentials.
Parameters
model_id (str) – The model_id of the model deployed in the Elasticsearch
cluster.
input_field (str) – The name of the key for the input text field in the
document. Defaults to ‘text_field’.
es_cloud_id (Optional[str]) – (str, optional): The Elasticsearch cloud ID to connect to.
es_user (Optional[str]) – (str, optional): Elasticsearch username.
es_password (Optional[str]) – (str, optional): Elasticsearch password.
Return type
langchain.embeddings.elasticsearch.ElasticsearchEmbeddings
Example
from langchain.embeddings import ElasticsearchEmbeddings
# Define the model ID and input field name (if different from default)
model_id = "your_model_id"
# Optional, only if different from 'text_field'
input_field = "your_input_field"
# Credentials can be passed in two ways. Either set the env vars
# ES_CLOUD_ID, ES_USER, ES_PASSWORD and they will be automatically
# pulled in, or pass them in directly as kwargs.
embeddings = ElasticsearchEmbeddings.from_credentials(
    model_id,
    input_field=input_field,
    # es_cloud_id="foo",
    # es_user="bar",
    # es_password="baz",
)
documents = [
    "This is an example document.",
    "Another example document to generate embeddings for.",
]
embeddings_generator.embed_documents(documents)
classmethod from_es_connection(model_id, es_connection, input_field='text_field')[source]
Instantiate embeddings from an existing Elasticsearch connection.
This method provides a way to create an instance of the ElasticsearchEmbeddings
class using an existing Elasticsearch connection. The connection object is used
to create an MlClient, which is then used to initialize the
ElasticsearchEmbeddings instance.
Args:
model_id (str): The model_id of the model deployed in the Elasticsearch cluster.
es_connection (elasticsearch.Elasticsearch): An existing Elasticsearch
connection object. input_field (str, optional): The name of the key for the
input text field in the document. Defaults to ‘text_field’.
Returns:
ElasticsearchEmbeddings: An instance of the ElasticsearchEmbeddings class.
Example
from elasticsearch import Elasticsearch
from langchain.embeddings import ElasticsearchEmbeddings
# Define the model ID and input field name (if different from default)
model_id = "your_model_id"
# Optional, only if different from 'text_field'
input_field = "your_input_field"
# Create Elasticsearch connection
es_connection = Elasticsearch(
    hosts=["localhost:9200"], http_auth=("user", "password")
)
# Instantiate ElasticsearchEmbeddings using the existing connection
embeddings = ElasticsearchEmbeddings.from_es_connection(
    model_id,
    es_connection,
    input_field=input_field,
)
documents = [
    "This is an example document.",
    "Another example document to generate embeddings for.",
]
embeddings_generator.embed_documents(documents)
Parameters
model_id (str) – 
es_connection (Elasticsearch) – 
input_field (str) – 
Return type
ElasticsearchEmbeddings
embed_documents(texts)[source]
Generate embeddings for a list of documents.
Parameters
texts (List[str]) – A list of document text strings to generate embeddings
for.
Returns
A list of embeddings, one for each document in the inputlist.
Return type
List[List[float]]
embed_query(text)[source]
Generate an embedding for a single query text.
Parameters
text (str) – The query text to generate an embedding for.
Returns
The embedding for the input query text.
Return type
List[float]
class langchain.embeddings.LlamaCppEmbeddings(*, client=None, model_path, n_ctx=512, n_parts=- 1, seed=- 1, f16_kv=False, logits_all=False, vocab_only=False, use_mlock=False, n_threads=None, n_batch=8, n_gpu_layers=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around llama.cpp embedding models.
To use, you should have the llama-cpp-python library installed, and provide the
path to the Llama model as a named parameter to the constructor.
Check out: https://github.com/abetlen/llama-cpp-python
Example
from langchain.embeddings import LlamaCppEmbeddings
llama = LlamaCppEmbeddings(model_path="/path/to/model.bin")
Parameters
client (Any) – 
model_path (str) – 
n_ctx (int) – 
n_parts (int) – 
seed (int) – 
f16_kv (bool) – 
logits_all (bool) – 
vocab_only (bool) – 
use_mlock (bool) – 
n_threads (Optional[int]) – 
n_batch (Optional[int]) – 
n_gpu_layers (Optional[int]) – 
Return type
None
attribute f16_kv: bool = False
Use half-precision for key/value cache.
attribute logits_all: bool = False
Return logits for all tokens, not just the last token.
attribute n_batch: Optional[int] = 8
Number of tokens to process in parallel.
Should be a number between 1 and n_ctx.
attribute n_ctx: int = 512
Token context window.
attribute n_gpu_layers: Optional[int] = None
Number of layers to be loaded into gpu memory. Default None.
attribute n_parts: int = -1
Number of parts to split the model into.
If -1, the number of parts is automatically determined.
attribute n_threads: Optional[int] = None
Number of threads to use. If None, the number
of threads is automatically determined.
attribute seed: int = -1
Seed. If -1, a random seed is used.
attribute use_mlock: bool = False
Force system to keep model in RAM.
attribute vocab_only: bool = False
Only load the vocabulary, no weights.
embed_documents(texts)[source]
Embed a list of documents using the Llama model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Embed a query using the Llama model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.HuggingFaceHubEmbeddings(*, client=None, repo_id='sentence-transformers/all-mpnet-base-v2', task='feature-extraction', model_kwargs=None, huggingfacehub_api_token=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around HuggingFaceHub embedding models.
To use, you should have the huggingface_hub python package installed, and the
environment variable HUGGINGFACEHUB_API_TOKEN set with your API token, or pass
it as a named parameter to the constructor.
Example
from langchain.embeddings import HuggingFaceHubEmbeddings
repo_id = "sentence-transformers/all-mpnet-base-v2"
hf = HuggingFaceHubEmbeddings(
    repo_id=repo_id,
    task="feature-extraction",
    huggingfacehub_api_token="my-api-key",
)
Parameters
client (Any) – 
repo_id (str) – 
task (Optional[str]) – 
model_kwargs (Optional[dict]) – 
huggingfacehub_api_token (Optional[str]) – 
Return type
None
attribute model_kwargs: Optional[dict] = None
Key word arguments to pass to the model.
attribute repo_id: str = 'sentence-transformers/all-mpnet-base-v2'
Model name to use.
attribute task: Optional[str] = 'feature-extraction'
Task to call the model with.
embed_documents(texts)[source]
Call out to HuggingFaceHub’s embedding endpoint for embedding search docs.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Call out to HuggingFaceHub’s embedding endpoint for embedding query text.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.ModelScopeEmbeddings(*, embed=None, model_id='damo/nlp_corom_sentence-embedding_english-base')[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around modelscope_hub embedding models.
To use, you should have the modelscope python package installed.
Example
from langchain.embeddings import ModelScopeEmbeddings
model_id = "damo/nlp_corom_sentence-embedding_english-base"
embed = ModelScopeEmbeddings(model_id=model_id)
Parameters
embed (Any) – 
model_id (str) – 
Return type
None
attribute model_id: str = 'damo/nlp_corom_sentence-embedding_english-base'
Model name to use.
embed_documents(texts)[source]
Compute doc embeddings using a modelscope embedding model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a modelscope embedding model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.TensorflowHubEmbeddings(*, embed=None, model_url='https://tfhub.dev/google/universal-sentence-encoder-multilingual/3')[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around tensorflow_hub embedding models.
To use, you should have the tensorflow_text python package installed.
Example
from langchain.embeddings import TensorflowHubEmbeddings
url = "https://tfhub.dev/google/universal-sentence-encoder-multilingual/3"
tf = TensorflowHubEmbeddings(model_url=url)
Parameters
embed (Any) – 
model_url (str) – 
Return type
None
attribute model_url: str = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual/3'
Model name to use.
embed_documents(texts)[source]
Compute doc embeddings using a TensorflowHub embedding model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a TensorflowHub embedding model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.SagemakerEndpointEmbeddings(*, client=None, endpoint_name='', region_name='', credentials_profile_name=None, content_handler, model_kwargs=None, endpoint_kwargs=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around custom Sagemaker Inference Endpoints.
To use, you must supply the endpoint name from your deployed
Sagemaker model & the region where it is deployed.
To authenticate, the AWS client uses the following methods to
automatically load credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
If a specific credential profile should be used, you must pass
the name of the profile from the ~/.aws/credentials file that is to be used.
Make sure the credentials / roles used have the required policies to
access the Sagemaker endpoint.
See: https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html
Parameters
client (Any) – 
endpoint_name (str) – 
region_name (str) – 
credentials_profile_name (Optional[str]) – 
content_handler (langchain.embeddings.sagemaker_endpoint.EmbeddingsContentHandler) – 
model_kwargs (Optional[Dict]) – 
endpoint_kwargs (Optional[Dict]) – 
Return type
None
attribute content_handler: langchain.embeddings.sagemaker_endpoint.EmbeddingsContentHandler [Required]
The content handler class that provides an input and
output transform functions to handle formats between LLM
and the endpoint.
attribute credentials_profile_name: Optional[str] = None
The name of the profile in the ~/.aws/credentials or ~/.aws/config files, which
has either access keys or role information specified.
If not specified, the default credential profile or, if on an EC2 instance,
credentials from IMDS will be used.
See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
attribute endpoint_kwargs: Optional[Dict] = None
Optional attributes passed to the invoke_endpoint
function. See `boto3`_. docs for more info.
.. _boto3: <https://boto3.amazonaws.com/v1/documentation/api/latest/index.html>
attribute endpoint_name: str = ''
The name of the endpoint from the deployed Sagemaker model.
Must be unique within an AWS Region.
attribute model_kwargs: Optional[Dict] = None
Key word arguments to pass to the model.
attribute region_name: str = ''
The aws region where the Sagemaker model is deployed, eg. us-west-2.
embed_documents(texts, chunk_size=64)[source]
Compute doc embeddings using a SageMaker Inference Endpoint.
Parameters
texts (List[str]) – The list of texts to embed.
chunk_size (int) – The chunk size defines how many input texts will
be grouped together as request. If None, will use the
chunk size specified by the class.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a SageMaker inference endpoint.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.HuggingFaceInstructEmbeddings(*, client=None, model_name='hkunlp/instructor-large', cache_folder=None, model_kwargs=None, encode_kwargs=None, embed_instruction='Represent the document for retrieval: ', query_instruction='Represent the question for retrieving supporting documents: ')[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around sentence_transformers embedding models.
To use, you should have the sentence_transformers
and InstructorEmbedding python packages installed.
Example
from langchain.embeddings import HuggingFaceInstructEmbeddings
model_name = "hkunlp/instructor-large"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': True}
hf = HuggingFaceInstructEmbeddings(
    model_name=model_name,
    model_kwargs=model_kwargs,
    encode_kwargs=encode_kwargs
)
Parameters
client (Any) – 
model_name (str) – 
cache_folder (Optional[str]) – 
model_kwargs (Dict[str, Any]) – 
encode_kwargs (Dict[str, Any]) – 
embed_instruction (str) – 
query_instruction (str) – 
Return type
None
attribute cache_folder: Optional[str] = None
Path to store models.
Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.
attribute embed_instruction: str = 'Represent the document for retrieval: '
Instruction to use for embedding documents.
attribute encode_kwargs: Dict[str, Any] [Optional]
Key word arguments to pass when calling the encode method of the model.
attribute model_kwargs: Dict[str, Any] [Optional]
Key word arguments to pass to the model.
attribute model_name: str = 'hkunlp/instructor-large'
Model name to use.
attribute query_instruction: str = 'Represent the question for retrieving supporting documents: '
Instruction to use for embedding query.
embed_documents(texts)[source]
Compute doc embeddings using a HuggingFace instruct model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a HuggingFace instruct model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.MosaicMLInstructorEmbeddings(*, endpoint_url='https://models.hosted-on.mosaicml.hosting/instructor-xl/v1/predict', embed_instruction='Represent the document for retrieval: ', query_instruction='Represent the question for retrieving supporting documents: ', retry_sleep=1.0, mosaicml_api_token=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around MosaicML’s embedding inference service.
To use, you should have the
environment variable MOSAICML_API_TOKEN set with your API token, or pass
it as a named parameter to the constructor.
Example
from langchain.llms import MosaicMLInstructorEmbeddings
endpoint_url = (
    "https://models.hosted-on.mosaicml.hosting/instructor-large/v1/predict"
)
mosaic_llm = MosaicMLInstructorEmbeddings(
    endpoint_url=endpoint_url,
    mosaicml_api_token="my-api-key"
)
Parameters
endpoint_url (str) – 
embed_instruction (str) – 
query_instruction (str) – 
retry_sleep (float) – 
mosaicml_api_token (Optional[str]) – 
Return type
None
attribute embed_instruction: str = 'Represent the document for retrieval: '
Instruction used to embed documents.
attribute endpoint_url: str = 'https://models.hosted-on.mosaicml.hosting/instructor-xl/v1/predict'
Endpoint URL to use.
attribute query_instruction: str = 'Represent the question for retrieving supporting documents: '
Instruction used to embed the query.
attribute retry_sleep: float = 1.0
How long to try sleeping for if a rate limit is encountered
embed_documents(texts)[source]
Embed documents using a MosaicML deployed instructor embedding model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Embed a query using a MosaicML deployed instructor embedding model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.SelfHostedEmbeddings(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, pipeline_ref=None, client=None, inference_fn=<function _embed_documents>, hardware=None, model_load_fn, load_fn_kwargs=None, model_reqs=['./', 'torch'], inference_kwargs=None)[source]
Bases: langchain.llms.self_hosted.SelfHostedPipeline, langchain.embeddings.base.Embeddings
Runs custom embedding models on self-hosted remote hardware.
Supported hardware includes auto-launched instances on AWS, GCP, Azure,
and Lambda, as well as servers specified
by IP address and SSH credentials (such as on-prem, or another
cloud like Paperspace, Coreweave, etc.).
To use, you should have the runhouse python package installed.
Example using a model load function:from langchain.embeddings import SelfHostedEmbeddings
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import runhouse as rh
gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
def get_pipeline():
    model_id = "facebook/bart-large"
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(model_id)
    return pipeline("feature-extraction", model=model, tokenizer=tokenizer)
embeddings = SelfHostedEmbeddings(
    model_load_fn=get_pipeline,
    hardware=gpu
    model_reqs=["./", "torch", "transformers"],
)
Example passing in a pipeline path:from langchain.embeddings import SelfHostedHFEmbeddings
import runhouse as rh
from transformers import pipeline
gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
pipeline = pipeline(model="bert-base-uncased", task="feature-extraction")
rh.blob(pickle.dumps(pipeline),
    path="models/pipeline.pkl").save().to(gpu, path="models")
embeddings = SelfHostedHFEmbeddings.from_pipeline(
    pipeline="models/pipeline.pkl",
    hardware=gpu,
    model_reqs=["./", "torch", "transformers"],
)
Parameters
cache (Optional[bool]) – 
verbose (bool) – 
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – 
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) – 
tags (Optional[List[str]]) – 
pipeline_ref (Any) – 
client (Any) – 
inference_fn (Callable) – 
hardware (Any) – 
model_load_fn (Callable) – 
load_fn_kwargs (Optional[dict]) – 
model_reqs (List[str]) – 
inference_kwargs (Any) – 
Return type
None
attribute inference_fn: Callable = <function _embed_documents>
Inference function to extract the embeddings on the remote hardware.
attribute inference_kwargs: Any = None
Any kwargs to pass to the model’s inference function.
embed_documents(texts)[source]
Compute doc embeddings using a HuggingFace transformer model.
Parameters
texts (List[str]) – The list of texts to embed.s
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a HuggingFace transformer model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.SelfHostedHuggingFaceEmbeddings(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, pipeline_ref=None, client=None, inference_fn=<function _embed_documents>, hardware=None, model_load_fn=<function load_embedding_model>, load_fn_kwargs=None, model_reqs=['./', 'sentence_transformers', 'torch'], inference_kwargs=None, model_id='sentence-transformers/all-mpnet-base-v2')[source]
Bases: langchain.embeddings.self_hosted.SelfHostedEmbeddings
Runs sentence_transformers embedding models on self-hosted remote hardware.
Supported hardware includes auto-launched instances on AWS, GCP, Azure,
and Lambda, as well as servers specified
by IP address and SSH credentials (such as on-prem, or another cloud
like Paperspace, Coreweave, etc.).
To use, you should have the runhouse python package installed.
Example
from langchain.embeddings import SelfHostedHuggingFaceEmbeddings
import runhouse as rh
model_name = "sentence-transformers/all-mpnet-base-v2"
gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
hf = SelfHostedHuggingFaceEmbeddings(model_name=model_name, hardware=gpu)
Parameters
cache (Optional[bool]) – 
verbose (bool) – 
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – 
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) – 
tags (Optional[List[str]]) – 
pipeline_ref (Any) – 
client (Any) – 
inference_fn (Callable) – 
hardware (Any) – 
model_load_fn (Callable) – 
load_fn_kwargs (Optional[dict]) – 
model_reqs (List[str]) – 
inference_kwargs (Any) – 
model_id (str) – 
Return type
None
attribute hardware: Any = None
Remote hardware to send the inference function to.
attribute inference_fn: Callable = <function _embed_documents>
Inference function to extract the embeddings.
attribute load_fn_kwargs: Optional[dict] = None
Key word arguments to pass to the model load function.
attribute model_id: str = 'sentence-transformers/all-mpnet-base-v2'
Model name to use.
attribute model_load_fn: Callable = <function load_embedding_model>
Function to load the model remotely on the server.
attribute model_reqs: List[str] = ['./', 'sentence_transformers', 'torch']
Requirements to install on hardware to inference the model.
class langchain.embeddings.SelfHostedHuggingFaceInstructEmbeddings(*, cache=None, verbose=None, callbacks=None, callback_manager=None, tags=None, pipeline_ref=None, client=None, inference_fn=<function _embed_documents>, hardware=None, model_load_fn=<function load_embedding_model>, load_fn_kwargs=None, model_reqs=['./', 'InstructorEmbedding', 'torch'], inference_kwargs=None, model_id='hkunlp/instructor-large', embed_instruction='Represent the document for retrieval: ', query_instruction='Represent the question for retrieving supporting documents: ')[source]
Bases: langchain.embeddings.self_hosted_hugging_face.SelfHostedHuggingFaceEmbeddings
Runs InstructorEmbedding embedding models on self-hosted remote hardware.
Supported hardware includes auto-launched instances on AWS, GCP, Azure,
and Lambda, as well as servers specified
by IP address and SSH credentials (such as on-prem, or another
cloud like Paperspace, Coreweave, etc.).
To use, you should have the runhouse python package installed.
Example
from langchain.embeddings import SelfHostedHuggingFaceInstructEmbeddings
import runhouse as rh
model_name = "hkunlp/instructor-large"
gpu = rh.cluster(name='rh-a10x', instance_type='A100:1')
hf = SelfHostedHuggingFaceInstructEmbeddings(
    model_name=model_name, hardware=gpu)
Parameters
cache (Optional[bool]) – 
verbose (bool) – 
callbacks (Optional[Union[List[langchain.callbacks.base.BaseCallbackHandler], langchain.callbacks.base.BaseCallbackManager]]) – 
callback_manager (Optional[langchain.callbacks.base.BaseCallbackManager]) – 
tags (Optional[List[str]]) – 
pipeline_ref (Any) – 
client (Any) – 
inference_fn (Callable) – 
hardware (Any) – 
model_load_fn (Callable) – 
load_fn_kwargs (Optional[dict]) – 
model_reqs (List[str]) – 
inference_kwargs (Any) – 
model_id (str) – 
embed_instruction (str) – 
query_instruction (str) – 
Return type
None
attribute embed_instruction: str = 'Represent the document for retrieval: '
Instruction to use for embedding documents.
attribute model_id: str = 'hkunlp/instructor-large'
Model name to use.
attribute model_reqs: List[str] = ['./', 'InstructorEmbedding', 'torch']
Requirements to install on hardware to inference the model.
attribute query_instruction: str = 'Represent the question for retrieving supporting documents: '
Instruction to use for embedding query.
embed_documents(texts)[source]
Compute doc embeddings using a HuggingFace instruct model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a HuggingFace instruct model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.FakeEmbeddings(*, size)[source]
Bases: langchain.embeddings.base.Embeddings, pydantic.main.BaseModel
Parameters
size (int) – 
Return type
None
embed_documents(texts)[source]
Embed search docs.
Parameters
texts (List[str]) – 
Return type
List[List[float]]
embed_query(text)[source]
Embed query text.
Parameters
text (str) – 
Return type
List[float]
class langchain.embeddings.AlephAlphaAsymmetricSemanticEmbedding(*, client=None, model='luminous-base', hosting='https://api.aleph-alpha.com', normalize=True, compress_to_size=128, contextual_control_threshold=None, control_log_additive=True, aleph_alpha_api_key=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper for Aleph Alpha’s Asymmetric Embeddings
AA provides you with an endpoint to embed a document and a query.
The models were optimized to make the embeddings of documents and
the query for a document as similar as possible.
To learn more, check out: https://docs.aleph-alpha.com/docs/tasks/semantic_embed/
Example
from aleph_alpha import AlephAlphaAsymmetricSemanticEmbedding
embeddings = AlephAlphaSymmetricSemanticEmbedding()
document = "This is a content of the document"
query = "What is the content of the document?"
doc_result = embeddings.embed_documents([document])
query_result = embeddings.embed_query(query)
Parameters
client (Any) – 
model (Optional[str]) – 
hosting (Optional[str]) – 
normalize (Optional[bool]) – 
compress_to_size (Optional[int]) – 
contextual_control_threshold (Optional[int]) – 
control_log_additive (Optional[bool]) – 
aleph_alpha_api_key (Optional[str]) – 
Return type
None
attribute aleph_alpha_api_key: Optional[str] = None
API key for Aleph Alpha API.
attribute compress_to_size: Optional[int] = 128
Should the returned embeddings come back as an original 5120-dim vector,
or should it be compressed to 128-dim.
attribute contextual_control_threshold: Optional[int] = None
Attention control parameters only apply to those tokens that have
explicitly been set in the request.
attribute control_log_additive: Optional[bool] = True
Apply controls on prompt items by adding the log(control_factor)
to attention scores.
attribute hosting: Optional[str] = 'https://api.aleph-alpha.com'
Optional parameter that specifies which datacenters may process the request.
attribute model: Optional[str] = 'luminous-base'
Model name to use.
attribute normalize: Optional[bool] = True
Should returned embeddings be normalized
embed_documents(texts)[source]
Call out to Aleph Alpha’s asymmetric Document endpoint.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Call out to Aleph Alpha’s asymmetric, query embedding endpoint
:param text: The text to embed.
Returns
Embeddings for the text.
Parameters
text (str) – 
Return type
List[float]
class langchain.embeddings.AlephAlphaSymmetricSemanticEmbedding(*, client=None, model='luminous-base', hosting='https://api.aleph-alpha.com', normalize=True, compress_to_size=128, contextual_control_threshold=None, control_log_additive=True, aleph_alpha_api_key=None)[source]
Bases: langchain.embeddings.aleph_alpha.AlephAlphaAsymmetricSemanticEmbedding
The symmetric version of the Aleph Alpha’s semantic embeddings.
The main difference is that here, both the documents and
queries are embedded with a SemanticRepresentation.Symmetric
.. rubric:: Example
from aleph_alpha import AlephAlphaSymmetricSemanticEmbedding
embeddings = AlephAlphaAsymmetricSemanticEmbedding()
text = "This is a test text"
doc_result = embeddings.embed_documents([text])
query_result = embeddings.embed_query(text)
Parameters
client (Any) – 
model (Optional[str]) – 
hosting (Optional[str]) – 
normalize (Optional[bool]) – 
compress_to_size (Optional[int]) – 
contextual_control_threshold (Optional[int]) – 
control_log_additive (Optional[bool]) – 
aleph_alpha_api_key (Optional[str]) – 
Return type
None
embed_documents(texts)[source]
Call out to Aleph Alpha’s Document endpoint.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Call out to Aleph Alpha’s asymmetric, query embedding endpoint
:param text: The text to embed.
Returns
Embeddings for the text.
Parameters
text (str) – 
Return type
List[float]
langchain.embeddings.SentenceTransformerEmbeddings
alias of langchain.embeddings.huggingface.HuggingFaceEmbeddings
class langchain.embeddings.MiniMaxEmbeddings(*, endpoint_url='https://api.minimax.chat/v1/embeddings', model='embo-01', embed_type_db='db', embed_type_query='query', minimax_group_id=None, minimax_api_key=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around MiniMax’s embedding inference service.
To use, you should have the environment variable MINIMAX_GROUP_ID and
MINIMAX_API_KEY set with your API token, or pass it as a named parameter to
the constructor.
Example
from langchain.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])
Parameters
endpoint_url (str) – 
model (str) – 
embed_type_db (str) – 
embed_type_query (str) – 
minimax_group_id (Optional[str]) – 
minimax_api_key (Optional[str]) – 
Return type
None
attribute embed_type_db: str = 'db'
For embed_documents
attribute embed_type_query: str = 'query'
For embed_query
attribute endpoint_url: str = 'https://api.minimax.chat/v1/embeddings'
Endpoint URL to use.
attribute minimax_api_key: Optional[str] = None
API Key for MiniMax API.
attribute minimax_group_id: Optional[str] = None
Group ID for MiniMax API.
attribute model: str = 'embo-01'
Embeddings model name to use.
embed_documents(texts)[source]
Embed documents using a MiniMax embedding endpoint.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Embed a query using a MiniMax embedding endpoint.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.BedrockEmbeddings(*, client=None, region_name=None, credentials_profile_name=None, model_id='amazon.titan-e1t-medium', model_kwargs=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Embeddings provider to invoke Bedrock embedding models.
To authenticate, the AWS client uses the following methods to
automatically load credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
If a specific credential profile should be used, you must pass
the name of the profile from the ~/.aws/credentials file that is to be used.
Make sure the credentials / roles used have the required policies to
access the Bedrock service.
Parameters
client (Any) – 
region_name (Optional[str]) – 
credentials_profile_name (Optional[str]) – 
model_id (str) – 
model_kwargs (Optional[Dict]) – 
Return type
None
attribute credentials_profile_name: Optional[str] = None
The name of the profile in the ~/.aws/credentials or ~/.aws/config files, which
has either access keys or role information specified.
If not specified, the default credential profile or, if on an EC2 instance,
credentials from IMDS will be used.
See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
attribute model_id: str = 'amazon.titan-e1t-medium'
Id of the model to call, e.g., amazon.titan-e1t-medium, this is
equivalent to the modelId property in the list-foundation-models api
attribute model_kwargs: Optional[Dict] = None
Key word arguments to pass to the model.
attribute region_name: Optional[str] = None
The aws region e.g., us-west-2. Fallsback to AWS_DEFAULT_REGION env variable
or region specified in ~/.aws/config in case it is not provided here.
embed_documents(texts, chunk_size=1)[source]
Compute doc embeddings using a Bedrock model.
Parameters
texts (List[str]) – The list of texts to embed.
chunk_size (int) – Bedrock currently only allows single string
inputs, so chunk size is always 1. This input is here
only for compatibility with the embeddings interface.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Compute query embeddings using a Bedrock model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.DeepInfraEmbeddings(*, model_id='sentence-transformers/clip-ViT-B-32', normalize=False, embed_instruction='passage: ', query_instruction='query: ', model_kwargs=None, deepinfra_api_token=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around Deep Infra’s embedding inference service.
To use, you should have the
environment variable DEEPINFRA_API_TOKEN set with your API token, or pass
it as a named parameter to the constructor.
There are multiple embeddings models available,
see https://deepinfra.com/models?type=embeddings.
Example
from langchain.embeddings import DeepInfraEmbeddings
deepinfra_emb = DeepInfraEmbeddings(
    model_id="sentence-transformers/clip-ViT-B-32",
    deepinfra_api_token="my-api-key"
)
r1 = deepinfra_emb.embed_documents(
    [
        "Alpha is the first letter of Greek alphabet",
        "Beta is the second letter of Greek alphabet",
    ]
)
r2 = deepinfra_emb.embed_query(
    "What is the second letter of Greek alphabet"
)
Parameters
model_id (str) – 
normalize (bool) – 
embed_instruction (str) – 
query_instruction (str) – 
model_kwargs (Optional[dict]) – 
deepinfra_api_token (Optional[str]) – 
Return type
None
attribute embed_instruction: str = 'passage: '
Instruction used to embed documents.
attribute model_id: str = 'sentence-transformers/clip-ViT-B-32'
Embeddings model to use.
attribute model_kwargs: Optional[dict] = None
Other model keyword args
attribute normalize: bool = False
whether to normalize the computed embeddings
attribute query_instruction: str = 'query: '
Instruction used to embed the query.
embed_documents(texts)[source]
Embed documents using a Deep Infra deployed embedding model.
Parameters
texts (List[str]) – The list of texts to embed.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Embed a query using a Deep Infra deployed embedding model.
Parameters
text (str) – The text to embed.
Returns
Embeddings for the text.
Return type
List[float]
class langchain.embeddings.DashScopeEmbeddings(*, client=None, model='text-embedding-v1', dashscope_api_key=None, max_retries=5)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around DashScope embedding models.
To use, you should have the dashscope python package installed, and the
environment variable DASHSCOPE_API_KEY set with your API key or pass it
as a named parameter to the constructor.
Example
from langchain.embeddings import DashScopeEmbeddings
embeddings = DashScopeEmbeddings(dashscope_api_key="my-api-key")
Example
import os
os.environ["DASHSCOPE_API_KEY"] = "your DashScope API KEY"
from langchain.embeddings.dashscope import DashScopeEmbeddings
embeddings = DashScopeEmbeddings(
    model="text-embedding-v1",
)
text = "This is a test query."
query_result = embeddings.embed_query(text)
Parameters
client (Any) – 
model (str) – 
dashscope_api_key (Optional[str]) – 
max_retries (int) – 
Return type
None
attribute dashscope_api_key: Optional[str] = None
Maximum number of retries to make when generating.
embed_documents(texts)[source]
Call out to DashScope’s embedding endpoint for embedding search docs.
Parameters
texts (List[str]) – The list of texts to embed.
chunk_size – The chunk size of embeddings. If None, will use the chunk size
specified by the class.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Call out to DashScope’s embedding endpoint for embedding query text.
Parameters
text (str) – The text to embed.
Returns
Embedding for the text.
Return type
List[float]
class langchain.embeddings.EmbaasEmbeddings(*, model='e5-large-v2', instruction=None, api_url='https://api.embaas.io/v1/embeddings/', embaas_api_key=None)[source]
Bases: pydantic.main.BaseModel, langchain.embeddings.base.Embeddings
Wrapper around embaas’s embedding service.
To use, you should have the
environment variable EMBAAS_API_KEY set with your API key, or pass
it as a named parameter to the constructor.
Example
# Initialise with default model and instruction
from langchain.embeddings import EmbaasEmbeddings
emb = EmbaasEmbeddings()
# Initialise with custom model and instruction
from langchain.embeddings import EmbaasEmbeddings
emb_model = "instructor-large"
emb_inst = "Represent the Wikipedia document for retrieval"
emb = EmbaasEmbeddings(
    model=emb_model,
    instruction=emb_inst
)
Parameters
model (str) – 
instruction (Optional[str]) – 
api_url (str) – 
embaas_api_key (Optional[str]) – 
Return type
None
attribute api_url: str = 'https://api.embaas.io/v1/embeddings/'
The URL for the embaas embeddings API.
attribute instruction: Optional[str] = None
Instruction used for domain-specific embeddings.
attribute model: str = 'e5-large-v2'
The model used for embeddings.
embed_documents(texts)[source]
Get embeddings for a list of texts.
Parameters
texts (List[str]) – The list of texts to get embeddings for.
Returns
List of embeddings, one for each text.
Return type
List[List[float]]
embed_query(text)[source]
Get embeddings for a single text.
Parameters
text (str) – The text to get embeddings for.
Returns
List of embeddings.
Return type
List[float]