service 修改 Redis 存储 KV

This commit is contained in:
2026-04-10 11:12:10 +00:00
parent c888ca8844
commit bc82e3e708
25 changed files with 322 additions and 3666 deletions

View File

@@ -28,14 +28,6 @@ type Config struct {
FrequencyPenalty float32 `mapstructure:"frequency_penalty"`
BotDesc string `mapstructure:"bot_desc"`
MinResponseTokens int `mapstructure:"min_response_tokens"`
ContextTTL int `mapstructure:"context_ttl"`
ContextLen int `mapstructure:"context_len"`
}
Mysql struct {
DSN string
MaxLifeTime int
MaxOpenConn int
MaxIdleConn int
}
Redis struct {
Host string
@@ -55,28 +47,6 @@ type Config struct {
Address string
}
}
Vector struct {
Provider string
Threshold float32
Tencent struct {
Url string
Username string
Pwd string
Database string
Timeout int
MaxIdleConnPerHost int
ReadConsistency string
IdleConnTimeout int
}
Pgvector struct {
DSN string `mapstructure:"dsn"`
Table string `mapstructure:"table"`
Dimensions int `mapstructure:"dimensions"`
MaxLifeTime int `mapstructure:"maxLifeTime"`
MaxOpenConn int `mapstructure:"maxOpenConn"`
MaxIdleConn int `mapstructure:"maxIdleConn"`
}
}
Embedding struct {
Provider string
BaseUrl string `mapstructure:"base_url"`
@@ -84,15 +54,11 @@ type Config struct {
Model string `mapstructure:"model"`
Timeout int
}
VectorDB struct {
Url string
Username string
Pwd string
Database string
Timeout int
MaxIdleConnPerHost int
ReadConsistency string
IdleConnTimeout int
Faiss struct {
BaseUrl string `mapstructure:"base_url"`
SearchK int `mapstructure:"search_k"`
SimilarityThreshold float32 `mapstructure:"similarity_threshold"`
Timeout int
}
}
@@ -123,39 +89,6 @@ func GetConfig() *Config {
}
func normalizeConfig(conf *Config) {
if conf.Vector.Provider == "" {
conf.Vector.Provider = "tencent"
}
if conf.Vector.Threshold == 0 {
conf.Vector.Threshold = 0.99
}
// Backfill the new vector.tencent block from the legacy vectorDB config.
if conf.Vector.Tencent.Url == "" {
conf.Vector.Tencent.Url = conf.VectorDB.Url
}
if conf.Vector.Tencent.Username == "" {
conf.Vector.Tencent.Username = conf.VectorDB.Username
}
if conf.Vector.Tencent.Pwd == "" {
conf.Vector.Tencent.Pwd = conf.VectorDB.Pwd
}
if conf.Vector.Tencent.Database == "" {
conf.Vector.Tencent.Database = conf.VectorDB.Database
}
if conf.Vector.Tencent.Timeout == 0 {
conf.Vector.Tencent.Timeout = conf.VectorDB.Timeout
}
if conf.Vector.Tencent.MaxIdleConnPerHost == 0 {
conf.Vector.Tencent.MaxIdleConnPerHost = conf.VectorDB.MaxIdleConnPerHost
}
if conf.Vector.Tencent.ReadConsistency == "" {
conf.Vector.Tencent.ReadConsistency = conf.VectorDB.ReadConsistency
}
if conf.Vector.Tencent.IdleConnTimeout == 0 {
conf.Vector.Tencent.IdleConnTimeout = conf.VectorDB.IdleConnTimeout
}
if conf.Embedding.Provider == "" {
conf.Embedding.Provider = "openai-compatible"
}
@@ -168,6 +101,18 @@ func normalizeConfig(conf *Config) {
if conf.Embedding.Timeout == 0 {
conf.Embedding.Timeout = 10
}
if conf.Faiss.BaseUrl == "" {
conf.Faiss.BaseUrl = "http://127.0.0.1:8451"
}
if conf.Faiss.SearchK == 0 {
conf.Faiss.SearchK = 1
}
if conf.Faiss.SimilarityThreshold == 0 {
conf.Faiss.SimilarityThreshold = 0.9
}
if conf.Faiss.Timeout == 0 {
conf.Faiss.Timeout = 10
}
}
func applySecretEnvOverrides(conf *Config) {
@@ -177,6 +122,9 @@ func applySecretEnvOverrides(conf *Config) {
if v := os.Getenv("AI_CHAT_EMBEDDING_API_KEY"); v != "" {
conf.Embedding.ApiKey = v
}
if v := os.Getenv("AI_CHAT_FAISS_BASE_URL"); v != "" {
conf.Faiss.BaseUrl = v
}
if v := os.Getenv("REDIS_PASSWORD"); v != "" {
conf.Redis.Pwd = v
}

View File

@@ -1,28 +0,0 @@
package mysql
import (
"ai-chat-service/pkg/config"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"time"
)
var db *sql.DB
func InitMysql(cnf *config.Config) {
var err error
if cnf.Mysql.DSN == "" {
panic("数据库连接字符串不能为空")
}
db, err = sql.Open("mysql", cnf.Mysql.DSN)
if err != nil {
panic(err)
}
db.SetMaxOpenConns(cnf.Mysql.MaxOpenConn)
db.SetMaxIdleConns(cnf.Mysql.MaxIdleConn)
db.SetConnMaxLifetime(time.Second * time.Duration(cnf.Mysql.MaxLifeTime))
}
func GetDB() *sql.DB {
return db
}

View File

@@ -1,29 +0,0 @@
package vector
import (
"ai-chat-service/pkg/config"
"ai-chat-service/pkg/log"
"github.com/tencent/vectordatabase-sdk-go/tcvectordb"
"time"
)
var vdb *tcvectordb.Client
func InitDB(config *config.Config) {
var defaultOption = &tcvectordb.ClientOption{
Timeout: time.Second * time.Duration(config.VectorDB.Timeout),
MaxIdldConnPerHost: config.VectorDB.MaxIdleConnPerHost,
IdleConnTimeout: time.Second * time.Duration(config.VectorDB.IdleConnTimeout),
ReadConsistency: tcvectordb.ReadConsistency(config.VectorDB.ReadConsistency),
}
var err error
vdb, err = tcvectordb.NewClient(config.VectorDB.Url, config.VectorDB.Username, config.VectorDB.Pwd, defaultOption)
if err != nil {
log.Error(err)
return
}
}
func GetVdb() *tcvectordb.Client {
return vdb
}