Embeddings#

Wrappers around embedding modules.

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