refactor: 优化文件处理和模型调用逻辑

This commit is contained in:
renzhiyuan 2026-02-09 13:32:05 +08:00
parent 9590721d30
commit ec53207225
5 changed files with 79 additions and 43 deletions

View File

@ -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
}

View File

@ -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真的性价比高现在不买以后这个板块可能就买不起了。"}]`
}

View File

@ -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"`

View File

@ -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

View File

@ -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
}