From fd972eabc7115fa4080ff6c424742e59bff6243e Mon Sep 17 00:00:00 2001 From: Havoc412 <2993167370@qq.com> Date: Wed, 6 Nov 2024 01:06:38 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=95Start=20nlp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/global/my_errors/my_errors.go | 3 +++ app/global/variable/variable.go | 5 +++++ app/utils/nlp/func.go | 18 ++++++++++++++++++ app/utils/nlp/glm/glm.go | 26 ++++++++++++++++++++++++++ bootstrap/init.go | 9 +++++++++ config/config.yml | 4 ++++ config/rag.yml | 2 ++ test/glm/glm.go | 15 ++++++++++----- 8 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 app/utils/nlp/func.go create mode 100644 app/utils/nlp/glm/glm.go create mode 100644 config/rag.yml diff --git a/app/global/my_errors/my_errors.go b/app/global/my_errors/my_errors.go index 43289bf..a6d289d 100644 --- a/app/global/my_errors/my_errors.go +++ b/app/global/my_errors/my_errors.go @@ -67,4 +67,7 @@ const ( ErrorCasbinCreateAdaptFail string = "casbin NewAdapterByDBUseTableName 发生错误:" ErrorCasbinCreateEnforcerFail string = "casbin NewEnforcer 发生错误:" ErrorCasbinNewModelFromStringFail string = "NewModelFromString 调用时出错:" + + // GLM 部分 + ErrorsGlmClientInitFail string = "GLM Client 初始化失败" ) diff --git a/app/global/variable/variable.go b/app/global/variable/variable.go index daa865d..48968a6 100644 --- a/app/global/variable/variable.go +++ b/app/global/variable/variable.go @@ -9,8 +9,10 @@ import ( "strings" "github.com/casbin/casbin/v2" + "github.com/yankeguo/zhipu" "go.uber.org/zap" "gorm.io/gorm" + ) var ( @@ -40,6 +42,9 @@ var ( //casbin 全局操作指针 Enforcer *casbin.SyncedEnforcer + + // GLM 全局客户端 + GlmClient *zhipu.Client ) func init() { diff --git a/app/utils/nlp/func.go b/app/utils/nlp/func.go new file mode 100644 index 0000000..8ab5273 --- /dev/null +++ b/app/utils/nlp/func.go @@ -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 +} diff --git a/app/utils/nlp/glm/glm.go b/app/utils/nlp/glm/glm.go new file mode 100644 index 0000000..a03a70f --- /dev/null +++ b/app/utils/nlp/glm/glm.go @@ -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 +} diff --git a/bootstrap/init.go b/bootstrap/init.go index cb91d6f..ab7644d 100644 --- a/bootstrap/init.go +++ b/bootstrap/init.go @@ -15,6 +15,8 @@ import ( "catface/app/utils/zap_factory" "log" "os" + + "github.com/yankeguo/zhipu" ) func checkRequiredFolders() { @@ -104,4 +106,11 @@ func init() { if err := validator_translation.InitTrans("zh"); err != nil { 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()) + } } diff --git a/config/config.yml b/config/config.yml index 23a67e8..4539f2a 100644 --- a/config/config.yml +++ b/config/config.yml @@ -149,4 +149,8 @@ Weixin: Code2Session: GrantType: "authorization_code" # 主要就是想避免硬编码。 + +Glm: + ApiKey: "0cf510ebc01599dba2a593069c1bdfbc.nQBQ4skP8xBh7ijU" + DefaultModel: "glm-4-flash" \ No newline at end of file diff --git a/config/rag.yml b/config/rag.yml new file mode 100644 index 0000000..4c16188 --- /dev/null +++ b/config/rag.yml @@ -0,0 +1,2 @@ +Prompt: + Title: "请根据以下长文本生成一个合适的标题,不需要书名号,长度10字内:" \ No newline at end of file diff --git a/test/glm/glm.go b/test/glm/glm.go index f407323..94b6f4b 100644 --- a/test/glm/glm.go +++ b/test/glm/glm.go @@ -4,9 +4,13 @@ import ( "context" "github.com/yankeguo/zhipu" - ) +var prompt = "请根据以下长文本生成一个合适的标题,不需要书名号,长度10字内:" + +// var content = "那天散步时,我遇见了一只瘦弱的流浪猫。我给了它食物,并带它去看病。之后,我决定收养它,给它一个家。现在,它是我生活中不可或缺的伙伴。" +var content = "因为猪皮脚崴了,带去医院拍了一下片子,无大碍,静养就好,最近这段时间不要太动他,让他慢慢恢复恢复就好。" + func main() { // 或者手动指定密钥 client, err := zhipu.NewClient(zhipu.WithAPIKey("0cf510ebc01599dba2a593069c1bdfbc.nQBQ4skP8xBh7ijU")) @@ -14,11 +18,12 @@ func main() { service := client.ChatCompletion("glm-4-flash"). AddMessage(zhipu.ChatCompletionMessage{ Role: "user", - Content: "你好", - }).SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error { - println(chunk.Choices[0].Delta.Content) - return nil + Content: prompt + content, }) + // .SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error { + // println(chunk.Choices[0].Delta.Content) + // return nil + // }) res, err := service.Do(context.Background())