feat(prompt): 实现历史消息注入功能和协议配置优化
- 在 handleCallbackSuccess 函数中新增获取协议配置逻辑 - 实现历史消息获取并在 rounds 中注入历史消息 - 添加 InjectHistory 函数实现按协议顺序合并历史消息 - 在 GetPromptText 接口中集成历史消息注入测试 - 更新 ProviderProtocol 实体中的 MergeOrder 类型为 []string - 新增 Capabilities 字段支持最大 token 配置 - 修改 renderTemplate 函数接收协议对象参数 - 优化会话历史存储逻辑,提取用户消息内容进行记录 - 移除无用的注释代码 handleCallbackSuccess 处理回调成功
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
|
||||
"prompts-core/dao"
|
||||
"prompts-core/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// PromptIR 统一 Prompt 中间表示
|
||||
@@ -34,6 +36,7 @@ type ProviderProtocol struct {
|
||||
ContentMapping ContentMapping `json:"content_mapping"`
|
||||
RequestTemplate map[string]any `json:"request_template"`
|
||||
SystemPromptTemplate string `json:"system_prompt_template"`
|
||||
Capabilities map[string]any `json:"capabilities"`
|
||||
}
|
||||
|
||||
// ContentMapping 内容字段映射
|
||||
@@ -175,6 +178,7 @@ func parseProtocol(e *entity.ProviderProtocol) *ProviderProtocol {
|
||||
util.ParseJSONFieldFromGvar(e.RoleMapping, &p.RoleMapping)
|
||||
util.ParseJSONFieldFromGvar(e.ContentMapping, &p.ContentMapping)
|
||||
util.ParseJSONFieldFromGvar(e.RequestTemplate, &p.RequestTemplate)
|
||||
util.ParseJSONFieldFromGvar(e.Capabilities, &p.Capabilities)
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -265,7 +269,7 @@ func mapContent(messages []map[string]any, cm ContentMapping) []map[string]any {
|
||||
// buildRequest 按 target_field 和 request_template 构建请求体
|
||||
func buildRequest(messages []map[string]any, p *ProviderProtocol, chatModel *gateway.AsynchModel) map[string]any {
|
||||
if len(p.RequestTemplate) > 0 {
|
||||
return renderTemplate(p.RequestTemplate, messages, chatModel)
|
||||
return renderTemplate(p, messages, chatModel)
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
@@ -274,8 +278,8 @@ func buildRequest(messages []map[string]any, p *ProviderProtocol, chatModel *gat
|
||||
}
|
||||
|
||||
// renderTemplate 简单的 {{key}} 模板替换
|
||||
func renderTemplate(tmpl map[string]any, messages []map[string]any, chatModel *gateway.AsynchModel) map[string]any {
|
||||
b, _ := json.Marshal(tmpl)
|
||||
func renderTemplate(p *ProviderProtocol, messages []map[string]any, chatModel *gateway.AsynchModel) map[string]any {
|
||||
b, _ := json.Marshal(p.RequestTemplate)
|
||||
str := string(b)
|
||||
|
||||
if chatModel != nil {
|
||||
@@ -288,5 +292,9 @@ func renderTemplate(tmpl map[string]any, messages []map[string]any, chatModel *g
|
||||
var result map[string]any
|
||||
_ = json.Unmarshal([]byte(str), &result)
|
||||
|
||||
if maxTokens := gconv.Int(p.Capabilities["max_tokens"]); maxTokens > 0 {
|
||||
result["max_tokens"] = maxTokens
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user