Add debug stats

This commit is contained in:
dwrz
2026-06-27 00:13:16 +00:00
parent bcdbd9092a
commit f2edd39160
8 changed files with 553 additions and 28 deletions

View File

@@ -457,6 +457,10 @@ type Response struct {
Provider string `json:"used_provider,omitempty"`
// Model is the LLM model used for the response.
Model string `json:"used_model,omitempty"`
// Usage is token usage from the API response.
Usage *llm.Usage `json:"usage,omitempty"`
// Timings is llama.cpp timing info from the API response.
Timings *llm.Timings `json:"timings,omitempty"`
}
// chat processes text chat requests.
@@ -536,7 +540,7 @@ func (svc *Service) chat(w http.ResponseWriter, r *http.Request) {
}
}
msgs, err := llmc.Completions(
res, err := llmc.Completions(
ctx, model, req.SystemMessage, 0, req.Messages,
)
if err != nil {
@@ -548,7 +552,7 @@ func (svc *Service) chat(w http.ResponseWriter, r *http.Request) {
http.Error(w, "LLM error", http.StatusInternalServerError)
return
}
if len(msgs) == 0 {
if len(res.Messages) == 0 {
http.Error(
w,
"no response from LLM",
@@ -556,7 +560,7 @@ func (svc *Service) chat(w http.ResponseWriter, r *http.Request) {
)
return
}
final := msgs[len(msgs)-1]
final := res.Messages[len(res.Messages)-1]
log.DebugContext(
ctx,
"LLM response",
@@ -567,9 +571,11 @@ func (svc *Service) chat(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(Response{
Messages: msgs,
Messages: res.Messages,
Provider: provider,
Model: model,
Usage: res.Usage,
Timings: res.Timings,
}); err != nil {
log.ErrorContext(
ctx,
@@ -593,6 +599,10 @@ type StreamMessage struct {
Provider string `json:"provider,omitempty"`
// Model is the LLM model used for the response.
Model string `json:"model,omitempty"`
// Usage is token usage from the API response.
Usage *llm.Usage `json:"usage,omitempty"`
// Timings is llama.cpp timing info from the API response.
Timings *llm.Timings `json:"timings,omitempty"`
}
// chatStream processes chat requests with streaming SSE output.
@@ -719,6 +729,12 @@ func (svc *Service) chatStream(w http.ResponseWriter, r *http.Request) {
if evt.Message.Role != "" {
msg.Message = &evt.Message
}
if evt.Usage != nil {
msg.Usage = evt.Usage
}
if evt.Timings != nil {
msg.Timings = evt.Timings
}
send(evt.Type, msg)
}
if err := <-errs; err != nil {