| |
| |
| package maxim |
|
|
| import ( |
| "context" |
| "fmt" |
| "log" |
| "os" |
| "testing" |
|
|
| bifrost "github.com/maximhq/bifrost/core" |
| "github.com/maximhq/bifrost/core/schemas" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func getPlugin() (schemas.LLMPlugin, error) { |
| |
| if os.Getenv("MAXIM_API_KEY") == "" { |
| return nil, fmt.Errorf("MAXIM_API_KEY is not set, please set it in your environment variables") |
| } |
|
|
| logger := bifrost.NewDefaultLogger(schemas.LogLevelDebug) |
| plugin, err := Init(&Config{ |
| APIKey: os.Getenv("MAXIM_API_KEY"), |
| LogRepoID: os.Getenv("MAXIM_LOG_REPO_ID"), |
| }, logger) |
| if err != nil { |
| return nil, err |
| } |
|
|
| return plugin, nil |
| } |
|
|
| |
| |
| |
| type BaseAccount struct{} |
|
|
| |
| |
| func (baseAccount *BaseAccount) GetConfiguredProviders() ([]schemas.ModelProvider, error) { |
| return []schemas.ModelProvider{schemas.OpenAI}, nil |
| } |
|
|
| |
| |
| func (baseAccount *BaseAccount) GetKeysForProvider(ctx context.Context, providerKey schemas.ModelProvider) ([]schemas.Key, error) { |
| return []schemas.Key{ |
| { |
| Value: *schemas.NewEnvVar("env.OPENAI_API_KEY"), |
| Models: []string{"gpt-4o-mini", "gpt-4-turbo"}, |
| Weight: 1.0, |
| }, |
| }, nil |
| } |
|
|
| |
| |
| func (baseAccount *BaseAccount) GetConfigForProvider(providerKey schemas.ModelProvider) (*schemas.ProviderConfig, error) { |
| return &schemas.ProviderConfig{ |
| NetworkConfig: schemas.DefaultNetworkConfig, |
| ConcurrencyAndBufferSize: schemas.DefaultConcurrencyAndBufferSize, |
| }, nil |
| } |
|
|
| func (baseAccount *BaseAccount) GetGlobalKeys(ctx context.Context) ([]schemas.Key, error) { |
| return nil, nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func TestMaximLoggerPlugin(t *testing.T) { |
| ctx := context.Background() |
| |
| plugin, err := getPlugin() |
| if err != nil { |
| t.Fatalf("Error setting up the plugin: %v", err) |
| } |
|
|
| account := BaseAccount{} |
|
|
| |
| client, err := bifrost.Init(ctx, schemas.BifrostConfig{ |
| Account: &account, |
| LLMPlugins: []schemas.LLMPlugin{plugin}, |
| Logger: bifrost.NewDefaultLogger(schemas.LogLevelDebug), |
| }) |
| if err != nil { |
| t.Fatalf("Error initializing Bifrost: %v", err) |
| } |
|
|
| |
| _, bifrostErr := client.ChatCompletionRequest(schemas.NewBifrostContext(context.Background(), schemas.NoDeadline), &schemas.BifrostChatRequest{ |
| Provider: schemas.OpenAI, |
| Model: "gpt-4o-mini", |
| Input: []schemas.ChatMessage{ |
| { |
| Role: "user", |
| Content: &schemas.ChatMessageContent{ |
| ContentStr: bifrost.Ptr("Hello, how are you?"), |
| }, |
| }, |
| }, |
| }) |
|
|
| if bifrostErr != nil { |
| log.Printf("Error in Bifrost request: %v", bifrostErr) |
| } |
|
|
| log.Println("Bifrost request completed, check your Maxim Dashboard for the trace") |
|
|
| client.Shutdown() |
| } |
|
|
| |
| func TestLogRepoIDSelection(t *testing.T) { |
| tests := []struct { |
| name string |
| defaultRepo string |
| headerRepo string |
| expectedRepo string |
| shouldLog bool |
| }{ |
| { |
| name: "Header repo takes priority", |
| defaultRepo: "default-repo", |
| headerRepo: "header-repo", |
| expectedRepo: "header-repo", |
| shouldLog: true, |
| }, |
| { |
| name: "Fall back to default repo when no header", |
| defaultRepo: "default-repo", |
| headerRepo: "", |
| expectedRepo: "default-repo", |
| shouldLog: true, |
| }, |
| { |
| name: "Use header repo when no default", |
| defaultRepo: "", |
| headerRepo: "header-repo", |
| expectedRepo: "header-repo", |
| shouldLog: true, |
| }, |
| { |
| name: "Skip logging when neither available", |
| defaultRepo: "", |
| headerRepo: "", |
| expectedRepo: "", |
| shouldLog: false, |
| }, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| |
| plugin := &Plugin{ |
| defaultLogRepoID: tt.defaultRepo, |
| } |
|
|
| |
| ctx := schemas.NewBifrostContext(context.Background(), schemas.NoDeadline) |
| if tt.headerRepo != "" { |
| ctx.SetValue(LogRepoIDKey, tt.headerRepo) |
| } |
|
|
| |
| result := plugin.getEffectiveLogRepoID(ctx) |
|
|
| if result != tt.expectedRepo { |
| t.Errorf("Expected repo '%s', got '%s'", tt.expectedRepo, result) |
| } |
|
|
| shouldLog := result != "" |
| if shouldLog != tt.shouldLog { |
| t.Errorf("Expected shouldLog=%t, got shouldLog=%t", tt.shouldLog, shouldLog) |
| } |
| }) |
| } |
| } |
|
|
| |
| func TestPluginInitialization(t *testing.T) { |
| logger := bifrost.NewDefaultLogger(schemas.LogLevelDebug) |
| tests := []struct { |
| name string |
| config Config |
| expectError bool |
| }{ |
| { |
| name: "Valid config with both fields", |
| config: Config{ |
| APIKey: "test-api-key", |
| LogRepoID: "test-repo-id", |
| }, |
| expectError: false, |
| }, |
| { |
| name: "Valid config with only API key", |
| config: Config{ |
| APIKey: "test-api-key", |
| LogRepoID: "", |
| }, |
| expectError: false, |
| }, |
| { |
| name: "Invalid config - missing API key", |
| config: Config{ |
| APIKey: "", |
| LogRepoID: "test-repo-id", |
| }, |
| expectError: true, |
| }, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| |
| if tt.expectError { |
| _, err := Init(&tt.config, logger) |
| if err == nil { |
| t.Error("Expected error but got none") |
| } |
| } else { |
| |
| |
| if tt.config.APIKey == "" { |
| t.Skip("Skipping valid config test - would need real Maxim API key") |
| } |
| } |
| }) |
| } |
| } |
|
|
| |
| func TestPluginName(t *testing.T) { |
| plugin := &Plugin{} |
| if plugin.GetName() != PluginName { |
| t.Errorf("Expected plugin name '%s', got '%s'", PluginName, plugin.GetName()) |
| } |
| if PluginName != "maxim" { |
| t.Errorf("Expected PluginName constant to be 'maxim', got '%s'", PluginName) |
| } |
| } |
|
|