跳到主要内容
Neon 是一个完全托管的无服务器 PostgreSQL 数据库。它将存储和计算分离,以提供即时分支和自动扩展等功能。 借助 pgvector 扩展,Neon 提供了一个向量存储,可与 LangChain.js 配合使用,用于存储和查询嵌入。

设置

选择一个 Neon 项目

如果您没有 Neon 帐户,请在 Neon 注册一个。登录 Neon 控制台后,进入 项目 部分,选择一个现有项目或创建一个新项目。 您的 Neon 项目带有一个名为 neondb 的即用型 Postgres 数据库,您可以使用它来存储嵌入。导航到连接详情部分以查找您的数据库连接字符串。它应该类似于:
postgres://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require
请妥善保管您的连接字符串,以备后用。

应用程序代码

要使用 Neon Postgres,您需要安装 @neondatabase/serverless 包,该包提供了一个 JavaScript/TypeScript 驱动程序来连接数据库。
npm
npm install @neondatabase/serverless
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
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' }
  }
]
*/

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.