Skip to main content

Pinecone

本指南将帮助您开始使用由Pinecone 向量存储支持的检索器。如需了解所有功能和配置的详细文档,请前往API 参考

概述

自查询检索器通过根据输入查询动态生成元数据过滤器来检索文档。这允许检索器在获取结果时不仅考虑纯语义相似性,还考虑底层文档元数据。

它使用一个名为Translator的模块,该模块根据有关元数据字段和特定向量存储支持的查询语言生成过滤器。

集成详情

底层向量存储自托管云服务Python 支持
PineconeStore@langchain/pinecone

安装与配置

按照此处所述设置一个 Pinecone 实例。请设置以下环境变量:

process.env.PINECONE_API_KEY = "YOUR_API_KEY";
process.env.PINECONE_ENVIRONMENT = "YOUR_ENVIRONMENT";
process.env.PINECONE_INDEX = "YOUR_INDEX";

如果您希望从单个查询中获得自动跟踪,也可以取消下面的注释来设置您的LangSmith API 密钥:

// process.env.LANGSMITH_API_KEY = "<YOUR API KEY HERE>";
// process.env.LANGSMITH_TRACING = "true";

安装

向量存储位于@langchain/pinecone包中。您还需要安装langchain包以导入主要的SelfQueryRetriever类。

此外,您还需要安装官方的 Pinecone SDK(@pinecone-database/pinecone@5)。

在本示例中,我们还将使用 OpenAI 嵌入,因此您需要安装@langchain/openai包并获取 API 密钥

:::提示 请参阅安装集成包的一般说明部分。 :::

yarn add @langchain/pinecone langchain @langchain/openai @langchain/core @pinecone-database/pinecone

实例化

首先,用一些包含元数据的文档初始化你的 Pinecone 向量存储:

import { Pinecone } from "@pinecone-database/pinecone";

import { OpenAIEmbeddings } from "@langchain/openai";
import { PineconeStore } from "@langchain/pinecone";
import { Document } from "@langchain/core/documents";
import type { AttributeInfo } from "langchain/chains/query_constructor";

/**
* First, we create a bunch of documents. You can load your own documents here instead.
* Each document has a pageContent and a metadata field. Make sure your metadata matches the AttributeInfo below.
*/
const docs = [
new Document({
pageContent:
"A bunch of scientists bring back dinosaurs and mayhem breaks loose",
metadata: { year: 1993, rating: 7.7, genre: "science fiction" },
}),
new Document({
pageContent:
"Leo DiCaprio gets lost in a dream within a dream within a dream within a ...",
metadata: { year: 2010, director: "Christopher Nolan", rating: 8.2 },
}),
new Document({
pageContent:
"A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea",
metadata: { year: 2006, director: "Satoshi Kon", rating: 8.6 },
}),
new Document({
pageContent:
"A bunch of normal-sized women are supremely wholesome and some men pine after them",
metadata: { year: 2019, director: "Greta Gerwig", rating: 8.3 },
}),
new Document({
pageContent: "Toys come alive and have a blast doing so",
metadata: { year: 1995, genre: "animated" },
}),
new Document({
pageContent: "Three men walk into the Zone, three men walk out of the Zone",
metadata: {
year: 1979,
director: "Andrei Tarkovsky",
genre: "science fiction",
rating: 9.9,
},
}),
];

/**
* Next, we define the attributes we want to be able to query on.
* in this case, we want to be able to query on the genre, year, director, rating, and length of the movie.
* We also provide a description of each attribute and the type of the attribute.
* This is used to generate the query prompts.
*/
const attributeInfo: AttributeInfo[] = [
{
name: "genre",
description: "The genre of the movie",
type: "string or array of strings",
},
{
name: "year",
description: "The year the movie was released",
type: "number",
},
{
name: "director",
description: "The director of the movie",
type: "string",
},
{
name: "rating",
description: "The rating of the movie (1-10)",
type: "number",
},
{
name: "length",
description: "The length of the movie in minutes",
type: "number",
},
];

