feat: add midjouney api implements, optimize function calls

This commit is contained in:
RockYang
2023-08-11 18:46:56 +08:00
parent b0e02b43fc
commit 2165ba3406
17 changed files with 298 additions and 183 deletions

View File

@@ -34,13 +34,10 @@ type AppServer struct {
ChatClients *types.LMap[string, *types.WsClient] // map[sessionId]Websocket 连接集合
ReqCancelFunc *types.LMap[string, context.CancelFunc] // HttpClient 请求取消 handle function
Functions map[string]function.Function
MjTasks *types.LMap[string, types.MjTask]
}
func NewServer(
appConfig *types.AppConfig,
funZaoBao function.FuncZaoBao,
funZhiHu function.FuncHeadlines,
funWeibo function.FuncWeiboHot) *AppServer {
func NewServer(appConfig *types.AppConfig, functions map[string]function.Function) *AppServer {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard
return &AppServer{
@@ -51,11 +48,8 @@ func NewServer(
ChatSession: types.NewLMap[string, types.ChatSession](),
ChatClients: types.NewLMap[string, *types.WsClient](),
ReqCancelFunc: types.NewLMap[string, context.CancelFunc](),
Functions: map[string]function.Function{
types.FuncZaoBao: funZaoBao,
types.FuncWeibo: funWeibo,
types.FuncHeadLine: funZhiHu,
},
MjTasks: types.NewLMap[string, types.MjTask](),
Functions: functions,
}
}
@@ -186,7 +180,8 @@ func authorizeMiddleware(s *AppServer) gin.HandlerFunc {
if c.Request.URL.Path == "/api/user/login" ||
c.Request.URL.Path == "/api/admin/login" ||
c.Request.URL.Path == "/api/user/register" ||
c.Request.URL.Path == "/api/reward/push" ||
c.Request.URL.Path == "/api/reward/notify" ||
c.Request.URL.Path == "/api/mj/notify" ||
strings.HasPrefix(c.Request.URL.Path, "/api/sms/") ||
strings.HasPrefix(c.Request.URL.Path, "/api/captcha/") ||
strings.HasPrefix(c.Request.URL.Path, "/static/") ||

View File

@@ -33,8 +33,8 @@ func NewDefaultConfig() *types.AppConfig {
HttpOnly: false,
SameSite: http.SameSiteLaxMode,
},
ApiConfig: types.ChatPlusApiConfig{},
ChatPlusExtApiToken: utils.RandString(32),
ApiConfig: types.ChatPlusApiConfig{},
ExtConfig: types.ChatPlusExtConfig{Token: utils.RandString(32)},
}
}

View File

@@ -42,6 +42,16 @@ type ChatSession struct {
Model string `json:"model"` // GPT 模型
}
type MjTask struct {
Client Client
ChatId string
MessageId string
MessageHash string
UserId uint
RoleId uint
Icon string
}
type ApiError struct {
Error struct {
Message string

View File

@@ -6,19 +6,19 @@ import (
)
type AppConfig struct {
Path string `toml:"-"`
Listen string
Session Session
ProxyURL string
MysqlDns string // mysql 连接地址
Manager Manager // 后台管理员账户信息
StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL
Redis RedisConfig // redis 连接信息
ApiConfig ChatPlusApiConfig // ChatPlus API authorization configs
AesEncryptKey string
SmsConfig AliYunSmsConfig // AliYun send message service config
ChatPlusExtApiToken string // chatgpt-plus-exts callback api token
Path string `toml:"-"`
Listen string
Session Session
ProxyURL string
MysqlDns string // mysql 连接地址
Manager Manager // 后台管理员账户信息
StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL
Redis RedisConfig // redis 连接信息
ApiConfig ChatPlusApiConfig // ChatPlus API authorization configs
AesEncryptKey string
SmsConfig AliYunSmsConfig // AliYun send message service config
ExtConfig ChatPlusExtConfig // ChatPlus extensions callback api config
}
type ChatPlusApiConfig struct {
@@ -27,6 +27,11 @@ type ChatPlusApiConfig struct {
Token string
}
type ChatPlusExtConfig struct {
ApiURL string
Token string
}
type AliYunSmsConfig struct {
AccessKey string
AccessSecret string

View File

@@ -23,9 +23,10 @@ type Property struct {
}
const (
FuncZaoBao = "zao_bao" // 每日早报
FuncHeadLine = "headline" // 今日头条
FuncWeibo = "weibo_hot" // 微博热搜
FuncZaoBao = "zao_bao" // 每日早报
FuncHeadLine = "headline" // 今日头条
FuncWeibo = "weibo_hot" // 微博热搜
FuncMidJourney = "mid_journey" // MJ 绘画
)
var InnerFunctions = []Function{
@@ -73,4 +74,23 @@ var InnerFunctions = []Function{
Required: []string{},
},
},
{
Name: FuncMidJourney,
Description: "AI 绘画工具,使用 MJ MidJourney API 进行 AI 绘画",
Parameters: Parameters{
Type: "object",
Properties: map[string]Property{
"prompt": {
Type: "string",
Description: "绘画内容描述,提示词,此参数需要翻译成英文",
},
"ar": {
Type: "string",
Description: "图片长宽比,如 16:9",
},
},
Required: []string{},
},
},
}

View File

@@ -9,7 +9,7 @@ type MKey interface {
string | int
}
type MValue interface {
*WsClient | ChatSession | context.CancelFunc | []interface{}
*WsClient | ChatSession | context.CancelFunc | []interface{} | MjTask
}
type LMap[K MKey, T MValue] struct {
lock sync.RWMutex