refactor(*): 平台架构 - 拆分后端领域模块并同步插件协议与模型能力
This commit is contained in:
1 parent
916043fb5a
commit
6e5cbc8b05
644 files changed
+62731
-2320
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// EnsureChatCompletionStreamUsageRequest 为 Token 计费的流式 Chat Completions
|
||||
// 请求开启最终 usage 块;非流式请求保持原样,避免改变普通接口合同。
|
||||
func EnsureChatCompletionStreamUsageRequest(data []byte) ([]byte, error) {
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stream, _ := payload["stream"].(bool)
|
||||
if !stream {
|
||||
return data, nil
|
||||
}
|
||||
if err := ensureChatCompletionStreamUsage(payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(payload)
|
||||
}
|
||||
|
||||
func ensureChatCompletionStreamUsage(payload map[string]any) error {
|
||||
options := map[string]any{}
|
||||
if value, exists := payload["stream_options"]; exists {
|
||||
var ok bool
|
||||
options, ok = value.(map[string]any)
|
||||
if !ok {
|
||||
return errors.New("stream_options 必须是 JSON 对象")
|
||||
}
|
||||
}
|
||||
options["include_usage"] = true
|
||||
payload["stream_options"] = options
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user