以编程方式使用文档

Pinecone 是一个向量数据库,为一些世界顶级公司的 AI 提供支持。

本指南提供了快速入门 Pinecone 向量存储的详细文档。如需了解所有 PineconeStore 功能和配置,请访问 API 参考.

概述

集成详情

PY 支持下载量版本
PineconeStore@langchain/pinecone!NPM - 下载量!NPM - 版本

设置

要使用 Pinecone 向量存储,请创建 Pinecone 账户,初始化一个索引,并安装 @langchain/pinecone, @langchain/core,以及 官方 Pinecone SDK (@pinecone-database/pinecone v5.x) 来初始化一个 PineconeStore.

本指南使用 OpenAI 嵌入 作为示例。您可以使用 其他支持的嵌入模型 instead.

npm install @langchain/pinecone @langchain/openai @langchain/core @pinecone-database/pinecone@5
yarn add @langchain/pinecone @langchain/openai @langchain/core @pinecone-database/pinecone@5
pnpm add @langchain/pinecone @langchain/openai @langchain/core @pinecone-database/pinecone@5

凭据

注册一个 Pinecone 账户并创建一个索引。请确保维度与您要使用的嵌入维度匹配(OpenAI 的默认维度是 1536) text-embedding-3-small)。完成此操作后,设置 PINECONE_INDEX, PINECONE_API_KEY,以及(可选) PINECONE_ENVIRONMENT 环境变量:

process.env.PINECONE_API_KEY = "your-pinecone-api-key";
process.env.PINECONE_INDEX = "your-pinecone-index";

// Optional
process.env.PINECONE_ENVIRONMENT = "your-pinecone-environment";

如果您在本指南中使用 OpenAI 嵌入,也请设置您的 OpenAI 密钥:

process.env.OPENAI_API_KEY = "YOUR_API_KEY";

如果您想获取模型调用的自动追踪,您还可以设置您的 LangSmith API 密钥(取消下方注释):

// process.env.LANGSMITH_TRACING="true"
// process.env.LANGSMITH_API_KEY="your-api-key"

实例化

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});

const pinecone = new PineconeClient();
// Will automatically read the PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars
const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!);

const vectorStore = await PineconeStore.fromExistingIndex(
  embeddings,
  {
    pineconeIndex,
    // Maximum number of batch requests to allow at once. Each batch is 1000 vectors.
    maxConcurrency: 5,
    // You can pass a namespace here too
    // namespace: "foo",
  }
);

管理向量存储

向向量存储添加内容

const document1: Document = {
  pageContent: "The powerhouse of the cell is the mitochondria",
  metadata: { source: "https://example.com" }
};

const document2: Document = {
  pageContent: "Buildings are made out of brick",
  metadata: { source: "https://example.com" }
};

const document3: Document = {
  pageContent: "Mitochondria are made out of lipids",
  metadata: { source: "https://example.com" }
};

const document4: Document = {
  pageContent: "The 2024 Olympics are in Paris",
  metadata: { source: "https://example.com" }
}

const documents = [document1, document2, document3, document4];

await vectorStore.addDocuments(documents, { ids: ["1", "2", "3", "4"] });
[ '1', '2', '3', '4' ]

Note: 添加文档后,需要稍等片刻才能进行查询。

从向量存储删除内容

await vectorStore.delete({ ids: ["4"] });

查询向量存储

创建向量存储并添加相关文档后,您很可能会希望在链或代理运行期间对其进行查询。

直接查询

执行简单的相似性搜索可以按如下方式进行:

// Optional filter
const filter = { source: "https://example.com" };

const similaritySearchResults = await vectorStore.similaritySearch("biology", 2, filter);

for (const doc of similaritySearchResults) {
  console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]

如果您想执行相似性搜索并获取相应的分数,可以运行:

const similaritySearchWithScoreResults = await vectorStore.similaritySearchWithScore("biology", 2, filter)

for (const [doc, score] of similaritySearchWithScoreResults) {
  console.log(`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(doc.metadata)}]`);
}
* [SIM=0.165] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=0.148] Mitochondria are made out of lipids [{"source":"https://example.com"}]

转换为检索器进行查询

您还可以将向量存储转换为 检索器 ,以便在链中更方便地使用。

const retriever = vectorStore.asRetriever({
  // Optional filter
  filter: filter,
  k: 2,
});

await retriever.invoke("biology");
[
  Document {
    pageContent: 'The powerhouse of the cell is the mitochondria',
    metadata: { source: 'https://example.com' },
    id: undefined
  },
  Document {
    pageContent: 'Mitochondria are made out of lipids',
    metadata: { source: 'https://example.com' },
    id: undefined
  }
]

用于检索增强生成

有关如何使用此向量存储进行检索增强生成 (RAG) 的指南,请参阅以下部分:

API 参考

有关所有 PineconeStore 功能和配置的详细文档,请前往 API 参考.