From 2b13b274ccbdff76d1ca0df922cf4734ac460163 Mon Sep 17 00:00:00 2001 From: shenlan Date: Thu, 7 Aug 2025 13:31:43 +0800 Subject: [PATCH] feat: add vectordb postgres config fields --- server/api/rag.go | 8 ++++++-- server/rag/config/runtime.go | 40 ++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/server/api/rag.go b/server/api/rag.go index c9412ef..f5227de 100644 --- a/server/api/rag.go +++ b/server/api/rag.go @@ -20,12 +20,16 @@ var ragSvc = initRAG() // initRAG attempts to construct a RAG service from server configuration. func initRAG() *rag.Service { cfg, err := rconfig.LoadServer() - if err != nil || cfg.VectorDB.PGURL == "" { + if err != nil { + return nil + } + dsn := cfg.VectorDB.DSN() + if dsn == "" { return nil } ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - st, err := store.New(ctx, cfg.VectorDB.PGURL) + st, err := store.New(ctx, dsn) if err != nil { return nil } diff --git a/server/rag/config/runtime.go b/server/rag/config/runtime.go index 54611e5..b3b9de9 100644 --- a/server/rag/config/runtime.go +++ b/server/rag/config/runtime.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "os" "path/filepath" @@ -19,13 +20,44 @@ type Runtime struct { Addr string `yaml:"addr"` Password string `yaml:"password"` } `yaml:"redis"` - Module string `yaml:"module"` - VectorDB struct { - PGURL string `yaml:"pgurl"` - } `yaml:"vectordb"` + Module string `yaml:"module"` + VectorDB VectorDB `yaml:"vectordb"` Datasources []Datasource `yaml:"datasources"` } +// VectorDB holds configuration for the PostgreSQL vector store. +type VectorDB struct { + PGURL string `yaml:"pgurl"` + PGHost string `yaml:"pg_host"` + PGPort int `yaml:"pg_port"` + PGUser string `yaml:"pg_user"` + PGPassword string `yaml:"pg_password"` + PGDBName string `yaml:"pg_db_name"` + PGSSLMode string `yaml:"pg_sslmode"` +} + +// DSN returns the connection string for the database. +// If PGURL is provided it is used, otherwise a DSN is constructed +// from individual fields. When insufficient fields are provided it +// returns an empty string. +func (v VectorDB) DSN() string { + if v.PGURL != "" { + return v.PGURL + } + if v.PGHost == "" || v.PGUser == "" || v.PGDBName == "" { + return "" + } + port := v.PGPort + if port == 0 { + port = 5432 + } + ssl := v.PGSSLMode + if ssl == "" { + ssl = "require" + } + return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", v.PGUser, v.PGPassword, v.PGHost, port, v.PGDBName, ssl) +} + // LoadServer loads RAG configuration from server/config/server.yaml. func LoadServer() (*Runtime, error) { path := filepath.Join("server", "config", "server.yaml")