prompts-core
This commit is contained in:
162
service/build_service.go
Normal file
162
service/build_service.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"prompts-core/model/dto"
|
||||
"prompts-core/model/entity"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// 获取系统模型提示词
|
||||
func getConfPrompt(ctx context.Context, modelType int) string {
|
||||
return g.Cfg().MustGet(ctx, "modelPrompts.types."+gconv.String(modelType), "").String()
|
||||
}
|
||||
|
||||
func buildInferenceRequest(ctx context.Context, req *dto.ComposeMessagesReq, sessionModel *entity.AsynchModel, model *entity.AsynchModel, historyMessages []Message) (map[string]any, error) {
|
||||
// 读取 task 相关的配置
|
||||
// 构建消息数组
|
||||
// 1. 系统提示词(不动)
|
||||
|
||||
fmt.Println("打印sessionModel结果", sessionModel)
|
||||
fmt.Println("打印model结果", model)
|
||||
|
||||
messages := []map[string]any{}
|
||||
messages = append(messages, map[string]any{
|
||||
"role": "system",
|
||||
"content": GetSystemPrompt(req, model),
|
||||
})
|
||||
|
||||
// 2. 历史对话 - 动态添加(新增部分)
|
||||
for _, msg := range historyMessages {
|
||||
messages = append(messages, map[string]any{
|
||||
"role": msg.Role,
|
||||
"content": msg.GetContentString(),
|
||||
})
|
||||
}
|
||||
// 3. 当前用户问题(原来的最后一条)
|
||||
messages = append(messages, map[string]any{
|
||||
"role": "user",
|
||||
"content": buildCombinedInput(req, getConfPrompt(ctx, model.ModelsType)),
|
||||
})
|
||||
|
||||
// 构建请求体
|
||||
return map[string]any{
|
||||
"modelName": sessionModel.ModelName,
|
||||
"bizName": "prompts-core",
|
||||
"callbackUrl": "/prompt/callback",
|
||||
"requestPayload": map[string]any{
|
||||
"model": sessionModel.ModelName,
|
||||
"messages": messages,
|
||||
"stream": false,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 输入构建
|
||||
// ============================================
|
||||
func buildCombinedInput(req *dto.ComposeMessagesReq, prompt string) string {
|
||||
payload := map[string]any{
|
||||
//数据库提示信息
|
||||
"promptInfo": prompt,
|
||||
// 系统表单
|
||||
"form": req.Form,
|
||||
// 用户表单
|
||||
"userForm": req.UserForm,
|
||||
//文件url
|
||||
"userFiles": req.UserFiles,
|
||||
//解读文件(只支持可读类型 如:xml,json,yaml)
|
||||
"userFilesText": fetchFileTexts(context.Background(), req.UserFiles),
|
||||
}
|
||||
return mustMarshal(payload)
|
||||
}
|
||||
|
||||
// GetSystemPrompt 定义获取系统提示词的函数
|
||||
func GetSystemPrompt(req *dto.ComposeMessagesReq, model *entity.AsynchModel) string {
|
||||
mappingBytes, _ := json.Marshal(model.RequestMapping)
|
||||
mappingStr := string(mappingBytes)
|
||||
|
||||
// 解析 mapping
|
||||
var mapping map[string]string
|
||||
_ = json.Unmarshal(mappingBytes, mapping)
|
||||
|
||||
// 字段映射说明
|
||||
var fieldDesc strings.Builder
|
||||
for key, path := range mapping {
|
||||
fieldDesc.WriteString(fmt.Sprintf("- %s → %s\n", key, path))
|
||||
}
|
||||
|
||||
// ======================
|
||||
// 【核心】UserForm 全部内容完整展开,让模型必须全文阅读
|
||||
// 严格按你的业务定义:所有字段作为用户提示词来源
|
||||
// ======================
|
||||
var userFormContent strings.Builder
|
||||
for k, v := range req.UserForm {
|
||||
userFormContent.WriteString(fmt.Sprintf("%s=%v;", k, v))
|
||||
}
|
||||
userFormFullText := strings.TrimSuffix(userFormContent.String(), ";")
|
||||
|
||||
// 拼接双表单
|
||||
formInfo := fmt.Sprintf(`
|
||||
【系统表单(系统提示词/参数)】
|
||||
%s
|
||||
|
||||
【用户表单全文(必须完整阅读,全部作为用户提示词)】
|
||||
%s
|
||||
`, formToJSON(req.Form), userFormFullText)
|
||||
|
||||
// 最终提示词(严格遵守你所有规则)
|
||||
systemPrompt := fmt.Sprintf(`
|
||||
你是【语义理解 + 结构对齐】的JSON生成专家,必须严格遵守以下所有规则。
|
||||
|
||||
【强制阅读规则 · 必须100%%遵守】
|
||||
1. 必须完整通读全部文本、上下文、规则、表单内容,严禁跳读、略读;
|
||||
2. 未读完全部信息前,禁止输出任何内容;
|
||||
3. 必须全覆盖所有约束、所有细节、所有字段后再推理;
|
||||
4. 禁止断章取义,禁止遗漏任何参数;
|
||||
5. 必须严格区分系统表单、用户表单。
|
||||
|
||||
【核心语义规则】
|
||||
1. Form = 系统提示词、系统参数、默认配置
|
||||
2. UserForm = 用户真实输入全文,所有字段都必须作为用户提示词来源
|
||||
3. 若 UserForm 字段与 Form 含义相同 → UserForm 严格覆盖 Form
|
||||
4. 必须完整使用 UserForm 所有内容,不得遗漏任何一个字段
|
||||
|
||||
【任务】
|
||||
根据双表单内容,智能填充JSON结构:
|
||||
1. 理解意图:图片/文案
|
||||
2. 自动推导数量:各2张=4,一共3张=3
|
||||
3. 自动补全默认值:size=1024*1024
|
||||
4. 严格按结构输出,不修改字段
|
||||
|
||||
【输出结构】
|
||||
%s
|
||||
【字段映射关系】
|
||||
%s
|
||||
【完整输入信息】
|
||||
%s
|
||||
|
||||
【输出铁律】
|
||||
1. 只输出单行JSON,无任何多余字符
|
||||
2. 禁止换行、禁止转义、禁止解释
|
||||
3. 内容准确、无废话、不编造
|
||||
4. 必须完整读取 UserForm 全部内容
|
||||
|
||||
请输出最终JSON:
|
||||
`, mappingStr, fieldDesc.String(), formInfo)
|
||||
|
||||
return systemPrompt
|
||||
}
|
||||
|
||||
func formToJSON(form map[string]any) string {
|
||||
if form == nil {
|
||||
return "{}"
|
||||
}
|
||||
b, _ := json.Marshal(form)
|
||||
return string(b)
|
||||
}
|
||||
Reference in New Issue
Block a user