diff --git a/api/handler/prompt_handler.go b/api/handler/prompt_handler.go new file mode 100644 index 00000000..2bf59f00 --- /dev/null +++ b/api/handler/prompt_handler.go @@ -0,0 +1,88 @@ +package handler + +import ( + "chatplus/core/types" + "chatplus/store/model" + "chatplus/utils/resp" + "fmt" + + "github.com/imroc/req/v3" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +const translatePromptTemplate = "Please rewrite the following text into AI painting prompt words, and please try to add detailed description of the picture, painting style, scene, rendering effect, picture light and other elements. Please output directly in English without any explanation, within 150 words. The text to be rewritten is: [%s]" + +type PromptHandler struct { + BaseHandler + db *gorm.DB +} + +func NewPromptHandler(db *gorm.DB) *PromptHandler { + return &PromptHandler{db: db} +} + +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 (h *PromptHandler) Translate(c *gin.Context) { + var data struct { + Prompt string `json:"prompt"` + } + if err := c.ShouldBindJSON(&data); err != nil { + resp.ERROR(c, types.InvalidArgs) + return + } + // 获取 OpenAI 的 API KEY + var apiKey model.ApiKey + res := h.db.Where("platform = ?", types.OpenAI).First(&apiKey) + if res.Error != nil { + resp.ERROR(c, "找不到可用 OpenAI API KEY") + return + } + + messages := make([]interface{}, 1) + messages[0] = types.Message{ + Role: "user", + Content: fmt.Sprintf(translatePromptTemplate, data.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() { + resp.ERROR(c, fmt.Sprintf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message)) + return + } + + resp.SUCCESS(c, response.Choices[0].Message.Content) +} diff --git a/api/main.go b/api/main.go index 40116c1b..303a9a71 100644 --- a/api/main.go +++ b/api/main.go @@ -17,7 +17,6 @@ import ( "chatplus/store" "context" "embed" - "github.com/go-redis/redis/v8" "io" "log" "os" @@ -26,6 +25,8 @@ import ( "syscall" "time" + "github.com/go-redis/redis/v8" + "github.com/lionsoul2014/ip2region/binding/golang/xdb" "go.uber.org/fx" "gorm.io/gorm" @@ -356,6 +357,12 @@ func main() { group.GET("hits", h.Hits) }), + fx.Provide(handler.NewPromptHandler), + fx.Invoke(func(s *core.AppServer, h *handler.PromptHandler) { + group := s.Engine.Group("/api/prompt/") + group.POST("translate", h.Translate) + }), + fx.Provide(handler.NewTestHandler), fx.Invoke(func(s *core.AppServer, h *handler.TestHandler) { group := s.Engine.Group("/test/") diff --git a/web/src/views/ChatPlus.vue b/web/src/views/ChatPlus.vue index 22159e19..d4eb0068 100644 --- a/web/src/views/ChatPlus.vue +++ b/web/src/views/ChatPlus.vue @@ -105,12 +105,12 @@ :value="item.id" /> - - - - - - + + + + + 新建对话 + @@ -237,7 +237,7 @@ import { Check, Close, Delete, - Edit, + Edit, Plus, Promotion, RefreshRight, Search, diff --git a/web/src/views/ImageMj.vue b/web/src/views/ImageMj.vue index b289c52d..1c0c0517 100644 --- a/web/src/views/ImageMj.vue +++ b/web/src/views/ImageMj.vue @@ -243,7 +243,7 @@ - + @@ -268,12 +268,12 @@ - - - - - 翻译 - + + + + + + @@ -286,7 +286,7 @@
立即生成
- 可用额度:{{ imgCalls }} + 绘图可用额度:{{ imgCalls }}
@@ -336,7 +336,7 @@

创作记录

- +