126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
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(ctx, 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 定义获取系统提示词的函数
|
||
// GetSystemPrompt 从配置文件读取提示词 + 格式化变量
|
||
func GetSystemPrompt(ctx context.Context, req *dto.ComposeMessagesReq, model *entity.AsynchModel) string {
|
||
// 1. 从配置文件读取提示词模板
|
||
promptTpl := g.Cfg().MustGet(ctx, "projectPrompts", "").String()
|
||
if promptTpl == "" {
|
||
return ""
|
||
}
|
||
|
||
// 2. 构建字段映射说明
|
||
mappingBytes, _ := json.Marshal(model.RequestMapping)
|
||
mappingStr := string(mappingBytes)
|
||
|
||
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))
|
||
}
|
||
|
||
// 3. 拼接 UserForm 全文(必须完整阅读)
|
||
var userFormContent strings.Builder
|
||
for k, v := range req.UserForm {
|
||
userFormContent.WriteString(fmt.Sprintf("%s=%v;", k, v))
|
||
}
|
||
userFormFullText := strings.TrimSuffix(userFormContent.String(), ";")
|
||
|
||
// 4. 双表单信息
|
||
formInfo := fmt.Sprintf(`
|
||
【系统表单(系统提示词/参数)】
|
||
%s
|
||
【用户表单全文(必须完整阅读,全部作为用户提示词来源)】
|
||
%s
|
||
`, formToJSON(req.Form), userFormFullText)
|
||
// 5. 格式化最终提示词(替换配置里的 %s)
|
||
return fmt.Sprintf(promptTpl, mappingStr, fieldDesc.String(), formInfo)
|
||
}
|
||
|
||
// formToJSON 工具函数不变
|
||
func formToJSON(form map[string]any) string {
|
||
if form == nil {
|
||
return "{}"
|
||
}
|
||
b, _ := json.Marshal(form)
|
||
return string(b)
|
||
}
|