mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-08 18:23:45 +08:00
feat: refactor MidJourney service for conpatible drawing in chat and draw in app
This commit is contained in:
64
api/service/function/func_mj.go
Normal file
64
api/service/function/func_mj.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package function
|
||||
|
||||
import (
|
||||
"chatplus/service"
|
||||
"chatplus/utils"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// AI 绘画函数
|
||||
|
||||
type FuncMidJourney struct {
|
||||
name string
|
||||
service *service.MjService
|
||||
}
|
||||
|
||||
func NewMidJourneyFunc(mjService *service.MjService) FuncMidJourney {
|
||||
return FuncMidJourney{
|
||||
name: "MidJourney AI 绘画",
|
||||
service: mjService}
|
||||
}
|
||||
|
||||
func (f FuncMidJourney) Invoke(params map[string]interface{}) (string, error) {
|
||||
logger.Infof("MJ 绘画参数:%+v", params)
|
||||
prompt := utils.InterfaceToString(params["prompt"])
|
||||
if !utils.IsEmptyValue(params["--ar"]) {
|
||||
prompt = fmt.Sprintf("%s --ar %s", prompt, params["--ar"])
|
||||
delete(params, "--ar")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--s"]) {
|
||||
prompt = fmt.Sprintf("%s --s %s", prompt, params["--s"])
|
||||
delete(params, "--s")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--seed"]) {
|
||||
prompt = fmt.Sprintf("%s --seed %s", prompt, params["--seed"])
|
||||
delete(params, "--seed")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--no"]) {
|
||||
prompt = fmt.Sprintf("%s --no %s", prompt, params["--no"])
|
||||
delete(params, "--no")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--niji"]) {
|
||||
prompt = fmt.Sprintf("%s --niji %s", prompt, params["--niji"])
|
||||
delete(params, "--niji")
|
||||
} else {
|
||||
prompt = prompt + " --v 5.2"
|
||||
}
|
||||
|
||||
f.service.PushTask(service.MjTask{
|
||||
Id: utils.InterfaceToString(params["session_id"]),
|
||||
Src: service.TaskSrcChat,
|
||||
Prompt: prompt,
|
||||
UserId: utils.IntValue(utils.InterfaceToString(params["user_id"]), 0),
|
||||
RoleId: utils.IntValue(utils.InterfaceToString(params["role_id"]), 0),
|
||||
Icon: utils.InterfaceToString(params["icon"]),
|
||||
ChatId: utils.InterfaceToString(params["chat_id"]),
|
||||
})
|
||||
return prompt, nil
|
||||
}
|
||||
|
||||
func (f FuncMidJourney) Name() string {
|
||||
return f.name
|
||||
}
|
||||
|
||||
var _ Function = &FuncMidJourney{}
|
||||
@@ -1,129 +0,0 @@
|
||||
package function
|
||||
|
||||
import (
|
||||
"chatplus/core/types"
|
||||
"chatplus/utils"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/imroc/req/v3"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AI 绘画函数
|
||||
|
||||
type FuncMidJourney struct {
|
||||
name string
|
||||
config types.ChatPlusExtConfig
|
||||
client *req.Client
|
||||
}
|
||||
|
||||
func NewMidJourneyFunc(config types.ChatPlusExtConfig) FuncMidJourney {
|
||||
return FuncMidJourney{
|
||||
name: "MidJourney AI 绘画",
|
||||
config: config,
|
||||
client: req.C().SetTimeout(30 * time.Second)}
|
||||
}
|
||||
|
||||
func (f FuncMidJourney) Invoke(params map[string]interface{}) (string, error) {
|
||||
if f.config.Token == "" {
|
||||
return "", errors.New("无效的 API Token")
|
||||
}
|
||||
|
||||
logger.Infof("MJ 绘画参数:%+v", params)
|
||||
prompt := utils.InterfaceToString(params["prompt"])
|
||||
if !utils.IsEmptyValue(params["--ar"]) {
|
||||
prompt = fmt.Sprintf("%s --ar %s", prompt, params["--ar"])
|
||||
delete(params, "--ar")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--s"]) {
|
||||
prompt = fmt.Sprintf("%s --s %s", prompt, params["--s"])
|
||||
delete(params, "--s")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--seed"]) {
|
||||
prompt = fmt.Sprintf("%s --seed %s", prompt, params["--seed"])
|
||||
delete(params, "--seed")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--no"]) {
|
||||
prompt = fmt.Sprintf("%s --no %s", prompt, params["--no"])
|
||||
delete(params, "--no")
|
||||
}
|
||||
if !utils.IsEmptyValue(params["--niji"]) {
|
||||
prompt = fmt.Sprintf("%s --niji %s", prompt, params["--niji"])
|
||||
delete(params, "--niji")
|
||||
} else {
|
||||
prompt = prompt + " --v 5.2"
|
||||
}
|
||||
params["prompt"] = prompt
|
||||
url := fmt.Sprintf("%s/api/mj/image", f.config.ApiURL)
|
||||
var res types.BizVo
|
||||
r, err := f.client.R().
|
||||
SetHeader("Authorization", f.config.Token).
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetBody(params).
|
||||
SetSuccessResult(&res).Post(url)
|
||||
if err != nil || r.IsErrorState() {
|
||||
return "", fmt.Errorf("%v%v", r.String(), err)
|
||||
}
|
||||
|
||||
if res.Code != types.Success {
|
||||
return "", errors.New(res.Message)
|
||||
}
|
||||
|
||||
return prompt, nil
|
||||
}
|
||||
|
||||
type MjUpscaleReq struct {
|
||||
Index int32 `json:"index"`
|
||||
MessageId string `json:"message_id"`
|
||||
MessageHash string `json:"message_hash"`
|
||||
}
|
||||
|
||||
func (f FuncMidJourney) Upscale(upReq MjUpscaleReq) error {
|
||||
url := fmt.Sprintf("%s/api/mj/upscale", f.config.ApiURL)
|
||||
var res types.BizVo
|
||||
r, err := f.client.R().
|
||||
SetHeader("Authorization", f.config.Token).
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetBody(upReq).
|
||||
SetSuccessResult(&res).Post(url)
|
||||
if err != nil || r.IsErrorState() {
|
||||
return fmt.Errorf("%v%v", r.String(), err)
|
||||
}
|
||||
|
||||
if res.Code != types.Success {
|
||||
return errors.New(res.Message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type MjVariationReq struct {
|
||||
Index int32 `json:"index"`
|
||||
MessageId string `json:"message_id"`
|
||||
MessageHash string `json:"message_hash"`
|
||||
}
|
||||
|
||||
func (f FuncMidJourney) Variation(upReq MjVariationReq) error {
|
||||
url := fmt.Sprintf("%s/api/mj/variation", f.config.ApiURL)
|
||||
var res types.BizVo
|
||||
r, err := f.client.R().
|
||||
SetHeader("Authorization", f.config.Token).
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetBody(upReq).
|
||||
SetSuccessResult(&res).Post(url)
|
||||
if err != nil || r.IsErrorState() {
|
||||
return fmt.Errorf("%v%v", r.String(), err)
|
||||
}
|
||||
|
||||
if res.Code != types.Success {
|
||||
return errors.New(res.Message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FuncMidJourney) Name() string {
|
||||
return f.name
|
||||
}
|
||||
|
||||
var _ Function = &FuncMidJourney{}
|
||||
Reference in New Issue
Block a user