Documentation
¶
Overview ¶
Package logstore provides a logs store for Bifrost.
Index ¶
- Variables
- type Config
- type Log
- type LogStore
- type LogStoreType
- type PaginationOptions
- type PostgresConfig
- type RDBLogStore
- func (s *RDBLogStore) Close(ctx context.Context) error
- func (s *RDBLogStore) Create(ctx context.Context, entry *Log) error
- func (s *RDBLogStore) FindAll(ctx context.Context, query any, fields ...string) ([]*Log, error)
- func (s *RDBLogStore) FindFirst(ctx context.Context, query any, fields ...string) (*Log, error)
- func (s *RDBLogStore) Flush(ctx context.Context, since time.Time) error
- func (s *RDBLogStore) SearchLogs(ctx context.Context, filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)
- func (s *RDBLogStore) Update(ctx context.Context, id string, entry any) error
- type SQLiteConfig
- type SearchFilters
- type SearchResult
- type SearchStats
- type SortBy
- type SortOrder
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotFound = fmt.Errorf("log not found")
)
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Enabled bool `json:"enabled"`
Type LogStoreType `json:"type"`
Config any `json:"config"`
}
Config represents the configuration for the logs store.
func (*Config) UnmarshalJSON ¶
UnmarshalJSON is the custom unmarshal logic for Config
type Log ¶
type Log struct {
ID string `gorm:"primaryKey;type:varchar(255)" json:"id"`
ParentRequestID *string `gorm:"type:varchar(255)" json:"parent_request_id"`
Timestamp time.Time `gorm:"index;not null" json:"timestamp"`
Object string `gorm:"type:varchar(255);index;not null;column:object_type" json:"object"` // text.completion, chat.completion, or embedding
Provider string `gorm:"type:varchar(255);index;not null" json:"provider"`
Model string `gorm:"type:varchar(255);index;not null" json:"model"`
InputHistory string `gorm:"type:text" json:"-"` // JSON serialized []schemas.ChatMessage
OutputMessage string `gorm:"type:text" json:"-"` // JSON serialized *schemas.ChatMessage
ResponsesOutput string `gorm:"type:text" json:"-"` // JSON serialized *schemas.ResponsesMessage
EmbeddingOutput string `gorm:"type:text" json:"-"` // JSON serialized [][]float32
Params string `gorm:"type:text" json:"-"` // JSON serialized *schemas.ModelParameters
Tools string `gorm:"type:text" json:"-"` // JSON serialized []schemas.Tool
ToolCalls string `gorm:"type:text" json:"-"` // JSON serialized []schemas.ToolCall (For backward compatibility, tool calls are now in the content)
SpeechInput string `gorm:"type:text" json:"-"` // JSON serialized *schemas.SpeechInput
TranscriptionInput string `gorm:"type:text" json:"-"` // JSON serialized *schemas.TranscriptionInput
SpeechOutput string `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostSpeech
TranscriptionOutput string `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostTranscribe
CacheDebug string `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostCacheDebug
Latency *float64 `json:"latency,omitempty"`
TokenUsage string `gorm:"type:text" json:"-"` // JSON serialized *schemas.LLMUsage
Cost *float64 `gorm:"index" json:"cost,omitempty"` // Cost in dollars (total cost of the request - includes cache lookup cost)
Status string `gorm:"type:varchar(50);index;not null" json:"status"` // "processing", "success", or "error"
ErrorDetails string `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostError
Stream bool `gorm:"default:false" json:"stream"` // true if this was a streaming response
ContentSummary string `gorm:"type:text" json:"-"` // For content search
RawResponse string `gorm:"type:text" json:"raw_response"` // Populated when `send-back-raw-response` is on
// Denormalized token fields for easier querying
PromptTokens int `gorm:"default:0" json:"-"`
CompletionTokens int `gorm:"default:0" json:"-"`
TotalTokens int `gorm:"default:0" json:"-"`
CreatedAt time.Time `gorm:"index;not null" json:"created_at"`
// Virtual fields for JSON output - these will be populated when needed
InputHistoryParsed []schemas.ChatMessage `gorm:"-" json:"input_history,omitempty"`
OutputMessageParsed *schemas.ChatMessage `gorm:"-" json:"output_message,omitempty"`
ResponsesOutputParsed []schemas.ResponsesMessage `gorm:"-" json:"responses_output,omitempty"`
EmbeddingOutputParsed []schemas.EmbeddingData `gorm:"-" json:"embedding_output,omitempty"`
ParamsParsed interface{} `gorm:"-" json:"params,omitempty"`
ToolsParsed []schemas.ChatTool `gorm:"-" json:"tools,omitempty"`
ToolCallsParsed []schemas.ChatAssistantMessageToolCall `gorm:"-" json:"tool_calls,omitempty"` // For backward compatibility, tool calls are now in the content
TokenUsageParsed *schemas.BifrostLLMUsage `gorm:"-" json:"token_usage,omitempty"`
ErrorDetailsParsed *schemas.BifrostError `gorm:"-" json:"error_details,omitempty"`
SpeechInputParsed *schemas.SpeechInput `gorm:"-" json:"speech_input,omitempty"`
TranscriptionInputParsed *schemas.TranscriptionInput `gorm:"-" json:"transcription_input,omitempty"`
SpeechOutputParsed *schemas.BifrostSpeechResponse `gorm:"-" json:"speech_output,omitempty"`
TranscriptionOutputParsed *schemas.BifrostTranscriptionResponse `gorm:"-" json:"transcription_output,omitempty"`
CacheDebugParsed *schemas.BifrostCacheDebug `gorm:"-" json:"cache_debug,omitempty"`
}
Log represents a complete log entry for a request/response cycle This is the GORM model with appropriate tags
func (*Log) BeforeCreate ¶
BeforeCreate GORM hook to set created_at and serialize JSON fields
func (*Log) BeforeSave ¶
BeforeSave GORM hook to serialize JSON fields
func (*Log) BuildContentSummary ¶
BuildContentSummary creates a searchable text summary
func (*Log) DeserializeFields ¶
DeserializeFields converts JSON strings back to Go structs
func (*Log) SerializeFields ¶
SerializeFields converts Go structs to JSON strings for storage
type LogStore ¶
type LogStore interface {
Create(ctx context.Context, entry *Log) error
FindFirst(ctx context.Context, query any, fields ...string) (*Log, error)
FindAll(ctx context.Context, query any, fields ...string) ([]*Log, error)
SearchLogs(ctx context.Context, filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)
Update(ctx context.Context, id string, entry any) error
Flush(ctx context.Context, since time.Time) error
Close(ctx context.Context) error
}
LogStore is the interface for the log store.
type LogStoreType ¶
type LogStoreType string
LogStoreType represents the type of log store.
const ( LogStoreTypeSQLite LogStoreType = "sqlite" LogStoreTypePostgres LogStoreType = "postgres" )
LogStoreTypeSQLite is the type of log store for SQLite.
type PaginationOptions ¶
type PaginationOptions struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
SortBy string `json:"sort_by"` // "timestamp", "latency", "tokens", "cost"
Order string `json:"order"` // "asc", "desc"
}
PaginationOptions represents pagination parameters
type PostgresConfig ¶ added in v1.1.0
type PostgresConfig struct {
Host string `json:"host"`
Port string `json:"port"`
User string `json:"user"`
Password string `json:"password"`
DBName string `json:"db_name"`
SSLMode string `json:"ssl_mode"`
}
PostgresConfig represents the configuration for a Postgres database.
type RDBLogStore ¶ added in v1.1.0
type RDBLogStore struct {
// contains filtered or unexported fields
}
RDBLogStore represents a log store that uses a SQLite database.
func (*RDBLogStore) Close ¶ added in v1.1.0
func (s *RDBLogStore) Close(ctx context.Context) error
Close closes the log store.
func (*RDBLogStore) Create ¶ added in v1.1.0
func (s *RDBLogStore) Create(ctx context.Context, entry *Log) error
Create inserts a new log entry into the database.
func (*RDBLogStore) SearchLogs ¶ added in v1.1.0
func (s *RDBLogStore) SearchLogs(ctx context.Context, filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)
SearchLogs searches for logs in the database.
type SQLiteConfig ¶
type SQLiteConfig struct {
Path string `json:"path"`
}
SQLiteConfig represents the configuration for a SQLite database.
type SearchFilters ¶
type SearchFilters struct {
Providers []string `json:"providers,omitempty"`
Models []string `json:"models,omitempty"`
Status []string `json:"status,omitempty"`
Objects []string `json:"objects,omitempty"` // For filtering by request type (chat.completion, text.completion, embedding)
StartTime *time.Time `json:"start_time,omitempty"`
EndTime *time.Time `json:"end_time,omitempty"`
MinLatency *float64 `json:"min_latency,omitempty"`
MaxLatency *float64 `json:"max_latency,omitempty"`
MinTokens *int `json:"min_tokens,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
MinCost *float64 `json:"min_cost,omitempty"`
MaxCost *float64 `json:"max_cost,omitempty"`
ContentSearch string `json:"content_search,omitempty"`
}
SearchFilters represents the available filters for log searches
type SearchResult ¶
type SearchResult struct {
Logs []Log `json:"logs"`
Pagination PaginationOptions `json:"pagination"`
Stats SearchStats `json:"stats"`
}
SearchResult represents the result of a log search
type SearchStats ¶
type SearchStats struct {
TotalRequests int64 `json:"total_requests"`
SuccessRate float64 `json:"success_rate"` // Percentage of successful requests
AverageLatency float64 `json:"average_latency"` // Average latency in milliseconds
TotalTokens int64 `json:"total_tokens"` // Total tokens used
TotalCost float64 `json:"total_cost"` // Total cost in dollars
}