🆕Start nlp
This commit is contained in:
parent
198af64379
commit
fd972eabc7
@ -67,4 +67,7 @@ const (
|
|||||||
ErrorCasbinCreateAdaptFail string = "casbin NewAdapterByDBUseTableName 发生错误:"
|
ErrorCasbinCreateAdaptFail string = "casbin NewAdapterByDBUseTableName 发生错误:"
|
||||||
ErrorCasbinCreateEnforcerFail string = "casbin NewEnforcer 发生错误:"
|
ErrorCasbinCreateEnforcerFail string = "casbin NewEnforcer 发生错误:"
|
||||||
ErrorCasbinNewModelFromStringFail string = "NewModelFromString 调用时出错:"
|
ErrorCasbinNewModelFromStringFail string = "NewModelFromString 调用时出错:"
|
||||||
|
|
||||||
|
// GLM 部分
|
||||||
|
ErrorsGlmClientInitFail string = "GLM Client 初始化失败"
|
||||||
)
|
)
|
||||||
|
@ -9,8 +9,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/casbin/casbin/v2"
|
"github.com/casbin/casbin/v2"
|
||||||
|
"github.com/yankeguo/zhipu"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -40,6 +42,9 @@ var (
|
|||||||
|
|
||||||
//casbin 全局操作指针
|
//casbin 全局操作指针
|
||||||
Enforcer *casbin.SyncedEnforcer
|
Enforcer *casbin.SyncedEnforcer
|
||||||
|
|
||||||
|
// GLM 全局客户端
|
||||||
|
GlmClient *zhipu.Client
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
18
app/utils/nlp/func.go
Normal file
18
app/utils/nlp/func.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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
|
||||||
|
title, _ := glm.Chat(message)
|
||||||
|
return title
|
||||||
|
}
|
26
app/utils/nlp/glm/glm.go
Normal file
26
app/utils/nlp/glm/glm.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package glm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"catface/app/global/variable"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/yankeguo/zhipu"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChatWithGLM 封装了与GLM模型进行对话的逻辑
|
||||||
|
func Chat(message string) (string, error) {
|
||||||
|
service := variable.GlmClient.ChatCompletion("glm-4-flash").
|
||||||
|
AddMessage(zhipu.ChatCompletionMessage{
|
||||||
|
Role: "user",
|
||||||
|
Content: message,
|
||||||
|
})
|
||||||
|
|
||||||
|
res, err := service.Do(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
apiErrorCode := zhipu.GetAPIErrorCode(err)
|
||||||
|
return "", errors.New(apiErrorCode) // 将字符串包装成 error 类型
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.Choices[0].Message.Content, nil
|
||||||
|
}
|
@ -15,6 +15,8 @@ import (
|
|||||||
"catface/app/utils/zap_factory"
|
"catface/app/utils/zap_factory"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/yankeguo/zhipu"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkRequiredFolders() {
|
func checkRequiredFolders() {
|
||||||
@ -104,4 +106,11 @@ func init() {
|
|||||||
if err := validator_translation.InitTrans("zh"); err != nil {
|
if err := validator_translation.InitTrans("zh"); err != nil {
|
||||||
log.Fatal(my_errors.ErrorsValidatorTransInitFail + err.Error())
|
log.Fatal(my_errors.ErrorsValidatorTransInitFail + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 11. GLM 客户端启动
|
||||||
|
var err error
|
||||||
|
variable.GlmClient, err = zhipu.NewClient(zhipu.WithAPIKey(variable.ConfigYml.GetString("Glm.ApiKey")))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(my_errors.ErrorsGlmClientInitFail + err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,4 +149,8 @@ Weixin:
|
|||||||
|
|
||||||
Code2Session:
|
Code2Session:
|
||||||
GrantType: "authorization_code" # 主要就是想避免硬编码。
|
GrantType: "authorization_code" # 主要就是想避免硬编码。
|
||||||
|
|
||||||
|
Glm:
|
||||||
|
ApiKey: "0cf510ebc01599dba2a593069c1bdfbc.nQBQ4skP8xBh7ijU"
|
||||||
|
DefaultModel: "glm-4-flash"
|
||||||
|
|
2
config/rag.yml
Normal file
2
config/rag.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Prompt:
|
||||||
|
Title: "请根据以下长文本生成一个合适的标题,不需要书名号,长度10字内:"
|
@ -4,9 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/yankeguo/zhipu"
|
"github.com/yankeguo/zhipu"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var prompt = "请根据以下长文本生成一个合适的标题,不需要书名号,长度10字内:"
|
||||||
|
|
||||||
|
// var content = "那天散步时,我遇见了一只瘦弱的流浪猫。我给了它食物,并带它去看病。之后,我决定收养它,给它一个家。现在,它是我生活中不可或缺的伙伴。"
|
||||||
|
var content = "因为猪皮脚崴了,带去医院拍了一下片子,无大碍,静养就好,最近这段时间不要太动他,让他慢慢恢复恢复就好。"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 或者手动指定密钥
|
// 或者手动指定密钥
|
||||||
client, err := zhipu.NewClient(zhipu.WithAPIKey("0cf510ebc01599dba2a593069c1bdfbc.nQBQ4skP8xBh7ijU"))
|
client, err := zhipu.NewClient(zhipu.WithAPIKey("0cf510ebc01599dba2a593069c1bdfbc.nQBQ4skP8xBh7ijU"))
|
||||||
@ -14,11 +18,12 @@ func main() {
|
|||||||
service := client.ChatCompletion("glm-4-flash").
|
service := client.ChatCompletion("glm-4-flash").
|
||||||
AddMessage(zhipu.ChatCompletionMessage{
|
AddMessage(zhipu.ChatCompletionMessage{
|
||||||
Role: "user",
|
Role: "user",
|
||||||
Content: "你好",
|
Content: prompt + content,
|
||||||
}).SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {
|
|
||||||
println(chunk.Choices[0].Delta.Content)
|
|
||||||
return nil
|
|
||||||
})
|
})
|
||||||
|
// .SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {
|
||||||
|
// println(chunk.Choices[0].Delta.Content)
|
||||||
|
// return nil
|
||||||
|
// })
|
||||||
|
|
||||||
res, err := service.Do(context.Background())
|
res, err := service.Do(context.Background())
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user