CohereEmbeddings
这将帮助您开始使用 LangChain 的 CohereEmbeddings 嵌入模型。有关 CohereEmbeddings
功能和配置选项的详细文档,请参阅 API 参考。
概述
集成详细信息
类 | 包 | 本地 | Py 支持 | 包下载 | 包最新 |
---|---|---|---|---|---|
CohereEmbeddings | @langchain/cohere | ❌ | ✅ |
设置
要访问 Cohere 嵌入模型,您需要创建一个 Cohere 帐户,获取 API 密钥并安装 @langchain/cohere
集成包。
凭据
前往 cohere.com 注册 Cohere
并生成 API 密钥。完成此操作后,设置 COHERE_API_KEY
环境变量
export COHERE_API_KEY="your-api-key"
如果您希望获得对模型调用的自动跟踪,还可以通过取消注释以下内容来设置您的 LangSmith API 密钥
# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"
安装
LangChain CohereEmbeddings 集成位于 @langchain/cohere
包中
- npm
- yarn
- pnpm
npm i @langchain/cohere
yarn add @langchain/cohere
pnpm add @langchain/cohere
实例化
现在我们可以实例化模型对象并生成聊天完成
import { CohereEmbeddings } from "@langchain/cohere";
const embeddings = new CohereEmbeddings({
apiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.COHERE_API_KEY
batchSize: 48, // Default value if omitted is 48. Max value is 96
model: "embed-english-v3.0",
});
索引和检索
嵌入模型通常用于检索增强生成 (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.022979736, -0.030212402, -0.08886719, -0.08569336, 0.007030487,
-0.0010671616, -0.033813477, 0.08843994, 0.0119018555, 0.049926758,
-0.03616333, 0.007408142, 0.00034809113, -0.005744934, -0.016021729,
-0.015296936, -0.0011606216, -0.02458191, -0.044006348, -0.0335083,
0.024658203, -0.051086426, 0.0020427704, 0.06298828, 0.020507812,
0.037475586, 0.05117798, 0.0059814453, 0.025360107, 0.0060577393,
0.02255249, -0.070129395, 0.024017334, 0.022766113, -0.042755127,
-0.024673462, -0.0236969, -0.0073623657, 0.002161026, 0.011329651,
0.038330078, -0.03050232, 0.0022201538, -0.007911682, -0.0023536682,
0.029937744, -0.027297974, -0.064086914, 0.027267456, 0.016738892,
0.0028972626, 0.015510559, -0.01725769, 0.011497498, -0.012954712,
0.002380371, -0.03366089, -0.02746582, 0.014022827, 0.04196167,
0.007698059, -0.027069092, 0.025405884, -0.029815674, 0.013298035,
0.01737976, 0.07269287, 0.017822266, 0.0012550354, -0.009597778,
-0.02961731, 0.0049057007, 0.01965332, -0.009994507, -0.019561768,
-0.004764557, 0.019317627, -0.0045433044, 0.031143188, -0.018188477,
-0.0026893616, 0.0050964355, -0.044189453, 0.02029419, -0.019088745,
0.02166748, -0.011657715, -0.025405884, -0.028030396, -0.0051460266,
-0.010818481, -0.000364542, -0.028686523, 0.015029907, 0.0013790131,
-0.0069770813, -0.030639648, -0.051208496, 0.005279541, -0.0109939575
]
嵌入多个文本
您可以使用 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.028869629, -0.030410767, -0.099121094, -0.07116699, -0.012748718,
-0.0059432983, -0.04360962, 0.07965088, -0.027114868, 0.057403564,
-0.013549805, 0.014480591, 0.021697998, -0.026870728, 0.0071983337,
-0.0099105835, -0.0034332275, -0.026031494, -0.05206299, -0.045288086,
0.027450562, -0.060333252, -0.019210815, 0.039794922, 0.0055351257,
0.046325684, 0.017837524, -0.012619019, 0.023147583, -0.008201599,
0.022155762, -0.035888672, 0.016921997, 0.027679443, -0.023605347,
-0.0022029877, -0.025253296, 0.013076782, 0.0049705505, -0.0024280548,
0.021957397, -0.008644104, -0.00004029274, -0.003501892, -0.012641907,
0.01600647, -0.014312744, -0.037841797, 0.011764526, -0.019622803,
-0.01928711, -0.017044067, -0.017547607, 0.028533936, -0.019073486,
-0.0061073303, -0.024520874, 0.01638794, 0.017852783, -0.0013303757,
-0.023040771, -0.01713562, 0.027786255, -0.02583313, 0.03060913,
0.00013923645, 0.01977539, 0.025283813, -0.00068569183, 0.032806396,
-0.021392822, -0.016174316, 0.016464233, 0.006023407, -0.0025043488,
-0.033813477, 0.023269653, 0.012329102, 0.030334473, 0.014419556,
-0.026245117, -0.018356323, -0.016433716, 0.022628784, -0.024108887,
0.02897644, -0.017105103, -0.009208679, -0.015541077, -0.020004272,
-0.005153656, 0.03741455, -0.050750732, 0.012176514, -0.017501831,
-0.014503479, 0.0052223206, -0.03250122, 0.008666992, -0.015823364
]
[
-0.047332764, -0.049957275, -0.07458496, -0.034332275, -0.057922363,
-0.0112838745, -0.06994629, 0.06347656, -0.03326416, 0.019897461,
0.0103302, 0.04660034, -0.059753418, -0.027511597, 0.012245178,
-0.03164673, -0.010215759, -0.00687027, -0.03314209, -0.019866943,
0.008399963, -0.042144775, -0.03781128, 0.025970459, 0.007335663,
0.04107666, -0.015991211, 0.0158844, -0.008483887, -0.008399963,
0.01777649, -0.01109314, 0.01864624, 0.014328003, -0.005264282,
0.077697754, 0.017684937, 0.0020427704, 0.032470703, -0.0029354095,
0.003063202, 0.0008301735, 0.016281128, -0.005897522, -0.023254395,
0.004043579, -0.021987915, -0.015419006, 0.0009803772, 0.044677734,
-0.0045814514, 0.0039901733, -0.019058228, 0.063964844, -0.012496948,
-0.027755737, 0.01574707, -0.03781128, 0.0038909912, -0.00002193451,
0.00013685226, 0.027832031, 0.015182495, -0.008590698, 0.03933716,
-0.0020141602, -0.050567627, 0.02017212, 0.020523071, 0.07287598,
0.0031375885, -0.05227661, -0.01838684, -0.0019626617, -0.0039482117,
0.02494812, 0.0009508133, 0.008583069, 0.02923584, 0.028198242,
-0.030334473, -0.014076233, -0.017990112, 0.0026245117, -0.017150879,
0.004497528, -0.00365448, -0.0012168884, 0.011741638, 0.012886047,
0.00084400177, 0.060638428, -0.024002075, 0.022415161, -0.015823364,
-0.0026760101, 0.028625488, 0.041015625, 0.006893158, -0.01902771
]
API 参考
有关所有 CohereEmbeddings 功能和配置的详细文档,请前往 API 参考:https://api.js.langchain.com/classes/langchain_cohere.CohereEmbeddings.html