Skip to main content

Neon Postgres

Neon 是一个完全托管的无服务器 PostgreSQL 数据库。它将存储和计算分离,从而提供诸如即时分支和自动扩展等功能。

通过 pgvector 扩展,Neon 提供了一个向量存储,可以与 LangChain.js 一起使用来存储和查询嵌入向量。

准备工作

选择一个 Neon 项目

如果您还没有 Neon 账户,请前往 Neon 注册一个。登录 Neon 控制台后,进入 项目 页面,选择现有项目或创建一个新项目。

您的 Neon 项目附带了一个开箱即用的 PostgreSQL 数据库,名为 neondb,可用于存储嵌入向量。进入连接详细信息部分,找到数据库连接字符串。它应该类似于以下内容:

postgres://alex:[email protected]/dbname?sslmode=require

请将连接字符串保存下来以供后续使用。

应用代码

要使用 Neon Postgres,您需要安装 @neondatabase/serverless 包,该包提供了一个用于连接数据库的 JavaScript/TypeScript 驱动程序。

npm install @neondatabase/serverless

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

npm install @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 Reference:

相关内容


Was this page helpful?


You can also leave detailed feedback on GitHub.