feat: auto translate image creating prompt

This commit is contained in:
RockYang
2023-12-19 18:54:19 +08:00
parent cae5c049e4
commit 6e58ddf681
11 changed files with 446 additions and 99 deletions

View File

@@ -83,7 +83,7 @@ var InnerFunctions = []Function{
Properties: map[string]Property{
"prompt": {
Type: "string",
Description: "提示词,如果该参数中有中文的话,则需要翻译成英文。",
Description: "提示词,请自动将该参数翻译成英文。",
},
},
Required: []string{},

View File

@@ -132,11 +132,13 @@ func (h *ChatHandler) sendOpenAiMessage(
utils.ReplyMessage(ws, ErrImg)
} else {
f := h.App.Functions[functionName]
// translate prompt
if functionName == types.FuncImage {
params["user_id"] = userVo.Id
params["role_id"] = role.Id
params["chat_id"] = session.ChatId
params["session_id"] = session.SessionId
const translatePromptTemplate = "Translate the following painting prompt words into English keyword phrases. Without any explanation, directly output the keyword phrases separated by commas. The content to be translated is: [%s]"
r, err := utils.OpenAIRequest(fmt.Sprintf(translatePromptTemplate, params["prompt"]), apiKey, h.App.Config.ProxyURL, chatConfig.OpenAI.ApiURL)
if err == nil {
params["prompt"] = r
}
}
data, err := f.Invoke(params)
if err != nil {

View File

@@ -4,11 +4,10 @@ import (
"chatplus/core"
"chatplus/core/types"
"chatplus/store/model"
"chatplus/utils"
"chatplus/utils/resp"
"fmt"
"github.com/imroc/req/v3"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
@@ -27,27 +26,6 @@ func NewPromptHandler(app *core.AppServer, db *gorm.DB) *PromptHandler {
return h
}
type apiRes struct {
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
}
type apiErrRes struct {
Error struct {
Code interface{} `json:"code"`
Message string `json:"message"`
Param interface{} `json:"param"`
Type string `json:"type"`
} `json:"error"`
}
// Rewrite translate and rewrite prompt with ChatGPT
func (h *PromptHandler) Rewrite(c *gin.Context) {
var data struct {
@@ -93,28 +71,5 @@ func (h *PromptHandler) request(prompt string, promptTemplate string) (string, e
return "", fmt.Errorf("error with fetch OpenAI API KEY%v", res.Error)
}
messages := make([]interface{}, 1)
messages[0] = types.Message{
Role: "user",
Content: fmt.Sprintf(promptTemplate, prompt),
}
var response apiRes
var errRes apiErrRes
r, err := req.C().SetProxyURL(h.App.Config.ProxyURL).R().SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+apiKey.Value).
SetBody(types.ApiRequest{
Model: "gpt-3.5-turbo",
Temperature: 0.9,
MaxTokens: 1024,
Stream: false,
Messages: messages,
}).
SetErrorResult(&errRes).
SetSuccessResult(&response).Post(h.App.ChatConfig.OpenAI.ApiURL)
if err != nil || r.IsErrorState() {
return "", fmt.Errorf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message)
}
return response.Choices[0].Message.Content, nil
return utils.OpenAIRequest(fmt.Sprintf(promptTemplate, prompt), apiKey.Value, h.App.Config.ProxyURL, h.App.ChatConfig.OpenAI.ApiURL)
}

View File

@@ -105,7 +105,7 @@ func (f FuncImage) Invoke(params map[string]interface{}) (string, error) {
return "", fmt.Errorf("下载图片失败: %s", err.Error())
}
logger.Info(imgURL)
//logger.Info(imgURL)
return fmt.Sprintf("\n\n![](%s)\n", imgURL), nil
}

View File

@@ -0,0 +1,11 @@
package model
type Function struct {
Id uint `gorm:"primarykey;column:id"`
Name string
Description string
Parameters string
Required string
Action string
Enabled bool
}

22
api/store/vo/function.go Normal file
View File

@@ -0,0 +1,22 @@
package vo
type Parameters struct {
Type string `json:"type"`
Required []string `json:"required"`
Properties map[string]Property `json:"properties"`
}
type Property struct {
Type string `json:"type"`
Description string `json:"description"`
}
type Function struct {
Id uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Parameters Parameters `json:"parameters"`
Required []string `json:"required"`
Action string `json:"action"`
Enabled bool `json:"enabled"`
}

View File

@@ -4,6 +4,8 @@ import (
"chatplus/core/types"
logger2 "chatplus/logger"
"encoding/json"
"fmt"
"github.com/imroc/req/v3"
"io"
"net/http"
"net/url"
@@ -61,3 +63,51 @@ func DownloadImage(imageURL string, proxy string) ([]byte, error) {
return imageBytes, nil
}
type apiRes struct {
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
}
type apiErrRes struct {
Error struct {
Code interface{} `json:"code"`
Message string `json:"message"`
Param interface{} `json:"param"`
Type string `json:"type"`
} `json:"error"`
}
func OpenAIRequest(prompt string, apiKey string, proxy string, apiURL string) (string, error) {
messages := make([]interface{}, 1)
messages[0] = types.Message{
Role: "user",
Content: prompt,
}
var response apiRes
var errRes apiErrRes
r, err := req.C().SetProxyURL(proxy).R().SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+apiKey).
SetBody(types.ApiRequest{
Model: "gpt-3.5-turbo",
Temperature: 0.9,
MaxTokens: 1024,
Stream: false,
Messages: messages,
}).
SetErrorResult(&errRes).
SetSuccessResult(&response).Post(apiURL)
if err != nil || r.IsErrorState() {
return "", fmt.Errorf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message)
}
return response.Choices[0].Message.Content, nil
}