Overview
Mage includes AI-powered features that help you generate code, create pipelines, and write documentation using Large Language Models (LLMs). These features support both OpenAI and Hugging Face models.Installation
Install Mage with AI capabilities:- astor >= 0.8.1
- langchain == 0.2.5
- langchain_community == 0.2.5
- openai == 1.82.0
Configuration
OpenAI Setup
Enable OpenAI and configure your API key:
Mage uses GPT-4 by default (openai_client.py:80):
Hugging Face Setup
Enable Hugging Face models:AI Client Architecture
Mage’s AI system uses a client-based architecture (llm_pipeline_wizard.py:193-202):Features
1. Block Generation
Generate blocks from natural language descriptions.- Block Type:
data_loader,transformer, ordata_exporter - Language:
python,sql,r,yaml, ormarkdown - Pipeline Type:
python,pyspark,streaming, etc. - Action Type: For transformers (filter, group, aggregate, etc.)
- Data Source: For loaders/exporters (postgres, bigquery, s3, etc.)
2. Pipeline Generation
Generate entire pipelines from descriptions.- Splits the description into logical blocks (llm_pipeline_wizard.py:103-126)
- Determines upstream dependencies
- Generates code for each block
- Configures proper block connections
3. Code Generation
Generate custom code within blocks.- Python
- SQL
4. Documentation Generation
Generate documentation for blocks and pipelines.- Analyzes block code and purpose
- Focuses on business logic, not boilerplate
- Follows Google Docstring format for function comments
- Generates block-level and pipeline-level documentation
5. Function Comments
Add AI-generated comments to existing code.- Parses the Python AST
- Identifies all functions
- Generates Google Docstring format comments
- Inserts comments while preserving code structure
Prompt Engineering
Mage uses carefully crafted prompts for different tasks (llm_pipeline_wizard.py:51-135):Block Documentation Prompt
Code Generation Prompt
Advanced Usage
Custom Inference
Use the AI client directly for custom prompts (openai_client.py:107-150):Function Calling
Mage uses OpenAI function calling for structured outputs (openai_client.py:92-105):Retry Logic
The AI client includes automatic retry logic (openai_client.py:196-203):Best Practices
Secure API keys
Use environment variables or secret management for API keys, never commit them to version control.