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

概览

集成详情

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

设置

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

凭据

注册一个 Pinecone 账户,获取您的 API 密钥,并将其设置为名为 PINECONE_API_KEY 的环境变量
process.env.PINECONE_API_KEY = "your-pinecone-api-key";
如果您想获取模型调用的自动化跟踪,您还可以通过取消注释下方来设置您的 LangSmith API 密钥
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain PineconeEmbeddings 集成位于 @langchain/pinecone 包中
npm install @langchain/pinecone @langchain/core @pinecone-database/pinecone@5

实例化

现在我们可以实例化我们的模型对象并生成聊天完成
import { PineconeEmbeddings } from "@langchain/pinecone";

const embeddings = new PineconeEmbeddings({
  model: "multilingual-e5-large",
});

索引和检索

嵌入模型通常用于检索增强生成 (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.0191650390625,  0.004924774169921875,     -0.015838623046875,
          -0.04248046875,     0.040191650390625,      -0.02679443359375,
     -0.0240936279296875,     0.058624267578125,      0.027069091796875,
        -0.0435791015625,      0.01934814453125,      0.040191650390625,
     -0.0194244384765625,   0.01386260986328125,    -0.0216827392578125,
    -0.01073455810546875,   -0.0166168212890625,    0.01073455810546875,
        -0.0228271484375,       0.0062255859375,      0.035064697265625,
     -0.0114593505859375,   -0.0257110595703125,    -0.0285797119140625,
        0.01190185546875,    -0.022186279296875,   -0.01500701904296875,
       -0.03240966796875, 0.0019063949584960938,     -0.039337158203125,
     -0.0047454833984375,              -0.03125,       -0.0123291015625,
    -0.00899505615234375,        -0.02880859375,      0.014678955078125,
         0.0452880859375,      0.05035400390625,     -0.053436279296875,
      0.0265960693359375,   -0.0206756591796875,       0.06658935546875,
      -0.032989501953125,  -0.00724029541015625,  0.0024967193603515625,
      0.0282135009765625,     0.047088623046875,       -0.0255126953125,
      -0.008453369140625,   -0.0039215087890625,     0.0282135009765625,
      0.0270843505859375,      -0.0133056640625,    -0.0296173095703125,
        -0.0455322265625,    0.0225982666015625,      -0.04803466796875,
    -0.00891876220703125,     -0.04669189453125,      0.022064208984375,
     -0.0266571044921875,  -0.01480865478515625,     0.0295257568359375,
    -0.01561737060546875,      -0.0411376953125,    0.01345062255859375,
      0.0219879150390625,    -0.012786865234375,     -0.051727294921875,
  -0.0002830028533935547,   0.00690460205078125,   -0.01303863525390625,
        -0.0457763671875,    -0.026763916015625,    -0.0181121826171875,
     0.00946807861328125,       0.0250244140625,      -0.01458740234375,
         0.0394287109375,   -0.0162200927734375,       0.05169677734375,
     0.01126861572265625,   0.01265716552734375,     -0.009307861328125,
          0.052490234375,    0.0135345458984375,    0.01332855224609375,
       0.040130615234375,       0.0638427734375,     0.0181121826171875,
    0.004207611083984375,          0.0771484375,      0.024078369140625,
       0.012420654296875,       -0.030517578125, -0.0019245147705078125,
      0.0243682861328125,    0.0254974365234375,  0.0036334991455078125,
   -0.004550933837890625
]

嵌入多个文本

