|
| 1 | +""" |
| 2 | +BlueCastAI: Multi-agent LLM-powered AutoML. |
| 3 | +
|
| 4 | +Optional module -- install dependencies with: |
| 5 | + pip install bluecast[ai] # all providers |
| 6 | + pip install bluecast[ai-gemini] # Google Gemini only |
| 7 | + pip install bluecast[ai-openai] # OpenAI only |
| 8 | + pip install bluecast[ai-anthropic] # Anthropic Claude only |
| 9 | +
|
| 10 | +Usage:: |
| 11 | +
|
| 12 | + from bluecast.ai import BlueCastAI |
| 13 | +
|
| 14 | + ai = BlueCastAI(api_key="...", provider="gemini") |
| 15 | + result = ai.run(df_train, target_col="target", |
| 16 | + prompt="Build a precise binary classifier") |
| 17 | + result.predict(df_test) |
| 18 | + result.save_code("pipeline.py") |
| 19 | +""" |
| 20 | + |
| 21 | +import logging |
| 22 | +from typing import List, Literal, Optional |
| 23 | + |
| 24 | +import pandas as pd |
| 25 | + |
| 26 | +from bluecast.ai.config import AIConfig |
| 27 | +from bluecast.ai.result import BlueCastAIResult |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +def _create_provider(config: AIConfig): |
| 33 | + """Factory to create the right LLM provider based on config.""" |
| 34 | + model = config.get_model_name() |
| 35 | + |
| 36 | + if config.provider == "gemini": |
| 37 | + from bluecast.ai.providers.gemini import GeminiProvider |
| 38 | + return GeminiProvider(api_key=config.api_key, model=model, temperature=config.temperature) |
| 39 | + elif config.provider == "openai": |
| 40 | + from bluecast.ai.providers.openai_provider import OpenAIProvider |
| 41 | + return OpenAIProvider(api_key=config.api_key, model=model, temperature=config.temperature) |
| 42 | + elif config.provider == "anthropic": |
| 43 | + from bluecast.ai.providers.anthropic_provider import AnthropicProvider |
| 44 | + return AnthropicProvider(api_key=config.api_key, model=model, temperature=config.temperature) |
| 45 | + else: |
| 46 | + raise ValueError(f"Unknown provider: {config.provider}. Use 'gemini', 'openai', or 'anthropic'.") |
| 47 | + |
| 48 | + |
| 49 | +class BlueCastAI: |
| 50 | + """Multi-agent LLM-powered AutoML for BlueCast. |
| 51 | +
|
| 52 | + Provide an API key, a dataset, and a natural language prompt. |
| 53 | + BlueCastAI will analyze the data, engineer features, build a pipeline, |
| 54 | + evaluate it, and iteratively improve it -- all guided by LLM agents. |
| 55 | +
|
| 56 | + :param api_key: API key for the LLM provider. |
| 57 | + :param provider: LLM provider: 'gemini', 'openai', or 'anthropic'. |
| 58 | + :param model: Provider-specific model name (e.g. 'gpt-4o', 'claude-sonnet-4-20250514'). |
| 59 | + Uses a sensible default per provider if not specified. |
| 60 | + :param enable_web_search: Whether agents can search the web for techniques. |
| 61 | + :param verbose: Whether to print progress to stdout. |
| 62 | + :param temperature: LLM temperature (0.0 = deterministic, 1.0 = creative). |
| 63 | + :param checkpoint_dir: Directory for saving checkpoints. If a run crashes, |
| 64 | + the next call to .run() with the same checkpoint_dir resumes from where |
| 65 | + it left off. Set to None to disable checkpointing. |
| 66 | +
|
| 67 | + Usage:: |
| 68 | +
|
| 69 | + from bluecast.ai import BlueCastAI |
| 70 | +
|
| 71 | + ai = BlueCastAI(api_key="your-key", provider="gemini") |
| 72 | + result = ai.run( |
| 73 | + df_train, |
| 74 | + target_col="target", |
| 75 | + prompt="Build a high-precision binary classifier with hill climbing ensemble", |
| 76 | + mode="precise", |
| 77 | + ) |
| 78 | +
|
| 79 | + # Use the trained pipeline |
| 80 | + predictions = result.predict(df_test) |
| 81 | +
|
| 82 | + # Export reproducible code |
| 83 | + result.save_code("my_pipeline.py") |
| 84 | +
|
| 85 | + # View what happened |
| 86 | + result.show_report() |
| 87 | + """ |
| 88 | + |
| 89 | + def __init__( |
| 90 | + self, |
| 91 | + api_key: str, |
| 92 | + provider: Literal["gemini", "openai", "anthropic"] = "gemini", |
| 93 | + model: Optional[str] = None, |
| 94 | + enable_web_search: bool = False, |
| 95 | + verbose: bool = True, |
| 96 | + temperature: float = 0.2, |
| 97 | + checkpoint_dir: Optional[str] = None, |
| 98 | + ): |
| 99 | + self.config = AIConfig( |
| 100 | + api_key=api_key, |
| 101 | + provider=provider, |
| 102 | + model=model, |
| 103 | + enable_web_search=enable_web_search, |
| 104 | + verbose=verbose, |
| 105 | + temperature=temperature, |
| 106 | + checkpoint_dir=checkpoint_dir, |
| 107 | + ) |
| 108 | + self._llm = _create_provider(self.config) |
| 109 | + |
| 110 | + def run( |
| 111 | + self, |
| 112 | + df: pd.DataFrame, |
| 113 | + target_col: str, |
| 114 | + prompt: str = "Build a good model", |
| 115 | + context_files: Optional[List[str]] = None, |
| 116 | + mode: Literal["fast", "balanced", "precise"] = "balanced", |
| 117 | + max_iterations: int = 0, |
| 118 | + ) -> BlueCastAIResult: |
| 119 | + """Run the multi-agent pipeline on the dataset. |
| 120 | +
|
| 121 | + :param df: Training DataFrame including the target column. |
| 122 | + :param target_col: Name of the target column. |
| 123 | + :param prompt: Natural language instructions for what to build. |
| 124 | + Examples: |
| 125 | + - "Build a fast baseline model" |
| 126 | + - "Build a precise binary classifier with stacking ensemble" |
| 127 | + - "Maximize ROC AUC using hill climbing and feature engineering" |
| 128 | + :param context_files: Optional list of file paths containing domain knowledge. |
| 129 | + :param mode: Speed vs thoroughness trade-off: |
| 130 | + 'fast' = skip FE, 1 iteration (~2 min), |
| 131 | + 'balanced' = targeted FE, 3 iterations (~10 min), |
| 132 | + 'precise' = full FE, ensemble, 5+ iterations (~30 min). |
| 133 | + :param max_iterations: Override the number of build-evaluate-improve cycles. |
| 134 | + If 0, uses the mode default. |
| 135 | + :returns: BlueCastAIResult with trained pipeline, code, metrics, and logs. |
| 136 | + """ |
| 137 | + self.config.mode = mode |
| 138 | + self.config.context_files = context_files or [] |
| 139 | + if max_iterations > 0: |
| 140 | + self.config.max_iterations = max_iterations |
| 141 | + |
| 142 | + from bluecast.ai.orchestrator import Orchestrator |
| 143 | + |
| 144 | + orchestrator = Orchestrator( |
| 145 | + llm=self._llm, |
| 146 | + config=self.config, |
| 147 | + df=df, |
| 148 | + target_col=target_col, |
| 149 | + prompt=prompt, |
| 150 | + ) |
| 151 | + return orchestrator.run() |
0 commit comments