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)
|
||||
}
|
||||
362
service/compose_parser.go
Normal file
362
service/compose_parser.go
Normal file
@@ -0,0 +1,362 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"prompts-core/model/dto"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// 类型定义
|
||||
// ============================================
|
||||
|
||||
// modelOutput 推理模型的标准输出格式
|
||||
type modelOutput struct {
|
||||
Messages []dto.Message `json:"messages"`
|
||||
System any `json:"system"`
|
||||
User any `json:"user"`
|
||||
}
|
||||
|
||||
// gatewayResponse 模型网关的标准响应格式
|
||||
type gatewayResponse struct {
|
||||
Choices []choice `json:"choices"`
|
||||
}
|
||||
|
||||
type choice struct {
|
||||
Message message `json:"message"`
|
||||
}
|
||||
|
||||
type message struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 核心解析函数
|
||||
// ============================================
|
||||
|
||||
// ParseModelResponse 解析推理模型的文本响应,返回消息列表
|
||||
// 支持三种格式:
|
||||
// 1. 标准 messages 格式: {"messages": [...]}
|
||||
// 2. 简化 system/user 格式: {"system": "...", "user": "..."}
|
||||
// 3. 网关包装格式: {"choices": [{"message": {"content": "..."}}]}
|
||||
func ParseModelResponse(text string) ([]dto.Message, error) {
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
return nil, errors.New("模型响应为空")
|
||||
}
|
||||
|
||||
// 1. 尝试解包网关响应
|
||||
if content := unwrapGatewayResponse(text); content != "" {
|
||||
text = content
|
||||
}
|
||||
|
||||
// 2. 解析为标准格式
|
||||
output, err := parseAsModelOutput(text)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析模型输出失败: %w", err)
|
||||
}
|
||||
|
||||
// 3. 优先使用 messages 字段
|
||||
if len(output.Messages) > 0 {
|
||||
messages := normalizeMessageContents(output.Messages)
|
||||
if err := validateMessageList(messages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// 4. 兼容 system/user 格式
|
||||
return buildMessagesFromSystemUser(output)
|
||||
}
|
||||
|
||||
// ParseStoredMessages 从数据库存储的数据中解析消息列表
|
||||
func ParseStoredMessages(data any) []dto.Message {
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 统一序列化
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 尝试直接解析
|
||||
var messages []dto.Message
|
||||
if err := json.Unmarshal(jsonBytes, &messages); err == nil {
|
||||
return messages
|
||||
}
|
||||
|
||||
// 尝试解析为 JSON 字符串再解析
|
||||
var jsonStr string
|
||||
if err := json.Unmarshal(jsonBytes, &jsonStr); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(jsonStr), &messages); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 内部解析函数
|
||||
// ============================================
|
||||
|
||||
// parseAsModelOutput 将文本解析为 modelOutput 结构
|
||||
func parseAsModelOutput(text string) (*modelOutput, error) {
|
||||
// 清理可能的 Markdown 代码块标记
|
||||
text = cleanMarkdownCodeBlock(text)
|
||||
|
||||
var output modelOutput
|
||||
if err := json.Unmarshal([]byte(text), &output); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &output, nil
|
||||
}
|
||||
|
||||
// unwrapGatewayResponse 解包网关的标准响应格式
|
||||
func unwrapGatewayResponse(text string) string {
|
||||
// 快速检查是否可能是网关响应
|
||||
if !strings.Contains(text, `"choices"`) {
|
||||
return ""
|
||||
}
|
||||
|
||||
var resp gatewayResponse
|
||||
if err := json.Unmarshal([]byte(text), &resp); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(resp.Choices) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
content := strings.TrimSpace(resp.Choices[0].Message.Content)
|
||||
return content
|
||||
}
|
||||
|
||||
// buildMessagesFromSystemUser 从 system/user 字段构建消息列表
|
||||
func buildMessagesFromSystemUser(output *modelOutput) ([]dto.Message, error) {
|
||||
messages := make([]dto.Message, 0, 2)
|
||||
|
||||
// 添加 user 消息
|
||||
if !isEmptyValue(output.User) {
|
||||
messages = append(messages, dto.Message{
|
||||
Role: "user",
|
||||
Content: normalizeContent(output.User),
|
||||
})
|
||||
}
|
||||
|
||||
// 添加 system 消息
|
||||
if !isEmptyValue(output.System) {
|
||||
messages = append(messages, dto.Message{
|
||||
Role: "system",
|
||||
Content: normalizeContent(output.System),
|
||||
})
|
||||
}
|
||||
|
||||
if len(messages) == 0 {
|
||||
return nil, errors.New("未解析到有效的 system 或 user 内容")
|
||||
}
|
||||
|
||||
if err := validateMessageList(messages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 内容规范化
|
||||
// ============================================
|
||||
|
||||
// normalizeMessageContents 规范化消息列表中的所有内容
|
||||
func normalizeMessageContents(messages []dto.Message) []dto.Message {
|
||||
for i := range messages {
|
||||
messages[i].Content = normalizeContent(messages[i].Content)
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
// normalizeContent 规范化单个消息内容
|
||||
// - 如果是 JSON 字符串,尝试解析为对象/数组
|
||||
// - 否则保持原样
|
||||
func normalizeContent(content any) any {
|
||||
switch v := content.(type) {
|
||||
case string:
|
||||
return tryUnmarshalJSON(v)
|
||||
default:
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
// tryUnmarshalJSON 尝试将 JSON 字符串解析为结构化对象
|
||||
func tryUnmarshalJSON(s string) any {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
|
||||
// 只处理看起来像 JSON 的内容
|
||||
if !looksLikeJSON(s) {
|
||||
return s
|
||||
}
|
||||
|
||||
var result any
|
||||
if err := json.Unmarshal([]byte(s), &result); err != nil || result == nil {
|
||||
return s
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// looksLikeJSON 判断字符串是否可能是 JSON
|
||||
func looksLikeJSON(s string) bool {
|
||||
s = strings.TrimSpace(s)
|
||||
return strings.HasPrefix(s, "{") || strings.HasPrefix(s, "[")
|
||||
}
|
||||
|
||||
// cleanMarkdownCodeBlock 清理 Markdown 代码块标记
|
||||
func cleanMarkdownCodeBlock(text string) string {
|
||||
// 去除可能的 ```json 和 ``` 标记
|
||||
text = strings.TrimPrefix(text, "```json")
|
||||
text = strings.TrimPrefix(text, "```JSON")
|
||||
text = strings.TrimPrefix(text, "```")
|
||||
text = strings.TrimSuffix(text, "```")
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 验证
|
||||
// ============================================
|
||||
|
||||
// validateMessageList 验证消息列表的合法性
|
||||
func validateMessageList(messages []dto.Message) error {
|
||||
if len(messages) == 0 {
|
||||
return errors.New("消息列表不能为空")
|
||||
}
|
||||
|
||||
hasUser := false
|
||||
for i, msg := range messages {
|
||||
if err := validateMessage(msg); err != nil {
|
||||
return fmt.Errorf("消息[%d]验证失败: %w", i, err)
|
||||
}
|
||||
if msg.Role == "user" {
|
||||
hasUser = true
|
||||
}
|
||||
}
|
||||
|
||||
// 至少需要一条 user 消息
|
||||
if !hasUser {
|
||||
return errors.New("消息列表必须包含至少一条 user 角色消息")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateMessage 验证单条消息的合法性
|
||||
func validateMessage(msg dto.Message) error {
|
||||
role := strings.TrimSpace(msg.Role)
|
||||
if role == "" {
|
||||
return errors.New("role 不能为空")
|
||||
}
|
||||
|
||||
if !isValidRole(role) {
|
||||
return fmt.Errorf("role 值非法: %s (仅允许 system/user/assistant)", role)
|
||||
}
|
||||
|
||||
// user 角色的 content 不能为空
|
||||
if role == "user" && isEmptyValue(msg.Content) {
|
||||
return errors.New("user 角色的 content 不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// isValidRole 判断角色是否合法
|
||||
func isValidRole(role string) bool {
|
||||
switch role {
|
||||
case "system", "user", "assistant":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// HasUserMessage 判断消息列表中是否包含非空的 user 消息
|
||||
func HasUserMessage(messages []dto.Message) bool {
|
||||
for _, msg := range messages {
|
||||
if msg.Role == "user" && !isEmptyValue(msg.Content) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HasSystemMessage 判断消息列表中是否包含非空的 system 消息
|
||||
func HasSystemMessage(messages []dto.Message) bool {
|
||||
for _, msg := range messages {
|
||||
if msg.Role == "system" && !isEmptyValue(msg.Content) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ExtractUserContent 提取消息列表中第一个 user 角色的内容
|
||||
func ExtractUserContent(messages []dto.Message) any {
|
||||
for _, msg := range messages {
|
||||
if msg.Role == "user" {
|
||||
return msg.Content
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractSystemContent 提取消息列表中第一个 system 角色的内容
|
||||
func ExtractSystemContent(messages []dto.Message) any {
|
||||
for _, msg := range messages {
|
||||
if msg.Role == "system" {
|
||||
return msg.Content
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 测试辅助函数 (可选)
|
||||
// ============================================
|
||||
|
||||
// MockModelResponse 创建模拟的模型响应用于测试
|
||||
func MockModelResponse(systemContent, userContent string) string {
|
||||
output := modelOutput{
|
||||
Messages: []dto.Message{
|
||||
{Role: "system", Content: systemContent},
|
||||
{Role: "user", Content: userContent},
|
||||
},
|
||||
}
|
||||
|
||||
bytes, _ := json.Marshal(output)
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
// MockGatewayResponse 创建模拟的网关响应用于测试
|
||||
func MockGatewayResponse(innerJSON string) string {
|
||||
resp := gatewayResponse{
|
||||
Choices: []choice{
|
||||
{
|
||||
Message: message{
|
||||
Content: innerJSON,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
bytes, _ := json.Marshal(resp)
|
||||
return string(bytes)
|
||||
}
|
||||
510
service/compose_service.go
Normal file
510
service/compose_service.go
Normal file
@@ -0,0 +1,510 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"prompts-core/consts/public"
|
||||
"prompts-core/dao"
|
||||
"prompts-core/model/dto"
|
||||
"prompts-core/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// 核心业务流程
|
||||
// ============================================
|
||||
|
||||
// ComposeMessages 拼接提示词主流程
|
||||
func (s *promptService) ComposeMessages(ctx context.Context, req *dto.ComposeMessagesReq) (*dto.ComposeMessagesRes, error) {
|
||||
var (
|
||||
epicycleId int64
|
||||
err error
|
||||
historyMessages []Message // 用来存放历史会话
|
||||
)
|
||||
// 1. 如果不需要构建返回记录id
|
||||
if req.IsBuilder == false {
|
||||
epicycleId, err = dao.ComposeSession.Insert(ctx, &entity.ComposeSession{
|
||||
SessionId: req.SessionId,
|
||||
Remark: req.Cause,
|
||||
})
|
||||
return &dto.ComposeMessagesRes{
|
||||
EpicycleId: epicycleId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 2. 获取当前用户模型信息
|
||||
sessionModel, err := dao.Model.GetByIsChatModel(ctx) //获取会话模型
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sessionModel == nil {
|
||||
return nil, errors.New("当前没有对话模型,请添加")
|
||||
}
|
||||
model, err := dao.Model.GetByModelName(ctx, req.ModelName) //获取模型信息
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("模型 %s 不存在", sessionModel.ModelName)
|
||||
}
|
||||
|
||||
// 3 获取历史会话
|
||||
historyMessages, err = Session.GetSessionHistoryForInference(ctx, req.SessionId)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "获取历史会话失败: %v,将不使用历史会话", err)
|
||||
historyMessages = nil // 出错就用空的,不影响主流程
|
||||
}
|
||||
|
||||
// 4. 调用推理模型
|
||||
taskID, err := s.callInferenceModel(ctx, req, sessionModel, model, historyMessages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 5. 保存相关记录
|
||||
_, err = dao.ComposeTask.Insert(ctx, &entity.ComposeTask{
|
||||
TaskId: taskID,
|
||||
ModelName: req.ModelName,
|
||||
SkillName: req.SkillName,
|
||||
RequestPayload: mustMarshal(req),
|
||||
Status: public.ComposeStatusPending,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 6. 等待结果
|
||||
taskRecord, err := s.waitForResult(ctx, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 7. 处理返回结果
|
||||
messages := s.processResult(taskRecord)
|
||||
|
||||
//8.1 数据库查询当前会话是否存在
|
||||
session, err := dao.ComposeSession.GetBySessionId(ctx, req.SessionId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if session == nil {
|
||||
//8.2 不存在则创建新会话记录
|
||||
epicycleId, err = dao.ComposeSession.Insert(ctx, &entity.ComposeSession{
|
||||
SessionId: req.SessionId,
|
||||
RequestContent: messages,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 9. 更新历史会话
|
||||
_, err = dao.ComposeSession.UpdateById(ctx, epicycleId, map[string]any{
|
||||
entity.ComposeSessionCol.RequestContent: messages,
|
||||
})
|
||||
|
||||
return &dto.ComposeMessagesRes{
|
||||
Messages: messages,
|
||||
EpicycleId: epicycleId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *promptService) Callback(ctx context.Context, req *dto.CallbackReq) error {
|
||||
g.Log().Infof(ctx, "[Callback][RECV] taskId=%s state=%d ossFile=%s fileType=%s textLen=%d",
|
||||
req.TaskId, req.State, req.OssFile, req.FileType, len(req.Text))
|
||||
|
||||
// ============ 先查任务是否存在 ============
|
||||
task, err := dao.ComposeTask.GetByTaskId(ctx, req.TaskId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if task == nil {
|
||||
return fmt.Errorf("任务不存在: %s", req.TaskId)
|
||||
}
|
||||
// ============ 根据状态区分处理 ============
|
||||
if req.State == 3 {
|
||||
// 失败:直接更新状态
|
||||
_, err = dao.ComposeTask.UpdateByTaskId(ctx, req.TaskId, map[string]any{
|
||||
entity.ComposeTaskCol.Status: public.ComposeStatusFailed,
|
||||
entity.ComposeTaskCol.ErrorMessage: req.ErrorMsg,
|
||||
})
|
||||
return err
|
||||
}
|
||||
// ======================================
|
||||
// 成功:解析模型输出
|
||||
result, err := parseModelOutput(req.Text)
|
||||
if err != nil {
|
||||
_, updateErr := dao.ComposeTask.UpdateByTaskId(ctx, req.TaskId, map[string]any{
|
||||
entity.ComposeTaskCol.Status: public.ComposeStatusFailed,
|
||||
entity.ComposeTaskCol.ErrorMessage: err.Error(),
|
||||
})
|
||||
if updateErr != nil {
|
||||
g.Log().Warningf(ctx, "[Callback] 更新失败状态出错 taskId=%s err=%v", req.TaskId, updateErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ============ result 可能为 nil ============
|
||||
var messages any
|
||||
if result != nil {
|
||||
messages = result
|
||||
}
|
||||
// =======================================
|
||||
|
||||
_, err = dao.ComposeTask.UpdateByTaskId(ctx, req.TaskId, map[string]any{
|
||||
entity.ComposeTaskCol.Status: public.ComposeStatusSuccess,
|
||||
entity.ComposeTaskCol.Messages: messages,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "[Callback] 更新任务失败 taskId=%s err=%v", req.TaskId, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// GetComposeTask 查询任务结果
|
||||
func (s *promptService) GetComposeTask(ctx context.Context, taskID string) (*dto.GetComposeTaskRes, error) {
|
||||
record, err := dao.ComposeTask.GetByTaskId(ctx, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if record == nil {
|
||||
return nil, fmt.Errorf("未找到任务(taskId=%s)", taskID)
|
||||
}
|
||||
|
||||
// 如果 Messages 是字符串,反序列化为 JSON 数组
|
||||
messages := record.Messages
|
||||
if str, ok := messages.(string); ok && str != "" {
|
||||
var parsed any
|
||||
if err := json.Unmarshal([]byte(str), &parsed); err == nil {
|
||||
messages = parsed
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.GetComposeTaskRes{
|
||||
TaskId: record.TaskId,
|
||||
Status: record.Status,
|
||||
ErrorMessage: record.ErrorMessage,
|
||||
Messages: messages,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 步骤4:调用推理模型
|
||||
// ============================================
|
||||
|
||||
func (s *promptService) callInferenceModel(ctx context.Context, req *dto.ComposeMessagesReq, sessionModel *entity.AsynchModel, model *entity.AsynchModel, historyMessages []Message) (string, error) {
|
||||
// 构建推理模型请求
|
||||
taskReq, err := buildInferenceRequest(ctx, req, sessionModel, model, historyMessages)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("构建推理请求失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建网关任务
|
||||
taskID, err := createGatewayTask(ctx, taskReq)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("创建网关任务失败: %w", err)
|
||||
}
|
||||
|
||||
if taskID == "" {
|
||||
return "", errors.New("网关未返回taskId")
|
||||
}
|
||||
|
||||
return taskID, nil
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 步骤6:等待结果
|
||||
// ============================================
|
||||
|
||||
func (s *promptService) waitForResult(ctx context.Context, taskID string) (*entity.ComposeTask, error) {
|
||||
timeout := time.Duration(getIntConfig(ctx, "task.waitTimeoutSeconds", 30)) * time.Second
|
||||
pollInterval := time.Duration(getIntConfig(ctx, "task.pollIntervalMillis", 500)) * time.Millisecond
|
||||
deadline := time.Now().Add(timeout)
|
||||
|
||||
for {
|
||||
// 1. 查数据库
|
||||
record, err := dao.ComposeTask.GetByTaskId(ctx, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if record != nil {
|
||||
switch record.Status {
|
||||
case public.ComposeStatusSuccess:
|
||||
return record, nil
|
||||
case public.ComposeStatusFailed:
|
||||
return nil, formatTaskError(taskID, record.ErrorMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 查网关状态
|
||||
state, err := queryGatewayTaskState(ctx, taskID)
|
||||
if err != nil {
|
||||
// ============ 网关不可达不终止,继续轮询 ============
|
||||
g.Log().Warningf(ctx, "[waitForResult] 查询网关失败 taskId=%s err=%v", taskID, err)
|
||||
} else {
|
||||
switch state {
|
||||
case 2: // 网关成功
|
||||
// ============ 网关已成功,主动更新数据库 ============
|
||||
if record != nil {
|
||||
dao.ComposeTask.UpdateByTaskId(ctx, taskID, map[string]any{
|
||||
entity.ComposeTaskCol.Status: public.ComposeStatusSuccess,
|
||||
})
|
||||
}
|
||||
case 3: // 网关失败
|
||||
if record != nil {
|
||||
dao.ComposeTask.UpdateByTaskId(ctx, taskID, map[string]any{
|
||||
entity.ComposeTaskCol.Status: public.ComposeStatusFailed,
|
||||
entity.ComposeTaskCol.ErrorMessage: "model-gateway 任务执行失败",
|
||||
})
|
||||
}
|
||||
return nil, fmt.Errorf("model-gateway 任务执行失败(taskId=%s)", taskID)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 超时检查
|
||||
if time.Now().After(deadline) {
|
||||
return nil, fmt.Errorf("等待任务回调超时(taskId=%s)", taskID)
|
||||
}
|
||||
time.Sleep(pollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 步骤6:处理结果
|
||||
// ============================================
|
||||
func (s *promptService) processResult(taskRecord *entity.ComposeTask) map[string]any {
|
||||
if taskRecord == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 1. 解析 Messages 获取 content
|
||||
var contentStr string
|
||||
switch v := taskRecord.Messages.(type) {
|
||||
case *gvar.Var:
|
||||
if v != nil {
|
||||
var mapped map[string]any
|
||||
json.Unmarshal([]byte(v.String()), &mapped)
|
||||
if c, ok := mapped["content"].(string); ok {
|
||||
contentStr = c
|
||||
}
|
||||
}
|
||||
case string:
|
||||
var mapped map[string]any
|
||||
json.Unmarshal([]byte(v), &mapped)
|
||||
if c, ok := mapped["content"].(string); ok {
|
||||
contentStr = c
|
||||
}
|
||||
case map[string]any:
|
||||
if c, ok := v["content"].(string); ok {
|
||||
contentStr = c
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 清理并解析
|
||||
contentStr = cleanJSONString(contentStr)
|
||||
var innerData map[string]any
|
||||
json.Unmarshal([]byte(contentStr), &innerData)
|
||||
|
||||
return innerData
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 消息处理管道
|
||||
// ============================================
|
||||
|
||||
// parseStoredMessages 从数据库存储的数据中解析消息列表
|
||||
// 处理多层 JSON 嵌套的情况
|
||||
func parseStoredMessages(data any) []dto.Message {
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 统一序列化为 JSON
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 第一层解析:尝试直接解析为消息数组
|
||||
var messages []dto.Message
|
||||
if err := json.Unmarshal(jsonBytes, &messages); err == nil {
|
||||
// 成功解析,但需要处理 content 可能是 JSON 字符串的情况
|
||||
return deepNormalizeMessages(messages)
|
||||
}
|
||||
|
||||
// 第二层解析:可能是 JSON 字符串包裹的数组
|
||||
var rawStr string
|
||||
if err := json.Unmarshal(jsonBytes, &rawStr); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 尝试解析字符串为消息数组
|
||||
if err := json.Unmarshal([]byte(rawStr), &messages); err == nil {
|
||||
return deepNormalizeMessages(messages)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// deepNormalizeMessages 深度规范化消息,处理 content 为 JSON 字符串的情况
|
||||
func deepNormalizeMessages(messages []dto.Message) []dto.Message {
|
||||
for i, msg := range messages {
|
||||
messages[i].Content = deepNormalizeContent(msg.Content)
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
||||
// deepNormalizeContent 递归处理 content,支持多层 JSON 嵌套
|
||||
func deepNormalizeContent(content any) any {
|
||||
switch v := content.(type) {
|
||||
case string:
|
||||
// 尝试解析 JSON 字符串
|
||||
v = strings.TrimSpace(v)
|
||||
if v == "" {
|
||||
return v
|
||||
}
|
||||
|
||||
// 如果看起来像 JSON,尝试解析
|
||||
if looksLikeJSON(v) {
|
||||
var parsed any
|
||||
if err := json.Unmarshal([]byte(v), &parsed); err == nil {
|
||||
// 递归处理解析后的内容
|
||||
return deepNormalizeContent(parsed)
|
||||
}
|
||||
}
|
||||
return v
|
||||
|
||||
case []any:
|
||||
// 递归处理数组中的每个元素
|
||||
result := make([]any, len(v))
|
||||
for i, item := range v {
|
||||
result[i] = deepNormalizeContent(item)
|
||||
}
|
||||
return result
|
||||
|
||||
case map[string]any:
|
||||
// 递归处理 map 中的每个值
|
||||
result := make(map[string]any, len(v))
|
||||
for k, val := range v {
|
||||
result[k] = deepNormalizeContent(val)
|
||||
}
|
||||
return result
|
||||
|
||||
default:
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
func NormalizeToTwoPart(messages []dto.Message, req *dto.ComposeMessagesReq) []dto.Message {
|
||||
var result []dto.Message
|
||||
|
||||
// 1. 提取 system
|
||||
sysContent := extractByRole(messages, "system")
|
||||
if sysContent == nil {
|
||||
sysContent = renderFormText(req.Form, false)
|
||||
}
|
||||
result = append(result, dto.Message{Role: "system", Content: sysContent})
|
||||
|
||||
// 2. 提取 form
|
||||
formContent := extractByRole(messages, "form")
|
||||
if formContent != nil {
|
||||
result = append(result, dto.Message{Role: "form", Content: formContent})
|
||||
} else if req != nil {
|
||||
result = append(result, dto.Message{Role: "form", Content: renderFormJSON(req.Form)})
|
||||
}
|
||||
|
||||
// 3. 提取 skill
|
||||
skillContent := extractByRole(messages, "skill")
|
||||
if skillContent != nil {
|
||||
result = append(result, dto.Message{Role: "skill", Content: skillContent})
|
||||
} else if req != nil && req.SkillName != "" {
|
||||
result = append(result, dto.Message{Role: "skill", Content: req.SkillName})
|
||||
}
|
||||
|
||||
// 4. 提取 history(如果模型返回了压缩后的历史)
|
||||
historyContent := extractByRole(messages, "history")
|
||||
if historyContent != nil {
|
||||
result = append(result, dto.Message{Role: "history", Content: historyContent})
|
||||
}
|
||||
|
||||
// 5. 提取 user
|
||||
usrContent := extractByRole(messages, "user")
|
||||
if usrContent == nil {
|
||||
usrContent = renderUserText(req.UserForm, req.Form)
|
||||
}
|
||||
result = append(result, dto.Message{Role: "user", Content: usrContent})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 辅助函数:按 role 提取第一个非空 content
|
||||
// ============================================
|
||||
|
||||
func extractByRole(messages []dto.Message, role string) any {
|
||||
for _, msg := range messages {
|
||||
if msg.Role == role && !isEmptyValue(msg.Content) {
|
||||
return msg.Content
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 辅助函数:将 form 渲染为 JSON 对象
|
||||
// ============================================
|
||||
|
||||
func renderFormJSON(form map[string]any) map[string]any {
|
||||
if form == nil {
|
||||
return nil
|
||||
}
|
||||
result := make(map[string]any)
|
||||
for k, v := range form {
|
||||
result[k] = v
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func enrichSystemMessages(messages []dto.Message, req *dto.ComposeMessagesReq) []dto.Message {
|
||||
if len(messages) == 0 {
|
||||
return messages
|
||||
}
|
||||
|
||||
// 获取系统字段的值映射
|
||||
systemValues := extractSystemValues(req)
|
||||
|
||||
for i, msg := range messages {
|
||||
if msg.Role != "system" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 为 schema 数组补充 value
|
||||
switch content := msg.Content.(type) {
|
||||
case []any:
|
||||
messages[i].Content = enrichSchemaWithValues(content, systemValues)
|
||||
case []map[string]any:
|
||||
arr := make([]any, len(content))
|
||||
for j, item := range content {
|
||||
arr[j] = item
|
||||
}
|
||||
messages[i].Content = enrichSchemaWithValues(arr, systemValues)
|
||||
case map[string]any:
|
||||
// 合并但不覆盖已有值
|
||||
for k, v := range systemValues {
|
||||
if _, exists := content[k]; !exists {
|
||||
content[k] = v
|
||||
}
|
||||
}
|
||||
messages[i].Content = content
|
||||
}
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
131
service/files_service.go
Normal file
131
service/files_service.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// 文件处理
|
||||
// ============================================
|
||||
|
||||
func fetchFileTexts(ctx context.Context, urls []string) map[string]string {
|
||||
result := make(map[string]string)
|
||||
|
||||
if len(urls) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 8 * time.Second,
|
||||
}
|
||||
|
||||
for _, rawURL := range urls {
|
||||
url := sanitizeURL(rawURL)
|
||||
if url == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
text, err := fetchFileContent(ctx, client, url)
|
||||
if err != nil {
|
||||
glog.Warningf(ctx,
|
||||
"[FetchFile] failed url=%s err=%v",
|
||||
url,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if text == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
result[url] = text
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func fetchFileContent(
|
||||
ctx context.Context,
|
||||
client *http.Client,
|
||||
url string,
|
||||
) (string, error) {
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
url,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// HTTP状态检查
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return "", fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Content-Type检查
|
||||
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
|
||||
|
||||
if !isTextContentType(contentType) {
|
||||
return "", fmt.Errorf("unsupported content-type: %s", contentType)
|
||||
}
|
||||
|
||||
// 最大读取20KB
|
||||
body, err := io.ReadAll(
|
||||
io.LimitReader(resp.Body, 20*1024),
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.TrimSpace(string(body)), nil
|
||||
}
|
||||
|
||||
// 判断是否为文本类型
|
||||
func isTextContentType(contentType string) bool {
|
||||
|
||||
// text/*
|
||||
if strings.HasPrefix(contentType, "text/") {
|
||||
return true
|
||||
}
|
||||
|
||||
// 常见文本类型
|
||||
allowTypes := []string{
|
||||
"application/json",
|
||||
"application/xml",
|
||||
"application/javascript",
|
||||
"application/x-yaml",
|
||||
"application/yaml",
|
||||
"application/toml",
|
||||
}
|
||||
|
||||
for _, t := range allowTypes {
|
||||
if strings.Contains(contentType, t) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func sanitizeURL(raw string) string {
|
||||
s := strings.TrimSpace(raw)
|
||||
s = strings.Trim(s, "`\"")
|
||||
return s
|
||||
}
|
||||
325
service/form_processor.go
Normal file
325
service/form_processor.go
Normal file
@@ -0,0 +1,325 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// 表单处理
|
||||
// ============================================
|
||||
|
||||
// FormProcessor 表单处理器
|
||||
type FormProcessor struct {
|
||||
SystemForm map[string]any
|
||||
UserForm map[string]any
|
||||
}
|
||||
|
||||
// NewFormProcessor 创建表单处理器
|
||||
func NewFormProcessor(systemForm, userForm map[string]any) *FormProcessor {
|
||||
return &FormProcessor{
|
||||
SystemForm: systemForm,
|
||||
UserForm: userForm,
|
||||
}
|
||||
}
|
||||
|
||||
// Merge 合并表单,用户表单覆盖系统表单
|
||||
func (p *FormProcessor) Merge() map[string]any {
|
||||
if len(p.SystemForm) == 0 {
|
||||
return p.SystemForm
|
||||
}
|
||||
|
||||
result := make(map[string]any)
|
||||
for k, v := range p.SystemForm {
|
||||
result[k] = v
|
||||
}
|
||||
|
||||
if len(p.UserForm) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
// 构建用户表单索引
|
||||
userIndex := buildFieldIndex(p.UserForm)
|
||||
|
||||
// 覆盖匹配的字段
|
||||
for key, value := range result {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
field := getField(item, key)
|
||||
|
||||
if userItem, exists := findInIndex(userIndex, field, getLabel(item)); exists {
|
||||
if userValue := getValue(userItem); !isNilOrEmpty(userValue) {
|
||||
result[key] = cloneWithValue(item, userValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// RemoveDuplicates 移除被用户表单覆盖的字段
|
||||
func (p *FormProcessor) RemoveDuplicates() map[string]any {
|
||||
if len(p.SystemForm) == 0 || len(p.UserForm) == 0 {
|
||||
return p.SystemForm
|
||||
}
|
||||
|
||||
userFields := buildFieldSet(p.UserForm)
|
||||
result := make(map[string]any)
|
||||
|
||||
for key, value := range p.SystemForm {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
result[key] = value
|
||||
continue
|
||||
}
|
||||
|
||||
field := getField(item, key)
|
||||
label := getLabel(item)
|
||||
|
||||
// 跳过重复字段
|
||||
if userFields.contains(field) || userFields.containsLabel(label) {
|
||||
continue
|
||||
}
|
||||
|
||||
result[key] = value
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// RemoveSemanticDuplicates 语义去重
|
||||
func (p *FormProcessor) RemoveSemanticDuplicates() map[string]any {
|
||||
if len(p.SystemForm) == 0 || len(p.UserForm) == 0 {
|
||||
return p.SystemForm
|
||||
}
|
||||
|
||||
userText := renderUserTextOnly(p.UserForm)
|
||||
if userText == "" {
|
||||
return p.SystemForm
|
||||
}
|
||||
|
||||
result := make(map[string]any)
|
||||
for key, value := range p.SystemForm {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
result[key] = value
|
||||
continue
|
||||
}
|
||||
|
||||
if isDuplicate(userText, getField(item, key), getLabel(item), getValue(item)) {
|
||||
continue
|
||||
}
|
||||
|
||||
result[key] = value
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// RenderSystemText 渲染系统提示词文本
|
||||
func (p *FormProcessor) RenderSystemText() string {
|
||||
return renderFormText(p.SystemForm, false)
|
||||
}
|
||||
|
||||
// RenderUserText 渲染用户提示词文本
|
||||
func (p *FormProcessor) RenderUserText() string {
|
||||
return renderUserText(p.UserForm, p.SystemForm)
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 表单处理辅助方法
|
||||
// ============================================
|
||||
|
||||
type fieldSet struct {
|
||||
fields map[string]bool
|
||||
labels map[string]bool
|
||||
}
|
||||
|
||||
func buildFieldSet(form map[string]any) *fieldSet {
|
||||
fs := &fieldSet{
|
||||
fields: make(map[string]bool),
|
||||
labels: make(map[string]bool),
|
||||
}
|
||||
|
||||
for key, value := range form {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
field := strings.ToLower(getField(item, key))
|
||||
if field != "" {
|
||||
fs.fields[field] = true
|
||||
}
|
||||
|
||||
if label := strings.ToLower(getLabel(item)); label != "" {
|
||||
fs.labels[label] = true
|
||||
}
|
||||
}
|
||||
|
||||
return fs
|
||||
}
|
||||
|
||||
func (fs *fieldSet) contains(field string) bool {
|
||||
return fs.fields[strings.ToLower(field)]
|
||||
}
|
||||
|
||||
func (fs *fieldSet) containsLabel(label string) bool {
|
||||
return label != "" && fs.labels[strings.ToLower(label)]
|
||||
}
|
||||
|
||||
func buildFieldIndex(form map[string]any) map[string]map[string]any {
|
||||
index := make(map[string]map[string]any)
|
||||
|
||||
for key, value := range form {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
field := strings.ToLower(getField(item, key))
|
||||
if field != "" {
|
||||
index[field] = item
|
||||
}
|
||||
|
||||
if label := strings.ToLower(getLabel(item)); label != "" {
|
||||
if _, exists := index[label]; !exists {
|
||||
index[label] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
func findInIndex(index map[string]map[string]any, field, label string) (map[string]any, bool) {
|
||||
key := strings.ToLower(field)
|
||||
if item, ok := index[key]; ok {
|
||||
return item, true
|
||||
}
|
||||
|
||||
if label != "" {
|
||||
key = strings.ToLower(label)
|
||||
if item, ok := index[key]; ok {
|
||||
return item, true
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 表单渲染
|
||||
// ============================================
|
||||
|
||||
func renderFormText(form map[string]any, isUserForm bool) string {
|
||||
if len(form) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 用户表单只有一个文本字段时,直接返回值
|
||||
if isUserForm && len(form) == 1 {
|
||||
for _, value := range form {
|
||||
if item, ok := value.(map[string]any); ok {
|
||||
return strings.TrimSpace(asString(getValue(item)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接渲染
|
||||
items := extractFormItems(form)
|
||||
if isUserForm {
|
||||
return renderUserFormItems(items)
|
||||
}
|
||||
return renderSystemFormItems(items)
|
||||
}
|
||||
|
||||
type formItem struct {
|
||||
Key string
|
||||
Field string
|
||||
Label string
|
||||
Value any
|
||||
}
|
||||
|
||||
func extractFormItems(form map[string]any) []formItem {
|
||||
var items []formItem
|
||||
|
||||
keys := sortedKeys(form)
|
||||
for _, key := range keys {
|
||||
item, ok := form[key].(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
field := getField(item, key)
|
||||
value := getValue(item)
|
||||
|
||||
// 跳过敏感字段和空值
|
||||
if isSensitiveField(field) || isNilOrEmpty(value) {
|
||||
continue
|
||||
}
|
||||
|
||||
items = append(items, formItem{
|
||||
Key: key,
|
||||
Field: field,
|
||||
Label: getLabel(item),
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func renderUserFormItems(items []formItem) string {
|
||||
// 只有一个文本类型字段时,直接返回值
|
||||
if len(items) == 1 && isTextType(items[0].Field, items[0].Label) {
|
||||
return formatValue(items[0].Value)
|
||||
}
|
||||
|
||||
// 拼接
|
||||
var parts []string
|
||||
for _, item := range items {
|
||||
if isTextType(item.Field, item.Label) {
|
||||
parts = append(parts, formatValue(item.Value))
|
||||
} else {
|
||||
label := item.Label
|
||||
if label == "" {
|
||||
label = item.Field
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("%s:%s", label, formatValue(item.Value)))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
|
||||
func renderSystemFormItems(items []formItem) string {
|
||||
var parts []string
|
||||
for _, item := range items {
|
||||
label := item.Label
|
||||
if label == "" {
|
||||
label = item.Field
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("%s:%s", label, formatValue(item.Value)))
|
||||
}
|
||||
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
|
||||
func renderUserText(userForm, systemForm map[string]any) string {
|
||||
if text := renderFormText(userForm, true); text != "" {
|
||||
return text
|
||||
}
|
||||
// 用户表单为空时,使用系统表单生成
|
||||
if text := renderFormText(systemForm, false); text != "" {
|
||||
return "参考系统字段生成用户提示词:" + text
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func renderUserTextOnly(userForm map[string]any) string {
|
||||
return renderFormText(userForm, true)
|
||||
}
|
||||
69
service/headers.go
Normal file
69
service/headers.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// asyncCtx 固化异步执行所需的 token/user,避免请求结束后丢失(仅在“同请求内起 goroutine”有用)。
|
||||
// 本项目当前是“落库 + 后台 worker”模式,因此还会把必要信息持久化到任务表的 request_payload 中。
|
||||
func asyncCtx(ctx context.Context) context.Context {
|
||||
asyncCtx := context.WithoutCancel(ctx)
|
||||
if r := g.RequestFromCtx(ctx); r != nil {
|
||||
if token := r.Header.Get("Authorization"); token != "" {
|
||||
asyncCtx = context.WithValue(asyncCtx, "token", token)
|
||||
}
|
||||
if userInfo := r.Header.Get("X-User-Info"); userInfo != "" {
|
||||
asyncCtx = context.WithValue(asyncCtx, "xUserInfo", userInfo)
|
||||
}
|
||||
}
|
||||
if user, err := utils.GetUserInfo(ctx); err == nil && user != nil {
|
||||
asyncCtx = context.WithValue(asyncCtx, "user", user)
|
||||
}
|
||||
return asyncCtx
|
||||
}
|
||||
|
||||
// forwardHeaders 透传调用链路中必须的头信息(优先使用 ctx 里固化的 token / xUserInfo)。
|
||||
func forwardHeaders(ctx context.Context) map[string]string {
|
||||
headers := make(map[string]string)
|
||||
|
||||
if token, ok := ctx.Value("token").(string); ok && token != "" {
|
||||
headers["Authorization"] = token
|
||||
}
|
||||
if x, ok := ctx.Value("xUserInfo").(string); ok && x != "" {
|
||||
headers["X-User-Info"] = x
|
||||
}
|
||||
|
||||
// 兜底:从请求头拿
|
||||
if r := g.RequestFromCtx(ctx); r != nil {
|
||||
if headers["Authorization"] == "" {
|
||||
if token := r.Header.Get("Authorization"); token != "" {
|
||||
headers["Authorization"] = token
|
||||
}
|
||||
}
|
||||
if headers["X-User-Info"] == "" {
|
||||
if userInfo := r.Header.Get("X-User-Info"); userInfo != "" {
|
||||
headers["X-User-Info"] = userInfo
|
||||
}
|
||||
}
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
// GetTenantId 获取租户ID
|
||||
func GetTenantId(ctx context.Context) int64 {
|
||||
var currentTenantID int64
|
||||
if r := g.RequestFromCtx(ctx); r != nil {
|
||||
currentTenantID = gconv.Int64(r.Header.Get("X-Tenant-Id"))
|
||||
}
|
||||
if currentTenantID == 0 {
|
||||
userInfo, err := utils.GetUserInfo(ctx)
|
||||
if err == nil && userInfo != nil {
|
||||
currentTenantID = int64(userInfo.TenantId)
|
||||
}
|
||||
}
|
||||
return currentTenantID
|
||||
}
|
||||
54
service/http_service.go
Normal file
54
service/http_service.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
commonHttp "gitea.com/red-future/common/http"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// model-gateway 网关交互
|
||||
// ============================================
|
||||
|
||||
// CreateTaskReq 创建任务请求
|
||||
type CreateTaskReq struct {
|
||||
TaskId string `json:"task_id"`
|
||||
State int `json:"state"`
|
||||
OssFile string `json:"oss_file"`
|
||||
FileType string `json:"file_type"`
|
||||
Text string `json:"text"`
|
||||
ErrorMsg string `json:"error_msg"`
|
||||
}
|
||||
|
||||
// createGatewayTask 调用 model-gateway 异步任务并同步等待结果
|
||||
func createGatewayTask(ctx context.Context, payload map[string]any) (string, error) {
|
||||
fullURL := "model-gateway/task/createTask"
|
||||
headers := forwardHeaders(ctx)
|
||||
var req CreateTaskReq
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := commonHttp.Post(ctx, fullURL, headers, &req, body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return req.TaskId, nil
|
||||
}
|
||||
|
||||
type GetTaskResultRes struct {
|
||||
OssFile string `json:"ossFile" dc:"结果文件OSS地址"`
|
||||
State int `json:"state" dc:"任务状态"`
|
||||
}
|
||||
|
||||
// queryGatewayTaskState 查询网关任务状态
|
||||
func queryGatewayTaskState(ctx context.Context, taskID string) (int, error) {
|
||||
fullURL := fmt.Sprintf("model-gateway/task/getTaskResult?taskId=%s", taskID)
|
||||
headers := forwardHeaders(ctx)
|
||||
var req GetTaskResultRes
|
||||
if err := commonHttp.Get(ctx, fullURL, headers, &req, nil); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return req.State, nil
|
||||
}
|
||||
43
service/model_output_parser.go
Normal file
43
service/model_output_parser.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// 消息解析
|
||||
// ============================================
|
||||
func parseModelOutput(text string) (map[string]any, error) {
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal([]byte(text), &result); err != nil {
|
||||
return nil, fmt.Errorf("解析模型输出失败: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// cleanJSONString 清理字符串中可能导致JSON解析失败的字符
|
||||
func cleanJSONString(s string) string {
|
||||
s = strings.ReplaceAll(s, "\u2018", "'") // 左单引号 ‘
|
||||
s = strings.ReplaceAll(s, "\u2019", "'") // 右单引号 ’
|
||||
s = strings.ReplaceAll(s, "\u201c", "\"") // 左双引号 “
|
||||
s = strings.ReplaceAll(s, "\u201d", "\"") // 右双引号 ”
|
||||
return s
|
||||
}
|
||||
|
||||
func truncateStr(s string, maxLen int) string {
|
||||
if len(s) <= maxLen {
|
||||
return s
|
||||
}
|
||||
return s[:maxLen]
|
||||
}
|
||||
|
||||
// sessionParseModelOutput 解析会话模型输出
|
||||
func sessionParseModelOutput(text string) (map[string]any, error) {
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal([]byte(text), &result); err != nil {
|
||||
return nil, fmt.Errorf("解析模型输出失败: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
92
service/prompt_service.go
Normal file
92
service/prompt_service.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"prompts-core/dao"
|
||||
"prompts-core/model/dto"
|
||||
"prompts-core/model/entity"
|
||||
)
|
||||
|
||||
var Prompt = &promptService{}
|
||||
|
||||
type promptService struct{}
|
||||
|
||||
func (s *promptService) Create(ctx context.Context, req *dto.CreatePromptReq) (res *dto.CreatePromptRes, err error) {
|
||||
// promptInfo 兜底校验:必须可序列化为 JSON
|
||||
if req.PromptInfo == nil {
|
||||
return nil, errors.New("promptInfo不能为空")
|
||||
}
|
||||
if _, err := json.Marshal(req.PromptInfo); err != nil {
|
||||
return nil, errors.New("promptInfo不是合法JSON")
|
||||
}
|
||||
if req.ResponseJsonSchema == nil {
|
||||
return nil, errors.New("responseJsonSchema不能为空")
|
||||
}
|
||||
if _, err := json.Marshal(req.ResponseJsonSchema); err != nil {
|
||||
return nil, errors.New("responseJsonSchema不是合法JSON")
|
||||
}
|
||||
|
||||
m := &entity.PromptConfig{
|
||||
ModelTypeId: req.ModelTypeId,
|
||||
ModelType: req.ModelType,
|
||||
PromptInfo: req.PromptInfo,
|
||||
ResponseJsonSchema: req.ResponseJsonSchema,
|
||||
Enabled: 1,
|
||||
Version: req.Version,
|
||||
}
|
||||
|
||||
id, err := dao.Prompt.Insert(ctx, m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.CreatePromptRes{ID: id}, nil
|
||||
}
|
||||
|
||||
func (s *promptService) Update(ctx context.Context, req *dto.UpdatePromptReq) error {
|
||||
data := map[string]any{}
|
||||
if req.ModelTypeId != nil && *req.ModelTypeId > 0 {
|
||||
data[entity.PromptConfigCol.ModelTypeId] = *req.ModelTypeId
|
||||
}
|
||||
if req.ModelType != nil && *req.ModelType != "" {
|
||||
data[entity.PromptConfigCol.ModelType] = *req.ModelType
|
||||
}
|
||||
if req.PromptInfo != nil {
|
||||
if _, err := json.Marshal(req.PromptInfo); err != nil {
|
||||
return errors.New("promptInfo不是合法JSON")
|
||||
}
|
||||
data[entity.PromptConfigCol.PromptInfo] = req.PromptInfo
|
||||
}
|
||||
if req.ResponseJsonSchema != nil {
|
||||
if _, err := json.Marshal(req.ResponseJsonSchema); err != nil {
|
||||
return errors.New("responseJsonSchema不是合法JSON")
|
||||
}
|
||||
data[entity.PromptConfigCol.ResponseJsonSchema] = req.ResponseJsonSchema
|
||||
}
|
||||
if req.Enabled != nil {
|
||||
data[entity.PromptConfigCol.Enabled] = *req.Enabled
|
||||
}
|
||||
if req.Version != nil {
|
||||
data[entity.PromptConfigCol.Version] = *req.Version
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return errors.New("无可更新字段")
|
||||
}
|
||||
_, err := dao.Prompt.UpdateByID(ctx, req.ID, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *promptService) Delete(ctx context.Context, id int64) error {
|
||||
_, err := dao.Prompt.DeleteByID(ctx, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *promptService) Get(ctx context.Context, id int64) (*entity.PromptConfig, error) {
|
||||
return dao.Prompt.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *promptService) List(ctx context.Context, pageNum, pageSize int, modelTypeID *int, modelTypeLike string) (list []*entity.PromptConfig, total int64, err error) {
|
||||
return dao.Prompt.List(ctx, pageNum, pageSize, modelTypeID, modelTypeLike)
|
||||
}
|
||||
181
service/session_redis_service.go
Normal file
181
service/session_redis_service.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// Message 消息结构(content 支持 string 或 []string)
|
||||
type Message struct {
|
||||
Role string `json:"role"` // user / assistant / system
|
||||
Content any `json:"content"` // 内容:string 或 []string
|
||||
Type string `json:"type,omitempty"` // text / file(可选扩展)
|
||||
}
|
||||
|
||||
// GetContentString 获取 Content 的字符串形式
|
||||
func (m Message) GetContentString() string {
|
||||
switch v := m.Content.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []interface{}:
|
||||
var parts []string
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok {
|
||||
parts = append(parts, s)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
default:
|
||||
b, _ := json.Marshal(m.Content)
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
|
||||
// SessionRoundData Redis存储的单轮会话数据
|
||||
type SessionRoundData struct {
|
||||
SessionId string `json:"sessionId"` // 会话ID
|
||||
RequestContent []Message `json:"requestContent"` // 用户请求会话
|
||||
ResponseContent []Message `json:"responseContent"` // AI回调会话
|
||||
Timestamp int64 `json:"timestamp"` // 存入时间戳
|
||||
}
|
||||
|
||||
// GetSessionHistory 获取多轮会话历史(供推理时使用)
|
||||
func (s *sessionService) GetSessionHistory(ctx context.Context, sessionId string) ([]SessionRoundData, error) {
|
||||
return s.getFromRedis(ctx, sessionId)
|
||||
}
|
||||
|
||||
// BuildMessages 根据Redis历史构建完整的Messages数组
|
||||
func (s *sessionService) BuildMessages(ctx context.Context, sessionId string, currentMessages []Message) ([]Message, error) {
|
||||
// 获取历史会话
|
||||
history, err := s.getFromRedis(ctx, sessionId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取历史会话失败: %w", err)
|
||||
}
|
||||
|
||||
var allMessages []Message
|
||||
|
||||
// 按时间顺序拼接历史消息
|
||||
for _, round := range history {
|
||||
allMessages = append(allMessages, round.RequestContent...)
|
||||
allMessages = append(allMessages, round.ResponseContent...)
|
||||
}
|
||||
|
||||
// 添加当前轮次的请求消息
|
||||
allMessages = append(allMessages, currentMessages...)
|
||||
|
||||
return allMessages, nil
|
||||
}
|
||||
|
||||
// ==================== Redis 操作 ====================
|
||||
|
||||
// saveToRedis 保存会话数据到Redis
|
||||
// sessionId: 会话ID作为key
|
||||
// 最大10轮,超出替换最早的,过期时间30分钟
|
||||
func (s *sessionService) saveToRedis(ctx context.Context, sessionId string, requestMessages []Message, responseMessages []Message) error {
|
||||
key := fmt.Sprintf("chat:session:%s", sessionId)
|
||||
|
||||
// 从配置读取,提供默认值
|
||||
maxRounds := g.Cfg().MustGet(ctx, "session.maxRounds", 10).Int()
|
||||
expireSeconds := g.Cfg().MustGet(ctx, "session.expireTime", 1800).Int64()
|
||||
expireTime := time.Duration(expireSeconds) * time.Second
|
||||
|
||||
// 构造存储数据
|
||||
data := SessionRoundData{
|
||||
SessionId: sessionId,
|
||||
RequestContent: requestMessages,
|
||||
ResponseContent: responseMessages,
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
// 序列化
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化会话数据失败: %w", err)
|
||||
}
|
||||
|
||||
// 写入 Redis(LPUSH 添加到最前面,新的在前)
|
||||
_, err = g.Redis().Do(ctx, "LPUSH", key, string(b))
|
||||
if err != nil {
|
||||
return fmt.Errorf("写入Redis失败: %w", err)
|
||||
}
|
||||
|
||||
// 裁剪到最新10轮(保留前10条)
|
||||
_, err = g.Redis().Do(ctx, "LTRIM", key, 0, maxRounds-1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("裁剪Redis列表失败: %w", err)
|
||||
}
|
||||
|
||||
// 重置过期时间
|
||||
_, err = g.Redis().Do(ctx, "EXPIRE", key, int64(expireTime.Seconds()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("设置过期时间失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getFromRedis 从Redis获取会话历史
|
||||
func (s *sessionService) getFromRedis(ctx context.Context, sessionId string) ([]SessionRoundData, error) {
|
||||
key := fmt.Sprintf("chat:session:%s", sessionId)
|
||||
|
||||
// 获取列表中所有数据(最多10条)
|
||||
result, err := g.Redis().Do(ctx, "LRANGE", key, 0, -1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("从Redis获取数据失败: %w", err)
|
||||
}
|
||||
|
||||
if result == nil || result.IsNil() {
|
||||
return []SessionRoundData{}, nil
|
||||
}
|
||||
|
||||
// 解析数据
|
||||
var sessions []SessionRoundData
|
||||
|
||||
// 将结果转换为字符串数组
|
||||
values := result.Strings()
|
||||
for _, str := range values {
|
||||
var data SessionRoundData
|
||||
if err := json.Unmarshal([]byte(str), &data); err != nil {
|
||||
g.Log().Warningf(ctx, "[会话] 解析Redis数据失败 err=%v", err)
|
||||
continue
|
||||
}
|
||||
sessions = append(sessions, data)
|
||||
}
|
||||
|
||||
// 反转顺序(Redis存储最新在前,使用时按时间正序)
|
||||
for i, j := 0, len(sessions)-1; i < j; i, j = i+1, j-1 {
|
||||
sessions[i], sessions[j] = sessions[j], sessions[i]
|
||||
}
|
||||
|
||||
return sessions, nil
|
||||
}
|
||||
|
||||
// GetSessionHistoryForInference 获取历史会话,直接返回Message数组(给推理用)
|
||||
func (s *sessionService) GetSessionHistoryForInference(ctx context.Context, sessionId string) ([]Message, error) {
|
||||
// 从Redis获取历史会话数据
|
||||
historyData, err := s.getFromRedis(ctx, sessionId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取历史会话失败: %w", err)
|
||||
}
|
||||
|
||||
// 如果没有任何历史数据,返回空
|
||||
if len(historyData) == 0 {
|
||||
return []Message{}, nil
|
||||
}
|
||||
|
||||
// 把SessionRoundData转换成扁平的Message数组
|
||||
var messages []Message
|
||||
for _, round := range historyData {
|
||||
// 先加用户的请求
|
||||
messages = append(messages, round.RequestContent...)
|
||||
// 再加AI的回答
|
||||
messages = append(messages, round.ResponseContent...)
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
54
service/session_service.go
Normal file
54
service/session_service.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"prompts-core/dao"
|
||||
"prompts-core/model/dto"
|
||||
"prompts-core/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var Session = &sessionService{}
|
||||
|
||||
type sessionService struct{}
|
||||
|
||||
// SessionCallback 会话回调处理
|
||||
func (s *sessionService) SessionCallback(ctx context.Context, req *dto.SessionCallbackReq) (res *beans.ResponseEmpty, err error) {
|
||||
// 1. 解析AI返回的文本
|
||||
result, err := sessionParseModelOutput(req.Text)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "[会话回调] 解析模型输出失败 epicycleId=%d err=%v", req.EpicycleId, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 更新数据库(AI回调内容)
|
||||
_, err = dao.ComposeSession.UpdateById(ctx, req.EpicycleId, map[string]any{
|
||||
entity.ComposeSessionCol.ResponseContent: result,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "[会话回调] 更新数据库失败 epicycleId=%d err=%v", req.EpicycleId, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 3. 获取当前轮次的完整数据
|
||||
session, err := dao.ComposeSession.GetById(ctx, req.EpicycleId)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "[会话回调] 获取会话数据失败 epicycleId=%d err=%v", req.EpicycleId, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 4. 写入 Redis(多轮记忆)
|
||||
requestMessages := s.convertToMessages(session.RequestContent)
|
||||
responseMessages := s.convertToMessages(session.ResponseContent)
|
||||
if err = s.saveToRedis(ctx, session.SessionId, requestMessages, responseMessages); err != nil {
|
||||
g.Log().Errorf(ctx, "[会话回调] Redis存储失败 sessionId=%s id=%d err=%v",
|
||||
session.SessionId, session.Id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "[会话回调] 存储成功 sessionId=%s id=%d requestLen=%d responseLen=%d",
|
||||
session.SessionId, session.Id, len(requestMessages), len(responseMessages))
|
||||
return &beans.ResponseEmpty{}, nil
|
||||
}
|
||||
337
service/utils.go
Normal file
337
service/utils.go
Normal file
@@ -0,0 +1,337 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"prompts-core/model/dto"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
// 工具函数
|
||||
// ============================================
|
||||
|
||||
func getField(item map[string]any, fallback string) string {
|
||||
if field := asString(item["field"]); field != "" {
|
||||
return field
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func getLabel(item map[string]any) string {
|
||||
return asString(item["label"])
|
||||
}
|
||||
|
||||
func getValue(item map[string]any) any {
|
||||
return item["value"]
|
||||
}
|
||||
|
||||
func cloneWithValue(item map[string]any, value any) map[string]any {
|
||||
cloned := make(map[string]any)
|
||||
for k, v := range item {
|
||||
cloned[k] = v
|
||||
}
|
||||
cloned["value"] = value
|
||||
return cloned
|
||||
}
|
||||
|
||||
func isSensitiveField(field string) bool {
|
||||
f := strings.ToLower(field)
|
||||
return f == "apikey" || f == "authorization"
|
||||
}
|
||||
|
||||
func isAPIKeyField(field string) bool {
|
||||
f := strings.ToLower(field)
|
||||
return f == "apikey" || f == "authorization"
|
||||
}
|
||||
|
||||
func isTextType(field, label string) bool {
|
||||
f := strings.ToLower(field)
|
||||
l := strings.ToLower(label)
|
||||
return f == "prompt" || f == "text" ||
|
||||
l == "提示词" || l == "文本内容" || l == "prompt" || l == "text"
|
||||
}
|
||||
|
||||
func isDuplicate(userText, field, label string, value any) bool {
|
||||
lowerText := strings.ToLower(userText)
|
||||
|
||||
if label != "" && strings.Contains(lowerText, strings.ToLower(label)) {
|
||||
return true
|
||||
}
|
||||
if field != "" && strings.Contains(lowerText, strings.ToLower(field)) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 检查值
|
||||
if v := asString(value); v != "" && strings.Contains(lowerText, strings.ToLower(v)) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isEmptyValue(v any) bool {
|
||||
if v == nil {
|
||||
return true
|
||||
}
|
||||
if s, ok := v.(string); ok {
|
||||
return strings.TrimSpace(s) == ""
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isNilOrEmpty(v any) bool {
|
||||
if v == nil {
|
||||
return true
|
||||
}
|
||||
if s, ok := v.(string); ok {
|
||||
return strings.TrimSpace(s) == ""
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func asString(v any) string {
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
return t
|
||||
default:
|
||||
b, _ := json.Marshal(t)
|
||||
return strings.Trim(string(b), "\"")
|
||||
}
|
||||
}
|
||||
|
||||
func formatValue(v any) string {
|
||||
return strings.TrimSpace(asString(v))
|
||||
}
|
||||
|
||||
func mapToText(m map[string]any) string {
|
||||
if len(m) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
keys := sortedKeys(m)
|
||||
var parts []string
|
||||
for _, k := range keys {
|
||||
if isNilOrEmpty(m[k]) {
|
||||
continue
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("%s:%s", k, formatValue(m[k])))
|
||||
}
|
||||
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
|
||||
func sortedKeys(m map[string]any) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func mustMarshal(v any) string {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func formatTaskError(taskID, errMsg string) error {
|
||||
if strings.TrimSpace(errMsg) == "" {
|
||||
return fmt.Errorf("任务失败(taskId=%s)", taskID)
|
||||
}
|
||||
return fmt.Errorf("任务失败(taskId=%s): %s", taskID, errMsg)
|
||||
}
|
||||
|
||||
func getIntConfig(ctx context.Context, key string, fallback int) int {
|
||||
v := g.Cfg().MustGet(ctx, key)
|
||||
if v.IsEmpty() {
|
||||
return fallback
|
||||
}
|
||||
return v.Int()
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Schema 处理
|
||||
// ============================================
|
||||
|
||||
func enrichSchemaWithValues(schema []any, values map[string]any) []any {
|
||||
if len(schema) == 0 || len(values) == 0 {
|
||||
return schema
|
||||
}
|
||||
|
||||
result := make([]any, len(schema))
|
||||
copy(result, schema)
|
||||
|
||||
for i, item := range result {
|
||||
m, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
field := getField(m, "")
|
||||
if field == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 已有 value 则跳过
|
||||
if _, hasValue := m["value"]; hasValue {
|
||||
continue
|
||||
}
|
||||
|
||||
// 补充 value
|
||||
if v, exists := values[field]; exists {
|
||||
m["value"] = v
|
||||
result[i] = m
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// extractContentFromResponse 从模型完整响应中提取 content 字段
|
||||
func extractContentFromResponse(text string) string {
|
||||
// 尝试解析为完整的 choices 响应
|
||||
var response struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(text), &response); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(response.Choices) > 0 && response.Choices[0].Message.Content != "" {
|
||||
return response.Choices[0].Message.Content
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// 值提取
|
||||
// ============================================
|
||||
|
||||
func extractSystemValues(req *dto.ComposeMessagesReq) map[string]any {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
values := make(map[string]any)
|
||||
|
||||
for _, value := range req.Form {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok || len(item) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
field := getField(item, "")
|
||||
if field == "" || isSensitiveField(field) {
|
||||
continue
|
||||
}
|
||||
|
||||
if v := getValue(item); !isNilOrEmpty(v) {
|
||||
values[field] = v
|
||||
}
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
func extractModelKey(form map[string]any) string {
|
||||
for _, value := range form {
|
||||
item, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
field := getField(item, "")
|
||||
if isAPIKeyField(field) {
|
||||
key := strings.TrimSpace(asString(getValue(item)))
|
||||
if key != "" {
|
||||
if strings.Contains(key, ":") {
|
||||
return key
|
||||
}
|
||||
return "Authorization:" + key
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ==================== 工具方法 ====================
|
||||
|
||||
// convertToMessages 将数据库 any 类型转换为 []Message
|
||||
// 支持:JSON字符串、[]byte、[]interface{}、以及 content 为字符串数组的格式
|
||||
func (s *sessionService) convertToMessages(data any) []Message {
|
||||
if data == nil {
|
||||
return []Message{}
|
||||
}
|
||||
|
||||
// 处理 *gvar.Var
|
||||
if v, ok := data.(*gvar.Var); ok {
|
||||
if v == nil || v.IsNil() || v.IsEmpty() {
|
||||
return []Message{}
|
||||
}
|
||||
data = v.Val()
|
||||
}
|
||||
|
||||
var rawList []any
|
||||
|
||||
switch v := data.(type) {
|
||||
case string:
|
||||
if err := json.Unmarshal([]byte(v), &rawList); err != nil {
|
||||
g.Log().Warningf(context.Background(), "[会话] 解析JSON字符串失败 err=%v data=%.200s", err, v)
|
||||
return []Message{}
|
||||
}
|
||||
case []byte:
|
||||
if err := json.Unmarshal(v, &rawList); err != nil {
|
||||
g.Log().Warningf(context.Background(), "[会话] 解析字节数组失败 err=%v", err)
|
||||
return []Message{}
|
||||
}
|
||||
case []interface{}:
|
||||
rawList = v
|
||||
default:
|
||||
b, _ := json.Marshal(v)
|
||||
if err := json.Unmarshal(b, &rawList); err != nil {
|
||||
g.Log().Warningf(context.Background(), "[会话] 解析未知类型失败 err=%v type=%T", err, v)
|
||||
return []Message{}
|
||||
}
|
||||
}
|
||||
|
||||
// 转换每个元素为 Message
|
||||
var messages []Message
|
||||
for _, item := range rawList {
|
||||
var msg Message
|
||||
switch val := item.(type) {
|
||||
case string:
|
||||
if err := json.Unmarshal([]byte(val), &msg); err != nil {
|
||||
g.Log().Warningf(context.Background(), "[会话] 解析消息元素失败 err=%v data=%s", err, val)
|
||||
continue
|
||||
}
|
||||
case map[string]interface{}:
|
||||
b, _ := json.Marshal(val)
|
||||
json.Unmarshal(b, &msg)
|
||||
default:
|
||||
b, _ := json.Marshal(val)
|
||||
json.Unmarshal(b, &msg)
|
||||
}
|
||||
messages = append(messages, msg)
|
||||
}
|
||||
|
||||
if messages == nil {
|
||||
messages = []Message{}
|
||||
}
|
||||
return messages
|
||||
}
|
||||
Reference in New Issue
Block a user