跳到主要内容
LangChain.js 支持使用 @vercel/postgres 包将通用 Postgres 数据库用作向量存储,前提是它们支持 pgvector Postgres 扩展。 此集成在 Edge 函数等 Web 环境中特别有用。

设置

要使用 Vercel Postgres,您需要安装 @vercel/postgres
npm
npm install @vercel/postgres
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/community @langchain/core
此集成会自动使用 process.env.POSTGRES_URL 中设置的连接字符串进行连接。您也可以像这样手动传递连接字符串
const vectorstore = await VercelPostgres.initialize(new OpenAIEmbeddings(), {
  postgresConnectionOptions: {
    connectionString:
      "postgres://<username>:<password>@<hostname>:<port>/<dbname>",
  },
});

连接到 Vercel Postgres

一个简单的入门方法是创建一个无服务器 Vercel Postgres 实例。如果您部署到具有关联 Vercel Postgres 实例的 Vercel 项目,所需的 POSTGRES_URL 环境变量将已在托管环境中填充。

连接到其他数据库

如果您更喜欢自行托管 Postgres 实例,可以使用与 LangChain 的 PGVector 向量存储集成类似的工作流程,并将连接字符串设置为环境变量或如上所示。

用法

import { CohereEmbeddings } from "@langchain/cohere";
import { VercelPostgres } from "@langchain/community/vectorstores/vercel_postgres";

// Config is only required if you want to override default values.
const config = {
  // tableName: "testvercelvectorstorelangchain",
  // postgresConnectionOptions: {
  //   connectionString: "postgres://<username>:<password>@<hostname>:<port>/<dbname>",
  // },
  // columns: {
  //   idColumnName: "id",
  //   vectorColumnName: "vector",
  //   contentColumnName: "content",
  //   metadataColumnName: "metadata",
  // },
};

const vercelPostgresStore = await VercelPostgres.initialize(
  new CohereEmbeddings({ model: "embed-english-v3.0" }),
  config
);

const docHello = {
  pageContent: "hello",
  metadata: { topic: "nonsense" },
};
const docHi = { pageContent: "hi", metadata: { topic: "nonsense" } };
const docMitochondria = {
  pageContent: "Mitochondria is the powerhouse of the cell",
  metadata: { topic: "science" },
};

const ids = await vercelPostgresStore.addDocuments([
  docHello,
  docHi,
  docMitochondria,
]);

const results = await vercelPostgresStore.similaritySearch("hello", 2);
console.log(results);
/*
  [
    Document { pageContent: 'hello', metadata: { topic: 'nonsense' } },
    Document { pageContent: 'hi', metadata: { topic: 'nonsense' } }
  ]
*/

// Metadata filtering
const results2 = await vercelPostgresStore.similaritySearch(
  "Irrelevant query, metadata filtering",
  2,
  {
    topic: "science",
  }
);
console.log(results2);
/*
  [
    Document {
      pageContent: 'Mitochondria is the powerhouse of the cell',
      metadata: { topic: 'science' }
    }
  ]
*/

// Metadata filtering with IN-filters works as well
const results3 = await vercelPostgresStore.similaritySearch(
  "Irrelevant query, metadata filtering",
  3,
  {
    topic: { in: ["science", "nonsense"] },
  }
);
console.log(results3);
/*
  [
    Document {
      pageContent: 'hello',
      metadata: { topic: 'nonsense' }
    },
    Document {
      pageContent: 'hi',
      metadata: { topic: 'nonsense' }
    },
    Document {
      pageContent: 'Mitochondria is the powerhouse of the cell',
      metadata: { topic: 'science' }
    }
  ]
*/

// Upserting is supported as well
await vercelPostgresStore.addDocuments(
  [
    {
      pageContent: "ATP is the powerhouse of the cell",
      metadata: { topic: "science" },
    },
  ],
  { ids: [ids[2]] }
);

const results4 = await vercelPostgresStore.similaritySearch(
  "What is the powerhouse of the cell?",
  1
);
console.log(results4);
/*
  [
    Document {
      pageContent: 'ATP is the powerhouse of the cell',
      metadata: { topic: 'science' }
    }
  ]
*/

await vercelPostgresStore.delete({ ids: [ids[2]] });

const results5 = await vercelPostgresStore.similaritySearch(
  "No more metadata",
  2,
  {
    topic: "science",
  }
);
console.log(results5);
/*
  []
*/

// Remember to call .end() to close the connection!
await vercelPostgresStore.end();

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