refactor(prompt): 重构提示词构建服务和回调处理
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"prompts-core/service/gateway"
|
||||
"prompts-core/service/session"
|
||||
"strings"
|
||||
|
||||
"prompts-core/common/util"
|
||||
@@ -17,34 +16,14 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// UserPromptPayload 用户提示词请求体
|
||||
type UserPromptPayload struct {
|
||||
Model string `json:"model"`
|
||||
PromptInfo string `json:"promptInfo"`
|
||||
Form any `json:"form"`
|
||||
UserForm any `json:"userForm"`
|
||||
Consult []dto.ConsultItem `json:"consult"`
|
||||
UserFilesText map[string]string `json:"userFilesText"`
|
||||
Skills string `json:"skills"`
|
||||
BuildType int `json:"buildType"`
|
||||
}
|
||||
|
||||
// buildPromptTypeRequest 构建提示词类型请求(BuildType=1)
|
||||
func buildPromptTypeRequest(ctx context.Context, req *dto.ComposeMessagesReq, aiModel *gateway.AsynchModel, chatModel *gateway.AsynchModel, ir *PromptIR, totalBatches int) (map[string]any, error) {
|
||||
//1) 构建系统提示词
|
||||
systemPrompt := promptBuildWithRounds(ctx, req, chatModel, aiModel, totalBatches)
|
||||
systemPrompt := promptBuildWithRounds(ctx, chatModel, aiModel)
|
||||
ir.AddSystem(systemPrompt)
|
||||
//2) 构建历史对话
|
||||
history, _ := session.GetHistoryMessages(ctx, req.SessionId)
|
||||
for _, msg := range history {
|
||||
role := gconv.String(msg["role"])
|
||||
if role != "user" && role != "assistant" {
|
||||
continue
|
||||
}
|
||||
ir.AddHistory(role, gconv.String(msg["content"]))
|
||||
}
|
||||
userPrompt := buildUserPrompt(ctx, req, util.GetModelPrompt(ctx, aiModel.ModelType))
|
||||
ir.AddUser(userPrompt)
|
||||
//2) 检查整体内容是否超出窗口
|
||||
if !checkOverallContent(ir, aiModel) {
|
||||
availableWindow := util.GetAvailableWindow(aiModel.TokenConfig)
|
||||
return nil, fmt.Errorf("整体内容超出模型窗口大小限制(可用窗口=%d tokens),请精简后重试", availableWindow)
|
||||
@@ -96,8 +75,7 @@ func compileToProviderRequest(ctx context.Context, ir *PromptIR, chatModel *gate
|
||||
}, nil
|
||||
}
|
||||
|
||||
// promptBuildWithRounds 构建系统提示词
|
||||
func promptBuildWithRounds(ctx context.Context, req *dto.ComposeMessagesReq, chatModel *gateway.AsynchModel, aiModel *gateway.AsynchModel, batches int) string {
|
||||
func promptBuildWithRounds(ctx context.Context, chatModel *gateway.AsynchModel, aiModel *gateway.AsynchModel) string {
|
||||
providerProtocol, err := dao.ProviderProtocol.Get(ctx, &entity.ProviderProtocol{
|
||||
ProviderName: chatModel.OperatorName,
|
||||
Status: 1,
|
||||
@@ -105,32 +83,9 @@ func promptBuildWithRounds(ctx context.Context, req *dto.ComposeMessagesReq, cha
|
||||
if err != nil || providerProtocol == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
outputJSON := util.JSONPretty(util.ReverseMap(aiModel.RequestMapping, map[string]any{}))
|
||||
maxWindowSize := util.GetMaxWindowSize(chatModel.TokenConfig)
|
||||
availableWindow := util.GetAvailableWindow(chatModel.TokenConfig)
|
||||
formContent := buildUserFormContent(req.Form)
|
||||
userFormContent := buildUserFormContent(req.UserForm)
|
||||
formInfo := fmt.Sprintf(`
|
||||
【系统表单(系统提示词/参数)】
|
||||
%s
|
||||
【用户表单全文(必须完整阅读,全部作为用户提示词来源)】
|
||||
%s
|
||||
`, formContent, userFormContent)
|
||||
|
||||
inputInfo := fmt.Sprintf(`
|
||||
目标模型: %s
|
||||
%s
|
||||
技能名称: %s
|
||||
用户文件: %v
|
||||
`, req.ModelName, formInfo, req.SkillName, req.Consult)
|
||||
|
||||
return fmt.Sprintf(providerProtocol.SystemPromptTemplate,
|
||||
req.ModelName, // %s 目标模型名称
|
||||
maxWindowSize, // %d 最大窗口
|
||||
availableWindow, // %d 可用窗口
|
||||
outputJSON, // %s 输出结构
|
||||
inputInfo, // %s 完整输入信息
|
||||
outputJSON, //【输出结构】 %s
|
||||
)
|
||||
}
|
||||
|
||||
@@ -151,43 +106,67 @@ func checkOverallContent(ir *PromptIR, model *gateway.AsynchModel) bool {
|
||||
|
||||
// buildUserPrompt 构建用户提示词
|
||||
func buildUserPrompt(ctx context.Context, req *dto.ComposeMessagesReq, prompt string) string {
|
||||
payload := UserPromptPayload{
|
||||
Model: req.ModelName,
|
||||
PromptInfo: prompt,
|
||||
Form: prepareUserFormPayload(req.Form),
|
||||
UserForm: prepareUserFormPayload(req.UserForm),
|
||||
Consult: req.Consult,
|
||||
UserFilesText: ExtractFileTexts(ctx, req.Consult),
|
||||
Skills: SkillMdContent(ctx, req.SkillName),
|
||||
BuildType: req.BuildType,
|
||||
var b strings.Builder
|
||||
b.WriteString(fmt.Sprintf("目标模型:%s\n", req.ModelName))
|
||||
if prompt != "" {
|
||||
b.WriteString(fmt.Sprintf("系统提示词:%s\n", prompt))
|
||||
}
|
||||
return gjson.New(payload).String()
|
||||
if skills := SkillMdContent(ctx, req.SkillName); skills != "" {
|
||||
b.WriteString(fmt.Sprintf("技能内容:\n%s\n", skills))
|
||||
}
|
||||
if formText := buildUserFormText(req.Form); formText != "" {
|
||||
b.WriteString(fmt.Sprintf("系统参数:\n%s\n", formText))
|
||||
}
|
||||
if userFormText := buildUserFormText(req.UserForm); userFormText != "" {
|
||||
b.WriteString(fmt.Sprintf("用户需求:\n%s\n", userFormText))
|
||||
}
|
||||
if len(req.Consult) > 0 {
|
||||
b.WriteString(fmt.Sprintf("参考附件:%s\n", gjson.New(req.Consult).String()))
|
||||
}
|
||||
if fileTexts := ExtractFileTexts(ctx, req.Consult); fileTexts != "" {
|
||||
b.WriteString(fmt.Sprintf("附件内容:\n%s\n", fileTexts))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// prepareUserFormPayload 准备用户表单载荷
|
||||
func prepareUserFormPayload(userForm []map[string]any) any {
|
||||
if len(userForm) == 0 {
|
||||
return nil
|
||||
// buildUserFormText 构建用户表单内容字符串
|
||||
func buildUserFormText(form []map[string]any) string {
|
||||
if len(form) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
if _, ok := userForm[0]["batch_index"]; ok {
|
||||
return userForm
|
||||
}
|
||||
|
||||
return mergeUserFormTexts(userForm)
|
||||
}
|
||||
|
||||
// mergeUserFormTexts 合并 UserForm 中的所有文本内容
|
||||
func mergeUserFormTexts(userForm []map[string]any) string {
|
||||
var builder strings.Builder
|
||||
for i, item := range userForm {
|
||||
text := getItemText(item)
|
||||
if i > 0 {
|
||||
builder.WriteString("\n\n")
|
||||
for _, item := range form {
|
||||
for k, v := range item {
|
||||
switch val := v.(type) {
|
||||
case []any:
|
||||
// 数组类型:逐条列出
|
||||
builder.WriteString(fmt.Sprintf("%s:\n", k))
|
||||
for i, elem := range val {
|
||||
if m, ok := elem.(map[string]any); ok {
|
||||
builder.WriteString(fmt.Sprintf(" %d. ", i+1))
|
||||
for mk, mv := range m {
|
||||
builder.WriteString(fmt.Sprintf("%s:%v ", mk, mv))
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
} else {
|
||||
builder.WriteString(fmt.Sprintf(" %d. %v\n", i+1, elem))
|
||||
}
|
||||
}
|
||||
case []map[string]any:
|
||||
builder.WriteString(fmt.Sprintf("%s:\n", k))
|
||||
for i, m := range val {
|
||||
builder.WriteString(fmt.Sprintf(" %d. ", i+1))
|
||||
for mk, mv := range m {
|
||||
builder.WriteString(fmt.Sprintf("%s:%v ", mk, mv))
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
default:
|
||||
builder.WriteString(fmt.Sprintf("%s:%v\n", k, v))
|
||||
}
|
||||
}
|
||||
builder.WriteString(text)
|
||||
}
|
||||
return builder.String()
|
||||
return strings.TrimSpace(builder.String())
|
||||
}
|
||||
|
||||
// NodeBuild 节点构建
|
||||
|
||||
Reference in New Issue
Block a user