492e92580b
Closes #620 #497. Add opt-in Langfuse observability covering all five model types (chat, embedding, rerank, VLM, ASR) with HTTP-request-scoped traces and Docker Compose support (both cloud and self-hosted). Core package internal/tracing/langfuse: - HTTP client with batched async ingestion (non-blocking in request path) - Sampling, environment / release tagging, and graceful fallback when LANGFUSE_* env vars are absent (wrappers become no-ops) - Gin middleware opens one trace per traced request and finishes it after the handler chain returns, attaching method / path / user / session - Trace context is stored under a typed key exported from internal/types so logger.CloneContext can preserve it across handler / goroutine boundaries (otherwise each LLM call auto-created an orphan trace, fragmenting one request into many) Per-model generation wrappers (opt-in via NewChat/NewEmbedder/...): - chat: captures prompt, streaming output, token usage + TTFT - embedding: approximates tokens when the provider omits usage - rerank: previews query/docs, summarizes results to keep payload small - vlm: records image count and total bytes, never uploads raw pixels - asr: records file size and audio duration, never uploads audio bytes Async title generation (GenerateTitleAsync) now forwards the trace key into the goroutine so title calls appear under the parent chat trace. Docker Compose: - LANGFUSE_* env passthrough on the `app` service for cloud deployments - Optional `langfuse` profile spins up a self-hosted Langfuse stack that reuses WeKnora's existing PostgreSQL (separate database via an idempotent init container that fixes ICU collation drift) and Redis (separate DB number), adding only ClickHouse, MinIO, web and worker containers - web/worker entrypoints URL-encode DB_PASSWORD / REDIS_PASSWORD at start to avoid Prisma P1013 when passwords contain @ / # / etc. Docs: docs/Langfuse集成.md covers cloud vs self-hosted, per-model usage strategy, code map, and resource footprint.
68 lines
2.9 KiB
Go
68 lines
2.9 KiB
Go
package langfuse
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// Langfuse ingestion API event envelope.
|
|
// https://api.reference.langfuse.com/#tag/ingestion
|
|
type ingestionEvent struct {
|
|
ID string `json:"id"`
|
|
Timestamp string `json:"timestamp"`
|
|
Type string `json:"type"`
|
|
Body interface{} `json:"body"`
|
|
}
|
|
|
|
// TokenUsage captures the input/output/total token counts reported by the
|
|
// underlying model, in Langfuse's canonical schema.
|
|
type TokenUsage struct {
|
|
Input int `json:"input,omitempty"`
|
|
Output int `json:"output,omitempty"`
|
|
Total int `json:"total,omitempty"`
|
|
Unit string `json:"unit,omitempty"`
|
|
}
|
|
|
|
// traceBody mirrors the /api/public/ingestion trace-create body.
|
|
type traceBody struct {
|
|
ID string `json:"id"`
|
|
Timestamp string `json:"timestamp,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
UserID string `json:"userId,omitempty"`
|
|
SessionID string `json:"sessionId,omitempty"`
|
|
Release string `json:"release,omitempty"`
|
|
Environment string `json:"environment,omitempty"`
|
|
Input interface{} `json:"input,omitempty"`
|
|
Output interface{} `json:"output,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
Public bool `json:"public,omitempty"`
|
|
}
|
|
|
|
// observationBody is shared between span-create / generation-create /
|
|
// span-update / generation-update events (different fields are populated
|
|
// depending on the event type).
|
|
type observationBody struct {
|
|
ID string `json:"id,omitempty"`
|
|
TraceID string `json:"traceId,omitempty"`
|
|
ParentObservationID string `json:"parentObservationId,omitempty"`
|
|
Type string `json:"type,omitempty"` // SPAN, GENERATION, EVENT
|
|
Name string `json:"name,omitempty"`
|
|
StartTime string `json:"startTime,omitempty"`
|
|
EndTime string `json:"endTime,omitempty"`
|
|
Input interface{} `json:"input,omitempty"`
|
|
Output interface{} `json:"output,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
Level string `json:"level,omitempty"` // DEFAULT | ERROR | WARNING
|
|
StatusMessage string `json:"statusMessage,omitempty"` // free-form
|
|
|
|
// Generation-specific fields
|
|
Model string `json:"model,omitempty"`
|
|
ModelParameters map[string]interface{} `json:"modelParameters,omitempty"`
|
|
Usage *TokenUsage `json:"usage,omitempty"`
|
|
CompletionStart string `json:"completionStartTime,omitempty"`
|
|
}
|
|
|
|
func isoTime(t time.Time) string {
|
|
return t.UTC().Format("2006-01-02T15:04:05.000Z")
|
|
}
|