Vector Database

Introduction #

SmartChart supports vector database integration, converting text to vectors and storing them for semantic knowledge retrieval (RAG). This is the core technology for building enterprise private knowledge bases and intelligent Q&A bots. SmartChart supports PostgreSQL + pgvector, and also StarRocks vectorization (enterprise). Once configured, semantic queries in Python datasets find the most relevant knowledge fragments, then pass them to LLM for accurate answers.


Use Cases #

  • Knowledge management

Vector Database Installation #

Example: Installing PostgreSQL on Linux

# Source installation
tar -zxvf postgresql-15.4.tar.gz
cd postgresql-15.4
./configure --prefix=/usr/local/postgresql
# If error: readline library not found -> yum install -y readline-devel
# If error: zlib library not found -> yum install zlib-devel

sudo make && sudo make install

mkdir /usr/local/postgresql/data
mkdir /usr/local/postgresql/log
vim /etc/profile
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export PGHOST=/tmp
export PATH=$PATH:$PGHOME/bin

source /etc/profile

useradd postgres
chown -R postgres:root /usr/local/postgresql
su postgres
/usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/

# Configure postgresql.conf
vim /usr/local/postgresql/data/postgresql.conf
# Change listen_addresses = 'localhost' to '*'
# Uncomment port=5432

# Configure pg_hba.conf
vim /usr/local/postgresql/data/pg_hba.conf
# Add: host all all 0.0.0.0/0 password

# Start
pg_ctl start -l /usr/local/postgresql/log/pg_server.log

# Connect
psql -U postgres -d postgres -h /tmp

# Create user and database
create user username with encrypted password 'password';
create database smartdb;
grant all privileges on database smartdb to username;
ALTER DATABASE smartdb OWNER TO username;
  • Install vector extension
export PG_CONFIG=/usr/local/postgresql/bin/pg_config
cd /tmp
git clone --branch v0.4.2 https://github.com/pgvector/pgvector.git
cd pgvector
make clean
make && make install
sudo --preserve-env=PG_CONFIG make install

CREATE EXTENSION IF NOT EXISTS vector;
  • Create vector table
CREATE TABLE IF NOT EXISTS vectors(
    id bigserial,
    collection varchar(100) not null,
    document text,
    answer text,
    embedding vector,
    owner varchar(50),
    update_time timestamp DEFAULT CURRENT_TIMESTAMP
)

Configuration #

Configure vector model info in homepage -> avatar dropdown -> “Service Configuration”

{
  "smtvector": {
    "dbtype": "vectorPostgres",
    "api_key":"dashscope_key",
    "model":"text-embedding-v4",
    "host": "",
    "port": 5432,
    "user": "",
    "password": "",
    "db": "smartdb",
    "table": "vectors"
  }
}
  • Create data source using vectorPostgres connector, configure PostgreSQL connection

Query Methods #

  • Create dataset (agent), select vector data source for querying
Hello   -- question
document,answer  -- fields
2    -- return count
collection='abc'   -- condition

Returns 2D array: document, answer, distance (0-1)

  • Also supports direct SQL queries
select id,collection,document,answer,owner,update_time from vectors

Write Methods #

Same as ds_save:

dataset={ "table": "vectors" }
dataset=[['collection','document','answer','owner'],['category', 'question', 'answer', '$username']]
print(ds_save(1, dataset));

Knowledge Base Maintenance #

  • Use the platform’s CRUD template feature to develop maintenance UI Screenshot