From 4f33781a6067cb4205ff6e176d98cea404f61b53 Mon Sep 17 00:00:00 2001 From: zouyq Date: Thu, 10 Apr 2025 16:52:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9redis=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E4=B8=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81rediss=EF=BC=8C=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8D=E5=92=8C=E5=AF=86=E7=A0=81=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?gemini=E4=B8=8D=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AEModel?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8gemini-pro=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E4=BF=AE=E5=A4=8Dmysql=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E8=AF=AD=E5=8F=A5=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat/build/mysql/init/bots.sql | 9 ++-- chat/build/mysql/init/bots_prompt.sql | 4 +- chat/build/mysql/init/chat.sql | 2 +- chat/build/mysql/init/chat_config.sql | 2 +- chat/common/redis/redis.go | 46 ++++++++++++++++--- chat/service/chat/api/chat.go | 2 +- .../chat/api/internal/config/config.go | 5 +- .../chat/api/internal/logic/chatlogic.go | 2 +- doc/config.md | 5 +- 9 files changed, 55 insertions(+), 22 deletions(-) diff --git a/chat/build/mysql/init/bots.sql b/chat/build/mysql/init/bots.sql index 5c259c7d..f1182407 100644 --- a/chat/build/mysql/init/bots.sql +++ b/chat/build/mysql/init/bots.sql @@ -5,11 +5,10 @@ CREATE TABLE `bots` `name` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '机器人名称', `avatar` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '机器人头像', `desc` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '机器人描述', - `user_id` bigint unsigned NOT NULL NOT NULL DEFAULT 0 COMMENT '创建人用户ID 关联 user.id', + `user_id` bigint unsigned NOT NULL DEFAULT 0 COMMENT '创建人用户ID 关联 user.id', + PRIMARY KEY (`id`), `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', - `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', - PRIMARY KEY (`id`) -) ENGINE=InnoDB COMMENT="机器人基础设置表" AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间' +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='机器人基础设置表'; -- 加入索引 id_user_id ALTER TABLE `bots` ADD INDEX `idx_user_id` (`user_id`) USING BTREE; - diff --git a/chat/build/mysql/init/bots_prompt.sql b/chat/build/mysql/init/bots_prompt.sql index af9fe240..90bf2cad 100644 --- a/chat/build/mysql/init/bots_prompt.sql +++ b/chat/build/mysql/init/bots_prompt.sql @@ -8,5 +8,5 @@ CREATE TABLE `bots_prompt` `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`) -) ENGINE=InnoDB COMMENT="机器人prompt设置表" AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -Alter TABLE `bots_prompt` ADD INDEX `idx_bot_id` (`bot_id`) USING BTREE; \ No newline at end of file +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='机器人prompt设置表'; +ALTER TABLE `bots_prompt` ADD INDEX `idx_bot_id` (`bot_id`) USING BTREE; diff --git a/chat/build/mysql/init/chat.sql b/chat/build/mysql/init/chat.sql index 624a379a..ecbc5373 100644 --- a/chat/build/mysql/init/chat.sql +++ b/chat/build/mysql/init/chat.sql @@ -13,4 +13,4 @@ CREATE TABLE `chat` PRIMARY KEY (`id`), KEY `user_idx` (`user`,`agent_id`) USING BTREE, KEY `user_message_idx` (`user`,`message_id`) USING BTREE -) ENGINE=InnoDB COMMENT="聊天记录表" AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; \ No newline at end of file +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='聊天记录表'; diff --git a/chat/build/mysql/init/chat_config.sql b/chat/build/mysql/init/chat_config.sql index f11574e6..6955a42c 100644 --- a/chat/build/mysql/init/chat_config.sql +++ b/chat/build/mysql/init/chat_config.sql @@ -10,4 +10,4 @@ CREATE TABLE `chat_config` `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), KEY `user_idx` (`user`,`agent_id`) USING BTREE -) ENGINE=InnoDB COMMENT="聊天配置表" AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; \ No newline at end of file +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='聊天配置表'; diff --git a/chat/common/redis/redis.go b/chat/common/redis/redis.go index b482fe5a..4c18f03c 100644 --- a/chat/common/redis/redis.go +++ b/chat/common/redis/redis.go @@ -1,7 +1,10 @@ package redis import ( + "crypto/tls" "github.com/redis/go-redis/v9" + "net/url" + "strings" ) var Rdb *redis.Client @@ -22,12 +25,43 @@ const DifyCustomerConversationKey = "dify:conversation:%s:%s" // ImageTemporaryKey 图片临时存储 const ImageTemporaryKey = "chat:image:temporary:%d-%s" -func Init(Host, Pass string) { - Rdb = redis.NewClient(&redis.Options{ - Addr: Host, - Password: Pass, - DB: 1, - }) +func Init(connString string) { + options := &redis.Options{ + DB: 0, + } + + // 解析连接字符串 + if strings.HasPrefix(connString, "rediss://") || strings.HasPrefix(connString, "redis://") { + // 使用 url.Parse 解析整个 URL + parsedURL, err := url.Parse(connString) + if err != nil { + panic(err) + } + + // 设置地址(主机名+端口) + options.Addr = parsedURL.Host + + // 解析用户名和密码 + if parsedURL.User != nil { + options.Username = parsedURL.User.Username() + password, isSet := parsedURL.User.Password() + if isSet { + options.Password = password + } + } + + // 如果是 rediss 协议,启用 TLS + if parsedURL.Scheme == "rediss" { + options.TLSConfig = &tls.Config{ + MinVersion: tls.VersionTLS12, + } + } + } else { + // 如果没有协议前缀,假设是普通的 host:port + options.Addr = connString + } + + Rdb = redis.NewClient(options) } func Close() { diff --git a/chat/service/chat/api/chat.go b/chat/service/chat/api/chat.go index 5e2187e9..45471ee7 100644 --- a/chat/service/chat/api/chat.go +++ b/chat/service/chat/api/chat.go @@ -43,7 +43,7 @@ func main() { ) defer server.Stop() - redis.Init(c.RedisCache[0].Host, c.RedisCache[0].Pass) + redis.Init(c.RedisCache.RedisURL) defer redis.Close() ctx := svc.NewServiceContext(c) diff --git a/chat/service/chat/api/internal/config/config.go b/chat/service/chat/api/internal/config/config.go index 7cf6d34c..36f51022 100644 --- a/chat/service/chat/api/internal/config/config.go +++ b/chat/service/chat/api/internal/config/config.go @@ -1,7 +1,6 @@ package config import ( - "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/rest" ) @@ -12,7 +11,9 @@ type Config struct { DataSource string } - RedisCache cache.CacheConf + RedisCache struct { + RedisURL string + } SystemVersion string `json:",optional,default=v1.0.0-beat"` diff --git a/chat/service/chat/api/internal/logic/chatlogic.go b/chat/service/chat/api/internal/logic/chatlogic.go index 63080298..e89695db 100644 --- a/chat/service/chat/api/internal/logic/chatlogic.go +++ b/chat/service/chat/api/internal/logic/chatlogic.go @@ -66,7 +66,7 @@ func (l *ChatLogic) Chat(req *types.ChatReq) (resp *types.ChatReply, err error) // gemini client c := gemini.NewChatClient(l.svcCtx.Config.Gemini.Key).WithHost(l.svcCtx.Config.Gemini.Host). - WithTemperature(l.svcCtx.Config.Gemini.Temperature) + WithTemperature(l.svcCtx.Config.Gemini.Temperature).WithModel(l.svcCtx.Config.Gemini.Model) if l.svcCtx.Config.Gemini.EnableProxy { c = c.WithHttpProxy(l.svcCtx.Config.Proxy.Http).WithSocks5Proxy(l.svcCtx.Config.Proxy.Socket5). WithProxyUserName(l.svcCtx.Config.Proxy.Auth.Username). diff --git a/doc/config.md b/doc/config.md index 6edd6259..621fc4f3 100644 --- a/doc/config.md +++ b/doc/config.md @@ -7,9 +7,8 @@ Port: 8888 # 项目监听端口 Mysql: # mysql配置 DataSource: chat:123456@tcp(mysql57:3306)/chat?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai # 数据库连接地址 自建最好修改下密码 -RedisCache: # redis缓存配置 - - Host: redis7:6379 # redis缓存地址 - Pass: "123456" # redis缓存密码 自建最好修改下密码 +RedisCache: + RedisURL: rediss://default:password@aaa.com:12345 # redis连接地址 Auth: # jwt配置(可选)自建最好修改下 AccessSecret: "xxxxxxxxxxxxxxx" # jwt加密密钥(可选) 默认为 xxxxxxxxxxxx