Skip to main content
`Ctrl`+`K`

# Embeddings

Wrappers around embedding modules.

pydantic model langchain.embeddings.AlephAlphaAsymmetricSemanticEmbedding

  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)

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

    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]

    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

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

    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]

    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

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

    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]

    Call out to Cohere’s embedding endpoint.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

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

    Embed search docs.

  embed_query(text: str) → List[float]

    Embed query text.

pydantic model langchain.embeddings.HuggingFaceEmbeddings

  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'}
      hf = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs)

  field cache_folder: Optional[str] = None

    Path to store models. Can be also set by SENTENCE_TRANSFORMERS_HOME enviroment variable.

  field model_kwargs: Dict[str, Any] [Optional]

    Key word arguments to pass to the model.

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

    Model name to use.

  embed_documents(texts: List[str]) → List[List[float]]

    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]

    Compute query embeddings using a HuggingFace transformer model.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

pydantic model langchain.embeddings.HuggingFaceHubEmbeddings

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

    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]

    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

  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"
      model_kwargs = {'device': 'cpu'}
      hf = HuggingFaceInstructEmbeddings(
          model_name=model_name, model_kwargs=model_kwargs
      )

  field cache_folder: Optional[str] = None

    Path to store models. Can be also set by SENTENCE_TRANSFORMERS_HOME enviroment variable.

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

    Instruction to use for embedding documents.

  field model_kwargs: Dict[str, Any] [Optional]

    Key word arguments to pass to the model.

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

    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]

    Compute query embeddings using a HuggingFace instruct model.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

pydantic model langchain.embeddings.LlamaCppEmbeddings

  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: abetlen/llama-cpp-python

  Example

      from langchain.embeddings import LlamaCppEmbeddings
      llama = LlamaCppEmbeddings(model_path="/path/to/model.bin")

  field f16_kv: bool = False

    Use half-precision for key/value cache.

  field logits_all: bool = False

    Return logits for all tokens, not just the last token.

  field n_batch: Optional[int] = 8

    Number of tokens to process in parallel. Should be a number between 1 and n_ctx.

  field n_ctx: int = 512

    Token context window.

  field n_parts: int = -1

    Number of parts to split the model into. If -1, the number of parts is automatically determined.

  field n_threads: Optional[int] = None

    Number of threads to use. If None, the number of threads is automatically determined.

  field seed: int = -1

    Seed. If -1, a random seed is used.

  field use_mlock: bool = False

    Force system to keep model in RAM.

  field vocab_only: bool = False

    Only load the vocabulary, no weights.

  embed_documents(texts: List[str]) → List[List[float]]

    Embed a list of documents using the Llama model.

    Parameters

      texts – The list of texts to embed.

    Returns

      List of embeddings, one for each text.

  embed_query(text: str) → List[float]

    Embed a query using the Llama model.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

pydantic model langchain.embeddings.OpenAIEmbeddings

  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(
          deployment="your-embeddings-deployment-name",
          model="your-embeddings-model-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]]

    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]

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

    Parameters

      text – The text to embed.

    Returns

      Embedding for the text.

pydantic model langchain.embeddings.SagemakerEndpointEmbeddings

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

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

    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]

    Compute query embeddings using a SageMaker inference endpoint.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

pydantic model langchain.embeddings.SelfHostedEmbeddings

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

    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]

    Compute query embeddings using a HuggingFace transformer model.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

pydantic model langchain.embeddings.SelfHostedHuggingFaceEmbeddings

  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

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

    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]

    Compute query embeddings using a HuggingFace instruct model.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.

pydantic model langchain.embeddings.TensorflowHubEmbeddings

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

    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]

    Compute query embeddings using a TensorflowHub embedding model.

    Parameters

      text – The text to embed.

    Returns

      Embeddings for the text.