Skip to content

redis配置改成连接字符串,修复gemini配置Model不生效问题 #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions chat/build/mysql/init/bots.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

4 changes: 2 additions & 2 deletions chat/build/mysql/init/bots_prompt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
) 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;
2 changes: 1 addition & 1 deletion chat/build/mysql/init/chat.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='聊天记录表';
2 changes: 1 addition & 1 deletion chat/build/mysql/init/chat_config.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='聊天配置表';
46 changes: 40 additions & 6 deletions chat/common/redis/redis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package redis

import (
"crypto/tls"
"github.com/redis/go-redis/v9"
"net/url"
"strings"
)

var Rdb *redis.Client
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion chat/service/chat/api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions chat/service/chat/api/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/rest"
)

Expand All @@ -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"`

Expand Down
2 changes: 1 addition & 1 deletion chat/service/chat/api/internal/logic/chatlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
5 changes: 2 additions & 3 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:12345 # redis连接地址

Auth: # jwt配置(可选)自建最好修改下
AccessSecret: "xxxxxxxxxxxxxxx" # jwt加密密钥(可选) 默认为 xxxxxxxxxxxx
Expand Down