OllamaEmbeddings
这将帮助您开始使用 Ollama 嵌入模型 使用 LangChain。有关 OllamaEmbeddings
功能和配置选项的详细文档,请参阅 API 参考。
概述
集成详细信息
类 | 包 | 本地 | Py 支持 | 包下载 | 包最新 |
---|---|---|---|---|---|
OllamaEmbeddings | @langchain/ollama | ✅ | ✅ |
设置
要访问 Ollama 嵌入模型,您需要按照 这些说明 安装 Ollama,并安装 @langchain/ollama
集成包。
凭据
如果您想获得模型调用的自动跟踪,您还可以通过取消以下注释来设置您的 LangSmith API 密钥
# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"
安装
LangChain OllamaEmbeddings 集成位于 @langchain/ollama
包中
请参阅 本节了解有关安装集成包的通用说明。
- npm
- yarn
- pnpm
npm i @langchain/ollama @langchain/core
yarn add @langchain/ollama @langchain/core
pnpm add @langchain/ollama @langchain/core
实例化
现在我们可以实例化我们的模型对象并嵌入文本
import { OllamaEmbeddings } from "@langchain/ollama";
const embeddings = new OllamaEmbeddings({
model: "mxbai-embed-large", // Default value
baseUrl: "http://localhost:11434", // Default value
});
索引和检索
嵌入模型通常用于检索增强型生成 (RAG) 流,既作为索引数据的一部分,也作为稍后检索数据的一部分。有关更详细的说明,请参阅 使用外部知识教程 下的 RAG 教程。
下面,请参阅如何使用我们上面初始化的 embeddings
对象来索引和检索数据。在此示例中,我们将使用演示 MemoryVectorStore
来索引和检索示例文档。
// Create a vector store with a sample text
import { MemoryVectorStore } from "langchain/vectorstores/memory";
const text =
"LangChain is the framework for building context-aware reasoning applications";
const vectorstore = await MemoryVectorStore.fromDocuments(
[{ pageContent: text, metadata: {} }],
embeddings
);
// Use the vector store as a retriever that returns a single document
const retriever = vectorstore.asRetriever(1);
// Retrieve the most similar text
const retrievedDocuments = await retriever.invoke("What is LangChain?");
retrievedDocuments[0].pageContent;
LangChain is the framework for building context-aware reasoning applications
直接使用
在幕后,向量存储和检索器实现调用 embeddings.embedDocument(...)
和 embeddings.embedQuery(...)
来为分别用于 fromDocuments
和检索器的 invoke
操作的文本创建嵌入。
您可以直接调用这些方法来获取嵌入以供您自己的用例使用。
嵌入单个文本
您可以使用 embedQuery
来嵌入查询以进行搜索。这会生成特定于查询的向量表示。
const singleVector = await embeddings.embedQuery(text);
console.log(singleVector.slice(0, 100));
[
0.026051683, 0.029081265, -0.040726297, -0.015116953, -0.010691089,
0.030181013, -0.0065084146, -0.02079503, 0.013575795, 0.03452527,
0.009578291, 0.007026421, -0.030110886, 0.013489622, -0.04294787,
0.011141899, -0.043768786, -0.00362867, -0.0081198225, -0.03426076,
0.010075142, 0.027787417, -0.09052663, -0.06039698, -0.009462592,
0.06232288, 0.051121354, 0.011977532, 0.089046724, 0.059000008,
0.031860664, -0.034242127, 0.020339863, 0.011483523, -0.05429335,
-0.04963588, 0.03263794, -0.05581542, 0.013908403, -0.012356067,
-0.007802118, -0.010027855, 0.00281217, -0.101886116, -0.079341754,
0.011269771, 0.0035983133, -0.027667878, 0.032092705, -0.052843474,
-0.045283325, 0.0382421, 0.0193055, 0.011050924, 0.021132186,
-0.037696265, 0.0006107435, 0.0043520257, -0.028798066, 0.049155913,
0.03590549, -0.0040995986, 0.019772101, -0.076119535, 0.0031298609,
0.03368174, 0.039398745, -0.011813277, -0.019313531, -0.013108803,
-0.044905286, -0.022326004, -0.01656178, -0.06658457, 0.016789088,
0.049952697, 0.006615693, -0.01694402, -0.018105473, 0.0049101883,
-0.004966945, 0.049762275, -0.03556957, -0.015986584, -0.03190983,
-0.05336687, -0.0020468342, -0.0016106658, -0.035291273, -0.029783724,
-0.010153295, 0.052100364, 0.05528949, 0.01379487, -0.024542747,
0.028773975, 0.010087022, 0.030448131, -0.042391222, 0.016596776
]
嵌入多个文本
您可以使用 embedDocuments
来嵌入多个文本以进行索引。此方法内部使用的机制可能(但不一定)与嵌入查询不同。
const text2 =
"LangGraph is a library for building stateful, multi-actor applications with LLMs";
const vectors = await embeddings.embedDocuments([text, text2]);
console.log(vectors[0].slice(0, 100));
console.log(vectors[1].slice(0, 100));
[
0.026051683, 0.029081265, -0.040726297, -0.015116953, -0.010691089,
0.030181013, -0.0065084146, -0.02079503, 0.013575795, 0.03452527,
0.009578291, 0.007026421, -0.030110886, 0.013489622, -0.04294787,
0.011141899, -0.043768786, -0.00362867, -0.0081198225, -0.03426076,
0.010075142, 0.027787417, -0.09052663, -0.06039698, -0.009462592,
0.06232288, 0.051121354, 0.011977532, 0.089046724, 0.059000008,
0.031860664, -0.034242127, 0.020339863, 0.011483523, -0.05429335,
-0.04963588, 0.03263794, -0.05581542, 0.013908403, -0.012356067,
-0.007802118, -0.010027855, 0.00281217, -0.101886116, -0.079341754,
0.011269771, 0.0035983133, -0.027667878, 0.032092705, -0.052843474,
-0.045283325, 0.0382421, 0.0193055, 0.011050924, 0.021132186,
-0.037696265, 0.0006107435, 0.0043520257, -0.028798066, 0.049155913,
0.03590549, -0.0040995986, 0.019772101, -0.076119535, 0.0031298609,
0.03368174, 0.039398745, -0.011813277, -0.019313531, -0.013108803,
-0.044905286, -0.022326004, -0.01656178, -0.06658457, 0.016789088,
0.049952697, 0.006615693, -0.01694402, -0.018105473, 0.0049101883,
-0.004966945, 0.049762275, -0.03556957, -0.015986584, -0.03190983,
-0.05336687, -0.0020468342, -0.0016106658, -0.035291273, -0.029783724,
-0.010153295, 0.052100364, 0.05528949, 0.01379487, -0.024542747,
0.028773975, 0.010087022, 0.030448131, -0.042391222, 0.016596776
]
[
0.0558515, 0.028698817, -0.037476595, 0.0048659276, -0.019229038,
-0.04713716, -0.020947812, -0.017550547, 0.01205507, 0.027693441,
-0.011791304, 0.009862203, 0.019662278, -0.037511427, -0.022662448,
0.036224432, -0.051760387, -0.030165697, -0.008899774, -0.024518963,
0.010077767, 0.032209765, -0.0854303, -0.038666975, -0.036021013,
0.060899545, 0.045867186, 0.003365381, 0.09387081, 0.038216405,
0.011449426, -0.016495887, 0.020602569, -0.02368503, -0.014733645,
-0.065408126, -0.0065152845, -0.027103946, 0.00038956117, -0.08648814,
0.029316466, -0.054449145, 0.034129277, -0.055225655, -0.043182302,
0.0011148591, 0.044116337, -0.046552557, 0.032423045, -0.03269365,
-0.05062933, 0.021473562, -0.011019348, -0.019621233, -0.0003149565,
-0.0046085776, 0.0052610254, -0.0029293327, -0.035793293, 0.034469575,
0.037724957, 0.009572597, 0.014198464, -0.0878237, 0.0056973165,
0.023563445, 0.030928325, 0.025520306, 0.01836824, -0.016456697,
-0.061934732, 0.009764942, -0.035812028, -0.04429064, 0.031323086,
0.056027107, -0.0019782048, -0.015204176, -0.008684945, -0.0010460864,
0.054642987, 0.044149086, -0.032964867, -0.012044753, -0.019075096,
-0.027932597, 0.018542245, -0.02602878, -0.04645578, -0.020976603,
0.018999187, 0.050663687, 0.016725155, 0.0076955976, 0.011448177,
0.053931057, -0.03234989, 0.024429373, -0.023123834, 0.02197912
]
Ollama 模型参数 也受支持。
import { OllamaEmbeddings } from "@langchain/ollama";
const embeddingsCustomParams = new OllamaEmbeddings({
requestOptions: {
useMmap: true, // use_mmap 1
numThread: 6, // num_thread 6
numGpu: 1, // num_gpu 1
},
});
相关
API 参考
有关所有 OllamaEmbeddings
功能和配置的详细文档,请访问 API 参考。