Skip to content

Commit ba7b44c

Browse files
feat: vk provider routing added
1 parent b91d410 commit ba7b44c

File tree

36 files changed

+1383
-595
lines changed

36 files changed

+1383
-595
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Variables
44
HOST ?= localhost
55
PORT ?= 8080
6-
APP_DIR ?=
6+
APP_DIR ?=
77
PROMETHEUS_LABELS ?=
88
LOG_STYLE ?= json
99
LOG_LEVEL ?= info

core/bifrost.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ func (bifrost *Bifrost) selectKeyFromProviderForModel(ctx *context.Context, prov
15831583
}
15841584

15851585
if len(keys) == 0 {
1586-
return schemas.Key{}, fmt.Errorf("no keys found for provider: %v", providerKey)
1586+
return schemas.Key{}, fmt.Errorf("no keys found for provider: %v and model: %s", providerKey, model)
15871587
}
15881588

15891589
// filter out keys which dont support the model, if the key has no models, it is supported for all models

core/providers/bedrock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type BedrockMistralContent struct {
8888
type BedrockMistralChatMessage struct {
8989
Role schemas.ModelChatMessageRole `json:"role"` // Role of the message sender
9090
Content []BedrockMistralContent `json:"content"` // Array of message content
91-
ToolCalls []BedrockAnthropicToolCall `json:"tool_calls,omitempty"` // Optional tool calls
91+
ToolCalls []BedrockAnthropicToolCall `json:"tool_calls,omitempty"` // Optional tool calls
9292
ToolCallID *string `json:"tool_call_id,omitempty"` // Optional tool call ID
9393
}
9494

core/schemas/bifrost.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const (
118118
// a text completion, a chat completion, an embedding request, a speech request, or a transcription request.
119119
type RequestInput struct {
120120
TextCompletionInput *string `json:"text_completion_input,omitempty"`
121-
ChatCompletionInput []BifrostMessage `json:"chat_completion_input,omitempty"`
121+
ChatCompletionInput []BifrostMessage `json:"chat_completion_input,omitempty"`
122122
EmbeddingInput *EmbeddingInput `json:"embedding_input,omitempty"`
123123
SpeechInput *SpeechInput `json:"speech_input,omitempty"`
124124
TranscriptionInput *TranscriptionInput `json:"transcription_input,omitempty"`
@@ -295,12 +295,12 @@ type Fallback struct {
295295
// mapped to the provider's parameters.
296296
type ModelParameters struct {
297297
ToolChoice *ToolChoice `json:"tool_choice,omitempty"` // Whether to call a tool
298-
Tools []Tool `json:"tools,omitempty"` // Tools to use
298+
Tools []Tool `json:"tools,omitempty"` // Tools to use
299299
Temperature *float64 `json:"temperature,omitempty"` // Controls randomness in the output
300300
TopP *float64 `json:"top_p,omitempty"` // Controls diversity via nucleus sampling
301301
TopK *int `json:"top_k,omitempty"` // Controls diversity via top-k sampling
302302
MaxTokens *int `json:"max_tokens,omitempty"` // Maximum number of tokens to generate
303-
StopSequences []string `json:"stop_sequences,omitempty"` // Sequences that stop generation
303+
StopSequences []string `json:"stop_sequences,omitempty"` // Sequences that stop generation
304304
PresencePenalty *float64 `json:"presence_penalty,omitempty"` // Penalizes repeated tokens
305305
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` // Penalizes frequent tokens
306306
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"` // Enables parallel tool calls
@@ -318,7 +318,7 @@ type FunctionParameters struct {
318318
Description *string `json:"description,omitempty"` // Description of the parameters
319319
Required []string `json:"required,omitempty"` // Required parameter names
320320
Properties map[string]interface{} `json:"properties,omitempty"` // Parameter properties
321-
Enum []string `json:"enum,omitempty"` // Enum values for the parameters
321+
Enum []string `json:"enum,omitempty"` // Enum values for the parameters
322322
}
323323

324324
// Function represents a function that can be called by the model.
@@ -492,7 +492,7 @@ type ToolMessage struct {
492492
type AssistantMessage struct {
493493
Refusal *string `json:"refusal,omitempty"`
494494
Annotations []Annotation `json:"annotations,omitempty"`
495-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
495+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
496496
Thought *string `json:"thought,omitempty"`
497497
}
498498

@@ -795,7 +795,7 @@ type BifrostResponseExtraFields struct {
795795
Provider ModelProvider `json:"provider"`
796796
Params ModelParameters `json:"model_params"`
797797
Latency *int64 `json:"latency,omitempty"`
798-
ChatHistory []BifrostMessage `json:"chat_history,omitempty"`
798+
ChatHistory []BifrostMessage `json:"chat_history,omitempty"`
799799
BilledUsage *BilledLLMUsage `json:"billed_usage,omitempty"`
800800
ChunkIndex int `json:"chunk_index"` // used for streaming responses to identify the chunk index, will be 0 for non-streaming responses
801801
RawResponse interface{} `json:"raw_response,omitempty"`

framework/configstore/migrations.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func triggerMigrations(ctx context.Context, db *gorm.DB) error {
1919
if err := migrationAddCustomProviderConfigJSONColumn(ctx, db); err != nil {
2020
return err
2121
}
22+
if err := migrationAddVirtualKeyProviderConfigTable(db); err != nil {
23+
return err
24+
}
2225
return nil
2326
}
2427

@@ -247,3 +250,33 @@ func migrationAddCustomProviderConfigJSONColumn(ctx context.Context, db *gorm.DB
247250
}
248251
return nil
249252
}
253+
254+
func migrationAddVirtualKeyProviderConfigTable(db *gorm.DB) error {
255+
m := migration.New(db, migration.DefaultOptions, []*migration.Migration{{
256+
ID: "addvirtualkeyproviderconfig",
257+
Migrate: func(tx *gorm.DB) error {
258+
migrator := tx.Migrator()
259+
260+
if !migrator.HasTable(&TableVirtualKeyProviderConfig{}) {
261+
if err := migrator.CreateTable(&TableVirtualKeyProviderConfig{}); err != nil {
262+
return err
263+
}
264+
}
265+
266+
return nil
267+
},
268+
Rollback: func(tx *gorm.DB) error {
269+
migrator := tx.Migrator()
270+
271+
if err := migrator.DropTable(&TableVirtualKeyProviderConfig{}); err != nil {
272+
return err
273+
}
274+
return nil
275+
},
276+
}})
277+
err := m.Migrate()
278+
if err != nil {
279+
return fmt.Errorf("error while running db migration: %s", err.Error())
280+
}
281+
return nil
282+
}

framework/configstore/sqlite.go

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ func (s *SQLiteConfigStore) GetVirtualKeys(ctx context.Context) ([]TableVirtualK
845845
Preload("Customer").
846846
Preload("Budget").
847847
Preload("RateLimit").
848+
Preload("ProviderConfigs").
848849
Preload("Keys", func(db *gorm.DB) *gorm.DB {
849850
return db.Select("id, key_id, models_json")
850851
}).Find(&virtualKeys).Error; err != nil {
@@ -861,6 +862,7 @@ func (s *SQLiteConfigStore) GetVirtualKey(ctx context.Context, id string) (*Tabl
861862
Preload("Customer").
862863
Preload("Budget").
863864
Preload("RateLimit").
865+
Preload("ProviderConfigs").
864866
Preload("Keys", func(db *gorm.DB) *gorm.DB {
865867
return db.Select("id, key_id, models_json")
866868
}).First(&virtualKey, "id = ?", id).Error; err != nil {
@@ -869,6 +871,21 @@ func (s *SQLiteConfigStore) GetVirtualKey(ctx context.Context, id string) (*Tabl
869871
return &virtualKey, nil
870872
}
871873

874+
func (s *SQLiteConfigStore) GetVirtualKeyByValue(ctx context.Context, value string) (*TableVirtualKey, error) {
875+
var virtualKey TableVirtualKey
876+
if err := s.db.WithContext(ctx).Preload("Team").
877+
Preload("Customer").
878+
Preload("Budget").
879+
Preload("RateLimit").
880+
Preload("ProviderConfigs").
881+
Preload("Keys", func(db *gorm.DB) *gorm.DB {
882+
return db.Select("id, key_id, models_json")
883+
}).First(&virtualKey, "value = ?", value).Error; err != nil {
884+
return nil, err
885+
}
886+
return &virtualKey, nil
887+
}
888+
872889
func (s *SQLiteConfigStore) CreateVirtualKey(ctx context.Context, virtualKey *TableVirtualKey, tx ...*gorm.DB) error {
873890
var txDB *gorm.DB
874891
if len(tx) > 0 {
@@ -923,6 +940,53 @@ func (s *SQLiteConfigStore) UpdateVirtualKey(ctx context.Context, virtualKey *Ta
923940
return nil
924941
}
925942

943+
func (s *SQLiteConfigStore) GetVirtualKeyProviderConfigs(ctx context.Context, virtualKeyID string) ([]TableVirtualKeyProviderConfig, error) {
944+
var virtualKey TableVirtualKey
945+
if err := s.db.WithContext(ctx).First(&virtualKey, "id = ?", virtualKeyID).Error; err != nil {
946+
return nil, err
947+
}
948+
949+
if virtualKey.ID == "" {
950+
return nil, nil
951+
}
952+
953+
var providerConfigs []TableVirtualKeyProviderConfig
954+
if err := s.db.WithContext(ctx).Where("virtual_key_id = ?", virtualKey.ID).Find(&providerConfigs).Error; err != nil {
955+
return nil, err
956+
}
957+
return providerConfigs, nil
958+
}
959+
960+
func (s *SQLiteConfigStore) CreateVirtualKeyProviderConfig(ctx context.Context, virtualKeyProviderConfig *TableVirtualKeyProviderConfig, tx ...*gorm.DB) error {
961+
var txDB *gorm.DB
962+
if len(tx) > 0 {
963+
txDB = tx[0]
964+
} else {
965+
txDB = s.db
966+
}
967+
return txDB.WithContext(ctx).Create(virtualKeyProviderConfig).Error
968+
}
969+
970+
func (s *SQLiteConfigStore) UpdateVirtualKeyProviderConfig(ctx context.Context, virtualKeyProviderConfig *TableVirtualKeyProviderConfig, tx ...*gorm.DB) error {
971+
var txDB *gorm.DB
972+
if len(tx) > 0 {
973+
txDB = tx[0]
974+
} else {
975+
txDB = s.db
976+
}
977+
return txDB.WithContext(ctx).Save(virtualKeyProviderConfig).Error
978+
}
979+
980+
func (s *SQLiteConfigStore) DeleteVirtualKeyProviderConfig(ctx context.Context, id uint, tx ...*gorm.DB) error {
981+
var txDB *gorm.DB
982+
if len(tx) > 0 {
983+
txDB = tx[0]
984+
} else {
985+
txDB = s.db
986+
}
987+
return txDB.WithContext(ctx).Delete(&TableVirtualKeyProviderConfig{}, "id = ?", id).Error
988+
}
989+
926990
// GetKeysByIDs retrieves multiple keys by their IDs
927991
func (s *SQLiteConfigStore) GetKeysByIDs(ctx context.Context, ids []string) ([]TableKey, error) {
928992
if len(ids) == 0 {
@@ -1246,10 +1310,10 @@ func (s *SQLiteConfigStore) removeDuplicateKeysAndNullKeys(ctx context.Context)
12461310
// Close closes the SQLite config store.
12471311
func (s *SQLiteConfigStore) Close(ctx context.Context) error {
12481312
sqlDB, err := s.db.DB()
1249-
if err != nil {
1250-
return err
1251-
}
1252-
return sqlDB.Close()
1313+
if err != nil {
1314+
return err
1315+
}
1316+
return sqlDB.Close()
12531317
}
12541318

12551319
// newSqliteConfigStore creates a new SQLite config store.

framework/configstore/store.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@ type ConfigStore interface {
5555
// Governance config CRUD
5656
GetVirtualKeys(ctx context.Context) ([]TableVirtualKey, error)
5757
GetVirtualKey(ctx context.Context, id string) (*TableVirtualKey, error)
58+
GetVirtualKeyByValue(ctx context.Context, value string) (*TableVirtualKey, error)
5859
CreateVirtualKey(ctx context.Context, virtualKey *TableVirtualKey, tx ...*gorm.DB) error
5960
UpdateVirtualKey(ctx context.Context, virtualKey *TableVirtualKey, tx ...*gorm.DB) error
6061
DeleteVirtualKey(ctx context.Context, id string) error
6162

63+
// Virtual key provider config CRUD
64+
GetVirtualKeyProviderConfigs(ctx context.Context, virtualKeyID string) ([]TableVirtualKeyProviderConfig, error)
65+
CreateVirtualKeyProviderConfig(ctx context.Context, virtualKeyProviderConfig *TableVirtualKeyProviderConfig, tx ...*gorm.DB) error
66+
UpdateVirtualKeyProviderConfig(ctx context.Context, virtualKeyProviderConfig *TableVirtualKeyProviderConfig, tx ...*gorm.DB) error
67+
DeleteVirtualKeyProviderConfig(ctx context.Context, id uint, tx ...*gorm.DB) error
68+
6269
// Team CRUD
6370
GetTeams(ctx context.Context, customerID string) ([]TableTeam, error)
6471
GetTeam(ctx context.Context, id string) (*TableTeam, error)

framework/configstore/tables.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -616,13 +616,12 @@ type TableTeam struct {
616616

617617
// TableVirtualKey represents a virtual key with budget, rate limits, and team/customer association
618618
type TableVirtualKey struct {
619-
ID string `gorm:"primaryKey;type:varchar(255)" json:"id"`
620-
Name string `gorm:"uniqueIndex:idx_virtual_key_name;type:varchar(255);not null" json:"name"`
621-
Description string `gorm:"type:text" json:"description,omitempty"`
622-
Value string `gorm:"uniqueIndex:idx_virtual_key_value;type:varchar(255);not null" json:"value"` // The virtual key value
623-
IsActive bool `gorm:"default:true" json:"is_active"`
624-
AllowedModels []string `gorm:"type:text;serializer:json" json:"allowed_models"` // Empty means all models allowed
625-
AllowedProviders []string `gorm:"type:text;serializer:json" json:"allowed_providers"` // Empty means all providers allowed
619+
ID string `gorm:"primaryKey;type:varchar(255)" json:"id"`
620+
Name string `gorm:"uniqueIndex:idx_virtual_key_name;type:varchar(255);not null" json:"name"`
621+
Description string `gorm:"type:text" json:"description,omitempty"`
622+
Value string `gorm:"uniqueIndex:idx_virtual_key_value;type:varchar(255);not null" json:"value"` // The virtual key value
623+
IsActive bool `gorm:"default:true" json:"is_active"`
624+
ProviderConfigs []TableVirtualKeyProviderConfig `gorm:"foreignKey:VirtualKeyID;constraint:OnDelete:CASCADE" json:"provider_configs"` // Empty means all providers allowed
626625

627626
// Foreign key relationships (mutually exclusive: either TeamID or CustomerID, not both)
628627
TeamID *string `gorm:"type:varchar(255);index" json:"team_id,omitempty"`
@@ -641,6 +640,15 @@ type TableVirtualKey struct {
641640
UpdatedAt time.Time `gorm:"index;not null" json:"updated_at"`
642641
}
643642

643+
// TableVirtualKeyProviderConfig represents a provider configuration for a virtual key
644+
type TableVirtualKeyProviderConfig struct {
645+
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
646+
VirtualKeyID string `gorm:"type:varchar(255);not null" json:"virtual_key_id"`
647+
Provider string `gorm:"type:varchar(50);not null" json:"provider"`
648+
Weight float64 `gorm:"default:1.0" json:"weight"`
649+
AllowedModels []string `gorm:"type:text;serializer:json" json:"allowed_models"` // Empty means all models allowed
650+
}
651+
644652
// TableModelPricing represents pricing information for AI models
645653
type TableModelPricing struct {
646654
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
@@ -675,11 +683,14 @@ type TableModelPricing struct {
675683
}
676684

677685
// Table names
678-
func (TableBudget) TableName() string { return "governance_budgets" }
679-
func (TableRateLimit) TableName() string { return "governance_rate_limits" }
680-
func (TableCustomer) TableName() string { return "governance_customers" }
681-
func (TableTeam) TableName() string { return "governance_teams" }
682-
func (TableVirtualKey) TableName() string { return "governance_virtual_keys" }
686+
func (TableBudget) TableName() string { return "governance_budgets" }
687+
func (TableRateLimit) TableName() string { return "governance_rate_limits" }
688+
func (TableCustomer) TableName() string { return "governance_customers" }
689+
func (TableTeam) TableName() string { return "governance_teams" }
690+
func (TableVirtualKey) TableName() string { return "governance_virtual_keys" }
691+
func (TableVirtualKeyProviderConfig) TableName() string {
692+
return "governance_virtual_key_provider_configs"
693+
}
683694
func (TableConfig) TableName() string { return "governance_config" }
684695
func (TableModelPricing) TableName() string { return "governance_model_pricing" }
685696

framework/logstore/tables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ type Log struct {
102102
// Virtual fields for JSON output - these will be populated when needed
103103
InputHistoryParsed []schemas.BifrostMessage `gorm:"-" json:"input_history,omitempty"`
104104
OutputMessageParsed *schemas.BifrostMessage `gorm:"-" json:"output_message,omitempty"`
105-
EmbeddingOutputParsed []schemas.BifrostEmbedding `gorm:"-" json:"embedding_output,omitempty"`
105+
EmbeddingOutputParsed []schemas.BifrostEmbedding `gorm:"-" json:"embedding_output,omitempty"`
106106
ParamsParsed *schemas.ModelParameters `gorm:"-" json:"params,omitempty"`
107-
ToolsParsed []schemas.Tool `gorm:"-" json:"tools,omitempty"`
108-
ToolCallsParsed []schemas.ToolCall `gorm:"-" json:"tool_calls,omitempty"`
107+
ToolsParsed []schemas.Tool `gorm:"-" json:"tools,omitempty"`
108+
ToolCallsParsed []schemas.ToolCall `gorm:"-" json:"tool_calls,omitempty"`
109109
TokenUsageParsed *schemas.LLMUsage `gorm:"-" json:"token_usage,omitempty"`
110110
ErrorDetailsParsed *schemas.BifrostError `gorm:"-" json:"error_details,omitempty"`
111111
SpeechInputParsed *schemas.SpeechInput `gorm:"-" json:"speech_input,omitempty"`

0 commit comments

Comments
 (0)