197 lines
7.1 KiB
Go
197 lines
7.1 KiB
Go
//go:build ignore
|
||
|
||
package main
|
||
|
||
import (
|
||
"os"
|
||
"strings"
|
||
)
|
||
|
||
func main() {
|
||
f := "internal/biz/advice_chat.go"
|
||
b, _ := os.ReadFile(f)
|
||
s := string(b)
|
||
ok := true
|
||
|
||
// === 1. Regis: 存 ChatData 到 Redis ===
|
||
old1 := ` err = a.rdb.Rdb.SetEx(ctx, sessionId, promptJson, 3600*time.Second).Err()
|
||
|
||
return sessionId, err
|
||
}`
|
||
new1 := ` err = a.rdb.Rdb.SetEx(ctx, sessionId, promptJson, 3600*time.Second).Err()
|
||
// 存储 ChatData 供后续 Chat 全量注入提示词
|
||
if chatDataJson, e := json.Marshal(chatData); e == nil {
|
||
_ = a.rdb.Rdb.SetEx(ctx, "chatdata:"+sessionId, string(chatDataJson), 3600*time.Second).Err()
|
||
}
|
||
return sessionId, err
|
||
}`
|
||
if strings.Contains(s, old1) {
|
||
s = strings.Replace(s, old1, new1, 1)
|
||
println("1. Regis: ChatData store added")
|
||
} else {
|
||
println("1. ERROR: Regis block not found")
|
||
ok = false
|
||
}
|
||
|
||
// === 2. Chat: 从 Redis 取 ChatData,传给 buildChatPromptResponse ===
|
||
old2 := ` prompt, err := a.buildChatPromptResponse(ctx, chat, &session, chatHis)
|
||
if err != nil {
|
||
return assistant, err
|
||
}
|
||
log.Infof("[提示词] sessionId=%s prompt=%s", session.SessionID, pkg.JsonStringIgonErr(prompt))
|
||
resContent, err := a.callLlmResponse(ctx, prompt, modelInfo.Key, modelInfo.URL, modelInfo.ChatModel, session.ContextCache)`
|
||
new2 := ` var chatData entitys.ChatData
|
||
if cdStr, e := a.rdb.Rdb.Get(ctx, "chatdata:"+session.SessionID).Result(); e == nil {
|
||
_ = json.Unmarshal([]byte(cdStr), &chatData)
|
||
}
|
||
prompt, err := a.buildChatPromptResponse(ctx, chat, &session, chatHis, &chatData)
|
||
if err != nil {
|
||
return assistant, err
|
||
}
|
||
log.Infof("[提示词] sessionId=%s prompt=%s", session.SessionID, pkg.JsonStringIgonErr(prompt))
|
||
resContent, err := a.callLlmResponse(ctx, prompt, modelInfo.Key, modelInfo.URL, modelInfo.ChatModel, session.ContextCache)`
|
||
if strings.Contains(s, old2) {
|
||
s = strings.Replace(s, old2, new2, 1)
|
||
println("2. Chat: ChatData load + pass added")
|
||
} else {
|
||
println("2. ERROR: Chat block not found")
|
||
ok = false
|
||
}
|
||
|
||
// === 3. ChatWithSession: 同样从 Redis 取 ChatData ===
|
||
old3 := ` prompt, err := a.buildChatPromptResponse(ctx, chat, session, chatHis)
|
||
if err != nil {
|
||
return assistant, err
|
||
}
|
||
log.Infof("[提示词] sessionId=%s prompt=%s", session.SessionID, pkg.JsonStringIgonErr(prompt))
|
||
resContent, err := a.callLlmResponse(ctx, prompt, modelInfo.Key, modelInfo.URL, modelInfo.ChatModel, session.ContextCache)`
|
||
new3 := ` var chatData entitys.ChatData
|
||
if cdStr, e := a.rdb.Rdb.Get(ctx, "chatdata:"+session.SessionID).Result(); e == nil {
|
||
_ = json.Unmarshal([]byte(cdStr), &chatData)
|
||
}
|
||
prompt, err := a.buildChatPromptResponse(ctx, chat, session, chatHis, &chatData)
|
||
if err != nil {
|
||
return assistant, err
|
||
}
|
||
log.Infof("[提示词] sessionId=%s prompt=%s", session.SessionID, pkg.JsonStringIgonErr(prompt))
|
||
resContent, err := a.callLlmResponse(ctx, prompt, modelInfo.Key, modelInfo.URL, modelInfo.ChatModel, session.ContextCache)`
|
||
if strings.Contains(s, old3) {
|
||
s = strings.Replace(s, old3, new3, 1)
|
||
println("3. ChatWithSession: ChatData load + pass added")
|
||
} else {
|
||
println("3. ERROR: ChatWithSession block not found")
|
||
ok = false
|
||
}
|
||
|
||
// === 4. buildChatPromptResponse: 全量数据注入 ===
|
||
old4 := `func (a *AdviceChatBiz) buildChatPromptResponse(ctx context.Context, chat *entitys.AdvicerChatReq, session *dbmodel.AiAdviceSession, chatList []mongo_model.AdvicerChatHisMongoEntity) ([]openai.ResponseInputMessage, error) {
|
||
// 输出格式指令:模型使用 response_format=json_object 时,prompt 必须包含 "json" 关键词,
|
||
// 同时明确告诉模型返回 JSON 结构,便于下游 json.Unmarshal 解析为 Assistant。
|
||
const outputFormatHint = "\n[输出格式]请用 json 格式返回你的回复,结构为 {\"result\":\"回复内容\",\"mission_status\":\"in_progress|completed|fail\",\"mission_complete_desc\":\"本轮进展小结\"}。"
|
||
message := []openai.ResponseInputMessage{
|
||
{
|
||
Role: openai.ChatMessageRoleSystem,
|
||
Content: a.taskPrompt(session) + outputFormatHint,
|
||
},
|
||
{
|
||
Role: openai.ChatMessageRoleSystem,
|
||
Content: formatChatHis(chatList),
|
||
},
|
||
{
|
||
Role: openai.ChatMessageRoleUser,
|
||
Content: chat.Content,
|
||
},
|
||
}
|
||
return message, nil
|
||
}`
|
||
new4 := `func (a *AdviceChatBiz) buildChatPromptResponse(ctx context.Context, chat *entitys.AdvicerChatReq, session *dbmodel.AiAdviceSession, chatList []mongo_model.AdvicerChatHisMongoEntity, chatData *entitys.ChatData) ([]openai.ResponseInputMessage, error) {
|
||
const outputFormatHint = "\n[输出格式]请用 json 格式返回你的回复,结构为 {\"result\":\"回复内容\",\"mission_status\":\"in_progress|completed|fail\",\"mission_complete_desc\":\"本轮进展小结\"}。"
|
||
var sys strings.Builder
|
||
// 基础规则 + 任务 + 风控红线
|
||
sys.WriteString(constants.BasePrompt)
|
||
if session.Mission != "" {
|
||
sys.WriteString(session.Mission)
|
||
sys.WriteString("\n")
|
||
}
|
||
if chatData != nil && chatData.RuleDimension != "" {
|
||
sys.WriteString(constants.RulePromptPrefix)
|
||
sys.WriteString(chatData.RuleDimension)
|
||
sys.WriteString("\n")
|
||
}
|
||
sys.WriteString(constants.BasePrompt2)
|
||
// 全量结构化数据注入
|
||
if chatData != nil {
|
||
if chatData.ProjectInfo != nil {
|
||
sys.WriteString("\n[项目信息]\n")
|
||
sys.WriteString(pkg.JsonStringIgonErr(chatData.ProjectInfo))
|
||
}
|
||
if chatData.AdvicerInfo != nil {
|
||
sys.WriteString("\n[销售信息]\n")
|
||
sys.WriteString(pkg.JsonStringIgonErr(chatData.AdvicerInfo))
|
||
}
|
||
if chatData.AdvicerVersion != nil && len(chatData.AdvicerVersion) > 0 {
|
||
sys.WriteString("\n[销售人设风格]\n")
|
||
sys.WriteString(pkg.JsonStringIgonErr(chatData.AdvicerVersion))
|
||
}
|
||
if chatData.ClientInfo != nil {
|
||
sys.WriteString("\n[客户信息]\n")
|
||
sys.WriteString(pkg.JsonStringIgonErr(chatData.ClientInfo))
|
||
}
|
||
if chatData.TalkSkill != nil {
|
||
sys.WriteString("\n[销售技巧]\n")
|
||
sys.WriteString(pkg.JsonStringIgonErr(chatData.TalkSkill))
|
||
}
|
||
}
|
||
sys.WriteString(outputFormatHint)
|
||
message := []openai.ResponseInputMessage{
|
||
{
|
||
Role: openai.ChatMessageRoleSystem,
|
||
Content: sys.String(),
|
||
},
|
||
{
|
||
Role: openai.ChatMessageRoleSystem,
|
||
Content: formatChatHis(chatList),
|
||
},
|
||
{
|
||
Role: openai.ChatMessageRoleUser,
|
||
Content: chat.Content,
|
||
},
|
||
}
|
||
return message, nil
|
||
}`
|
||
if strings.Contains(s, old4) {
|
||
s = strings.Replace(s, old4, new4, 1)
|
||
println("4. buildChatPromptResponse: full data injection done")
|
||
} else {
|
||
println("4. ERROR: buildChatPromptResponse block not found")
|
||
ok = false
|
||
}
|
||
|
||
// === 5. 删掉 taskPrompt 中的 session.Mission 注入(避免重复,已在 buildChatPromptResponse 中处理)===
|
||
old5 := ` // 注入 mission(含人格 + 项目信息),确保每次 Chat 模型都能看到
|
||
if session.Mission != "" {
|
||
b.WriteString("\n" + session.Mission)
|
||
}
|
||
return b.String()
|
||
}
|
||
|
||
func (a *AdviceChatBiz) buildBasePromptResponse`
|
||
new5 := ` return b.String()
|
||
}
|
||
|
||
func (a *AdviceChatBiz) buildBasePromptResponse`
|
||
if strings.Contains(s, old5) {
|
||
s = strings.Replace(s, old5, new5, 1)
|
||
println("5. taskPrompt: removed redundant mission injection")
|
||
} else {
|
||
println("5. SKIP: taskPrompt already clean")
|
||
}
|
||
|
||
if ok {
|
||
os.WriteFile(f, []byte(s), 0644)
|
||
println("\nALL DONE - advice_chat.go updated")
|
||
} else {
|
||
println("\nFAILED - some blocks not found")
|
||
}
|
||
}
|