refactor: refactor chat_handler, add support for Azure and ChatGLM

This commit is contained in:
RockYang
2023-09-04 06:43:15 +08:00
parent d06f94bddd
commit 0cc9cf8b45
34 changed files with 1212 additions and 513 deletions

View File

@@ -3,7 +3,7 @@ package model
// ApiKey OpenAI API 模型
type ApiKey struct {
BaseModel
UserId uint //用户ID系统添加的用户 ID 为 0
Platform string
Value string // API Key 的值
LastUsedAt int64 // 最后使用时间
}

View File

@@ -1,5 +1,7 @@
package model
import "gorm.io/gorm"
type HistoryMessage struct {
BaseModel
ChatId string // 会话 ID
@@ -10,6 +12,7 @@ type HistoryMessage struct {
Tokens int
Content string
UseContext bool // 是否可以作为聊天上下文
DeletedAt gorm.DeletedAt
}
func (HistoryMessage) TableName() string {

View File

@@ -1,10 +1,13 @@
package model
import "gorm.io/gorm"
type ChatItem struct {
BaseModel
ChatId string `gorm:"column:chat_id;unique"` // 会话 ID
UserId uint // 用户 ID
RoleId uint // 角色 ID
Model string // 会话模型
Title string // 会话标题
ChatId string `gorm:"column:chat_id;unique"` // 会话 ID
UserId uint // 用户 ID
RoleId uint // 角色 ID
ModelId uint // 会话模型
Title string // 会话标题
DeletedAt gorm.DeletedAt
}

View File

@@ -0,0 +1,10 @@
package model
type ChatModel struct {
BaseModel
Platform string
Name string
Value string // API Key 的值
SortNum int
Enabled bool
}

View File

@@ -2,13 +2,11 @@ package model
type User struct {
BaseModel
Username string `gorm:"index:username,unique"`
Mobile string
Password string
Nickname string
Avatar string
Salt string // 密码盐
Tokens int64 // 剩余tokens
TotalTokens int64 // 总消耗 tokens
Calls int // 剩余对话次数
ImgCalls int // 剩余绘图次数
ChatConfig string `gorm:"column:chat_config_json"` // 聊天配置 json

20
api/store/redis.go Normal file
View File

@@ -0,0 +1,20 @@
package store
import (
"chatplus/core/types"
"context"
"github.com/go-redis/redis/v8"
)
func NewRedisClient(config *types.AppConfig) (*redis.Client, error) {
client := redis.NewClient(&redis.Options{
Addr: config.Redis.Url(),
Password: config.Redis.Password,
DB: config.Redis.DB,
})
_, err := client.Ping(context.Background()).Result()
if err != nil {
return nil, err
}
return client, nil
}

View File

@@ -3,7 +3,7 @@ package vo
// ApiKey OpenAI API 模型
type ApiKey struct {
BaseVo
UserId uint `json:"user_id"` //用户ID系统添加的用户 ID 为 0
Platform string `json:"platform"`
Value string `json:"value"` // API Key 的值
LastUsedAt int64 `json:"last_used_at"` // 最后使用时间
}

View File

@@ -11,7 +11,3 @@ type HistoryMessage struct {
Content string `json:"content"`
UseContext bool `json:"use_context"`
}
func (HistoryMessage) TableName() string {
return "chatgpt_chat_history"
}

View File

@@ -2,10 +2,10 @@ package vo
type ChatItem struct {
BaseVo
UserId uint `json:"user_id"`
Icon string `json:"icon"`
RoleId uint `json:"role_id"`
ChatId string `json:"chat_id"`
Model string `json:"model"`
Title string `json:"title"`
UserId uint `json:"user_id"`
Icon string `json:"icon"`
RoleId uint `json:"role_id"`
ChatId string `json:"chat_id"`
ModelId uint `json:"model_id"`
Title string `json:"title"`
}

View File

@@ -0,0 +1,9 @@
package vo
type ChatModel struct {
BaseVo
Platform string `json:"platform"`
Name string `json:"name"`
Value string `json:"value"`
Enabled bool `json:"enabled"`
}

View File

@@ -4,13 +4,11 @@ import "chatplus/core/types"
type User struct {
BaseVo
Username string `json:"username"`
Mobile string `json:"mobile"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Salt string `json:"salt"` // 密码盐
Tokens int64 `json:"tokens"` // 剩余tokens
Calls int `json:"calls"` // 剩余对话次数
Salt string `json:"salt"` // 密码盐
TotalTokens int64 `json:"total_tokens"` // 总消耗tokens
Calls int `json:"calls"` // 剩余对话次数
ImgCalls int `json:"img_calls"`
ChatConfig types.ChatConfig `json:"chat_config"` // 聊天配置
ChatRoles []string `json:"chat_roles"` // 聊天角色集合