mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-19 01:36:38 +08:00
feat: add prompt translate handler
This commit is contained in:
parent
91ed41b536
commit
5e1fe88b8b
88
api/handler/prompt_handler.go
Normal file
88
api/handler/prompt_handler.go
Normal file
@ -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)
|
||||||
|
}
|
@ -17,7 +17,6 @@ import (
|
|||||||
"chatplus/store"
|
"chatplus/store"
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
"github.com/go-redis/redis/v8"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -26,6 +25,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
|
||||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@ -356,6 +357,12 @@ func main() {
|
|||||||
group.GET("hits", h.Hits)
|
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.Provide(handler.NewTestHandler),
|
||||||
fx.Invoke(func(s *core.AppServer, h *handler.TestHandler) {
|
fx.Invoke(func(s *core.AppServer, h *handler.TestHandler) {
|
||||||
group := s.Engine.Group("/test/")
|
group := s.Engine.Group("/test/")
|
||||||
|
@ -105,12 +105,12 @@
|
|||||||
:value="item.id"
|
:value="item.id"
|
||||||
/>
|
/>
|
||||||
</el-select>
|
</el-select>
|
||||||
<!-- <el-button type="primary" @click="newChat">-->
|
<el-button type="primary" @click="newChat">
|
||||||
<!-- <el-icon>-->
|
<el-icon>
|
||||||
<!-- <Plus/>-->
|
<Plus/>
|
||||||
<!-- </el-icon>-->
|
</el-icon>
|
||||||
<!-- 新建会话-->
|
新建对话
|
||||||
<!-- </el-button>-->
|
</el-button>
|
||||||
|
|
||||||
<el-button type="success" @click="exportChat" plain>
|
<el-button type="success" @click="exportChat" plain>
|
||||||
<i class="iconfont icon-export"></i>
|
<i class="iconfont icon-export"></i>
|
||||||
@ -237,7 +237,7 @@ import {
|
|||||||
Check,
|
Check,
|
||||||
Close,
|
Close,
|
||||||
Delete,
|
Delete,
|
||||||
Edit,
|
Edit, Plus,
|
||||||
Promotion,
|
Promotion,
|
||||||
RefreshRight,
|
RefreshRight,
|
||||||
Search,
|
Search,
|
||||||
|
@ -243,7 +243,7 @@
|
|||||||
</el-icon>
|
</el-icon>
|
||||||
</el-tooltip>
|
</el-tooltip>
|
||||||
</div>
|
</div>
|
||||||
<el-button type="success">
|
<el-button type="success" @click="translatePrompt">
|
||||||
<el-icon style="margin-right: 6px;font-size: 18px;">
|
<el-icon style="margin-right: 6px;font-size: 18px;">
|
||||||
<Refresh/>
|
<Refresh/>
|
||||||
</el-icon>
|
</el-icon>
|
||||||
@ -268,12 +268,12 @@
|
|||||||
</el-icon>
|
</el-icon>
|
||||||
</el-tooltip>
|
</el-tooltip>
|
||||||
</div>
|
</div>
|
||||||
<el-button type="success">
|
<!-- <el-button type="success">-->
|
||||||
<el-icon style="margin-right: 6px;font-size: 18px;">
|
<!-- <el-icon style="margin-right: 6px;font-size: 18px;">-->
|
||||||
<Refresh/>
|
<!-- <Refresh/>-->
|
||||||
</el-icon>
|
<!-- </el-icon>-->
|
||||||
翻译
|
<!-- 翻译-->
|
||||||
</el-button>
|
<!-- </el-button>-->
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -286,7 +286,7 @@
|
|||||||
<div class="submit-btn">
|
<div class="submit-btn">
|
||||||
<el-button color="#47fff1" :dark="false" @click="generate" round>立即生成</el-button>
|
<el-button color="#47fff1" :dark="false" @click="generate" round>立即生成</el-button>
|
||||||
<div class="text-info">
|
<div class="text-info">
|
||||||
<el-tag type="success">可用额度:{{ imgCalls }}</el-tag>
|
<el-tag type="success">绘图可用额度:{{ imgCalls }}</el-tag>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</el-form>
|
</el-form>
|
||||||
@ -336,7 +336,7 @@
|
|||||||
|
|
||||||
<h2>创作记录</h2>
|
<h2>创作记录</h2>
|
||||||
<div class="finish-job-list">
|
<div class="finish-job-list">
|
||||||
<ItemList :items="finishedJobs" v-if="finishedJobs.length > 0" width="240" :gap="16">
|
<ItemList :items="finishedJobs" v-if="finishedJobs.length > 0" :width="240" :gap="16">
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
<div class="job-item">
|
<div class="job-item">
|
||||||
<el-image
|
<el-image
|
||||||
@ -487,7 +487,7 @@ const params = ref({
|
|||||||
img: "",
|
img: "",
|
||||||
weight: 0.25,
|
weight: 0.25,
|
||||||
prompt: "",
|
prompt: "",
|
||||||
neg_prompt: "",
|
neg_prompt: "nsfw, paintings, cartoon, anime, sketches, low quality,easynegative,ng_deepnegative _v1 75t,(worst quality:2),(low quality:2),(normalquality:2),lowres,bad anatomy,bad hands,normal quality,((monochrome)),((grayscale)),((watermark))",
|
||||||
tile: false,
|
tile: false,
|
||||||
quality: 0.5
|
quality: 0.5
|
||||||
})
|
})
|
||||||
@ -568,6 +568,14 @@ const connect = () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const translatePrompt = () => {
|
||||||
|
httpPost("/api/prompt/translate",{"prompt":params.value.prompt}).then(res => {
|
||||||
|
params.value.prompt = res.data
|
||||||
|
}).then(e => {
|
||||||
|
ElMessage.error("翻译失败:"+e.message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
checkSession().then(user => {
|
checkSession().then(user => {
|
||||||
imgCalls.value = user['img_calls']
|
imgCalls.value = user['img_calls']
|
||||||
|
@ -502,14 +502,14 @@ const samplers = ["Euler a", "Euler", "DPM2 a Karras", "DPM++ 2S a Karras", "DPM
|
|||||||
"LMS Karras", "DPM2 Karras", "DDIM", "PLMS", "UniPC", "LMS", "Heun",]
|
"LMS Karras", "DPM2 Karras", "DDIM", "PLMS", "UniPC", "LMS", "Heun",]
|
||||||
const scaleAlg = ["Latent", "ESRGAN_4x", "R-ESRGAN 4x+", "SwinIR_4x", "LDSR"]
|
const scaleAlg = ["Latent", "ESRGAN_4x", "R-ESRGAN 4x+", "SwinIR_4x", "LDSR"]
|
||||||
const params = ref({
|
const params = ref({
|
||||||
width: 512,
|
width: 1024,
|
||||||
height: 512,
|
height: 1024,
|
||||||
sampler: samplers[0],
|
sampler: samplers[0],
|
||||||
seed: -1,
|
seed: -1,
|
||||||
steps: 30,
|
steps: 30,
|
||||||
cfg_scale: 7,
|
cfg_scale: 7,
|
||||||
face_fix: false,
|
face_fix: false,
|
||||||
hd_fix: true,
|
hd_fix: false,
|
||||||
hd_redraw_rate: 0.7,
|
hd_redraw_rate: 0.7,
|
||||||
hd_scale: 2,
|
hd_scale: 2,
|
||||||
hd_scale_alg: scaleAlg[0],
|
hd_scale_alg: scaleAlg[0],
|
||||||
|
Loading…
Reference in New Issue
Block a user