Embeddings#

Wrappers around embedding modules.

pydantic model langchain.embeddings.AlephAlphaAsymmetricSemanticEmbedding[source]#

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 a document and the query about the document as similar to each other as possible. 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/ 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)
field 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.

field contextual_control_threshold: Optional[int] = None#

Attention control parameters only apply to those tokens that have explicitly been set in the request.

field control_log_additive: Optional[bool] = True#

Apply controls on prompt items by adding the log(control_factor) to attention scores.

field hosting: Optional[str] = 'https://api.aleph-alpha.com'#

Optional parameter that specifies which datacenters may process the request.

field model: Optional[str] = 'luminous-base'#

Model name to use.

field normalize: Optional[bool] = True#

Should returned embeddings be normalized

embed_documents(texts: List[str]) List[List[float]][source]#

Call out to Aleph Alpha’s asymmetric Document endpoint.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Call out to Aleph Alpha’s asymmetric, query embedding endpoint :param text: The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.AlephAlphaSymmetricSemanticEmbedding[source]#

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

embed_documents(texts: List[str]) List[List[float]][source]#

Call out to Aleph Alpha’s Document endpoint.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Call out to Aleph Alpha’s asymmetric, query embedding endpoint :param text: The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.CohereEmbeddings[source]#

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="medium", cohere_api_key="my-api-key")
field model: str = 'large'#

Model name to use.

field truncate: Optional[str] = None#

Truncate embeddings that are too long from start or end (“NONE”|”START”|”END”)

embed_documents(texts: List[str]) List[List[float]][source]#

Call out to Cohere’s embedding endpoint.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Call out to Cohere’s embedding endpoint.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.FakeEmbeddings[source]#
embed_documents(texts: List[str]) List[List[float]][source]#

Embed search docs.

embed_query(text: str) List[float][source]#

Embed query text.

pydantic model langchain.embeddings.HuggingFaceEmbeddings[source]#

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"
hf = HuggingFaceEmbeddings(model_name=model_name)
field model_name: str = 'sentence-transformers/all-mpnet-base-v2'#

Model name to use.

embed_documents(texts: List[str]) List[List[float]][source]#

Compute doc embeddings using a HuggingFace transformer model.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Compute query embeddings using a HuggingFace transformer model.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.HuggingFaceHubEmbeddings[source]#

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",
)
field model_kwargs: Optional[dict] = None#

Key word arguments to pass to the model.

field repo_id: str = 'sentence-transformers/all-mpnet-base-v2'#

Model name to use.

field task: Optional[str] = 'feature-extraction'#

Task to call the model with.

embed_documents(texts: List[str]) List[List[float]][source]#

Call out to HuggingFaceHub’s embedding endpoint for embedding search docs.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Call out to HuggingFaceHub’s embedding endpoint for embedding query text.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.HuggingFaceInstructEmbeddings[source]#

Wrapper around sentence_transformers embedding models.

To use, you should have the sentence_transformers and InstructorEmbedding python package installed.

Example

from langchain.embeddings import HuggingFaceInstructEmbeddings
model_name = "hkunlp/instructor-large"
hf = HuggingFaceInstructEmbeddings(model_name=model_name)
field embed_instruction: str = 'Represent the document for retrieval: '#

Instruction to use for embedding documents.

field model_name: str = 'hkunlp/instructor-large'#

Model name to use.

field query_instruction: str = 'Represent the question for retrieving supporting documents: '#

Instruction to use for embedding query.

embed_documents(texts: List[str]) List[List[float]][source]#

Compute doc embeddings using a HuggingFace instruct model.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Compute query embeddings using a HuggingFace instruct model.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.OpenAIEmbeddings[source]#

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 optionally and 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"

from langchain.embeddings.openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="your-embeddings-deployment-name")
text = "This is a test query."
query_result = embeddings.embed_query(text)
field chunk_size: int = 1000#

Maximum number of texts to embed in each batch

field max_retries: int = 6#

Maximum number of retries to make when generating.

embed_documents(texts: List[str], chunk_size: Optional[int] = 0) List[List[float]][source]#

Call out to OpenAI’s embedding endpoint for embedding search docs.

Parameters
  • texts – 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.

embed_query(text: str) List[float][source]#

Call out to OpenAI’s embedding endpoint for embedding query text.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.SagemakerEndpointEmbeddings[source]#

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

field content_handler: langchain.llms.sagemaker_endpoint.ContentHandlerBase [Required]#

The content handler class that provides an input and output transform functions to handle formats between LLM and the endpoint.

field 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

field 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>

field endpoint_name: str = ''#

The name of the endpoint from the deployed Sagemaker model. Must be unique within an AWS Region.

field model_kwargs: Optional[Dict] = None#

Key word arguments to pass to the model.

field region_name: str = ''#

The aws region where the Sagemaker model is deployed, eg. us-west-2.

embed_documents(texts: List[str], chunk_size: int = 64) List[List[float]][source]#

Compute doc embeddings using a SageMaker Inference Endpoint.

Parameters
  • texts – The list of texts to embed.

  • chunk_size – 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.

embed_query(text: str) List[float][source]#

Compute query embeddings using a SageMaker inference endpoint.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.SelfHostedEmbeddings[source]#

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"],
)
Validators
  • set_callback_manager » callback_manager

  • set_verbose » verbose

field inference_fn: Callable = <function _embed_documents>#

Inference function to extract the embeddings on the remote hardware.

field inference_kwargs: Any = None#

Any kwargs to pass to the model’s inference function.

embed_documents(texts: List[str]) List[List[float]][source]#

Compute doc embeddings using a HuggingFace transformer model.

Parameters

texts – The list of texts to embed.s

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Compute query embeddings using a HuggingFace transformer model.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.SelfHostedHuggingFaceEmbeddings[source]#

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)
Validators
  • set_callback_manager » callback_manager

  • set_verbose » verbose

field hardware: Any = None#

Remote hardware to send the inference function to.

field inference_fn: Callable = <function _embed_documents>#

Inference function to extract the embeddings.

field load_fn_kwargs: Optional[dict] = None#

Key word arguments to pass to the model load function.

field model_id: str = 'sentence-transformers/all-mpnet-base-v2'#

Model name to use.

field model_load_fn: Callable = <function load_embedding_model>#

Function to load the model remotely on the server.

field model_reqs: List[str] = ['./', 'sentence_transformers', 'torch']#

Requirements to install on hardware to inference the model.

pydantic model langchain.embeddings.SelfHostedHuggingFaceInstructEmbeddings[source]#

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)
Validators
  • set_callback_manager » callback_manager

  • set_verbose » verbose

field embed_instruction: str = 'Represent the document for retrieval: '#

Instruction to use for embedding documents.

field model_id: str = 'hkunlp/instructor-large'#

Model name to use.

field model_reqs: List[str] = ['./', 'InstructorEmbedding', 'torch']#

Requirements to install on hardware to inference the model.

field query_instruction: str = 'Represent the question for retrieving supporting documents: '#

Instruction to use for embedding query.

embed_documents(texts: List[str]) List[List[float]][source]#

Compute doc embeddings using a HuggingFace instruct model.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Compute query embeddings using a HuggingFace instruct model.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

pydantic model langchain.embeddings.TensorflowHubEmbeddings[source]#

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)
field model_url: str = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual/3'#

Model name to use.

embed_documents(texts: List[str]) List[List[float]][source]#

Compute doc embeddings using a TensorflowHub embedding model.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]#

Compute query embeddings using a TensorflowHub embedding model.

Parameters

text – The text to embed.

Returns

Embeddings for the text.