Skip to main content

Neon Postgres

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

¥Neon is a fully managed serverless PostgreSQL database. It separates storage and compute to offer features such as instant branching and automatic scaling.

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

¥With the pgvector extension, Neon provides a vector store that can be used with LangChain.js to store and query embeddings.

设置

¥Setup

选择 Neon 项目

¥Select a Neon project

如果你没有 Neon 账户,请在 Neon 注册一个。登录 Neon 控制台后,进入 项目 部分,选择一个现有项目或创建一个新项目。

¥If you do not have a Neon account, sign up for one at Neon. After logging into the Neon Console, proceed to the Projects section and select an existing project or create a new one.

你的 Neon 项目附带一个名为 neondb 的现成 Postgres 数据库,你可以使用它来存储嵌入。导航到“连接详细信息”部分以查找数据库连接字符串。它看起来应该类似于:

¥Your Neon project comes with a ready-to-use Postgres database named neondb that you can use to store embeddings. Navigate to the Connection Details section to find your database connection string. It should look similar to this:

postgres://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require

请妥善保管你的连接字符串以备后用。

¥Keep your connection string handy for later use.

应用代码

¥Application code

要使用 Neon Postgres,你需要安装 @neondatabase/serverless 包,它提供了 JavaScript/TypeScript 驱动程序来连接数据库。

¥To work with Neon Postgres, you need to install the @neondatabase/serverless package which provides a JavaScript/TypeScript driver to connect to the database.

npm install @neondatabase/serverless
npm install @langchain/community @langchain/core

要初始化 NeonPostgres 矢量存储,你需要提供 Neon 数据库连接字符串。你可以直接使用我们上面获取的连接字符串,也可以将其存储为环境变量并在代码中使用。

¥To initialize a NeonPostgres vectorstore, you need to provide your Neon database connection string. You can use the connection string we fetched above directly, or store it as an environment variable and use it in your code.

const vectorStore = await NeonPostgres.initialize(embeddings, {
connectionString: NEON_POSTGRES_CONNECTION_STRING,
});

用法

¥Usage

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:

¥Related