원클릭으로
langchain-embeddings
Guide to using embedding model integrations in LangChain including OpenAI, Azure, and local embeddings
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Guide to using embedding model integrations in LangChain including OpenAI, Azure, and local embeddings
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Using the Deep Agents CLI - terminal interface, persistent memory with AGENTS.md, project conventions, skills directories, and CLI commands.
Understanding Deep Agents framework - what they are, how to create them with createDeepAgent, and the agent harness architecture with built-in middleware for planning, filesystems, and subagents.
Creating and using custom skills with progressive disclosure, SKILL.md format, and the Agent Skills protocol in Deep Agents.
Using the Deep Agents CLI - terminal interface, persistent memory with AGENTS.md, project conventions, skills directories, and CLI commands.
Using FilesystemMiddleware with virtual filesystems, backends (State, Store, Filesystem, Composite), and context management for Deep Agents.
Using FilesystemMiddleware with virtual filesystems, backends (State, Store, Filesystem, Composite), and context management for Deep Agents.
| name | langchain-embeddings |
| description | Guide to using embedding model integrations in LangChain including OpenAI, Azure, and local embeddings |
| language | js |
Embedding models convert text into numerical vector representations that capture semantic meaning. These vectors enable semantic search, similarity comparison, and are essential for building RAG (Retrieval-Augmented Generation) systems with vector databases.
| Provider | Best For | Model Examples | Dimensions | Package | Key Features |
|---|---|---|---|---|---|
| OpenAI | General purpose, high quality | text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002 | 1536, 3072 | @langchain/openai | High quality, reliable, flexible dimensions |
| Azure OpenAI | Enterprise, compliance | text-embedding-ada-002 (Azure) | 1536 | @langchain/openai | Enterprise SLAs, data residency |
| Cohere | Multilingual, search optimization | embed-english-v3.0, embed-multilingual-v3.0 | 1024 | @langchain/cohere | Search/clustering modes, multilingual |
| HuggingFace | Open source, customizable | all-MiniLM-L6-v2, BGE models | Varies | @langchain/community | Free, local inference, many models |
| GCP integration | textembedding-gecko | 768 | @langchain/google-genai | GCP ecosystem, multimodal | |
| Ollama | Local, privacy | llama2, mistral, nomic-embed-text | Varies | @langchain/ollama | Fully local, no API costs, privacy |
Choose OpenAI if:
Choose Azure OpenAI if:
Choose Cohere if:
Choose HuggingFace if:
Choose Ollama if:
import { OpenAIEmbeddings } from "@langchain/openai";
// Basic initialization
const embeddings = new OpenAIEmbeddings({
modelName: "text-embedding-3-small",
openAIApiKey: process.env.OPENAI_API_KEY, // Optional if set in env
});
// Embed a single query
const queryEmbedding = await embeddings.embedQuery(
"What is the capital of France?"
);
console.log(`Vector dimensions: ${queryEmbedding.length}`);
console.log(`First few values: ${queryEmbedding.slice(0, 5)}`);
// Embed multiple documents
const documents = [
"Paris is the capital of France.",
"London is the capital of England.",
"Berlin is the capital of Germany.",
];
const docEmbeddings = await embeddings.embedDocuments(documents);
console.log(`Embedded ${docEmbeddings.length} documents`);
// Using newer models with custom dimensions
const smallEmbeddings = new OpenAIEmbeddings({
modelName: "text-embedding-3-small",
dimensions: 512, // Reduce from default 1536 for efficiency
});
import { AzureOpenAIEmbeddings } from "@langchain/openai";
const embeddings = new AzureOpenAIEmbeddings({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
azureOpenAIApiEmbeddingsDeploymentName: "text-embedding-ada-002",
azureOpenAIApiVersion: "2024-02-01",
});
const embedding = await embeddings.embedQuery("Hello world");
import { HuggingFaceTransformersEmbeddings } from "@langchain/community/embeddings/hf_transformers";
// Run embeddings locally with Transformers.js
const embeddings = new HuggingFaceTransformersEmbeddings({
modelName: "Xenova/all-MiniLM-L6-v2",
});
const embedding = await embeddings.embedQuery("This runs locally!");
import { OllamaEmbeddings } from "@langchain/ollama";
// Requires Ollama running locally: ollama pull nomic-embed-text
const embeddings = new OllamaEmbeddings({
model: "nomic-embed-text",
baseUrl: "http://localhost:11434", // Default Ollama URL
});
const embedding = await embeddings.embedQuery("Fully local embeddings");
import { CohereEmbeddings } from "@langchain/cohere";
const embeddings = new CohereEmbeddings({
apiKey: process.env.COHERE_API_KEY,
model: "embed-english-v3.0",
inputType: "search_query", // or "search_document", "classification", "clustering"
});
const queryEmbedding = await embeddings.embedQuery("Search query");
const docEmbeddings = await embeddings.embedDocuments(["doc1", "doc2"]);
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings();
// Embed query and documents
const query = "What is machine learning?";
const docs = [
"Machine learning is a branch of AI",
"Paris is the capital of France",
"Neural networks are used in deep learning",
];
const queryVec = await embeddings.embedQuery(query);
const docVecs = await embeddings.embedDocuments(docs);
// Compute cosine similarity
function cosineSimilarity(vecA: number[], vecB: number[]): number {
const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));
const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));
return dotProduct / (magnitudeA * magnitudeB);
}
// Find most similar document
const similarities = docVecs.map((docVec) =>
cosineSimilarity(queryVec, docVec)
);
console.log("Similarities:", similarities);
const mostSimilarIdx = similarities.indexOf(Math.max(...similarities));
console.log("Most similar doc:", docs[mostSimilarIdx]);
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings({
batchSize: 512, // OpenAI allows up to 2048 in one request
});
// Efficiently embed large document sets
const largeDocSet = Array.from({ length: 1000 }, (_, i) =>
`Document ${i}: Some content here`
);
const docEmbeddings = await embeddings.embedDocuments(largeDocSet);
console.log(`Embedded ${docEmbeddings.length} documents in batches`);
✅ Initialize embedding models
✅ Embed text content
embedQuery()embedDocuments()✅ Use embeddings with vector stores
✅ Choose appropriate models
✅ Optimize for use case
❌ Modify embedding dimensions arbitrarily
❌ Mix embeddings from different models
❌ Exceed API rate limits
❌ Generate embeddings without proper authentication
// ❌ BAD: Using different models
const embeddings1 = new OpenAIEmbeddings({
modelName: "text-embedding-3-small"
});
const embeddings2 = new OpenAIEmbeddings({
modelName: "text-embedding-ada-002"
});
const queryVec = await embeddings1.embedQuery("query");
const docVec = await embeddings2.embedQuery("document");
// Similarity comparison will be meaningless!
// ✅ GOOD: Use same model for everything
const embeddings = new OpenAIEmbeddings({
modelName: "text-embedding-3-small"
});
const queryVec = await embeddings.embedQuery("query");
const docVec = await embeddings.embedQuery("document");
// Now similarity makes sense
Fix: Always use the same embedding model for all texts you want to compare.
// ❌ Potential API error with too many docs
const embeddings = new OpenAIEmbeddings();
const hugeDocs = Array(5000).fill("text");
await embeddings.embedDocuments(hugeDocs); // May fail!
// ✅ Configure appropriate batch size
const embeddings = new OpenAIEmbeddings({
batchSize: 512, // OpenAI limit is 2048, use smaller for safety
});
await embeddings.embedDocuments(hugeDocs); // Handles batching automatically
Fix: Set appropriate batchSize parameter for the provider.
// ❌ Hardcoded API key
const embeddings = new OpenAIEmbeddings({
openAIApiKey: "sk-...", // Never commit this!
});
// ✅ Use environment variables
const embeddings = new OpenAIEmbeddings({
openAIApiKey: process.env.OPENAI_API_KEY,
});
// ✅ Even better: auto-detection
const embeddings = new OpenAIEmbeddings();
// Reads OPENAI_API_KEY from environment automatically
Fix: Use environment variables for API keys.
// ❌ Text too long
const embeddings = new OpenAIEmbeddings();
const veryLongText = "...".repeat(100000);
await embeddings.embedQuery(veryLongText); // Will fail!
// ✅ Chunk long texts first
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 8000, // OpenAI limit is ~8191 tokens
});
const chunks = await splitter.splitText(veryLongText);
const embeddings = await embeddings.embedDocuments(chunks);
Fix: Split long texts into chunks before embedding. Most models have 8k token limits.
// ❌ Ollama not running
import { OllamaEmbeddings } from "@langchain/ollama";
const embeddings = new OllamaEmbeddings({ model: "nomic-embed-text" });
await embeddings.embedQuery("test"); // Connection error!
// ✅ Ensure Ollama is running and model is pulled
// Terminal:
// ollama pull nomic-embed-text
// ollama serve
const embeddings = new OllamaEmbeddings({ model: "nomic-embed-text" });
await embeddings.embedQuery("test"); // Works!
Fix: For local models, ensure the service is running and model is downloaded.
// ❌ Missing required fields
const embeddings = new AzureOpenAIEmbeddings({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
});
// ✅ All required fields
const embeddings = new AzureOpenAIEmbeddings({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiInstanceName: "my-instance",
azureOpenAIApiEmbeddingsDeploymentName: "text-embedding-ada-002",
azureOpenAIApiVersion: "2024-02-01",
});
Fix: Azure requires instance name, deployment name, and API version.
// ❌ Vector store expecting 1536 dimensions, model produces 512
const embeddings = new OpenAIEmbeddings({
modelName: "text-embedding-3-small",
dimensions: 512,
});
// Vector store created with default 1536 dimensions
const vectorStore = await MemoryVectorStore.fromTexts(
["text1"],
embeddings, // Mismatch!
);
// ✅ Consistent dimensions
const embeddings = new OpenAIEmbeddings({
modelName: "text-embedding-3-small",
// Don't override dimensions, or ensure vector store matches
});
Fix: Ensure vector store and embeddings use compatible dimensions.
# OpenAI
npm install @langchain/openai
# Cohere
npm install @langchain/cohere
# Ollama
npm install @langchain/ollama
# Community (HuggingFace, etc.)
npm install @langchain/community