From ec53207225736ed0ba0c266990f0d25467f4b195 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Mon, 9 Feb 2026 13:32:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=84=E7=90=86=E5=92=8C=E6=A8=A1=E5=9E=8B=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/biz/advice_file.go | 68 +++++++++++++------- internal/data/mongo_model/advicer_version.go | 16 ++--- internal/entitys/advicer_data.go | 5 ++ internal/entitys/bot.go | 4 -- internal/services/advice/file.go | 29 +++++++-- 5 files changed, 79 insertions(+), 43 deletions(-) diff --git a/internal/biz/advice_file.go b/internal/biz/advice_file.go index e437bf8..6ccba98 100644 --- a/internal/biz/advice_file.go +++ b/internal/biz/advice_file.go @@ -2,7 +2,9 @@ package biz import ( "ai_scheduler/internal/biz/llm_service/third_party" + dbmodel "ai_scheduler/internal/data/model" "ai_scheduler/internal/data/mongo_model" + "ai_scheduler/internal/entitys" "ai_scheduler/internal/pkg" "context" "encoding/json" @@ -12,6 +14,7 @@ import ( "time" "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model" + "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model/responses" "github.com/volcengine/volcengine-go-sdk/volcengine" ) @@ -25,12 +28,6 @@ func NewAdviceFileBiz(hsyq *third_party.Hsyq) *AdviceFileBiz { } } -const ( - key = "236ba4b6-9daa-4755-b22f-2fd274cd223a" - fileModel = "doubao-seed-1-8-251228" - jsonModel = "doubao-seed-1-6-flash-250828" -) - var DataMap = map[string]mongo_model.AdviceData{ "dialectFeatures": &mongo_model.DialectFeatures{}, "sentencePatterns": &mongo_model.SentencePatterns{}, @@ -50,7 +47,10 @@ var DataMap = map[string]mongo_model.AdviceData{ "customer": &mongo_model.Customer{}, } -func (a *AdviceFileBiz) WordAna(ctx context.Context, wordContent string) (map[mongo_model.AdviceRole]map[string]mongo_model.AdviceData, error) { +func (a *AdviceFileBiz) WordAna(ctx context.Context, wordContent string, projectInfo *entitys.AdvicerProjectInfoRes) (map[mongo_model.AdviceRole]map[string]mongo_model.AdviceData, error) { + if len(projectInfo.ModelInfo.FileModel) == 0 { + return nil, fmt.Errorf("项目文件模型信息缺失") + } timeSte := time.Now().Format("200601021504") dir := "./cache/" + timeSte os.Mkdir(dir, 0755) @@ -62,7 +62,7 @@ func (a *AdviceFileBiz) WordAna(ctx context.Context, wordContent string) (map[mo os.WriteFile(dir+"/requset.json", []byte(prompt), 0644) //llm提取信息 - anaContent, err := a.callLlm(ctx, prompt, fileModel) + anaContent, err := a.callLlm2(ctx, prompt, &projectInfo.ModelInfo) if err != nil { return nil, err } @@ -93,12 +93,12 @@ func (a *AdviceFileBiz) cateData(data map[string]mongo_model.AdviceData) map[mon func (a *AdviceFileBiz) parseResponse(ctx context.Context, responseByte []byte) (resultOutPut map[string]mongo_model.AdviceData, err error) { //只尝试修复一次 - if isValid := json.Valid(responseByte); !isValid { - responseByte, err = a.fixJson(ctx, responseByte) - if err != nil { - return nil, fmt.Errorf("json格式错误,修复失败:%s", err.Error()) - } - } + //if isValid := json.Valid(responseByte); !isValid { + // + // if err != nil { + // return nil, fmt.Errorf("json格式错误,修复失败:%s", err.Error()) + // } + //} if isValid := json.Valid(responseByte); !isValid { return nil, fmt.Errorf("json格式错误") } @@ -131,17 +131,17 @@ func (a *AdviceFileBiz) parseResponse(ctx context.Context, responseByte []byte) return } -func (a *AdviceFileBiz) fixJson(ctx context.Context, json []byte) ([]byte, error) { - prompt := "你是一个专业的JSON修复专家。请帮我修复以下错误的JSON格式。\n\n要求:\n1. 保持原有数据的结构和内容不变\n2. 修复JSON语法错误\n3. 输出格式化的正确JSON\n4. 简要说明修复了哪些问题\n\n错误的JSON:\n" + string(json) + "\n\n请直接输出修复后的JSON。" - call, err := a.callLlm(ctx, prompt, jsonModel) - if err != nil { - return nil, err - } +//func (a *AdviceFileBiz) fixJson(ctx context.Context, json []byte) ([]byte, error) { +// prompt := "你是一个专业的JSON修复专家。请帮我修复以下错误的JSON格式。\n\n要求:\n1. 保持原有数据的结构和内容不变\n2. 修复JSON语法错误\n3. 输出格式化的正确JSON\n4. 简要说明修复了哪些问题\n\n错误的JSON:\n" + string(json) + "\n\n请直接输出修复后的JSON。" +// call, err := a.callLlm(ctx, prompt, jsonModel) +// if err != nil { +// return nil, err +// } +// +// return []byte(call), nil +//} - return []byte(call), nil -} - -func (a *AdviceFileBiz) callLlm(ctx context.Context, prompt string, modelName string) (string, error) { +func (a *AdviceFileBiz) callLlm(ctx context.Context, prompt string, modelInfo *dbmodel.AiAdviceModelSup) (string, error) { var message = make([]*model.ChatCompletionMessage, 1) message[0] = &model.ChatCompletionMessage{ Role: model.ChatMessageRoleUser, @@ -149,7 +149,7 @@ func (a *AdviceFileBiz) callLlm(ctx context.Context, prompt string, modelName st StringValue: volcengine.String(prompt), }, } - res, err := a.hsyq.Chat(ctx, key, modelName, message) + res, err := a.hsyq.Chat(ctx, modelInfo.Key, modelInfo.FileModel, message) if err != nil { return "", err } @@ -157,6 +157,24 @@ func (a *AdviceFileBiz) callLlm(ctx context.Context, prompt string, modelName st return *res.Choices[0].Message.Content.StringValue, nil } +func (a *AdviceFileBiz) callLlm2(ctx context.Context, prompt string, modelInfo *dbmodel.AiAdviceModelSup) (string, error) { + var message = make([]*responses.InputItem, 3) + message[0] = &responses.InputItem{ + Union: &responses.InputItem_EasyMessage{ + EasyMessage: &responses.ItemEasyMessage{ + Role: responses.MessageRole_system, + Content: &responses.MessageContent{Union: &responses.MessageContent_StringValue{StringValue: prompt}}, + }, + }, + } + res, err := a.hsyq.CreateResponse(ctx, modelInfo.Key, modelInfo.FileModel, message, "", false) + if err != nil { + return "", err + } + + return res.Output[0].GetOutputMessage().Content[0].GetText().GetText(), nil +} + func (a *AdviceFileBiz) getAllExamples() map[string]mongo_model.AdviceData { return DataMap } diff --git a/internal/data/mongo_model/advicer_version.go b/internal/data/mongo_model/advicer_version.go index 1a66049..1e86e05 100644 --- a/internal/data/mongo_model/advicer_version.go +++ b/internal/data/mongo_model/advicer_version.go @@ -41,21 +41,15 @@ func (a *AdvicerVersionMongo) Entity() *AdvicerVersionMongoEntity { } } -// SignatureDialogues 代表性对话示例 -type SignatureDialogues []struct { - Context string `json:"context"` - Dialogue string `json:"dialogue"` //解释 -} - // DialectFeatures 方言特征 type DialectFeatures struct { Region string `json:"region"` //方言使用程度 Intensity float64 `json:"intensity"` // 方言使用强度(0-1) - KeyWords []string `json:"KeyWords"` + KeyWords []string `json:"keyWords"` } func (e *DialectFeatures) Example() string { - return `{"region":"四川成都话","intensity":0.4,"key_words":["噻","要得","没得","不晓得","是不是"]}` + return `{"region":"四川成都话","intensity":0.4,"keyWords":["噻","要得","没得","不晓得","是不是"]}` } func (e *DialectFeatures) Copy() AdviceData { @@ -138,6 +132,12 @@ func (e *ToneTags) Desc() string { return "语气标签" } +// SignatureDialogues 代表性对话示例 +type SignatureDialogues []struct { + Context string `json:"context"` + Dialogue string `json:"dialogue"` //解释 +} + func (e *SignatureDialogues) Example() string { return `[{"context":"客户质疑地块大小","dialogue":"哥,14亩确实不大,但你要在成都是2.5环内城买房,这种是个普遍存在的一个现象。你看万景和绿城都是13亩,中铁建只有8.8亩,339那个帮泰只有11亩。我们虽然地小,但楼间距开阔啊,看过去都是200多米!"},{"context":"客户担心物业费高","dialogue":"姐,我懂你意思,我们也觉得物业费是有点贵。但招商物业是铂金服务,有管家送外卖、免费宠物喂养这些增值服务。你算一下,就算贵一块钱,十年也就多14000,但好物业让房子增值不止这点!"},{"context":"客户犹豫价格","dialogue":"说实话,这个地段的地价都比28板块贵5000多,但我们单价只贵3000。你看龙湖滨江云河颂套内单价都36000了,我们才33000,真的性价比高!现在不买,以后这个板块可能就买不起了。"}]` } diff --git a/internal/entitys/advicer_data.go b/internal/entitys/advicer_data.go index 2c28b48..78383d6 100644 --- a/internal/entitys/advicer_data.go +++ b/internal/entitys/advicer_data.go @@ -5,6 +5,11 @@ import ( "ai_scheduler/internal/data/mongo_model" ) +type WordAnaReq struct { + WordFileUrl string `json:"wordFileUrl"` + ProjectId int32 `json:"projectId"` +} + type AdvicerInitReq struct { AdvicerID int32 `json:"advicerId"` ProjectID int32 `json:"projectId"` diff --git a/internal/entitys/bot.go b/internal/entitys/bot.go index 0c8e70c..7f84e29 100644 --- a/internal/entitys/bot.go +++ b/internal/entitys/bot.go @@ -7,10 +7,6 @@ import ( "gitea.cdlsxd.cn/self-tools/l-dingtalk-stream-sdk-go/chatbot" ) -type WordAnaReq struct { - WordFileUrl string `json:"word_file_url"` -} - type RequireDataDingTalkBot struct { Histories []model.AiChatHi UserInfo *DingTalkUserInfo diff --git a/internal/services/advice/file.go b/internal/services/advice/file.go index 6152525..f2a7c7b 100644 --- a/internal/services/advice/file.go +++ b/internal/services/advice/file.go @@ -3,6 +3,7 @@ package advice import ( "ai_scheduler/internal/biz" "ai_scheduler/internal/config" + errorcode "ai_scheduler/internal/data/error" "ai_scheduler/internal/entitys" "ai_scheduler/internal/pkg" "ai_scheduler/internal/pkg/file_download" @@ -16,18 +17,21 @@ import ( // FileService 文件处理 type FileService struct { - adviceBiz *biz.AdviceFileBiz - cfg *config.Config + adviceBiz *biz.AdviceFileBiz + cfg *config.Config + adviceProjectBiz *biz.AdviceProjectBiz } // NewFileService func NewFileService( adviceBiz *biz.AdviceFileBiz, cfg *config.Config, + adviceProjectBiz *biz.AdviceProjectBiz, ) *FileService { return &FileService{ - adviceBiz: adviceBiz, - cfg: cfg, + adviceBiz: adviceBiz, + cfg: cfg, + adviceProjectBiz: adviceProjectBiz, } } @@ -36,6 +40,16 @@ func (a *FileService) WordAna(c *fiber.Ctx) error { if err := c.BodyParser(req); err != nil { return err } + if req.ProjectId == 0 { + return errorcode.ParamErr("ProjectId is empty") + } + //项目信息 + projectInfo, err := a.adviceProjectBiz.Info(c.UserContext(), &entitys.AdvicerProjectInfoReq{ + ProjectId: req.ProjectId, + }) + if err != nil { + return err + } // URL 解码 fileURL, err := url.PathUnescape(req.WordFileUrl) if err != nil { @@ -45,7 +59,7 @@ func (a *FileService) WordAna(c *fiber.Ctx) error { if err != nil { return err } - ana, err := a.adviceBiz.WordAna(context.Background(), result) + ana, err := a.adviceBiz.WordAna(context.Background(), result, projectInfo) if err != nil { return err } @@ -56,6 +70,9 @@ func (a *FileService) WordAna(c *fiber.Ctx) error { func (a *FileService) WordAnat(path string) ([]byte, error) { // URL 解码 + projectInfo, err := a.adviceProjectBiz.Info(context.Background(), &entitys.AdvicerProjectInfoReq{ + ProjectId: 127, + }) fileURL, err := url.PathUnescape(path) if err != nil { return nil, errors.New("URL 解码失败") @@ -64,7 +81,7 @@ func (a *FileService) WordAnat(path string) ([]byte, error) { if err != nil { return nil, err } - ana, err := a.adviceBiz.WordAna(context.Background(), result) + ana, err := a.adviceBiz.WordAna(context.Background(), result, projectInfo) if err != nil { return nil, err }