*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   Text embedding models

On this page

Text embedding models
=====================

info

Head to [Integrations](/v0.2/docs/integrations/text_embedding/) for documentation on built-in integrations with text embedding model providers.

The Embeddings class is a class designed for interfacing with text embedding models. There are lots of embedding model providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them.

Embeddings create a vector representation of a piece of text. This is useful because it means we can think about text in the vector space, and do things like semantic search where we look for pieces of text that are most similar in the vector space.

The base Embeddings class in LangChain provides two methods: one for embedding documents and one for embedding a query. The former, `.embed_documents`, takes as input multiple texts, while the latter, `.embed_query`, takes a single text. The reason for having these as two separate methods is that some embedding providers have different embedding methods for documents (to be searched over) vs queries (the search query itself). `.embed_query` will return a list of floats, whereas `.embed_documents` returns a list of lists of floats.

Get started[​](#get-started "Direct link to Get started")
---------------------------------------------------------

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

*   OpenAI
*   Cohere
*   Hugging Face

To start we'll need to install the OpenAI partner package:

    pip install langchain-openai

Accessing the API requires an API key, which you can get by creating an account and heading [here](https://platform.openai.com/account/api-keys). Once we have a key we'll want to set it as an environment variable by running:

    export OPENAI_API_KEY="..."

If you'd prefer not to set an environment variable you can pass the key in directly via the `api_key` named parameter when initiating the OpenAI LLM class:

    from langchain_openai import OpenAIEmbeddingsembeddings_model = OpenAIEmbeddings(api_key="...")

**API Reference:**[OpenAIEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html)

Otherwise you can initialize without any params:

    from langchain_openai import OpenAIEmbeddingsembeddings_model = OpenAIEmbeddings()

**API Reference:**[OpenAIEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html)

To start we'll need to install the Cohere SDK package:

    pip install langchain-cohere

Accessing the API requires an API key, which you can get by creating an account and heading [here](https://dashboard.cohere.com/api-keys). Once we have a key we'll want to set it as an environment variable by running:

    export COHERE_API_KEY="..."

If you'd prefer not to set an environment variable you can pass the key in directly via the `cohere_api_key` named parameter when initiating the Cohere LLM class:

    from langchain_cohere import CohereEmbeddingsembeddings_model = CohereEmbeddings(cohere_api_key="...")

**API Reference:**[CohereEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_cohere.embeddings.CohereEmbeddings.html)

Otherwise you can initialize without any params:

    from langchain_cohere import CohereEmbeddingsembeddings_model = CohereEmbeddings()

**API Reference:**[CohereEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_cohere.embeddings.CohereEmbeddings.html)

To start we'll need to install the Hugging Face partner package:

    pip install langchain-huggingface

You can then load any [Sentence Transformers model](https://huggingface.co/models?library=sentence-transformers) from the Hugging Face Hub.

    from langchain_huggingface import HuggingFaceEmbeddingsembeddings_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")

**API Reference:**[HuggingFaceEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_huggingface.embeddings.huggingface.HuggingFaceEmbeddings.html)

You can also leave the `model_name` blank to use the default [sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2) model.

    from langchain_huggingface import HuggingFaceEmbeddingsembeddings_model = HuggingFaceEmbeddings()

**API Reference:**[HuggingFaceEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_huggingface.embeddings.huggingface.HuggingFaceEmbeddings.html)

### `embed_documents`[​](#embed_documents "Direct link to embed_documents")

#### Embed list of texts[​](#embed-list-of-texts "Direct link to Embed list of texts")

Use `.embed_documents` to embed a list of strings, recovering a list of embeddings:

    embeddings = embeddings_model.embed_documents(    [        "Hi there!",        "Oh, hello!",        "What's your name?",        "My friends call me World",        "Hello World!"    ])len(embeddings), len(embeddings[0])

    (5, 1536)

### `embed_query`[​](#embed_query "Direct link to embed_query")

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

Use `.embed_query` to embed a single piece of text (e.g., for the purpose of comparing to other embedded pieces of texts).

    embedded_query = embeddings_model.embed_query("What was the name mentioned in the conversation?")embedded_query[:5]

    [0.0053587136790156364, -0.0004999046213924885, 0.038883671164512634, -0.003001077566295862, -0.00900818221271038]

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/embed_text.mdx)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to create a dynamic (self-constructing) chain

](/v0.2/docs/how_to/dynamic_chain/)[

Next

How to combine results from multiple retrievers

](/v0.2/docs/how_to/ensemble_retriever/)

*   [Get started](#get-started)
    *   [Setup](#setup)
    *   [`embed_documents`](#embed_documents)
    *   [`embed_query`](#embed_query)