🎨 优化预设注入,删除预设权限认证,优化日志
This commit is contained in:
@@ -53,18 +53,6 @@ func (a *AiProxyApi) ChatCompletions(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证预设权限
|
||||
if req.PresetName != "" && !aiApiKeyService.CheckPresetPermission(apiKey, req.PresetName) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"message": "该API密钥无权使用此预设: " + req.PresetName,
|
||||
"type": "invalid_request_error",
|
||||
"code": "preset_not_allowed",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 处理流式响应
|
||||
if req.Stream {
|
||||
aiProxyService.ProcessChatCompletionStream(c, &req)
|
||||
@@ -145,18 +133,6 @@ func (a *AiProxyApi) ClaudeMessages(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证预设权限
|
||||
if req.PresetName != "" && !aiApiKeyService.CheckPresetPermission(apiKey, req.PresetName) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": gin.H{
|
||||
"message": "该API密钥无权使用此预设: " + req.PresetName,
|
||||
"type": "invalid_request_error",
|
||||
"code": "preset_not_allowed",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 处理流式响应
|
||||
if req.Stream {
|
||||
aiProxyService.ProcessClaudeMessageStream(c, &req)
|
||||
|
||||
@@ -10,8 +10,6 @@ type ChatCompletionResponse struct {
|
||||
Usage *ChatCompletionUsage `json:"usage,omitempty"`
|
||||
// 统一格式的用量统计(用于各种上游的标准化)
|
||||
StandardUsage *ChatCompletionUsage `json:"standard_usage,omitempty"`
|
||||
// 扩展字段:正则脚本执行日志
|
||||
RegexLogs *RegexExecutionLogs `json:"regex_logs,omitempty"`
|
||||
}
|
||||
|
||||
type ChatCompletionChoice struct {
|
||||
|
||||
@@ -129,37 +129,61 @@ func (p *PresetInjector) buildInjectedMessages(messages []request.ChatMessage, p
|
||||
}
|
||||
|
||||
// injectRelativePrompts 将相对位置的提示词注入到对话历史中
|
||||
// 注入深度从前往后计算:depth=0 表示索引0(最前面),depth=1 表示索引1,以此类推
|
||||
func (p *PresetInjector) injectRelativePrompts(messages []request.ChatMessage, prompts []app.PresetPrompt) []request.ChatMessage {
|
||||
if len(prompts) == 0 {
|
||||
return messages
|
||||
}
|
||||
|
||||
result := make([]request.ChatMessage, 0, len(messages)+len(prompts))
|
||||
messageCount := len(messages)
|
||||
|
||||
// 按深度分组提示词
|
||||
// 按深度分组提示词,并按 injection_order 排序
|
||||
depthMap := make(map[int][]app.PresetPrompt)
|
||||
for _, prompt := range prompts {
|
||||
depthMap[prompt.InjectionDepth] = append(depthMap[prompt.InjectionDepth], prompt)
|
||||
}
|
||||
|
||||
// 遍历消息,在指定深度注入提示词
|
||||
for i, msg := range messages {
|
||||
// 计算当前位置的深度(从末尾开始计数)
|
||||
depth := messageCount - i
|
||||
// 对每个深度的提示词按 injection_order 排序(从大到小,优先级高的在前)
|
||||
for depth := range depthMap {
|
||||
sort.Slice(depthMap[depth], func(i, j int) bool {
|
||||
return depthMap[depth][i].InjectionOrder > depthMap[depth][j].InjectionOrder
|
||||
})
|
||||
}
|
||||
|
||||
// 在当前消息之前注入对应深度的提示词
|
||||
result := make([]request.ChatMessage, 0, len(messages)+len(prompts))
|
||||
totalInserted := 0
|
||||
|
||||
// 找出最大深度
|
||||
maxDepth := 0
|
||||
for depth := range depthMap {
|
||||
if depth > maxDepth {
|
||||
maxDepth = depth
|
||||
}
|
||||
}
|
||||
|
||||
// 从 depth=0 开始,逐个深度注入
|
||||
for depth := 0; depth <= maxDepth; depth++ {
|
||||
// 计算实际注入位置(考虑之前已注入的消息数量)
|
||||
injectIdx := depth + totalInserted
|
||||
|
||||
// 如果注入位置超出当前消息列表,先添加原始消息直到该位置
|
||||
for len(result) < injectIdx && len(result)-totalInserted < len(messages) {
|
||||
result = append(result, messages[len(result)-totalInserted])
|
||||
}
|
||||
|
||||
// 注入当前深度的所有提示词
|
||||
if promptsAtDepth, exists := depthMap[depth]; exists {
|
||||
for _, prompt := range promptsAtDepth {
|
||||
result = append(result, request.ChatMessage{
|
||||
Role: prompt.Role,
|
||||
Content: p.processPromptContent(prompt.Content),
|
||||
})
|
||||
totalInserted++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加当前消息
|
||||
result = append(result, msg)
|
||||
// 添加剩余的原始消息
|
||||
for len(result)-totalInserted < len(messages) {
|
||||
result = append(result, messages[len(result)-totalInserted])
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@@ -64,12 +64,6 @@ func (s *AiProxyService) ProcessChatCompletion(ctx context.Context, req *request
|
||||
if injector != nil && len(resp.Choices) > 0 {
|
||||
resp.Choices[0].Message.Content = injector.ProcessResponse(resp.Choices[0].Message.Content)
|
||||
aiOutput = resp.Choices[0].Message.Content
|
||||
|
||||
// 添加正则执行日志到响应
|
||||
regexLogs := injector.GetRegexLogs()
|
||||
if regexLogs.TotalMatches > 0 || len(regexLogs.InputScripts) > 0 || len(regexLogs.OutputScripts) > 0 {
|
||||
resp.RegexLogs = regexLogs
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 处理响应并收集正则日志
|
||||
|
||||
Reference in New Issue
Block a user