/**
* Next, we instantiate a vector store. This is where we store the embeddings of the documents.
* We also need to provide an embeddings object. This is used to embed the documents.
*/

const pinecone = new Pinecone();

const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!);

const embeddings = new OpenAIEmbeddings();
const vectorStore = await PineconeStore.fromDocuments(docs, embeddings, {
pineconeIndex: pineconeIndex,
});

现在我们可以实例化我们的检索器:

Pick your chat model:

Install dependencies

yarn add @langchain/groq 

Add environment variables

GROQ_API_KEY=your-api-key

Instantiate the model

import { ChatGroq } from "@langchain/groq";

const llm = new ChatGroq({
model: "llama-3.3-70b-versatile",
temperature: 0
});
import { SelfQueryRetriever } from "langchain/retrievers/self_query";
import { PineconeTranslator } from "@langchain/pinecone";

const selfQueryRetriever = SelfQueryRetriever.fromLLM({
llm: llm,
vectorStore: vectorStore,
/** A short summary of what the document contents represent. */
documentContents: "Brief summary of a movie",
attributeInfo: attributeInfo,
/**
* We need to create a basic translator that translates the queries into a
* filter format that the vector store can understand. We provide a basic translator
* translator here, but you can create your own translator by extending BaseTranslator
* abstract class. Note that the vector store needs to support filtering on the metadata
* attributes you want to query on.
*/
structuredQueryTranslator: new PineconeTranslator(),
});

使用方法

现在,提出一个需要了解文档元数据才能回答的问题。你可以看到检索器将生成正确的结果:

await selfQueryRetriever.invoke("Which movies are rated higher than 8.5?");
[
Document {
pageContent: 'A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea',
metadata: { director: 'Satoshi Kon', rating: 8.6, year: 2006 },
id: undefined
},
Document {
pageContent: 'Three men walk into the Zone, three men walk out of the Zone',
metadata: {
director: 'Andrei Tarkovsky',
genre: 'science fiction',
rating: 9.9,
year: 1979
},
id: undefined
}
]

在链式应用中的使用

与其他检索器一样,Pinecone 自查询检索器可以通过链式应用集成到 LLM 应用程序中。

请注意,由于其返回的答案可能在很大程度上依赖于文档的元数据,我们以不同的格式返回检索到的文档,以便包含这些信息。

import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";

import type { Document } from "@langchain/core/documents";

const prompt = ChatPromptTemplate.fromTemplate(`
Answer the question based only on the context provided.

Context: {context}

Question: {question}`);

const formatDocs = (docs: Document[]) => {
return docs.map((doc) => JSON.stringify(doc)).join("\n\n");
};

// See https://js.langchain.com/docs/tutorials/rag
const ragChain = RunnableSequence.from([
{
context: selfQueryRetriever.pipe(formatDocs),
question: new RunnablePassthrough(),
},
prompt,
llm,
new StringOutputParser(),
]);
await ragChain.invoke("Which movies are rated higher than 8.5?");
The movies rated higher than 8.5 are the ones directed by Satoshi Kon (rating: 8.6) and Andrei Tarkovsky (rating: 9.9).

默认搜索参数

你还可以在上述方法中传入一个 searchParams 字段,该字段提供默认的过滤器,这些过滤器将与任何生成的查询一并应用。过滤器的语法与底层 Pinecone 向量存储相同:

const selfQueryRetrieverWithDefaultParams = SelfQueryRetriever.fromLLM({
llm: llm,
vectorStore: vectorStore,
documentContents: "Brief summary of a movie",
attributeInfo: attributeInfo,
structuredQueryTranslator: new PineconeTranslator(),
searchParams: {
filter: {
rating: {
$gt: 8.5,
},
},
mergeFiltersOperator: "and",
},
});

API 参考文档

如需详细了解所有 Pinecone 自查询检索器的功能和配置,请访问 API 参考文档


Was this page helpful?


You can also leave detailed feedback on GitHub.