feat: publish sanitized custom.97 source
This commit is contained in:
1022 files changed
+69903
-19207
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"github.com/QuantumNous/new-api/relaykit/relayconvert"
|
||||
"github.com/QuantumNous/new-api/tokenkit"
|
||||
)
|
||||
|
||||
// ResponsesUsageAccumulator owns the accounting facts for one Responses stream.
|
||||
@@ -29,7 +30,9 @@ func NewResponsesUsageAccumulator(info *relaycommon.RelayInfo) *ResponsesUsageAc
|
||||
return &ResponsesUsageAccumulator{info: info, usage: &dto.Usage{}}
|
||||
}
|
||||
|
||||
func (a *ResponsesUsageAccumulator) Observe(event *dto.ResponsesStreamResponse) {
|
||||
// Observe feeds one decoded stream event. raw is the same event's wire bytes;
|
||||
// the vendor tool-usage reader runs on it once, at the terminal event.
|
||||
func (a *ResponsesUsageAccumulator) Observe(event *dto.ResponsesStreamResponse, raw []byte) {
|
||||
if a == nil || event == nil || a.finished {
|
||||
return
|
||||
}
|
||||
@@ -50,13 +53,16 @@ func (a *ResponsesUsageAccumulator) Observe(event *dto.ResponsesStreamResponse)
|
||||
a.outputText.WriteString(relayconvert.ExtractOutputTextFromResponses(event.Response))
|
||||
}
|
||||
}
|
||||
// Vendor counts are cumulative on the terminal event and replace the
|
||||
// web_search_call items counted from output_item.done.
|
||||
a.info.ApplyVendorToolUsage(raw)
|
||||
if a.imageCommitted {
|
||||
return
|
||||
}
|
||||
failed := event.Type != "response.completed" && event.Type != "response.done"
|
||||
if failed || (event.Response != nil && relaycommon.IsNonBillableResponsesStatus(event.Response.Status)) {
|
||||
a.imageCounter.Reset()
|
||||
} else if event.Response != nil {
|
||||
// Images that completed before any terminal, failed ones included,
|
||||
// were delivered and stay billable; Observe still skips unfinished
|
||||
// image items.
|
||||
if event.Response != nil {
|
||||
for i := range event.Response.Output {
|
||||
a.imageCounter.Observe(&event.Response.Output[i], &i)
|
||||
}
|
||||
@@ -92,15 +98,14 @@ func (a *ResponsesUsageAccumulator) Finish() *dto.Usage {
|
||||
a.usage.UsageSource = "estimated"
|
||||
}
|
||||
// A final image item can already have reached the client before the stream
|
||||
// disconnects. Explicit failed/incomplete terminals reset and commit zero in
|
||||
// Observe; otherwise retain completed tool usage even without a terminal.
|
||||
// disconnects, so completed tool usage is retained even without a terminal.
|
||||
if !a.imageCommitted {
|
||||
a.imageCounter.Commit(a.info)
|
||||
a.imageCommitted = true
|
||||
}
|
||||
if a.usage.CompletionTokens == 0 {
|
||||
if output := a.outputText.String(); output != "" {
|
||||
a.usage.CompletionTokens = CountTextToken(output, a.info.GetUpstreamModelName())
|
||||
a.usage.CompletionTokens = tokenkit.Count(a.info.GetUpstreamModelName(), output)
|
||||
}
|
||||
}
|
||||
// Upstream bills the prompt as soon as it starts generating, so a stream
|
||||
|
||||
Reference in new issue
Block a user