parent
df52ddd35e
commit
8241bb72f7
@ -0,0 +1,20 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"goskeleton/app/global/consts"
|
||||
"goskeleton/app/service/ai_model_cli"
|
||||
"goskeleton/app/utils/response"
|
||||
)
|
||||
|
||||
type StyleGenerate struct {
|
||||
}
|
||||
|
||||
// ai生成样式
|
||||
func (s *StyleGenerate) StyleGenerate(c *gin.Context) {
|
||||
if res, err := ai_model_cli.RequestStyle(c); err==nil {
|
||||
response.Success(c, consts.CurdStatusOkMsg, res.(string))
|
||||
} else {
|
||||
response.Fail(c, consts.StyleGenerateFailCode, consts.StyleGenerateFailMsg, err)
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package ai_layout
|
||||
import(
|
||||
"github.com/gin-gonic/gin"
|
||||
// "goskeleton/app/utils/response"
|
||||
// "goskeleton/app/http/controller/web"
|
||||
// "goskeleton/app/http/validator/core/data_transfer"
|
||||
// "goskeleton/app/global/consts"
|
||||
)
|
||||
type LayoutGenerate struct {
|
||||
}
|
||||
func (s LayoutGenerate) CheckParams(context *gin.Context) {
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package ai_layout
|
||||
import(
|
||||
"github.com/gin-gonic/gin"
|
||||
"goskeleton/app/utils/response"
|
||||
"goskeleton/app/http/controller/web"
|
||||
"goskeleton/app/http/validator/core/data_transfer"
|
||||
"goskeleton/app/global/consts"
|
||||
)
|
||||
type ChatRecord struct {
|
||||
Role string `json:"role" binding:"required"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
}
|
||||
|
||||
type StyleGenerate struct {
|
||||
UserInput string `form:"user_input" json:"user_input" binding:"required"` // 必填
|
||||
ChatHistory []ChatRecord `form:"char_history" json:"chat_history"` // 非必填
|
||||
}
|
||||
|
||||
func (s StyleGenerate) CheckParams(context *gin.Context) {
|
||||
// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
|
||||
if err := context.ShouldBind(&s); err != nil {
|
||||
response.ValidatorError(context, err)
|
||||
return
|
||||
}
|
||||
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
|
||||
extraAddBindDataContext := data_transfer.DataAddContext(s, consts.ValidatorPrefix, context)
|
||||
if extraAddBindDataContext == nil {
|
||||
response.ErrorSystem(context, "StyleGenerate表单参数验证器json化失败", "")
|
||||
return
|
||||
}
|
||||
(&web.StyleGenerate{}).StyleGenerate(extraAddBindDataContext)
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package ai_model_cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"goskeleton/app/global/variable"
|
||||
"os"
|
||||
|
||||
"goskeleton/app/global/consts"
|
||||
|
||||
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RequestStyle(c *gin.Context) (interface{}, error) {
|
||||
|
||||
// userMsg := c.PostForm("user_input")
|
||||
userMsg:=c.GetString(consts.ValidatorPrefix+"user_input")
|
||||
|
||||
qianfan.GetConfig().AccessKey = variable.ConfigYml.GetString("BaiduCE.QianFanAccessKey")
|
||||
qianfan.GetConfig().SecretKey = variable.ConfigYml.GetString("BaiduCE.QianFanSecretKey")
|
||||
|
||||
chat := qianfan.NewChatCompletion(
|
||||
qianfan.WithModel("ERNIE-4.0-8K"),
|
||||
)
|
||||
|
||||
chatHistory := []qianfan.ChatCompletionMessage{}
|
||||
|
||||
// 读取prompt文件
|
||||
systemMsgPath := variable.ConfigYml.GetString("BaiduCE.StyleGeneratePromptPath")
|
||||
// 读取文件内容
|
||||
prompt, err := os.ReadFile(variable.BasePath+systemMsgPath)
|
||||
if err != nil || len(prompt) == 0 {
|
||||
variable.ZapLog.Error(fmt.Sprintf("读取提示词文件失败: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// add user history to chat history
|
||||
userHistory,exist := c.Get(consts.ValidatorPrefix+"chat_history")
|
||||
if exist&&userHistory!=nil{
|
||||
// TODO: check if userHistory is of type []struct{Role string;Content string}
|
||||
userHistory := userHistory.([]struct{Role string;Content string})
|
||||
if len(userHistory)%2!=0{
|
||||
variable.ZapLog.Error(fmt.Sprintf("用户历史对话格式错误: %v", userHistory))
|
||||
return nil, fmt.Errorf("用户历史对话格式错误")
|
||||
}
|
||||
for _,msg := range userHistory{
|
||||
chatHistory = append(chatHistory, qianfan.ChatCompletionMessage{Role:msg.Role,Content:msg.Content})
|
||||
}
|
||||
}
|
||||
|
||||
// add user input to chat history
|
||||
chatHistory = append(chatHistory, qianfan.ChatCompletionUserMessage(userMsg))
|
||||
|
||||
// define a stream chat client
|
||||
response,err:=chat.Do(context.TODO(),&qianfan.ChatCompletionRequest{System: string(prompt),Messages: chatHistory})
|
||||
if err != nil {
|
||||
variable.ZapLog.Error(fmt.Sprintf("对话失败: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response.Result, nil
|
||||
}
|
Loading…
Reference in new issue