在我的 NextJS 14 项目中,我尝试使用 Pinecone 对给定的 PDF 文件进行矢量化和索引,并将其推送到 Pinecone 数据库。我就是这样做的:
/* eslint-disable import/prefer-default-export, @typescript-eslint/no-unused-vars */
import { PDFLoader } from "langchain/document_loaders/fs/pdf"
import { NextRequest, NextResponse } from "next/server"
import { OpenAIEmbeddings } from "@langchain/openai"
import { PineconeStore } from "langchain/vectorstores/pinecone"
import getPineconeClient from "@/lib/pinecone"
async function streamToArrayBuffer(stream: ReadableStream) {
return new Uint8Array(await new Response(stream).arrayBuffer())
}
export async function POST(request: NextRequest) {
const data = await request.body
if (!data) return NextResponse.json({ success: false })
const arrayBuffer = await streamToArrayBuffer(data)
const blob = new Blob([arrayBuffer])
const loader = new PDFLoader(blob)
const pageLevelDocs = await loader.load()
const pagesAmt = pageLevelDocs.length
// Vectorize and index document
const pinecone = await getPineconeClient()
const pineconeIndex = pinecone.Index("poco")
const embeddings = new OpenAIEmbeddings({
openAIApiKey: process.env.OPENAI_API_KEY,
})
await PineconeStore.fromDocuments(pageLevelDocs, embeddings, {
pineconeIndex,
namespace: "pdf",
})
return NextResponse.json({ success: true, pages: pagesAmt })
}
我的松果配置(
pinecone.ts
)看起来像这样:
import { PineconeClient } from "@pinecone-database/pinecone"
const getPineconeClient = async () => {
const client = new PineconeClient()
await client.init({
apiKey: process.env.PINECONE_API_KEY!,
environment: process.env.PINECONE_ENVIRONMENT as string,
})
return client
}
export default getPineconeClient
这应该可以正常工作,但服务器仍然抛出一个类型错误说:
this.pineconeIndex.namespace 不是一个函数
我还尝试从“@langchain/community/vectorstores/pinecone”而不是“langchain/vectorstores/pinecone”导入PineconeStore,但错误仍然存在。
我已经找到了这个错误的解决方案,这已经是 langchain 仓库中已关闭的问题。 只需将
pinecone.ts
文件中的 pinecone 导入语句更改为此
import { Pinecone } from "@pinecone-database/pinecone";
附加问题链接我希望这有帮助