Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.15
1.0.16
2 changes: 1 addition & 1 deletion plugins/governance/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.8
1.2.9
2 changes: 1 addition & 1 deletion plugins/jsonparser/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.7
1.2.8
2 changes: 1 addition & 1 deletion plugins/logging/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.8
1.2.9
67 changes: 52 additions & 15 deletions plugins/maxim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ type ContextKey string
// This constant provides a consistent key for tracking request traces
// throughout the request/response lifecycle.
const (
SessionIDKey ContextKey = "session-id"
TraceIDKey ContextKey = "trace-id"
GenerationIDKey ContextKey = "generation-id"
SessionIDKey ContextKey = "session-id"
TraceIDKey ContextKey = "trace-id"
TraceNameKey ContextKey = "trace-name"
GenerationIDKey ContextKey = "generation-id"
GenerationNameKey ContextKey = "generation-name"
TagsKey ContextKey = "maxim-tags"
)

// The plugin provides request/response tracing functionality by integrating with Maxim's logging system.
Expand Down Expand Up @@ -113,7 +116,10 @@ func (plugin *Plugin) GetName() string {
// - error: Any error that occurred during trace/generation creation
func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.PluginShortCircuit, error) {
var traceID string
var traceName string
var sessionID string
var generationName string
var tags map[string]string

// Check if context already has traceID and generationID
if ctx != nil {
Expand All @@ -130,20 +136,42 @@ func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest)
if existingSessionID, ok := (*ctx).Value(SessionIDKey).(string); ok && existingSessionID != "" {
sessionID = existingSessionID
}

if existingTraceName, ok := (*ctx).Value(TraceNameKey).(string); ok && existingTraceName != "" {
traceName = existingTraceName
}

if existingGenerationName, ok := (*ctx).Value(GenerationNameKey).(string); ok && existingGenerationName != "" {
generationName = existingGenerationName
}

// retrieve all tags from context
// the transport layer now stores all maxim tags in a single map
if tagsValue := (*ctx).Value(TagsKey); tagsValue != nil {
if tagsMap, ok := tagsValue.(map[string]string); ok {
tags = make(map[string]string)
for key, value := range tagsMap {
tags[key] = value
}
}
}
Comment thread
TejasGhatte marked this conversation as resolved.
}

// Determine request type and set appropriate tags
var requestType string
var tags map[string]string
var messages []logging.CompletionRequest
var latestMessage string

// Initialize tags map if not already initialized from context
if tags == nil {
tags = make(map[string]string)
}

// Add model to tags
tags["model"] = req.Model

Comment thread
Pratham-Mishra04 marked this conversation as resolved.
if req.Input.ChatCompletionInput != nil {
requestType = "chat_completion"
tags = map[string]string{
"action": "chat_completion",
"model": req.Model,
}
for _, message := range *req.Input.ChatCompletionInput {
messages = append(messages, logging.CompletionRequest{
Role: string(message.Role),
Expand Down Expand Up @@ -171,24 +199,27 @@ func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest)
}
} else if req.Input.TextCompletionInput != nil {
requestType = "text_completion"
tags = map[string]string{
"action": "text_completion",
"model": req.Model,
}
messages = append(messages, logging.CompletionRequest{
Role: string(schemas.ModelChatMessageRoleUser),
Content: req.Input.TextCompletionInput,
})
latestMessage = *req.Input.TextCompletionInput
}

// Set action tag after determining request type
tags["action"] = requestType

if traceID == "" {
// If traceID is not set, create a new trace
traceID = uuid.New().String()
name := fmt.Sprintf("bifrost_%s", requestType)
if traceName != "" {
name = traceName
}

traceConfig := logging.TraceConfig{
Id: traceID,
Name: maxim.StrPtr(fmt.Sprintf("bifrost_%s", requestType)),
Name: maxim.StrPtr(name),
Tags: &tags,
}

Expand All @@ -213,14 +244,20 @@ func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest)

generationID := uuid.New().String()

plugin.logger.AddGenerationToTrace(traceID, &logging.GenerationConfig{
generationConfig := logging.GenerationConfig{
Id: generationID,
Model: req.Model,
Provider: string(req.Provider),
Tags: &tags,
Messages: messages,
ModelParameters: modelParams,
})
}

if generationName != "" {
generationConfig.Name = &generationName
}

plugin.logger.AddGenerationToTrace(traceID, &generationConfig)

if ctx != nil {
if _, ok := (*ctx).Value(TraceIDKey).(string); !ok {
Expand Down
2 changes: 1 addition & 1 deletion plugins/maxim/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.6
1.2.7
2 changes: 1 addition & 1 deletion plugins/mocker/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.6
1.2.7
2 changes: 1 addition & 1 deletion plugins/semanticcache/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.9
1.2.8
2 changes: 1 addition & 1 deletion plugins/telemetry/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.7
1.2.8
22 changes: 22 additions & 0 deletions transports/bifrost-http/lib/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func ConvertToBifrostContext(ctx *fasthttp.RequestCtx, allowDirectKeys bool) *co
}
bifrostCtx = context.WithValue(bifrostCtx, schemas.BifrostContextKey("request-id"), requestID)

// Initialize tags map for collecting maxim tags
maximTags := make(map[string]string)

Comment thread
TejasGhatte marked this conversation as resolved.
// Then process other headers
ctx.Request.Header.All()(func(key, value []byte) bool {
keyStr := strings.ToLower(string(key))
Expand All @@ -100,6 +103,20 @@ func ConvertToBifrostContext(ctx *fasthttp.RequestCtx, allowDirectKeys bool) *co
if labelName == string(maxim.SessionIDKey) {
bifrostCtx = context.WithValue(bifrostCtx, maxim.ContextKey(labelName), string(value))
}

if labelName == string(maxim.TraceNameKey) {
bifrostCtx = context.WithValue(bifrostCtx, maxim.ContextKey(labelName), string(value))
}

if labelName == string(maxim.GenerationNameKey) {
bifrostCtx = context.WithValue(bifrostCtx, maxim.ContextKey(labelName), string(value))
}

// apart from these all headers starting with x-bf-maxim- are keys for tags
// collect them in the maximTags map
if labelName != string(maxim.GenerationIDKey) && labelName != string(maxim.TraceIDKey) && labelName != string(maxim.SessionIDKey) && labelName != string(maxim.TraceNameKey) && labelName != string(maxim.GenerationNameKey) {
maximTags[labelName] = string(value)
}
}

if strings.HasPrefix(keyStr, "x-bf-mcp-") {
Expand Down Expand Up @@ -174,6 +191,11 @@ func ConvertToBifrostContext(ctx *fasthttp.RequestCtx, allowDirectKeys bool) *co
return true
})

// Store the collected maxim tags in the context
if len(maximTags) > 0 {
bifrostCtx = context.WithValue(bifrostCtx, maxim.ContextKey(maxim.TagsKey), maximTags)
}

if allowDirectKeys {
// Extract API key from Authorization header (Bearer format) or x-api-key header
var apiKey string
Expand Down
2 changes: 1 addition & 1 deletion transports/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.12
1.2.13