Skip to main content

AnalyticDB

AnalyticDB for PostgreSQL 是一项大规模并行处理 (MPP) 数据仓库服务,旨在在线分析海量数据。

¥AnalyticDB for PostgreSQL is a massively parallel processing (MPP) data warehousing service that is designed to analyze large volumes of data online.

AnalyticDB for PostgreSQL 基于开源 Greenplum Database 项目开发,并通过 Alibaba Cloud 进行了深入扩展。AnalyticDB for PostgreSQL 与 ANSI SQL 2003 语法以及 PostgreSQL 和 Oracle 数据库生态系统兼容。AnalyticDB for PostgreSQL 还支持行存储和列存储。AnalyticDB for PostgreSQL 能够高性能离线处理 PB 级数据,并支持高并发在线查询。

¥AnalyticDB for PostgreSQL is developed based on the open source Greenplum Database project and is enhanced with in-depth extensions by Alibaba Cloud. AnalyticDB for PostgreSQL is compatible with the ANSI SQL 2003 syntax and the PostgreSQL and Oracle database ecosystems. AnalyticDB for PostgreSQL also supports row store and column store. AnalyticDB for PostgreSQL processes petabytes of data offline at a high performance level and supports highly concurrent online queries.

本注意本展示了如何使用与 AnalyticDB 向量数据库相关的功能。

¥This notebook shows how to use functionality related to the AnalyticDB vector database.

要运行此加载器,你应该已经启动并运行了一个 AnalyticDB 实例:

¥To run, you should have an AnalyticDB instance up and running:

Compatibility

仅在 Node.js 上可用。

¥Only available on Node.js.

设置

¥Setup

LangChain.js 接受 node-postgres 作为 AnalyticDB 向量存储的连接池。

¥LangChain.js accepts node-postgres as the connections pool for AnalyticDB vectorstore.

npm install -S pg

我们需要 pg-copy-streams 来快速添加批量向量。

¥And we need pg-copy-streams to add batch vectors quickly.

npm install -S pg-copy-streams
npm install @langchain/openai @langchain/community @langchain/core

用法

¥Usage

Security

用户生成的数据(例如用户名)不应用作集合名称的输入。这可能导致 SQL 注入!

¥User-generated data such as usernames should not be used as input for the collection name.\ This may lead to SQL Injection!

import { AnalyticDBVectorStore } from "@langchain/community/vectorstores/analyticdb";
import { OpenAIEmbeddings } from "@langchain/openai";

const connectionOptions = {
host: process.env.ANALYTICDB_HOST || "localhost",
port: Number(process.env.ANALYTICDB_PORT) || 5432,
database: process.env.ANALYTICDB_DATABASE || "your_database",
user: process.env.ANALYTICDB_USERNAME || "username",
password: process.env.ANALYTICDB_PASSWORD || "password",
};

const vectorStore = await AnalyticDBVectorStore.fromTexts(
["foo", "bar", "baz"],
[{ page: 1 }, { page: 2 }, { page: 3 }],
new OpenAIEmbeddings(),
{ connectionOptions }
);
const result = await vectorStore.similaritySearch("foo", 1);
console.log(JSON.stringify(result));
// [{"pageContent":"foo","metadata":{"page":1}}]

await vectorStore.addDocuments([{ pageContent: "foo", metadata: { page: 4 } }]);

const filterResult = await vectorStore.similaritySearch("foo", 1, {
page: 4,
});
console.log(JSON.stringify(filterResult));
// [{"pageContent":"foo","metadata":{"page":4}}]

const filterWithScoreResult = await vectorStore.similaritySearchWithScore(
"foo",
1,
{ page: 3 }
);
console.log(JSON.stringify(filterWithScoreResult));
// [[{"pageContent":"baz","metadata":{"page":3}},0.26075905561447144]]

const filterNoMatchResult = await vectorStore.similaritySearchWithScore(
"foo",
1,
{ page: 5 }
);
console.log(JSON.stringify(filterNoMatchResult));
// []

// need to manually close the Connection pool
await vectorStore.end();

API Reference:

¥Related