向量数据库

应用场景 #

  • 知识管理

向量数据库安装 #

  • 以Linux安装PostgresSQL为例
源码安装
tar -zxvf postgresql-15.4.tar.gz
cd postgresql-15.4
./configure --prefix=/usr/local/postgresql
在执行./configure的时候报错:configure: error: readline library not found
解决方法:yum install -y readline-devel
报错:configure: 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 /ect/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/

vim /usr/local/postgresql/data/postgresql.conf
把60行的listen_addresses = 'localhost'改为listen_addresses = '*'
把对应的port=5432放开
netstat -tnlp|grep 5432

vim /usr/local/postgresql/data/pg_hba.conf
添加一行:
host  all  all  0.0.0.0/0 password 有密码
host all all    0.0.0.0/0 trust


#启动
pg_ctl start -l /usr/local/postgresql/log/pg_server.log
cd /usr/local/postgresql/bin 重启./pg_ctl reload

firewall-cmd --zone=public --add-port=5432/tcp --permanent

#进入, 注意/var/run/postgresql/.s.PGSQL.5432是没有的, 所以需要加/tmp
psql -U postgres -d postgres -h /tmp
#退出 \q

#权限
create user 帐号名 with encrypted password '密码';
create database smartdb;
grant all privileges on database smartdb to 帐号名;
ALTER DATABASE smartdb OWNER TO 帐号名;
  • 安装vector插件
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;
  • 创建vector表
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
 )

配置方法 #

  • 需要在首页,头像处下拉菜单"服务配置"中进行配置向量模型信息
{
  "smtvector": {
    "dbtype": "vectorPostgres",
    "api_key":"dashscope的key",
    "model":"text-embedding-v4",
    "host": "",
    "port": 5432,
    "user": "",
    "password": "",
    "db": "smartdb",
    "table": "vectors"
  }
}
  • 新建数据源,使用vectorPostgres连接器,配置PostgreSQL连接信息

查询方法 #

  • 你可以新建数据集(智能体),选择向量数据源进行查询
你好   -- 问题
document,answer  -- 字段
2    -- 返回数
collection='abc'   -- 条件

返回二维数组: document,answer,distance(0-1)

  • 也支持直接sql查询
select id,collection,document,answer,owner,update_time from vectors

写入方法 #

写入方法,同ds_save的方法,如:

dataset={
  "table": "vectors",
}
dataset=[['collection','document','answer','owner'],['分类名', '问题', '答案', '$username']]
print(ds_save(1, dataset));

知识库维护 #

  • 你可以采用平台的CRUD模板功能自行开发维护可视化界面 输入图片说明