增加文档优化相关功能后端接口

master
linlnf 4 months ago
parent 6507272923
commit 3f57e45619

@ -220,4 +220,23 @@ voc|form-data|string|必填|"" (示例内容在 storage/app/test/A13_221.txt 中
},
"msg": "Success"
}
```
#### 请求文本优化
> <font color=#FF4500>*post*/admin/ai_doc/doc_refine</font>
参数字段|参数属性|类型|选项|默认值
---|---|---|---|---
doc|form-data|string|必填|""
background|form-data|string||""
type|form-data|string|必填|"abstract"/"decorate"/"sequel_writing"/"rewrite_wrong"/"translate"
> 返回示例:
```json
{
"code": 200,
"data": {
"new_doc": "Whether it's a business report, an academic paper, or a creative piece of writing, this module can polish the language, correct grammar and spelling errors, and optimize the overall structure."
},
"msg": "Success"
}
```

@ -83,4 +83,6 @@ const (
PicRecognitionFailCode int = -400450
VocRecognitionFailMsg string = "语音识别失败"
VocRecognitionFailCode int = -400451
DocRefineFailMsg string = "文档优化失败"
DocRefineFailCode int = -400452
)

@ -0,0 +1,22 @@
package web
import (
"goskeleton/app/global/consts"
"goskeleton/app/service/ai_model_cli"
"goskeleton/app/utils/response"
"github.com/gin-gonic/gin"
)
type DocRefine struct {
}
// Ai 模型进行文档优化
func (u *DocRefine) DocRefine(context *gin.Context) {
if r, recogWords := ai_model_cli.RequestQianFan(context); r {
response.Success(context, consts.CurdStatusOkMsg, recogWords)
} else {
response.Fail(context, consts.DocRefineFailCode, consts.DocRefineFailMsg, "")
}
}

@ -5,6 +5,7 @@ import (
"goskeleton/app/global/consts"
"goskeleton/app/http/validator/common/upload_files"
"goskeleton/app/http/validator/common/websocket"
"goskeleton/app/http/validator/web/ai_doc.go"
"goskeleton/app/http/validator/web/ai_recognition"
"goskeleton/app/http/validator/web/users"
)
@ -53,4 +54,7 @@ func WebRegisterValidator() {
containers.Set(key, ai_recognition.PicRecognition{})
key = consts.ValidatorPrefix + "VocRecognition"
containers.Set(key, ai_recognition.VocRecognition{})
// ai文档优化
key = consts.ValidatorPrefix + "DocRefine"
containers.Set(key, ai_doc.DocRefine{})
}

@ -0,0 +1,47 @@
package ai_doc
import (
"goskeleton/app/global/consts"
"goskeleton/app/http/controller/web"
"goskeleton/app/http/validator/core/data_transfer"
"goskeleton/app/utils/response"
"strings"
"github.com/gin-gonic/gin"
)
type DocRefine struct {
Type string `form:"type" json:"type" binding:"required"` // 必填、
Doc string `form:"doc" json:"doc" binding:"required"` // 必填、对于文本,表示它的长度>=1
Background string `form:"background" json:"background" `
}
var Types = []string{"abstract", "decorate", "sequel_writing", "rewrite_wrong", "translate"}
func (d DocRefine) CheckParams(context *gin.Context) {
if err := context.ShouldBind(&d); err != nil {
// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
response.ValidatorError(context, err)
return
}
// 判断是否在类型中
t := d.Type
isExit := false
for _, e := range Types {
if strings.TrimSpace(t) == e {
isExit = true
break
}
}
if !isExit {
response.ErrorSystem(context, "DocRefine表单参数验证器json化失败", "")
return
}
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
extraAddBindDataContext := data_transfer.DataAddContext(d, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "DocRefine表单参数验证器json化失败", "")
return
}
(&web.DocRefine{}).DocRefine(extraAddBindDataContext)
}

@ -0,0 +1,50 @@
package ai_model_cli
import (
"context"
"fmt"
"goskeleton/app/global/variable"
"strings"
"github.com/baidubce/bce-qianfan-sdk/go/qianfan"
"github.com/gin-gonic/gin"
)
func RequestQianFan(cont *gin.Context) (r bool, c interface{}) {
// 使用安全认证AK/SK鉴权通过环境变量初始化
qianfan.GetConfig().AccessKey = variable.ConfigYml.GetString("BaiduCE.QianFanAccessKey")
qianfan.GetConfig().SecretKey = variable.ConfigYml.GetString("BaiduCE.QianFanSecretKey")
// 指定特定模型
chat := qianfan.NewChatCompletion(
qianfan.WithModel("ERNIE-3.5-8K-0701"),
)
message := ""
t := strings.TrimSpace(cont.PostForm("type"))
b := strings.TrimSpace(cont.PostForm("background"))
d := strings.TrimSpace(cont.PostForm("doc"))
switch t {
case "abstract":
message = "请在这个背景下:" + b + "\n对下文进行概括不要改变原有语言" + d
case "decorate":
message = "请在这个背景下:" + b + "\n对下文进行润色不要改变原有语言" + d
case "sequel_writing":
message = "请在这个背景下:" + b + "\n对下文进行续写不要改变原有语言" + d
case "rewrite_wrong":
message = "请在这个背景下:" + b + "\n对下文进行改错不要改变原有语言" + d
case "translate":
message = "请在这个背景下:" + b + "\n对下文进行翻译" + d
}
resp, _ := chat.Do(
context.TODO(),
&qianfan.ChatCompletionRequest{
Messages: []qianfan.ChatCompletionMessage{
qianfan.ChatCompletionUserMessage(message),
},
},
)
fmt.Println(resp.Result)
return true, gin.H{"new_doc": resp.Result}
}

@ -148,3 +148,5 @@ Captcha:
BaiduCE:
ApiKey: "AR1SUIjaKSsCcDjj11QzHDOc" # 生成鉴权签名时使用的 API_KEY
SecretKey: "zvEb5CzpuGCZNdQC1TPmDh3IOWn5aWDT" # 生成鉴权签名时使用的 SECRET_KEY
QianFanAccessKey: "ALTAKOxb5YvHncyFr7Qbuv1cK0" # 访问千帆sdk 时用的 AccessKey
QianFanSecretKey: "1edf17c358574e75b9913ebff7d95b61" # 访问千帆sdk 时用的 SecretKey

@ -31,6 +31,8 @@ require (
require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/baidubce/bce-qianfan-sdk/go/qianfan v0.0.12 // indirect
github.com/baidubce/bce-sdk-go v0.9.164 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/casbin/govaluate v1.2.0 // indirect
@ -67,6 +69,7 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect

@ -20,6 +20,10 @@ github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/baidubce/bce-qianfan-sdk/go/qianfan v0.0.12 h1:IGb3rV9QyCJa/d3ZvXiuZkacO0BS2pmGi1BP7dZ5IEA=
github.com/baidubce/bce-qianfan-sdk/go/qianfan v0.0.12/go.mod h1:f/kIWWvAHAcU7bzgkfN30SkpN0I4lLvsJkljVK6v5YY=
github.com/baidubce/bce-sdk-go v0.9.164 h1:7gswLMsdQyarovMKuv3i6wxFQ3BQgvc5CmyGXb/D/xA=
github.com/baidubce/bce-sdk-go v0.9.164/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@ -167,6 +171,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
@ -247,6 +253,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

@ -70,6 +70,13 @@ func InitWebRouter_Co() *gin.Engine {
// 请求语音识别
aiRecognition.POST("voc_recognition", validatorFactory.Create(consts.ValidatorPrefix+"VocRecognition"))
}
// TODO:linlnf
// 人工智能文档处理相关
aiDoc := backend.Group("ai_doc/")
{
// 请求优化文字
aiDoc.POST("doc_refine", validatorFactory.Create(consts.ValidatorPrefix+"DocRefine"))
}
// 【需要token】中间件验证的路由
backend.Use(authorization.CheckTokenAuth())

Loading…
Cancel
Save