mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-26 09:53:41 +00:00
- Add to embedding model tabs - Add tabs for vector stores - Add "hello world" examples in integration landing pages using tabs
62 lines
2.1 KiB
Plaintext
62 lines
2.1 KiB
Plaintext
# Text embedding models
|
|
|
|
:::info
|
|
Head to [Integrations](/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
|
|
|
|
### Setup
|
|
|
|
import EmbeddingTabs from "@theme/EmbeddingTabs";
|
|
|
|
<EmbeddingTabs customVarName="embeddings_model" />
|
|
|
|
### `embed_documents`
|
|
#### Embed list of texts
|
|
|
|
Use `.embed_documents` to embed a list of strings, recovering a list of embeddings:
|
|
|
|
```python
|
|
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])
|
|
```
|
|
|
|
```output
|
|
(5, 1536)
|
|
```
|
|
|
|
|
|
### `embed_query`
|
|
#### 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).
|
|
|
|
```python
|
|
embedded_query = embeddings_model.embed_query("What was the name mentioned in the conversation?")
|
|
embedded_query[:5]
|
|
```
|
|
|
|
```output
|
|
[0.0053587136790156364,
|
|
-0.0004999046213924885,
|
|
0.038883671164512634,
|
|
-0.003001077566295862,
|
|
-0.00900818221271038]
|
|
```
|
|
|