205 lines
6.8 KiB
Go
205 lines
6.8 KiB
Go
package prompt
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"prompts-core/service/gateway"
|
||
"prompts-core/service/session"
|
||
"strings"
|
||
|
||
"prompts-core/common/util"
|
||
"prompts-core/dao"
|
||
"prompts-core/model/dto"
|
||
"prompts-core/model/entity"
|
||
|
||
"gitea.com/red-future/common/utils"
|
||
"github.com/gogf/gf/v2/encoding/gjson"
|
||
"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)
|
||
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)
|
||
if !checkOverallContent(ir, aiModel) {
|
||
availableWindow := util.GetAvailableWindow(aiModel.TokenConfig)
|
||
return nil, fmt.Errorf("整体内容超出模型窗口大小限制(可用窗口=%d tokens),请精简后重试", availableWindow)
|
||
}
|
||
return compileToProviderRequest(ctx, ir, chatModel)
|
||
}
|
||
|
||
// buildNodeTypeRequest 构建节点类型请求(BuildType=2)
|
||
func buildNodeTypeRequest(ctx context.Context, req *dto.ComposeMessagesReq, chatModel *gateway.AsynchModel, ir *PromptIR) (map[string]any, error) {
|
||
ir.AddUser(NodeBuild(ctx, req))
|
||
return compileToProviderRequest(ctx, ir, chatModel)
|
||
}
|
||
|
||
// buildStructTypeRequest 构建结构体类型请求(BuildType=3)
|
||
func buildStructTypeRequest(ctx context.Context, req *dto.ComposeMessagesReq, chatModel *gateway.AsynchModel, ir *PromptIR) (map[string]any, error) {
|
||
// 提取 userForm 中的 prompt 作为自定义提示词
|
||
var customPrompt string
|
||
for _, item := range req.UserForm {
|
||
if prompt, ok := item["prompt"]; ok && gconv.String(prompt) != "" {
|
||
customPrompt = gconv.String(prompt)
|
||
break
|
||
}
|
||
}
|
||
// 用户消息
|
||
ir.AddSystem(customPrompt)
|
||
ir.AddUser(buildUserPrompt(ctx, req, ""))
|
||
return compileToProviderRequest(ctx, ir, chatModel, customPrompt)
|
||
}
|
||
|
||
// compileToProviderRequest 编译为 Provider 请求
|
||
func compileToProviderRequest(ctx context.Context, ir *PromptIR, chatModel *gateway.AsynchModel, customPrompt ...string) (map[string]any, error) {
|
||
protocol, err := GetProtocolByProvider(ctx, chatModel.OperatorName)
|
||
if err != nil || protocol == nil {
|
||
return nil, fmt.Errorf("协议配置不存在或获取失败: %w", err)
|
||
}
|
||
// 如果传了自定义提示词,替换掉协议模板
|
||
if len(customPrompt) > 0 && customPrompt[0] != "" {
|
||
protocol.SystemPromptTemplate = customPrompt[0]
|
||
}
|
||
providerReq, err := Compile(ir, protocol, chatModel)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("编译请求失败: %w", err)
|
||
}
|
||
return map[string]any{
|
||
"modelName": chatModel.ModelName,
|
||
"bizName": util.GetServerName(ctx),
|
||
"callbackUrl": utils.GetCallbackURL(ctx, "/prompt/callback"),
|
||
"requestPayload": providerReq,
|
||
}, nil
|
||
}
|
||
|
||
// promptBuildWithRounds 构建系统提示词
|
||
func promptBuildWithRounds(ctx context.Context, req *dto.ComposeMessagesReq, chatModel *gateway.AsynchModel, aiModel *gateway.AsynchModel, batches int) string {
|
||
providerProtocol, err := dao.ProviderProtocol.Get(ctx, &entity.ProviderProtocol{
|
||
ProviderName: chatModel.OperatorName,
|
||
Status: 1,
|
||
})
|
||
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 完整输入信息
|
||
)
|
||
}
|
||
|
||
// buildUserFormContent 构建用户表单内容字符串
|
||
func buildUserFormContent(userForm []map[string]any) string {
|
||
var builder strings.Builder
|
||
for _, item := range userForm {
|
||
builder.WriteString(fmt.Sprintf("%v\n", item))
|
||
}
|
||
return builder.String()
|
||
}
|
||
|
||
// checkOverallContent 检查整体内容是否超出窗口
|
||
func checkOverallContent(ir *PromptIR, model *gateway.AsynchModel) bool {
|
||
fullContent := ir.String()
|
||
return util.CountToken(fullContent, model.TokenConfig)
|
||
}
|
||
|
||
// 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,
|
||
}
|
||
return gjson.New(payload).String()
|
||
}
|
||
|
||
// prepareUserFormPayload 准备用户表单载荷
|
||
func prepareUserFormPayload(userForm []map[string]any) any {
|
||
if len(userForm) == 0 {
|
||
return nil
|
||
}
|
||
|
||
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")
|
||
}
|
||
builder.WriteString(text)
|
||
}
|
||
return builder.String()
|
||
}
|
||
|
||
// NodeBuild 节点构建
|
||
func NodeBuild(ctx context.Context, req *dto.ComposeMessagesReq) string {
|
||
promptTpl := util.GetBuildPrompt(ctx)
|
||
if promptTpl == "" {
|
||
return ""
|
||
}
|
||
|
||
formStr := util.FormToJSON(req.Form)
|
||
userFormStr := util.UserFormToJSON(req.UserForm)
|
||
|
||
return fmt.Sprintf(promptTpl, formStr, userFormStr)
|
||
}
|