本页还没有译文
本页 67 段还没译成한국어,下面显示的是英文原文。Embedding model integrations
还没翻译译自英文(docs.langchain.com)本页 67 段都还没译
Overview
Note
This overview covers text-based embedding models. LangChain does not currently support multimodal embeddings.
Embedding models transform raw text—such as a sentence, paragraph, or tweet—into a fixed-length vector of numbers that captures its semantic meaning. These vectors allow machines to compare and search text based on meaning rather than exact words.
In practice, this means that texts with similar ideas are placed close together in the vector space. For example, instead of matching only the phrase "machine learning", embeddings can surface documents that discuss related concepts even when different wording is used.
How it works
- Vectorization: The model encodes each input string as a high-dimensional vector.
- Similarity scoring: Vectors are compared using mathematical metrics to measure how closely related the underlying texts are.
Similarity metrics
Several metrics are commonly used to compare embeddings:
- Cosine similarity: measures the angle between two vectors.
- Euclidean distance: measures the straight-line distance between points.
- Dot product: measures how much one vector projects onto another.
Interface
LangChain provides a standard interface for text embedding models (e.g., OpenAI, Cohere, Hugging Face) via the Embeddings interface.
Two main methods are available:
embedDocuments(documents: string[]) → number[][]: Embeds a list of documents.embedQuery(text: string) → number[]: Embeds a single query.
Note
The interface allows queries and documents to be embedded with different strategies, though most providers handle them the same way in practice.
Install and use
Caching
Embeddings can be stored or temporarily cached to avoid needing to recompute them.
Caching embeddings can be done using a CacheBackedEmbeddings. This wrapper stores embeddings in a key-value store, where the text is hashed and the hash is used as the key in the cache.
The main supported way to initialize a CacheBackedEmbeddings is fromBytesStore. It takes the following parameters:
- underlyingEmbeddings: The embedder to use for embedding.
- documentEmbeddingStore: Any
BaseStorefor caching document embeddings. - options.namespace: (optional, defaults to
"") The namespace to use for the document cache. Helps avoid collisions (e.g., set it to the embedding model name).
Important
- Always set the
namespaceparameter to avoid collisions when using different embedding models. CacheBackedEmbeddingsdoes not cache query embeddings by default. To enable this, specify aquery_embedding_store.
import { CacheBackedEmbeddings } from "@langchain/classic/embeddings/cache_backed";
import { InMemoryStore } from "@langchain/core/stores";
const underlyingEmbeddings = new OpenAIEmbeddings();
const inMemoryStore = new InMemoryStore();
const cacheBackedEmbeddings = CacheBackedEmbeddings.fromBytesStore(
underlyingEmbeddings,
inMemoryStore,
{
namespace: underlyingEmbeddings.model,
}
);
// Example: caching a query embedding
const tic = Date.now();
const queryEmbedding = cacheBackedEmbeddings.embedQuery("Hello, world!");
console.log(`First call took: ${Date.now() - tic}ms`);
// Example: caching a document embedding
const tic = Date.now();
const documentEmbedding = cacheBackedEmbeddings.embedDocuments(["Hello, world!"]);
console.log(`Cached creation time: ${Date.now() - tic}ms`);In production, you would typically use a more robust persistent store, such as a database or cloud storage. Please see stores integrations for options.