跳到主要内容
这将帮助您开始使用 LangChain 中的 CohereEmbeddings 嵌入模型。有关 CohereEmbeddings 功能和配置选项的详细文档,请参阅API 参考

概览

集成详情

类别本地Python 支持下载量版本
CohereEmbeddings@langchain/cohereNPM - DownloadsNPM - Version

设置

要访问 Cohere 嵌入模型,您需要创建一个 Cohere 帐户,获取 API 密钥,并安装 @langchain/cohere 集成包。

凭据

前往cohere.com注册Cohere并生成一个 API 密钥。完成后,设置 COHERE_API_KEY 环境变量。
export COHERE_API_KEY="your-api-key"
如果您想获取模型调用的自动化跟踪,您还可以通过取消注释下方来设置您的 LangSmith API 密钥
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain CohereEmbeddings 集成位于 @langchain/cohere 包中。
npm install @langchain/cohere @langchain/core

实例化

现在我们可以实例化我们的模型对象并生成聊天完成
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",
});

Azure 上的 Cohere、AWS Bedrock 上的 Cohere 和独立的 Cohere 实例的自定义客户端

我们可以实例化一个自定义 CohereClient 并将其传递给 ChatCohere 构造函数。 注意:如果提供了自定义客户端,则构造函数中的 COHERE_API_KEY 环境变量和 apiKey 参数都将被忽略。
import { CohereEmbeddings } from "@langchain/cohere";
import { CohereClient } from "cohere-ai";

const client = new CohereClient({
  token: "<your-api-key>",
  environment: "<your-cohere-deployment-url>", //optional
  // other params
});

const embeddingsWithCustomClient = new CohereEmbeddings({
  client,
  // other params...
});

索引和检索

嵌入模型通常用于检索增强生成 (RAG) 流程中,既作为索引数据的一部分,也用于后续检索数据。有关更详细的说明,请参阅学习选项卡下的 RAG 教程。 下面,我们将演示如何使用我们上面初始化的 embeddings 对象来索引和检索数据。在此示例中,我们将使用演示 MemoryVectorStore 索引和检索一个示例文档。
// Create a vector store with a sample text
import { MemoryVectorStore } from "@langchain/classic/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 参考
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.