PGvector vector database support (#3788)

* PGVector support for vector db storage

* forgot files

* comments

* dev build

* Add ENV connection and table schema validations for vector table
add .reset call to drop embedding table when changing the AnythingLLM embedder
update instrutions
Add preCheck error reporting in UpdateENV
add timeout to pg connection

* update setup

* update README

* update doc
This commit is contained in:
Timothy Carambat 2025-05-09 12:27:11 -07:00 committed by GitHub
parent 2812fef01e
commit e1b7f5820c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 1164 additions and 24 deletions

View File

@ -6,7 +6,7 @@ concurrency:
on:
push:
branches: ['2237-auto-stt-submit-preference'] # put your current branch to create a build. Core team only.
branches: ['873-pgvector-support'] # put your current branch to create a build. Core team only.
paths-ignore:
- '**.md'
- 'cloud-deployments/*'

View File

@ -132,6 +132,7 @@ AnythingLLM divides your documents into objects called `workspaces`. A Workspace
**Vector Databases:**
- [LanceDB](https://github.com/lancedb/lancedb) (default)
- [PGVector](https://github.com/pgvector/pgvector)
- [Astra DB](https://www.datastax.com/products/datastax-astra)
- [Pinecone](https://pinecone.io)
- [Chroma](https://trychroma.com)

View File

@ -189,6 +189,14 @@ GID='1000'
###########################################
######## Vector Database Selection ########
###########################################
# Enable all below if you are using vector database: LanceDB.
# VECTOR_DB="lancedb"
# Enable all below if you are using vector database: Weaviate.
# VECTOR_DB="pgvector"
# PGVECTOR_CONNECTION_STRING="postgresql://dbuser:dbuserpass@localhost:5432/yourdb"
# PGVECTOR_TABLE_NAME="anythingllm_vectors" # optional, but can be defined
# Enable all below if you are using vector database: Chroma.
# VECTOR_DB="chroma"
# CHROMA_ENDPOINT='http://host.docker.internal:8000'
@ -200,9 +208,6 @@ GID='1000'
# PINECONE_API_KEY=
# PINECONE_INDEX=
# Enable all below if you are using vector database: LanceDB.
# VECTOR_DB="lancedb"
# Enable all below if you are using vector database: Weaviate.
# VECTOR_DB="weaviate"
# WEAVIATE_ENDPOINT="http://localhost:8080"

View File

@ -0,0 +1,103 @@
import { Info } from "@phosphor-icons/react";
import { Tooltip } from "react-tooltip";
export default function PGVectorOptions({ settings }) {
return (
<div className="w-full flex flex-col gap-y-7">
<div className="w-full flex items-center gap-[36px] mt-1.5">
<div className="flex flex-col w-96">
<div className="flex items-center gap-x-1 mb-3">
<label className="text-white text-sm font-semibold block">
Postgres Connection String
</label>
<Info
size={16}
className="text-theme-text-secondary cursor-pointer"
data-tooltip-id="pgvector-connection-string-tooltip"
data-tooltip-place="right"
/>
<Tooltip
delayHide={300}
id="pgvector-connection-string-tooltip"
className="max-w-md z-99"
clickable={true}
>
<p className="text-md whitespace-pre-line break-words">
This is the connection string for the Postgres database in the
format of <br />
<code>postgresql://username:password@host:port/database</code>
<br />
<br />
The user for the database must have the following permissions:
<ul className="list-disc list-inside">
<li>Read access to the database</li>
<li>Read access to the database schema</li>
<li>Create access to the database</li>
</ul>
<br />
<b>
You must have the pgvector extension installed on the
database.
</b>
</p>
</Tooltip>
</div>
<input
type="text"
name="PGVectorConnectionString"
className="border-none bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
placeholder="postgresql://username:password@host:port/database"
defaultValue={
settings?.PGVectorConnectionString ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<div className="flex flex-col w-60">
<div className="flex items-center gap-x-1 mb-3">
<label className="text-white text-sm font-semibold block">
Vector Table Name
</label>
<Info
size={16}
className="text-theme-text-secondary cursor-pointer"
data-tooltip-id="pgvector-table-name-tooltip"
data-tooltip-place="right"
/>
<Tooltip
delayHide={300}
id="pgvector-table-name-tooltip"
className="max-w-md z-99"
clickable={true}
>
<p className="text-md whitespace-pre-line break-words">
This is the name of the table in the Postgres database that will
store the vectors.
<br />
<br />
By default, the table name is <code>anythingllm_vectors</code>.
<br />
<br />
<b>
This table must not already exist on the database - it will be
created automatically.
</b>
</p>
</Tooltip>
</div>
<input
type="text"
name="PGVectorTableName"
autoComplete="off"
defaultValue={settings?.PGVectorTableName}
className="border-none bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
placeholder="vector_table"
/>
</div>
</div>
</div>
);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

View File

@ -3,30 +3,34 @@ import Sidebar from "@/components/SettingsSidebar";
import { isMobile } from "react-device-detect";
import System from "@/models/system";
import showToast from "@/utils/toast";
import { useModal } from "@/hooks/useModal";
import CTAButton from "@/components/lib/CTAButton";
import { CaretUpDown, MagnifyingGlass, X } from "@phosphor-icons/react";
import { useTranslation } from "react-i18next";
import PreLoader from "@/components/Preloader";
import ChangeWarningModal from "@/components/ChangeWarning";
import ModalWrapper from "@/components/ModalWrapper";
import VectorDBItem from "@/components/VectorDBSelection/VectorDBItem";
import LanceDbLogo from "@/media/vectordbs/lancedb.png";
import ChromaLogo from "@/media/vectordbs/chroma.png";
import PineconeLogo from "@/media/vectordbs/pinecone.png";
import LanceDbLogo from "@/media/vectordbs/lancedb.png";
import WeaviateLogo from "@/media/vectordbs/weaviate.png";
import QDrantLogo from "@/media/vectordbs/qdrant.png";
import MilvusLogo from "@/media/vectordbs/milvus.png";
import ZillizLogo from "@/media/vectordbs/zilliz.png";
import AstraDBLogo from "@/media/vectordbs/astraDB.png";
import PreLoader from "@/components/Preloader";
import ChangeWarningModal from "@/components/ChangeWarning";
import { CaretUpDown, MagnifyingGlass, X } from "@phosphor-icons/react";
import PGVectorLogo from "@/media/vectordbs/pgvector.png";
import LanceDBOptions from "@/components/VectorDBSelection/LanceDBOptions";
import ChromaDBOptions from "@/components/VectorDBSelection/ChromaDBOptions";
import PineconeDBOptions from "@/components/VectorDBSelection/PineconeDBOptions";
import QDrantDBOptions from "@/components/VectorDBSelection/QDrantDBOptions";
import WeaviateDBOptions from "@/components/VectorDBSelection/WeaviateDBOptions";
import VectorDBItem from "@/components/VectorDBSelection/VectorDBItem";
import QDrantDBOptions from "@/components/VectorDBSelection/QDrantDBOptions";
import MilvusDBOptions from "@/components/VectorDBSelection/MilvusDBOptions";
import ZillizCloudOptions from "@/components/VectorDBSelection/ZillizCloudOptions";
import { useModal } from "@/hooks/useModal";
import ModalWrapper from "@/components/ModalWrapper";
import AstraDBOptions from "@/components/VectorDBSelection/AstraDBOptions";
import CTAButton from "@/components/lib/CTAButton";
import { useTranslation } from "react-i18next";
import PGVectorOptions from "@/components/VectorDBSelection/PGVectorOptions";
export default function GeneralVectorDatabase() {
const [saving, setSaving] = useState(false);
@ -114,6 +118,13 @@ export default function GeneralVectorDatabase() {
description:
"100% local vector DB that runs on the same instance as AnythingLLM.",
},
{
name: "PGVector",
value: "pgvector",
logo: PGVectorLogo,
options: <PGVectorOptions settings={settings} />,
description: "Vector search powered by PostgreSQL.",
},
{
name: "Chroma",
value: "chroma",

View File

@ -36,7 +36,7 @@ import QDrantLogo from "@/media/vectordbs/qdrant.png";
import MilvusLogo from "@/media/vectordbs/milvus.png";
import VoyageAiLogo from "@/media/embeddingprovider/voyageai.png";
import PPIOLogo from "@/media/llmprovider/ppio.png";
import PGVectorLogo from "@/media/vectordbs/pgvector.png";
import React, { useState, useEffect } from "react";
import paths from "@/utils/paths";
import { useNavigate } from "react-router-dom";
@ -237,6 +237,14 @@ export const LLM_SELECTION_PRIVACY = {
};
export const VECTOR_DB_PRIVACY = {
pgvector: {
name: "PGVector",
description: [
"Your vectors and document text are stored on your PostgreSQL instance",
"Access to your instance is managed by you",
],
logo: PGVectorLogo,
},
chroma: {
name: "Chroma",
description: [

View File

@ -137,6 +137,7 @@ AnythingLLM اسناد شما را به اشیایی به نام `workspaces` ت
**پایگاه‌های داده برداری:**
- [LanceDB](https://github.com/lancedb/lancedb) (پیش‌فرض)
- [PGVector](https://github.com/pgvector/pgvector)
- [Astra DB](https://www.datastax.com/products/datastax-astra)
- [Pinecone](https://pinecone.io)
- [Chroma](https://trychroma.com)

View File

@ -120,6 +120,7 @@ AnythingLLMは、ドキュメントを`ワークスペース`と呼ばれるオ
**ベクトルデータベース:**
- [LanceDB](https://github.com/lancedb/lancedb)(デフォルト)
- [PGVector](https://github.com/pgvector/pgvector)
- [Astra DB](https://www.datastax.com/products/datastax-astra)
- [Pinecone](https://pinecone.io)
- [Chroma](https://trychroma.com)

View File

@ -132,6 +132,7 @@ AnythingLLM, belgelerinizi **"çalışma alanları" (workspaces)** adı verilen
**Vektör Databases:**
- [LanceDB](https://github.com/lancedb/lancedb) (default)
- [PGVector](https://github.com/pgvector/pgvector)
- [Astra DB](https://www.datastax.com/products/datastax-astra)
- [Pinecone](https://pinecone.io)
- [Chroma](https://trychroma.com)

View File

@ -131,6 +131,7 @@ AnythingLLM将您的文档划分为称为`workspaces` (工作区)的对象。工
**支持的向量数据库:**
- [LanceDB](https://github.com/lancedb/lancedb) (默认)
- [PGVector](https://github.com/pgvector/pgvector)
- [Astra DB](https://www.datastax.com/products/datastax-astra)
- [Pinecone](https://pinecone.io)
- [Chroma](https://trychroma.com)

View File

@ -206,6 +206,11 @@ SIG_SALT='salt' # Please generate random string at least 32 chars long.
# Enable all below if you are using vector database: LanceDB.
VECTOR_DB="lancedb"
# Enable all below if you are using vector database: Weaviate.
# VECTOR_DB="pgvector"
# PGVECTOR_CONNECTION_STRING="postgresql://dbuser:dbuserpass@localhost:5432/yourdb"
# PGVECTOR_TABLE_NAME="anythingllm_vectors" # optional, but can be defined
# Enable all below if you are using vector database: Weaviate.
# VECTOR_DB="weaviate"
# WEAVIATE_ENDPOINT="http://localhost:8080"

View File

@ -7,6 +7,7 @@ const { isValidUrl, safeJsonParse } = require("../utils/http");
const prisma = require("../utils/prisma");
const { v4 } = require("uuid");
const { MetaGenerator } = require("../utils/boot/MetaGenerator");
const { PGVector } = require("../utils/vectorDbProviders/pgvector");
function isNullOrNaN(value) {
if (value === null) return true;
@ -427,6 +428,10 @@ const SystemSettings = {
// AstraDB Keys
AstraDBApplicationToken: process?.env?.ASTRA_DB_APPLICATION_TOKEN,
AstraDBEndpoint: process?.env?.ASTRA_DB_ENDPOINT,
// PGVector Keys
PGVectorConnectionString: !!PGVector.connectionString() || false,
PGVectorTableName: PGVector.tableName(),
};
},

View File

@ -107,6 +107,9 @@ function getVectorDbClass(getExactly = null) {
case "astra":
const { AstraDB } = require("../vectorDbProviders/astra");
return AstraDB;
case "pgvector":
const { PGVector } = require("../vectorDbProviders/pgvector");
return PGVector;
default:
throw new Error("ENV: No VECTOR_DB value found in environment!");
}

View File

@ -371,7 +371,6 @@ const KEY_MAPPING = {
},
// Astra DB Options
AstraDBApplicationToken: {
envKey: "ASTRA_DB_APPLICATION_TOKEN",
checks: [isNotEmpty],
@ -381,6 +380,23 @@ const KEY_MAPPING = {
checks: [isNotEmpty],
},
/*
PGVector Options
- Does very simple validations - we should expand this in the future
- to ensure the connection string is valid and the table name is valid
- via direct query
*/
PGVectorConnectionString: {
envKey: "PGVECTOR_CONNECTION_STRING",
checks: [isNotEmpty, looksLikePostgresConnectionString],
preUpdate: [validatePGVectorConnectionString],
},
PGVectorTableName: {
envKey: "PGVECTOR_TABLE_NAME",
checks: [isNotEmpty],
preUpdate: [validatePGVectorTableName],
},
// Together Ai Options
TogetherAiApiKey: {
envKey: "TOGETHER_AI_API_KEY",
@ -802,6 +818,7 @@ function supportedVectorDB(input = "") {
"milvus",
"zilliz",
"astra",
"pgvector",
];
return supported.includes(input)
? null
@ -880,6 +897,70 @@ async function handleVectorStoreReset(key, prevValue, nextValue) {
return false;
}
/**
* Validates the Postgres connection string for the PGVector options.
* @param {string} input - The Postgres connection string to validate.
* @returns {string} - An error message if the connection string is invalid, otherwise null.
*/
async function looksLikePostgresConnectionString(connectionString = null) {
if (!connectionString || !connectionString.startsWith("postgresql://"))
return "Invalid Postgres connection string. Must start with postgresql://";
if (connectionString.includes(" "))
return "Invalid Postgres connection string. Must not contain spaces.";
return null;
}
/**
* Validates the Postgres connection string for the PGVector options.
* @param {string} key - The ENV key we are validating.
* @param {string} prevValue - The previous value of the key.
* @param {string} nextValue - The next value of the key.
* @returns {string} - An error message if the connection string is invalid, otherwise null.
*/
async function validatePGVectorConnectionString(key, prevValue, nextValue) {
const envKey = KEY_MAPPING[key].envKey;
if (prevValue === nextValue) return; // If the value is the same as the previous value, don't validate it.
if (!nextValue) return; // If the value is not set, don't validate it.
if (nextValue === process.env[envKey]) return; // If the value is the same as the current connection string, don't validate it.
const { PGVector } = require("../vectorDbProviders/pgvector");
const { error, success } = await PGVector.validateConnection({
connectionString: nextValue,
});
if (!success) return error;
// Set the ENV variable for the PGVector connection string early so we can use it in the table check.
process.env[envKey] = nextValue;
return null;
}
/**
* Validates the Postgres table name for the PGVector options.
* - Table should not already exist in the database.
* @param {string} key - The ENV key we are validating.
* @param {string} prevValue - The previous value of the key.
* @param {string} nextValue - The next value of the key.
* @returns {string} - An error message if the table name is invalid, otherwise null.
*/
async function validatePGVectorTableName(key, prevValue, nextValue) {
const envKey = KEY_MAPPING[key].envKey;
if (prevValue === nextValue) return; // If the value is the same as the previous value, don't validate it.
if (!nextValue) return; // If the value is not set, don't validate it.
if (nextValue === process.env[envKey]) return; // If the value is the same as the current table name, don't validate it.
if (!process.env.PGVECTOR_CONNECTION_STRING) return; // if connection string is not set, don't validate it since it will fail.
const { PGVector } = require("../vectorDbProviders/pgvector");
const { error, success } = await PGVector.validateConnection({
connectionString: process.env.PGVECTOR_CONNECTION_STRING,
tableName: nextValue,
});
if (!success) return error;
return null;
}
// This will force update .env variables which for any which reason were not able to be parsed or
// read from an ENV file as this seems to be a complicating step for many so allowing people to write
// to the process will at least alleviate that issue. It does not perform comprehensive validity checks or sanity checks
@ -901,15 +982,28 @@ async function updateENV(newENVs = {}, force = false, userId = null) {
} = KEY_MAPPING[key];
const prevValue = process.env[envKey];
const nextValue = newENVs[key];
let errors = await executeValidationChecks(checks, nextValue, force);
const errors = await executeValidationChecks(checks, nextValue, force);
// If there are any errors from regular simple validation checks
// exit early.
if (errors.length > 0) {
error += errors.join("\n");
break;
}
for (const preUpdateFunc of preUpdate)
await preUpdateFunc(key, prevValue, nextValue);
// Accumulate errors from preUpdate functions
errors = [];
for (const preUpdateFunc of preUpdate) {
const errorMsg = await preUpdateFunc(key, prevValue, nextValue);
if (!!errorMsg && typeof errorMsg === "string") errors.push(errorMsg);
}
// If there are any errors from preUpdate functions
// exit early.
if (errors.length > 0) {
error += errors.join("\n");
break;
}
newValues[key] = nextValue;
process.env[envKey] = nextValue;

View File

@ -0,0 +1,106 @@
# Setting up `PGVector` for AnythingLLM
Setting up PGVector for anythingllm to use as your vector database is quite easy. At a minimum, you will need the following:
- PostgreSQL v12+
- [`pgvector`](https://github.com/pgvector/pgvector) extension installed on DB
- User with DB table creation perms and READ access
## Setup on Mac (example)
### Install pgvector extension on PostgreSQL DB
```bash
brew install postgresql
brew services start postgresql
brew install pgvector
# assuming you have a database already set up + a user
psql <database-name>
CREATE EXTENSION vector;
```
### Set PG as your vector db
_this can be done via the UI or by directly editing the `.env` file_
First, obtain a valid connection string for the user, credentials, and db you want to target.
eg: `postgresql://dbuser:dbuserpass@localhost:5432/yourdb`
> [!IMPORTANT]
> If you have an existing table that you want to use as a vector database, AnythingLLM **requires** that the table be
> at least minimally conform to the expected schema - this can be seen in the [index.js](./index.js) file.
_optional_ - set a table name you wish to have AnythingLLM store vectors to. By default this is `anythingllm_vectors`
## Common Questions
### I cannot connect to the DB (Running AnythingLLM in Docker)
If you are running AnythingLLM in Docker, you will need to ensure that the DB is accessible from the container.
If you are running your DB in another Docker container **or** on the host machine, you will need to ensure that the container can access the DB.
`localhost` will not work in this case as it will attempt to connect to the DB _inside the AnythingLLM container_ instead of the host machine or another container.
You will need to use the `host.docker.internal` (or `172.17.0.1` on Linux/Ubuntu) address.
```
on Mac or Windows:
postgresql://dbuser:dbuserpass@localhost:5432/yourdb => postgresql://dbuser:dbuserpass@host.docker.internal:5432/yourdb
on Linux:
postgresql://dbuser:dbuserpass@localhost:5432/yourdb => postgresql://dbuser:dbuserpass@172.17.0.1:5432/yourdb
```
### Can I use an existing table as a vector database?
Yes, you can use an existing table as a vector database. However, AnythingLLM **requires** that the table be at least minimally conform to the expected schema - this can be seen in the [index.js](./index.js) file.
It is **absolutely critical** that the `embedding` column's `VECTOR(XXXX)` dimensions match the dimension of the embedder in AnythingLLM. The default embedding model is 384 dimensions. However, if you are using a custom embedder, you will need to ensure that the dimension value is set correctly.
### Validate the connection to the database
When setting the connection string in or table name via the AnythingLLM UI, the following validations will be attempted:
- Validate the connection string
- Validate the table name
- Run test connection to ensure the table exists and is accessible by the connection string used
- Check if the table name already exists and if so, validate that it is an embedding table with the correct schema
### My embedding table is not present in the DB
The embedding storage table is created by AnythingLLM **on the first upsert** of a vector. If you have not yet embedding any documents, the table will not be present in the DB.
### How do I reset my vector database?
_at the workspace level in Settings > Vector Database_
You can use the "Reset Vector Database" button in the AnythingLLM UI to reset your vector database. This will drop all vectors within that workspace, but the table will remain in the DB.
_reset the vector database at the db level_
For this, you will need to `DROP TABLE` from the command line or however you manage your DB. Once the table is dropped, it will be recreated by AnythingLLM on the next upsert.
## Troubleshooting
### Cannot connect to DB
- Ensure the connection string is valid
- Ensure the user has access to the database
- Ensure the pgvector extension is installed
### Cannot create table
- Ensure the user has `CREATE TABLE` permissions
### Cannot insert vector
- Ensure the user has `INSERT` permissions in the database
- Ensure the table has a dimension value set and this matches the dimension of the embedder in AnythingLLM
- Ensure the table has a vector column set
### Cannot query vector
- Ensure the user has `SELECT` permissions in the database
- Ensure the table has a vector column set
- Ensure the table has a dimension value set and this matches the dimension of the embedder in AnythingLLM

View File

@ -0,0 +1,784 @@
const pgsql = require("pg");
const { toChunks, getEmbeddingEngineSelection } = require("../../helpers");
const { TextSplitter } = require("../../TextSplitter");
const { v4: uuidv4 } = require("uuid");
const { sourceIdentifier } = require("../../chats");
/*
Embedding Table Schema (table name defined by user)
- id: UUID PRIMARY KEY
- namespace: TEXT
- embedding: vector(xxxx)
- metadata: JSONB
- created_at: TIMESTAMP
*/
const PGVector = {
name: "PGVector",
connectionTimeout: 30_000,
/**
* Get the table name for the PGVector database.
* - Defaults to "anythingllm_vectors" if no table name is provided.
* @returns {string}
*/
tableName: () => process.env.PGVECTOR_TABLE_NAME || "anythingllm_vectors",
/**
* Get the connection string for the PGVector database.
* - Requires a connection string to be present in the environment variables.
* @returns {string | null}
*/
connectionString: () => process.env.PGVECTOR_CONNECTION_STRING,
// Possible for this to be a user-configurable option in the future.
// Will require a handler per operator to ensure scores are normalized.
operator: {
l2: "<->",
innerProduct: "<#>",
cosine: "<=>",
l1: "<+>",
hamming: "<~>",
jaccard: "<%>",
},
getTablesSql:
"SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'public'",
getEmbeddingTableSchemaSql:
"SELECT column_name,data_type FROM information_schema.columns WHERE table_name = $1",
createTableSql: (dimensions) =>
`CREATE TABLE IF NOT EXISTS "${PGVector.tableName()}" (id UUID PRIMARY KEY, namespace TEXT, embedding vector(${Number(dimensions)}), metadata JSONB, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)`,
log: function (message = null, ...args) {
console.log(`\x1b[35m[PGVectorDb]\x1b[0m ${message}`, ...args);
},
client: function (connectionString = null) {
return new pgsql.Client({
connectionString: connectionString || PGVector.connectionString(),
});
},
/**
* Validate the existing embedding table schema.
* @param {pgsql.Client} pgClient
* @param {string} tableName
* @returns {Promise<boolean>}
*/
validateExistingEmbeddingTableSchema: async function (pgClient, tableName) {
const result = await pgClient.query(this.getEmbeddingTableSchemaSql, [
tableName,
]);
// Minimum expected schema for an embedding table.
// Extra columns are allowed but the minimum exact columns are required
// to be present in the table.
const expectedSchema = [
{
column_name: "id",
expected: "uuid",
validation: function (dataType) {
return dataType.toLowerCase() === this.expected;
},
},
{
column_name: "namespace",
expected: "text",
validation: function (dataType) {
return dataType.toLowerCase() === this.expected;
},
},
{
column_name: "embedding",
expected: "vector",
validation: function (dataType) {
return !!dataType;
},
}, // just check if it exists
{
column_name: "metadata",
expected: "jsonb",
validation: function (dataType) {
return dataType.toLowerCase() === this.expected;
},
},
{
column_name: "created_at",
expected: "timestamp",
validation: function (dataType) {
return dataType.toLowerCase().includes(this.expected);
},
},
];
if (result.rows.length === 0)
throw new Error(
`The table '${tableName}' was found but does not contain any columns or cannot be accessed by role. It cannot be used as an embedding table in AnythingLLM.`
);
for (const rowDef of expectedSchema) {
const column = result.rows.find(
(c) => c.column_name === rowDef.column_name
);
if (!column)
throw new Error(
`The column '${rowDef.column_name}' was expected but not found in the table '${tableName}'.`
);
if (!rowDef.validation(column.data_type))
throw new Error(
`Invalid data type for column: '${column.column_name}'. Got '${column.data_type}' but expected '${rowDef.expected}'`
);
}
this.log(
`✅ The pgvector table '${tableName}' was found and meets the minimum expected schema for an embedding table.`
);
return true;
},
/**
* Validate the connection to the database and verify that the table does not already exist.
* so that anythingllm can manage the table directly.
*
* @param {{connectionString: string | null, tableName: string | null}} params
* @returns {Promise<{error: string | null, success: boolean}>}
*/
validateConnection: async function ({
connectionString = null,
tableName = null,
}) {
if (!connectionString) throw new Error("No connection string provided");
try {
const timeoutPromise = new Promise((resolve) => {
setTimeout(() => {
resolve({
error: `Connection timeout (${(PGVector.connectionTimeout / 1000).toFixed(0)}s). Please check your connection string and try again.`,
success: false,
});
}, PGVector.connectionTimeout);
});
const connectionPromise = new Promise(async (resolve) => {
let pgClient = null;
try {
pgClient = this.client(connectionString);
await pgClient.connect();
const result = await pgClient.query(this.getTablesSql);
if (result.rows.length !== 0 && !!tableName) {
const tableExists = result.rows.some(
(row) => row.tablename === tableName
);
if (tableExists)
await this.validateExistingEmbeddingTableSchema(
pgClient,
tableName
);
}
resolve({ error: null, success: true });
} catch (err) {
resolve({ error: err.message, success: false });
} finally {
if (pgClient) await pgClient.end();
}
});
// Race the connection attempt against the timeout
const result = await Promise.race([connectionPromise, timeoutPromise]);
return result;
} catch (err) {
this.log("Validation Error:", err.message);
let readableError = err.message;
switch (true) {
case err.message.includes("ECONNREFUSED"):
readableError =
"The host could not be reached. Please check your connection string and try again.";
break;
default:
break;
}
return { error: readableError, success: false };
}
},
/**
* Test the connection to the database directly.
* @returns {{error: string | null, success: boolean}}
*/
testConnectionToDB: async function () {
try {
const pgClient = await this.connect();
await pgClient.query(this.getTablesSql);
await pgClient.end();
return { error: null, success: true };
} catch (err) {
return { error: err.message, success: false };
}
},
/**
* Connect to the database.
* - Throws an error if the connection string or table name is not provided.
* @returns {Promise<pgsql.Client>}
*/
connect: async function () {
if (!PGVector.connectionString())
throw new Error("No connection string provided");
if (!PGVector.tableName()) throw new Error("No table name provided");
const client = this.client();
await client.connect();
return client;
},
/**
* Test the connection to the database with already set credentials via ENV
* @returns {{error: string | null, success: boolean}}
*/
heartbeat: async function () {
return this.testConnectionToDB();
},
/**
* Check if the anythingllm embedding table exists in the database
* @returns {Promise<boolean>}
*/
dbTableExists: async function () {
let connection = null;
try {
connection = await this.connect();
const tables = await connection.query(this.getTablesSql);
if (tables.rows.length === 0) return false;
const tableExists = tables.rows.some(
(row) => row.tablename === PGVector.tableName()
);
return !!tableExists;
} catch (err) {
return false;
} finally {
if (connection) await connection.end();
}
},
totalVectors: async function () {
if (!(await this.dbTableExists())) return 0;
let connection = null;
try {
connection = await this.connect();
const result = await connection.query(
`SELECT COUNT(id) FROM "${PGVector.tableName()}"`
);
return result.rows[0].count;
} catch (err) {
return 0;
} finally {
if (connection) await connection.end();
}
},
// Distance for cosine is just the distance for pgvector.
distanceToSimilarity: function (distance = null) {
if (distance === null || typeof distance !== "number") return 0.0;
if (distance >= 1.0) return 1;
if (distance < 0) return 1 - Math.abs(distance);
return 1 - distance;
},
namespaceCount: async function (namespace = null) {
if (!(await this.dbTableExists())) return 0;
let connection = null;
try {
connection = await this.connect();
const result = await connection.query(
`SELECT COUNT(id) FROM "${PGVector.tableName()}" WHERE namespace = $1`,
[namespace]
);
return result.rows[0].count;
} catch (err) {
return 0;
} finally {
if (connection) await connection.end();
}
},
/**
* Performs a SimilaritySearch on a given PGVector namespace.
* @param {Object} params
* @param {pgsql.Client} params.client
* @param {string} params.namespace
* @param {number[]} params.queryVector
* @param {number} params.similarityThreshold
* @param {number} params.topN
* @param {string[]} params.filterIdentifiers
* @returns
*/
similarityResponse: async function ({
client,
namespace,
queryVector,
similarityThreshold = 0.25,
topN = 4,
filterIdentifiers = [],
}) {
const result = {
contextTexts: [],
sourceDocuments: [],
scores: [],
};
const embedding = `[${queryVector.map(Number).join(",")}]`;
const response = await client.query(
`SELECT embedding ${this.operator.cosine} $1 AS _distance, metadata FROM "${PGVector.tableName()}" WHERE namespace = $2 ORDER BY _distance ASC LIMIT $3`,
[embedding, namespace, topN]
);
response.rows.forEach((item) => {
if (this.distanceToSimilarity(item._distance) < similarityThreshold)
return;
if (filterIdentifiers.includes(sourceIdentifier(item.metadata))) {
this.log(
"A source was filtered from context as it's parent document is pinned."
);
return;
}
result.contextTexts.push(item.metadata.text);
result.sourceDocuments.push({
...item.metadata,
score: this.distanceToSimilarity(item._distance),
});
result.scores.push(this.distanceToSimilarity(item._distance));
});
return result;
},
normalizeVector: function (vector) {
const magnitude = Math.sqrt(
vector.reduce((sum, val) => sum + val * val, 0)
);
if (magnitude === 0) return vector; // Avoid division by zero
return vector.map((val) => val / magnitude);
},
/**
* Update or create a collection in the database
* @param {pgsql.Connection} connection
* @param {{id: number, vector: number[], metadata: Object}[]} submissions
* @param {string} namespace
* @returns {Promise<boolean>}
*/
updateOrCreateCollection: async function ({
connection,
submissions,
namespace,
dimensions = 384,
}) {
await this.createTableIfNotExists(connection, dimensions);
this.log(`Updating or creating collection ${namespace}`);
try {
// Create a transaction of all inserts
await connection.query(`BEGIN`);
for (const submission of submissions) {
const embedding = `[${submission.vector.map(Number).join(",")}]`; // stringify the vector for pgvector
await connection.query(
`INSERT INTO "${PGVector.tableName()}" (id, namespace, embedding, metadata) VALUES ($1, $2, $3, $4)`,
[submission.id, namespace, embedding, submission.metadata]
);
}
this.log(`Committing ${submissions.length} vectors to ${namespace}`);
await connection.query(`COMMIT`);
} catch (err) {
this.log(
`Rolling back ${submissions.length} vectors to ${namespace}`,
err
);
await connection.query(`ROLLBACK`);
}
return true;
},
/**
* create a table if it doesn't exist
* @param {pgsql.Client} connection
* @param {number} dimensions
* @returns
*/
createTableIfNotExists: async function (connection, dimensions = 384) {
this.log(`Creating embedding table with ${dimensions} dimensions`);
await connection.query(this.createTableSql(dimensions));
return true;
},
/**
* Get the namespace from the database
* @param {pgsql.Client} connection
* @param {string} namespace
* @returns {Promise<{name: string, vectorCount: number}>}
*/
namespace: async function (connection, namespace = null) {
if (!namespace) throw new Error("No namespace provided");
const result = await connection.query(
`SELECT COUNT(id) FROM "${PGVector.tableName()}" WHERE namespace = $1`,
[namespace]
);
return { name: namespace, vectorCount: result.rows[0].count };
},
/**
* Check if the namespace exists in the database
* @param {string} namespace
* @returns {Promise<boolean>}
*/
hasNamespace: async function (namespace = null) {
if (!namespace) throw new Error("No namespace provided");
let connection = null;
try {
connection = await this.connect();
return await this.namespaceExists(connection, namespace);
} catch (err) {
return false;
} finally {
if (connection) await connection.end();
}
},
/**
* Check if the namespace exists in the database
* @param {pgsql.Client} connection
* @param {string} namespace
* @returns {Promise<boolean>}
*/
namespaceExists: async function (connection, namespace = null) {
if (!namespace) throw new Error("No namespace provided");
const result = await connection.query(
`SELECT COUNT(id) FROM "${PGVector.tableName()}" WHERE namespace = $1 LIMIT 1`,
[namespace]
);
return result.rows[0].count > 0;
},
/**
* Delete all vectors in the namespace
* @param {pgsql.Client} connection
* @param {string} namespace
* @returns {Promise<boolean>}
*/
deleteVectorsInNamespace: async function (connection, namespace = null) {
if (!namespace) throw new Error("No namespace provided");
await connection.query(
`DELETE FROM "${PGVector.tableName()}" WHERE namespace = $1`,
[namespace]
);
return true;
},
addDocumentToNamespace: async function (
namespace,
documentData = {},
fullFilePath = null,
skipCache = false
) {
const { DocumentVectors } = require("../../../models/vectors");
const {
storeVectorResult,
cachedVectorInformation,
} = require("../../files");
let connection = null;
try {
const { pageContent, docId, ...metadata } = documentData;
if (!pageContent || pageContent.length == 0) return false;
connection = await this.connect();
this.log("Adding new vectorized document into namespace", namespace);
if (!skipCache) {
const cacheResult = await cachedVectorInformation(fullFilePath);
let vectorDimensions;
if (cacheResult.exists) {
const { chunks } = cacheResult;
const documentVectors = [];
const submissions = [];
for (const chunk of chunks.flat()) {
if (!vectorDimensions) vectorDimensions = chunk.values.length;
const id = uuidv4();
const { id: _id, ...metadata } = chunk.metadata;
documentVectors.push({ docId, vectorId: id });
submissions.push({ id: id, vector: chunk.values, metadata });
}
await this.updateOrCreateCollection({
connection,
submissions,
namespace,
dimensions: vectorDimensions,
});
await DocumentVectors.bulkInsert(documentVectors);
return { vectorized: true, error: null };
}
}
// If we are here then we are going to embed and store a novel document.
// We have to do this manually as opposed to using LangChains `xyz.fromDocuments`
// because we then cannot atomically control our namespace to granularly find/remove documents
// from vectordb.
const { SystemSettings } = require("../../../models/systemSettings");
const EmbedderEngine = getEmbeddingEngineSelection();
const textSplitter = new TextSplitter({
chunkSize: TextSplitter.determineMaxChunkSize(
await SystemSettings.getValueOrFallback({
label: "text_splitter_chunk_size",
}),
EmbedderEngine?.embeddingMaxChunkLength
),
chunkOverlap: await SystemSettings.getValueOrFallback(
{ label: "text_splitter_chunk_overlap" },
20
),
chunkHeaderMeta: TextSplitter.buildHeaderMeta(metadata),
});
const textChunks = await textSplitter.splitText(pageContent);
this.log("Chunks created from document:", textChunks.length);
const documentVectors = [];
const vectors = [];
const submissions = [];
const vectorValues = await EmbedderEngine.embedChunks(textChunks);
let vectorDimensions;
if (!!vectorValues && vectorValues.length > 0) {
for (const [i, vector] of vectorValues.entries()) {
if (!vectorDimensions) vectorDimensions = vector.length;
const vectorRecord = {
id: uuidv4(),
values: vector,
metadata: { ...metadata, text: textChunks[i] },
};
vectors.push(vectorRecord);
submissions.push({
id: vectorRecord.id,
vector: vectorRecord.values,
metadata: vectorRecord.metadata,
});
documentVectors.push({ docId, vectorId: vectorRecord.id });
}
} else {
throw new Error(
"Could not embed document chunks! This document will not be recorded."
);
}
if (vectors.length > 0) {
const chunks = [];
for (const chunk of toChunks(vectors, 500)) chunks.push(chunk);
this.log("Inserting vectorized chunks into PGVector collection.");
await this.updateOrCreateCollection({
connection,
submissions,
namespace,
dimensions: vectorDimensions,
});
await storeVectorResult(chunks, fullFilePath);
}
await DocumentVectors.bulkInsert(documentVectors);
return { vectorized: true, error: null };
} catch (err) {
this.log("addDocumentToNamespace", err.message);
return { vectorized: false, error: err.message };
} finally {
if (connection) await connection.end();
}
},
/**
* Delete a document from the namespace
* @param {string} namespace
* @param {string} docId
* @returns {Promise<boolean>}
*/
deleteDocumentFromNamespace: async function (namespace, docId) {
if (!namespace) throw new Error("No namespace provided");
if (!docId) throw new Error("No docId provided");
let connection = null;
try {
connection = await this.connect();
const exists = await this.namespaceExists(connection, namespace);
if (!exists)
throw new Error(
`PGVector:deleteDocumentFromNamespace - namespace ${namespace} does not exist.`
);
const { DocumentVectors } = require("../../../models/vectors");
const vectorIds = (await DocumentVectors.where({ docId })).map(
(record) => record.vectorId
);
if (vectorIds.length === 0) return;
try {
await connection.query(`BEGIN`);
for (const vectorId of vectorIds)
await connection.query(
`DELETE FROM "${PGVector.tableName()}" WHERE id = $1`,
[vectorId]
);
await connection.query(`COMMIT`);
} catch (err) {
await connection.query(`ROLLBACK`);
throw err;
}
this.log(
`Deleted ${vectorIds.length} vectors from namespace ${namespace}`
);
return true;
} catch (err) {
this.log(
`Error deleting document from namespace ${namespace}: ${err.message}`
);
return false;
} finally {
if (connection) await connection.end();
}
},
performSimilaritySearch: async function ({
namespace = null,
input = "",
LLMConnector = null,
similarityThreshold = 0.25,
topN = 4,
filterIdentifiers = [],
}) {
let connection = null;
if (!namespace || !input || !LLMConnector)
throw new Error("Invalid request to performSimilaritySearch.");
try {
connection = await this.connect();
const exists = await this.namespaceExists(connection, namespace);
if (!exists) {
this.log(
`The namespace ${namespace} does not exist or has no vectors. Returning empty results.`
);
return {
contextTexts: [],
sources: [],
message: null,
};
}
const queryVector = await LLMConnector.embedTextInput(input);
const result = await this.similarityResponse({
client: connection,
namespace,
queryVector,
similarityThreshold,
topN,
filterIdentifiers,
});
const { contextTexts, sourceDocuments } = result;
const sources = sourceDocuments.map((metadata, i) => {
return { metadata: { ...metadata, text: contextTexts[i] } };
});
return {
contextTexts,
sources: this.curateSources(sources),
message: false,
};
} catch (err) {
return { error: err.message, success: false };
} finally {
if (connection) await connection.end();
}
},
"namespace-stats": async function (reqBody = {}) {
const { namespace = null } = reqBody;
if (!namespace) throw new Error("namespace required");
if (!(await this.dbTableExists()))
return { message: "No table found in database" };
let connection = null;
try {
connection = await this.connect();
if (!(await this.namespaceExists(connection, namespace)))
throw new Error("Namespace by that name does not exist.");
const stats = await this.namespace(connection, namespace);
return stats
? stats
: { message: "No stats were able to be fetched from DB for namespace" };
} catch (err) {
return {
message: `Error fetching stats for namespace ${namespace}: ${err.message}`,
};
} finally {
if (connection) await connection.end();
}
},
"delete-namespace": async function (reqBody = {}) {
const { namespace = null } = reqBody;
if (!namespace) throw new Error("No namespace provided");
let connection = null;
try {
const existingCount = await this.namespaceCount(namespace);
if (existingCount === 0)
return {
message: `Namespace ${namespace} does not exist or has no vectors.`,
};
connection = await this.connect();
await this.deleteVectorsInNamespace(connection, namespace);
return {
message: `Namespace ${namespace} was deleted along with ${existingCount} vectors.`,
};
} catch (err) {
return {
message: `Error deleting namespace ${namespace}: ${err.message}`,
};
} finally {
if (connection) await connection.end();
}
},
/**
* Reset the entire vector database table associated with anythingllm
* @returns {Promise<{reset: boolean}>}
*/
reset: async function () {
let connection = null;
try {
connection = await this.connect();
await connection.query(`DROP TABLE IF EXISTS "${PGVector.tableName()}"`);
return { reset: true };
} catch (err) {
return { reset: false };
} finally {
if (connection) await connection.end();
}
},
curateSources: function (sources = []) {
const documents = [];
for (const source of sources) {
const { text, vector: _v, _distance: _d, ...rest } = source;
const metadata = rest.hasOwnProperty("metadata") ? rest.metadata : rest;
if (Object.keys(metadata).length > 0) {
documents.push({
...metadata,
...(text ? { text } : {}),
});
}
}
return documents;
},
};
module.exports.PGVector = PGVector;

View File

@ -30,11 +30,22 @@ async function resetAllVectorStores({ vectorDbKey }) {
vectorDbKey
);
const VectorDb = getVectorDbClass(vectorDbKey);
for (const workspace of workspaces) {
try {
await VectorDb["delete-namespace"]({ namespace: workspace.slug });
} catch (e) {
console.error(e.message);
if (vectorDbKey === "pgvector") {
/*
pgvector has a reset method that drops the entire embedding table
which is required since if this function is called we will need to
reset the embedding column VECTOR dimension value and you cannot change
the dimension value of an existing vector column.
*/
await VectorDb.reset();
} else {
for (const workspace of workspaces) {
try {
await VectorDb["delete-namespace"]({ namespace: workspace.slug });
} catch (e) {
console.error(e.message);
}
}
}