您可以使用 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.0190887451171875,  0.00482940673828125,   -0.0158233642578125,
       -0.04254150390625,    0.040130615234375,   -0.0268096923828125,
          -0.02392578125,    0.058624267578125,    0.0269927978515625,
          -0.04345703125,   0.0193328857421875,     0.040374755859375,
     -0.0196075439453125,  0.01384735107421875,    -0.021881103515625,
    -0.01068878173828125,   -0.016510009765625,   0.01079559326171875,
     -0.0227813720703125,        0.00634765625,     0.035064697265625,
     -0.0113983154296875,  -0.0257720947265625,   -0.0285491943359375,
       0.011749267578125,  -0.0222625732421875,   -0.0148468017578125,
        -0.0325927734375,  0.00203704833984375,      -0.0394287109375,
   -0.004878997802734375,  -0.0311126708984375,  -0.01248931884765625,
    -0.00897979736328125,  -0.0286407470703125,    0.0146331787109375,
        0.04522705078125,    0.050201416015625,    -0.053314208984375,
      0.0265960693359375,  -0.0207366943359375,      0.06658935546875,
       -0.03302001953125,  -0.0073699951171875,    0.0024261474609375,
       0.028228759765625,     0.04705810546875,   -0.0255279541015625,
     -0.0084075927734375,   -0.003814697265625,    0.0281524658203125,
      0.0272064208984375, -0.01322174072265625,   -0.0295257568359375,
      -0.045623779296875,    0.022735595703125,         -0.0478515625,
    -0.00885772705078125,   -0.046844482421875,     0.022003173828125,
      -0.026458740234375,  -0.0148468017578125,    0.0295562744140625,
    -0.01555633544921875,   -0.041229248046875,      0.01336669921875,
       0.022125244140625, -0.01276397705078125,    -0.051666259765625,
  -0.0002474784851074219, 0.006740570068359375,  -0.01306915283203125,
       -0.04583740234375,      -0.026611328125,   -0.0182342529296875,
        0.00946044921875,   0.0250701904296875,   -0.0146942138671875,
       0.039459228515625,   -0.016265869140625,     0.051788330078125,
     0.01110076904296875,         0.0126953125,  -0.00925445556640625,
       0.052581787109375,  0.01363372802734375,   0.01332855224609375,
        0.04010009765625,      0.0638427734375,     0.018157958984375,
      0.0040740966796875,     0.07720947265625,    0.0240325927734375,
      0.0123443603515625,  -0.0302886962890625, -0.001865386962890625,
       0.024383544921875,    0.025604248046875,   0.00353240966796875,
   -0.004474639892578125
]
[
     0.0053253173828125,    0.01305389404296875,    -0.0253448486328125,
      -0.04241943359375,      0.034942626953125,     -0.017425537109375,
         -0.02783203125,         0.064208984375,     0.0244903564453125,
       -0.0467529296875,      0.021209716796875,       0.02191162109375,
      -0.03131103515625,     -0.019073486328125,   -0.01413726806640625,
     -0.008636474609375,     -0.011627197265625,     0.0229339599609375,
      -0.00762939453125,    0.00594329833984375,     0.0201263427734375,
       -0.0247802734375,      -0.05047607421875,      -0.03765869140625,
     0.0034332275390625,     -0.014617919921875,     -0.043548583984375,
      -0.03594970703125,  0.0002884864807128906,      -0.03656005859375,
    -0.0102691650390625,     0.0121307373046875,    -0.0284271240234375,
       -0.0113525390625,   -0.01195526123046875,    0.01143646240234375,
      0.051727294921875,        0.0230712890625,     -0.046417236328125,
     0.0198211669921875,      -0.02337646484375,      0.040985107421875,
      -0.03314208984375,     -0.025909423828125,   -0.00809478759765625,
     0.0291595458984375,             0.04296875,     -0.016143798828125,
      0.005706787109375,       0.00860595703125, -0.0035343170166015625,
     0.0118560791015625,    -0.0135650634765625,    -0.0294036865234375,
     -0.029876708984375,             0.03515625,       -0.0545654296875,
   0.006862640380859375,     -0.041839599609375,      0.021148681640625,
    -0.0279998779296875,   -0.00949859619140625,       0.03314208984375,
  -0.002727508544921875,          -0.0400390625,    0.01311492919921875,
    0.01177215576171875, -0.0010013580322265625,        -0.052001953125,
    0.00112152099609375,   -0.00815582275390625,        0.0321044921875,
       -0.0496826171875,    -0.0151519775390625,    -0.0262908935546875,
  -0.005207061767578125,     0.0207977294921875,        -0.022705078125,
      0.009735107421875,   0.000682830810546875,       0.05792236328125,
       -0.0145263671875,       0.03643798828125,  0.0018339157104492188,
      0.047210693359375,  0.0017986297607421875,     0.0300140380859375,
      0.027923583984375,      0.044708251953125,      0.027618408203125,
    0.00104522705078125,       0.05987548828125,       0.06304931640625,
     -0.039703369140625,       -0.0386962890625,    0.00797271728515625,
     0.0254974365234375,     0.0245819091796875,      0.010467529296875,
    -0.0080413818359375
]

API 参考

有关所有 PineconeEmbeddings 功能和配置的详细文档,请前往 API 参考
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.