add title API
This commit is contained in:
parent
fd972eabc7
commit
f26c9c0528
@ -6,6 +6,7 @@ const (
|
||||
ErrorsPublicNotExists string = "public 目录不存在"
|
||||
ErrorsConfigYamlNotExists string = "config.yml 配置文件不存在"
|
||||
ErrorsConfigGormNotExists string = "gorm_v2.yml 配置文件不存在"
|
||||
ErrorsPromptsYmlNotExists string = "prompts.yml 配置文件不存在"
|
||||
ErrorsStorageLogsNotExists string = "storage/logs 目录不存在"
|
||||
ErrorsConfigInitFail string = "初始化配置文件发生错误"
|
||||
ErrorsSoftLinkCreateFail string = "自动创建软连接失败,请以管理员身份运行客户端(开发环境为goland等,生产环境检查命令执行者权限), " +
|
||||
|
@ -12,7 +12,6 @@ import (
|
||||
"github.com/yankeguo/zhipu"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
)
|
||||
|
||||
var (
|
||||
@ -26,6 +25,7 @@ var (
|
||||
// 全局配置文件
|
||||
ConfigYml ymlconfig_interf.YmlConfigInterf // 全局配置文件指针
|
||||
ConfigGormv2Yml ymlconfig_interf.YmlConfigInterf // 全局配置文件指针
|
||||
PromptsYml ymlconfig_interf.YmlConfigInterf // 提示词配置文件
|
||||
|
||||
//gorm 数据库客户端,如果您操作数据库使用的是gorm,请取消以下注释,在 bootstrap>init 文件,进行初始化即可使用
|
||||
GormDbMysql *gorm.DB // 全局gorm的客户端连接
|
||||
|
23
app/http/controller/web/nlp_controller.go
Normal file
23
app/http/controller/web/nlp_controller.go
Normal file
@ -0,0 +1,23 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/utils/nlp"
|
||||
"catface/app/utils/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Nlp struct {
|
||||
}
|
||||
|
||||
func (n *Nlp) Title(context *gin.Context) {
|
||||
content := context.GetString(consts.ValidatorPrefix + "content")
|
||||
|
||||
newTitle := nlp.GenerateTitle(content)
|
||||
if newTitle != "" {
|
||||
response.Success(context, consts.CurdStatusOkMsg, gin.H{"title": newTitle})
|
||||
} else {
|
||||
response.Fail(context, consts.CurdStatusOkCode, consts.CurdStatusOkMsg, "")
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ import (
|
||||
"catface/app/http/validator/web/animal_like"
|
||||
"catface/app/http/validator/web/encounter"
|
||||
"catface/app/http/validator/web/encounter_like"
|
||||
"catface/app/http/validator/web/nlp"
|
||||
"catface/app/http/validator/web/users"
|
||||
)
|
||||
|
||||
@ -74,4 +75,8 @@ func WebRegisterValidator() {
|
||||
containers.Set(key, encounter_like.Create{})
|
||||
key = consts.ValidatorPrefix + "EncounterLikeDelete"
|
||||
containers.Set(key, encounter_like.Delete{})
|
||||
|
||||
// TAG NLP
|
||||
key = consts.ValidatorPrefix + "NlpTitle"
|
||||
containers.Set(key, nlp.Title{})
|
||||
}
|
||||
|
34
app/http/validator/web/nlp/title.go
Normal file
34
app/http/validator/web/nlp/title.go
Normal file
@ -0,0 +1,34 @@
|
||||
package nlp
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/http/controller/web"
|
||||
"catface/app/http/validator/core/data_transfer"
|
||||
"catface/app/utils/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 用于生成标题的文本素材
|
||||
type Title struct {
|
||||
Content string `form:"content" json:"content"`
|
||||
Title string `form:"title" json:"title"` // 原标题
|
||||
Tags []string `form:"tags" json:"tags"`
|
||||
AnimalsName []string `form:"animals_name" json:"animals_name"`
|
||||
}
|
||||
|
||||
func (t Title) CheckParams(context *gin.Context) {
|
||||
if err := context.ShouldBind(&t); err != nil {
|
||||
response.ValidatorError(context, err)
|
||||
return
|
||||
}
|
||||
|
||||
extraAddBindDataContext := data_transfer.DataAddContext(t, consts.ValidatorPrefix, context)
|
||||
if extraAddBindDataContext == nil {
|
||||
response.ErrorSystem(context, "Animal Create 表单验证器json化失败", "")
|
||||
} else {
|
||||
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
|
||||
(&web.Nlp{}).Title(extraAddBindDataContext)
|
||||
}
|
||||
|
||||
}
|
@ -3,16 +3,10 @@ package nlp
|
||||
import (
|
||||
"catface/app/global/variable"
|
||||
"catface/app/utils/nlp/glm"
|
||||
"catface/app/utils/yml_config/ymlconfig_interf"
|
||||
)
|
||||
|
||||
var PromptsYml ymlconfig_interf.YmlConfigInterf
|
||||
|
||||
func init() {
|
||||
PromptsYml = variable.ConfigYml.Clone("rag")
|
||||
}
|
||||
func GenerateTitle(content string) string {
|
||||
message := PromptsYml.GetString("Prompt.Title") + content
|
||||
message := variable.PromptsYml.GetString("Prompt.Title") + content
|
||||
title, _ := glm.Chat(message)
|
||||
return title
|
||||
}
|
||||
|
@ -27,6 +27,9 @@ func checkRequiredFolders() {
|
||||
if _, err := os.Stat(variable.BasePath + "/config/gorm_v2.yml"); err != nil {
|
||||
log.Fatal(my_errors.ErrorsConfigGormNotExists + err.Error())
|
||||
}
|
||||
if _, err := os.Stat(variable.BasePath + "/config/prompts.yml"); err != nil {
|
||||
log.Fatal(my_errors.ErrorsPromptsYmlNotExists + err.Error())
|
||||
}
|
||||
//2.检查public目录是否存在
|
||||
if _, err := os.Stat(variable.BasePath + "/public/"); err != nil {
|
||||
log.Fatal(my_errors.ErrorsPublicNotExists + err.Error())
|
||||
@ -55,6 +58,9 @@ func init() {
|
||||
variable.ConfigGormv2Yml = variable.ConfigYml.Clone("gorm_v2")
|
||||
variable.ConfigGormv2Yml.ConfigFileChangeListen()
|
||||
|
||||
variable.PromptsYml = variable.ConfigYml.Clone("prompts")
|
||||
variable.PromptsYml.ConfigFileChangeListen()
|
||||
|
||||
// 5.初始化全局日志句柄,并载入日志钩子处理函数
|
||||
variable.ZapLog = zap_factory.CreateZapFactory(sys_log_hook.ZapLogHandler)
|
||||
|
||||
|
@ -3,10 +3,12 @@ package routers
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/global/variable"
|
||||
|
||||
// "catface/app/http/controller/captcha" // 验证码组件
|
||||
// "catface/app/http/middleware/authorization"
|
||||
"catface/app/http/middleware/cors"
|
||||
validatorFactory "catface/app/http/validator/core/factory"
|
||||
|
||||
// TODO validatorFactory "catface/app/http/validator/core/factory"
|
||||
"catface/app/utils/gin_release"
|
||||
"net/http"
|
||||
@ -141,6 +143,11 @@ func InitWebRouter() *gin.Engine {
|
||||
encounter.POST("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeCreate"))
|
||||
encounter.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"EncounterLikeDelete"))
|
||||
}
|
||||
|
||||
nlp := backend.Group("nlp")
|
||||
{
|
||||
nlp.POST("title", validatorFactory.Create(consts.ValidatorPrefix+"NlpTitle"))
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user