Neon Postgres
Neon 是一个完全托管的无服务器 PostgreSQL 数据库。它将存储和计算分开,以提供诸如即时分支和自动缩放等功能。
使用 pgvector
扩展,Neon 提供了一个向量存储,可以与 LangChain.js 一起使用来存储和查询嵌入。
设置
选择一个 Neon 项目
如果您没有 Neon 帐户,请在 Neon 注册一个。登录 Neon 控制台后,继续到 项目 部分并选择一个现有项目或创建一个新项目。
您的 Neon 项目附带一个名为 neondb
的即用型 Postgres 数据库,您可以使用它来存储嵌入。导航到连接详细信息部分以查找您的数据库连接字符串。它应该类似于以下内容
postgres://alex:[email protected]/dbname?sslmode=require
请将您的连接字符串保存好,以便将来使用。
应用程序代码
要使用 Neon Postgres,您需要安装 @neondatabase/serverless
包,它提供了一个 JavaScript/TypeScript 驱动程序来连接到数据库。
- npm
- Yarn
- pnpm
npm install @neondatabase/serverless
yarn add @neondatabase/serverless
pnpm add @neondatabase/serverless
提示
有关安装集成包的一般说明,请参阅 此部分。
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
要初始化一个 NeonPostgres
向量存储,您需要提供您的 Neon 数据库连接字符串。您可以直接使用我们上面获取的连接字符串,也可以将其存储为环境变量并在代码中使用它。
const vectorStore = await NeonPostgres.initialize(embeddings, {
connectionString: NEON_POSTGRES_CONNECTION_STRING,
});
用法
import { OpenAIEmbeddings } from "@langchain/openai";
import { NeonPostgres } from "@langchain/community/vectorstores/neon";
// Initialize an embeddings instance
const embeddings = new OpenAIEmbeddings({
apiKey: process.env.OPENAI_API_KEY,
dimensions: 256,
model: "text-embedding-3-small",
});
// Initialize a NeonPostgres instance to store embedding vectors
const vectorStore = await NeonPostgres.initialize(embeddings, {
connectionString: process.env.DATABASE_URL as string,
});
// You can add documents to the store, strings in the `pageContent` field will be embedded
// and stored in the database
const documents = [
{ pageContent: "Hello world", metadata: { topic: "greeting" } },
{ pageContent: "Bye bye", metadata: { topic: "greeting" } },
{
pageContent: "Mitochondria is the powerhouse of the cell",
metadata: { topic: "science" },
},
];
const idsInserted = await vectorStore.addDocuments(documents);
// You can now query the store for similar documents to the input query
const resultOne = await vectorStore.similaritySearch("hola", 1);
console.log(resultOne);
/*
[
Document {
pageContent: 'Hello world',
metadata: { topic: 'greeting' }
}
]
*/
// You can also filter by metadata
const resultTwo = await vectorStore.similaritySearch("Irrelevant query", 2, {
topic: "science",
});
console.log(resultTwo);
/*
[
Document {
pageContent: 'Mitochondria is the powerhouse of the cell',
metadata: { topic: 'science' }
}
]
*/
// Metadata filtering with IN-filters works as well
const resultsThree = await vectorStore.similaritySearch("Irrelevant query", 2, {
topic: { in: ["greeting"] },
});
console.log(resultsThree);
/*
[
Document { pageContent: 'Bye bye', metadata: { topic: 'greeting' } },
Document {
pageContent: 'Hello world',
metadata: { topic: 'greeting' }
}
]
*/
// Upserting is supported as well
await vectorStore.addDocuments(
[
{
pageContent: "ATP is the powerhouse of the cell",
metadata: { topic: "science" },
},
],
{ ids: [idsInserted[2]] }
);
const resultsFour = await vectorStore.similaritySearch(
"powerhouse of the cell",
1
);
console.log(resultsFour);
/*
[
Document {
pageContent: 'ATP is the powerhouse of the cell',
metadata: { topic: 'science' }
}
]
*/
API 参考
- OpenAIEmbeddings 来自
@langchain/openai
- NeonPostgres 来自
@langchain/community/vectorstores/neon