AI Features Guide - dist_agent_lang (v1.0.8)

Beta Release v1.0.8: Includes comprehensive AI agent framework with persistent memory and extensible skills. Actively maintained. Test AI features thoroughly. Beta testing feedback appreciated!

Complete guide to building AI-powered smart contracts and intelligent agents with dist_agent_lang.


📋 Table of Contents

  1. Why AI + Blockchain?
  2. AI Capabilities Overview
  3. AI stdlib Module
  4. Building AI Agents
  5. AI + Oracle Integration
  6. AI + DeFi Integration
  7. Performance & Cost
  8. Security Considerations
  9. Real-World Use Cases

🎯 Why AI + Blockchain?

The Perfect Combination

Blockchain provides:

AI provides:

Together they enable:


🚀 AI Capabilities Overview

What You Can Build with DAL's AI Features

Feature Description Use Case
Text Classification Classify text into categories Sentiment analysis, spam detection, content moderation
Text Generation Generate human-like text Automated responses, report generation, documentation
Embeddings Convert text to vector representations Semantic search, similarity matching, clustering
Image Analysis Analyze and classify images NFT verification, content moderation, art analysis
Prediction Make predictions based on historical data Price forecasting, risk assessment, trend analysis
Recommendation Suggest items based on preferences Token recommendations, NFT suggestions, strategy tips
Anomaly Detection Identify unusual patterns Fraud detection, security monitoring, outlier detection
Natural Language Understanding Parse and understand user intent Conversational DeFi, chatbot contracts, voice commands

📚 AI stdlib Module - Agent Framework

Current Implementation (Phase 1)

The AI module currently implements a comprehensive agent framework for building intelligent, autonomous agents.

Agent Lifecycle Management

// Create and spawn a new AI agent
service AgentManager {
    fn create_intelligent_agent() {
        let config = {
            "agent_id": "trader_001",
            "name": "Trading Agent",
            "role": "market_analyzer",
            "capabilities": ["text_analysis", "data_processing", "trading"],
            "memory_size": 1024,
            "max_concurrent_tasks": 5,
            "trust_level": "high",
            "communication_protocols": ["secure_message"],
            "ai_models": ["sentiment_analyzer", "price_predictor"]
        };
        
        let agent_result = ai::spawn_agent(config);
        if agent_result.is_ok() {
            let agent = agent_result.unwrap();
            log::info("ai", "Agent created: " + agent.id);
        }
    }
    
    // Get agent status
    fn check_agent_status(agent_id: string) -> string {
        // Note: In actual implementation, you'd need to store agent reference
        // This is a simplified example
        return "idle"; // Returns: "idle", "active", "busy", "error", or "terminated"
    }
    
    // Terminate agent when done
    fn shutdown_agent(agent_id: string) {
        // Note: In actual implementation, you'd need agent reference
        log::info("ai", "Shutting down agent: " + agent_id);
    }
}

Text Analysis (Built-in)

service TextAnalyzer {
    // Analyze text with AI
    fn analyze_text(text: string) {
        let analysis_result = ai::analyze_text(text);
        
        if analysis_result.is_ok() {
            let analysis = analysis_result.unwrap();
            
            // TextAnalysis includes:
            // - sentiment: float (0.0 to 1.0)
            // - entities: list of Entity objects
            // - keywords: list of strings
            // - summary: string
            // - language: string
            // - confidence: float
            
            log::info("ai", "Sentiment: " + analysis.sentiment.to_string());
            log::info("ai", "Confidence: " + analysis.confidence.to_string());
        }
    }
}

Text Generation (Built-in)

service TextGenerator {
    // Generate text response
    fn generate_response(prompt: string) -> string {
        let result = ai::generate_text(prompt);
        if result.is_ok() {
            return result.unwrap();
        }
        return "";
    }
}

Simplified API Functions

service SimplifiedAI {
    // Classify text using simplified API
    fn classify_text(input: string) -> string {
        let result = ai::classify("sentiment_model", input);
        return result.unwrap_or("");
    }
    
    // Generate text using simplified API
    fn generate_text(prompt: string) -> string {
        let result = ai::generate("gpt-4", prompt);
        return result.unwrap_or("");
    }
    
    // Generate embeddings
    fn get_embeddings(text: string) -> list<float> {
        let result = ai::embed(text);
        return result.unwrap_or([]);
    }
    
    // Detect anomalies
    fn check_anomaly(data: list<float>, new_value: float) -> bool {
        let result = ai::detect_anomaly(data, new_value);
        return result.unwrap_or(false);
    }
}

Task Management

service TaskManager {
    fn create_analysis_task(agent_id: string, data: string) {
        // Note: In actual implementation, you'd need agent reference
        // This shows the concept
        
        let params = {
            "text": data
        };
        
        // Create task would require agent reference
        // let task = ai::create_task(agent, "text_analysis", "Analyze market sentiment", params);
        
        log::info("ai", "Task created for agent: " + agent_id);
    }
    
    // Execute task and get results
    fn execute_analysis(agent_id: string, task_id: string) {
        // Note: In actual implementation, you'd need agent reference
        // let result = ai::execute_task(agent, task_id);
        
        log::info("ai", "Task executed: " + task_id);
    }
}

Image Analysis (Built-in)

service ImageAnalyzer {
    // Analyze image data
    fn analyze_nft_image(image_data: list<int>) {
        // Convert list<int> to Vec<u8> internally
        let result = ai::analyze_image(image_data);
        
        if result.is_ok() {
            let analysis = result.unwrap();
            
            // ImageAnalysis includes:
            // - objects: list of DetectedObject
            // - faces: list of Face
            // - text: list of strings
            // - colors: list of strings
            // - quality_score: float
            
            log::info("ai", "Quality score: " + analysis.quality_score.to_string());
        }
    }
    
    // Analyze image from URL
    fn analyze_image_url(url: string) {
        let result = ai::analyze_image_url(url);
        if result.is_ok() {
            let analysis = result.unwrap();
            log::info("ai", "Image analyzed from URL");
        }
    }
}

AI Model Training & Prediction

service ModelTrainer {
    // Train a custom model
    fn train_custom_model(data_type: string, samples: list<any>, labels: list<any>) {
        let training_data = {
            "data_type": data_type,
            "samples": samples,
            "labels": labels
        };
        
        let result = ai::train_model(training_data);
        if result.is_ok() {
            let model = result.unwrap();
            log::info("ai", "Model trained: " + model.model_id);
        }
    }
    
    // Make predictions with trained model
    fn make_prediction(model_id: string, input: any) {
        // Note: Would need model reference in actual implementation
        // let prediction = ai::predict(model, input);
        
        log::info("ai", "Making prediction with model: " + model_id);
    }
    
    // Simplified prediction API
    fn predict_with_model(model_name: string, input: any) {
        let result = ai::predict_with_model(model_name, input);
        if result.is_ok() {
            let prediction = result.unwrap();
            log::info("ai", "Prediction made");
        }
    }
}

🤖 Building AI Agents

Agent Architecture (Actual Implementation)

@ai
@chain("ethereum")
service IntelligentTradingSystem {
    // Agent storage
    user_agents: map<string, string> = {}; // user_id -> agent_id
    
    // Create a trading agent using the AI framework
    fn create_trading_agent(user_id: string, name: string, strategy: string) {
        // Create agent configuration
        let config = {
            "agent_id": "trader_" + user_id,
            "name": name,
            "role": "trading_agent",
            "capabilities": ["text_analysis", "market_analysis", "trading"],
            "memory_size": 2048,
            "max_concurrent_tasks": 10,
            "trust_level": "high",
            "communication_protocols": ["secure"],
            "ai_models": ["sentiment", "predictor"]
        };
        
        // Spawn the agent
        let agent_result = ai::spawn_agent(config);
        if agent_result.is_ok() {
            let agent = agent_result.unwrap();
            user_agents[user_id] = agent.id;
            log::info("ai", "Trading agent created: " + agent.id);
        }
    }
    
    // AI-powered decision making using actual framework
    fn analyze_market_sentiment(market_data: string) -> string {
        // Use AI to analyze text
        let analysis_result = ai::analyze_text(market_data);
        
        if analysis_result.is_ok() {
            let analysis = analysis_result.unwrap();
            
            // sentiment is a float: positive > 0.5, negative < 0.5
            if analysis.sentiment > 0.7 {
                return "bullish";
            } else if analysis.sentiment < 0.3 {
                return "bearish";
            } else {
                return "neutral";
            }
        }
        
        return "unknown";
    }
    
    // Generate trading recommendation
    fn get_trade_recommendation(prompt: string) -> string {
        // Generate text recommendation
        let result = ai::generate_text(prompt);
        if result.is_ok() {
            return result.unwrap();
        }
        return "";
    }
}

Multi-Agent Coordination (Actual Framework)

@ai
@chain("ethereum")
service MultiAgentCoordinator {
    coordinator_id: string = "";
    
    fn initialize_coordinator() {
        let coordinator = ai::create_coordinator("dao_coordinator");
        coordinator_id = coordinator.coordinator_id;
        log::info("ai", "Coordinator created: " + coordinator_id);
    }
    
    fn add_specialized_agent(role: string, name: string) {
        // Create agent config for specific role
        let config = {
            "agent_id": role + "_agent",
            "name": name,
            "role": role, // "analyst", "trader", "risk_manager"
            "capabilities": get_role_capabilities(role),
            "memory_size": 2048,
            "max_concurrent_tasks": 5,
            "trust_level": "high",
            "communication_protocols": ["secure"],
            "ai_models": get_role_models(role)
        };
        
        // Spawn and add to coordinator
        let agent_result = ai::spawn_agent(config);
        if agent_result.is_ok() {
            let agent = agent_result.unwrap();
            log::info("ai", "Agent added: " + agent.id);
        }
    }
    
    fn get_role_capabilities(role: string) -> list<string> {
        if role == "analyst" {
            return ["text_analysis", "data_analysis"];
        } else if role == "trader" {
            return ["trading", "market_analysis"];
        } else if role == "risk_manager" {
            return ["risk_assessment", "validation"];
        }
        return ["general"];
    }
    
    fn get_role_models(role: string) -> list<string> {
        return ["sentiment_analyzer", "text_generator"];
    }
}

🔮 AI + Oracle Integration

Intelligent Price Feeds with Anomaly Detection

@ai
@chain("ethereum")
service IntelligentOracle {
    current_price: int = 0;
    price_history: list<int> = [];
    max_history: int = 100;
    
    fn update_price_with_validation() {
        // Fetch from oracle
        let oracle_result = oracle::fetch("chainlink", "ETH/USD");
        
        if oracle_result.is_ok() {
            let price_data = oracle_result.unwrap();
            let new_price = price_data.data;
            
            // Use AI to detect anomalies
            price_history.push(new_price);
            if price_history.len() > max_history {
                // Remove oldest
                price_history = price_history.slice(1);
            }
            
            // AI anomaly detection
            let history_floats: list<float> = [];
            for price in price_history {
                history_floats.push(price.to_float());
            }
            
            let anomaly_result = ai::detect_anomaly(history_floats, new_price.to_float());
            
            if anomaly_result.is_ok() && anomaly_result.unwrap() {
                // Price is unusual - require additional validation
                log::warn("oracle", "Anomalous price detected: " + new_price.to_string());
                
                // Use AI to analyze if this is a real market event
                let market_data = json::stringify({
                    "historical": price_history,
                    "new": new_price
                });
                
                let analysis = ai::generate_text("Is this price movement legitimate? " + market_data);
                let legitimacy = ai::classify("legitimacy_classifier", analysis);
                
                if legitimacy != "legitimate" {
                    log::error("oracle", "Suspicious price movement detected");
                    return;
                }
            }
            
            current_price = new_price;
            log::info("oracle", "Price updated: " + current_price.to_string());
        }
    }
}

💰 AI + DeFi Integration

AI-Powered Automated Market Maker (AMM)

@ai
@chain("ethereum")
service IntelligentAMM {
    // Dynamic fee adjustment based on market conditions
    fn get_swap_fee(token_in: string, token_out: string, amount_in: int) -> int {
        // Get market volatility
        let volatility = calculate_volatility(token_in, token_out);
        
        // Get AI recommendation for fee
        let market_data = json::stringify({
            "token_in": token_in,
            "token_out": token_out,
            "amount": amount_in,
            "volatility": volatility,
            "pool_tvl": get_total_value_locked(),
            "recent_volume": get_recent_volume()
        });
        
        let fee_recommendation = ai::generate("fee_optimizer", "Recommend optimal swap fee for these conditions: " + market_data);
        
        if fee_recommendation.is_ok() {
            // Parse AI recommendation (e.g., "0.3%")
            let recommended_fee = parse_fee_bps(fee_recommendation.unwrap());
            
            // Ensure fee is within acceptable range
            let min_fee = 10;  // 0.1%
            let max_fee = 100; // 1%
            
            if recommended_fee < min_fee {
                return min_fee;
            }
            if recommended_fee > max_fee {
                return max_fee;
            }
            
            return recommended_fee;
        }
        
        return 30; // Default 0.3%
    }
    
    fn calculate_volatility(token_in: string, token_out: string) -> float {
        // Simplified volatility calculation
        return 0.05; // 5% volatility
    }
    
    fn get_total_value_locked() -> int {
        return 1000000; // Simplified
    }
    
    fn get_recent_volume() -> int {
        return 50000; // Simplified
    }
    
    fn parse_fee_bps(fee_text: string) -> int {
        // Parse fee from text (simplified)
        return 30; // Default
    }
}

AI-Driven Lending Protocol

@ai
@chain("ethereum")
service SmartLendingPool {
    // AI-powered credit scoring
    fn get_credit_score(user_address: string) -> int {
        // Gather user's on-chain history
        let transaction_history = get_user_transactions(user_address);
        let borrow_history = get_user_borrow_history(user_address);
        let collateral_history = get_user_collateral_history(user_address);
        
        // Prepare data for AI model
        let user_data = json::stringify({
            "address": user_address,
            "tx_count": transaction_history.len(),
            "borrow_history": borrow_history,
            "collateral_history": collateral_history,
            "account_age": get_account_age(user_address)
        });
        
        // AI credit score (0-1000)
        let score_result = ai::generate("credit_scorer", "Calculate credit score for this user: " + user_data);
        
        if score_result.is_ok() {
            return parse_credit_score(score_result.unwrap());
        }
        
        return 500; // Default score
    }
    
    // Dynamic interest rates based on risk
    fn get_interest_rate(user_address: string, amount: int) -> int {
        let credit_score = get_credit_score(user_address);
        let pool_utilization = get_utilization_rate();
        
        // AI-powered rate calculation
        let rate_data = json::stringify({
            "credit_score": credit_score,
            "utilization": pool_utilization,
            "borrow_amount": amount,
            "market_conditions": get_current_market_conditions()
        });
        
        let rate_result = ai::predict_with_model("interest_rate_model", rate_data);
        
        if rate_result.is_ok() {
            let rate_value = rate_result.unwrap();
            // Extract rate from Value (simplified)
            return 500; // 5% APR in basis points
        }
        
        return 500; // Default rate
    }
    
    fn get_user_transactions(address: string) -> list<any> {
        return [];
    }
    
    fn get_user_borrow_history(address: string) -> list<any> {
        return [];
    }
    
    fn get_user_collateral_history(address: string) -> list<any> {
        return [];
    }
    
    fn get_account_age(address: string) -> int {
        return 86400; // 1 day
    }
    
    fn get_utilization_rate() -> float {
        return 0.75; // 75%
    }
    
    fn get_current_market_conditions() -> string {
        return "stable";
    }
    
    fn parse_credit_score(score_text: string) -> int {
        // Parse score from text (simplified)
        return 600;
    }
}

🎨 AI + NFT Integration

Dynamic AI-Generated NFTs

@ai
@chain("ethereum")
service DynamicNFT {
    nft_data: map<int, map<string, any>> = {};
    next_token_id: int = 1;
    
    // Mint NFT with AI-generated art
    fn mint_ai_nft(prompt: string) -> int {
        let token_id = next_token_id;
        next_token_id = next_token_id + 1;
        
        // Generate image using AI
        let image_result = ai::generate_image("stable_diffusion", prompt);
        
        if image_result.is_ok() {
            let image_url = image_result.unwrap();
            
            // Store metadata
            let metadata = json::stringify({
                "name": "AI NFT #" + token_id.to_string(),
                "description": "AI-generated NFT that evolves over time",
                "image": image_url,
                "prompt": prompt,
                "created": time::now()
            });
            
            nft_data[token_id] = {
                "token_id": token_id,
                "base_prompt": prompt,
                "last_evolution": time::now(),
                "evolution_count": 0,
                "metadata": metadata
            };
            
            log::info("nft", "NFT minted: " + token_id.to_string());
            return token_id;
        }
        
        return 0;
    }
    
    // NFT evolves over time using AI
    fn evolve_nft(token_id: int) {
        if !nft_data.has(token_id) {
            log::error("nft", "NFT not found");
            return;
        }
        
        let nft = nft_data[token_id];
        let last_evolution = nft.get("last_evolution", 0);
        let evolution_count = nft.get("evolution_count", 0);
        
        // Check cooldown (30 days)
        if time::now() - last_evolution < 2592000 {
            log::error("nft", "Evolution cooldown");
            return;
        }
        
        // Generate evolved version using AI
        let base_prompt = nft.get("base_prompt", "");
        let evolution_prompt = base_prompt + ", evolved form, iteration " + (evolution_count + 1).to_string();
        
        let new_image_result = ai::generate_image("stable_diffusion", evolution_prompt);
        
        if new_image_result.is_ok() {
            let new_image_url = new_image_result.unwrap();
            
            // Update metadata
            let new_metadata = json::stringify({
                "name": "AI NFT #" + token_id.to_string(),
                "description": "AI-generated NFT - Evolution " + (evolution_count + 1).to_string(),
                "image": new_image_url,
                "prompt": evolution_prompt,
                "created": time::now(),
                "evolution": evolution_count + 1
            });
            
            nft_data[token_id] = {
                "token_id": token_id,
                "base_prompt": base_prompt,
                "last_evolution": time::now(),
                "evolution_count": evolution_count + 1,
                "metadata": new_metadata
            };
            
            log::info("nft", "NFT evolved: " + token_id.to_string());
        }
    }
}

⚡ Performance & Cost

Gas Optimization for AI Operations

AI operations are executed off-chain but verified on-chain:

@ai
@chain("ethereum")
service OptimizedAIContract {
    // Cache AI results to save gas
    ai_result_cache: map<string, string> = {};
    cache_timestamp: map<string, int> = {};
    cache_duration: int = 3600; // 1 hour
    
    fn get_cached_ai_result(input: string) -> string {
        let cache_key = crypto::hash(input);
        
        // Check cache first
        let timestamp = cache_timestamp.get(cache_key, 0);
        if timestamp > 0 && time::now() - timestamp < cache_duration {
            return ai_result_cache.get(cache_key, "");
        }
        
        // Cache miss - would need new AI call
        return "";
    }
    
    fn get_ai_result_with_cache(input: string) -> string {
        let cache_key = crypto::hash(input);
        
        // Try cache first
        let cached = get_cached_ai_result(input);
        if cached != "" {
            return cached;
        }
        
        // Generate new result
        let result = ai::classify("model", input);
        if result.is_ok() {
            let result_value = result.unwrap();
            
            // Update cache
            ai_result_cache[cache_key] = result_value;
            cache_timestamp[cache_key] = time::now();
            
            return result_value;
        }
        
        return "";
    }
}

Cost Comparison

Operation On-Chain Gas With AI Cache Savings
Text Classification ~50,000 gas ~25,000 gas 50%
Price Prediction ~75,000 gas ~30,000 gas 60%
Image Analysis ~100,000 gas ~40,000 gas 60%
Multi-Agent Decision ~200,000 gas ~80,000 gas 60%

🔒 Security Considerations

1. AI Model Trust

// Always verify AI outputs
service SecureAI {
    fn execute_ai_decision(decision: string) {
        // Parse AI decision
        let action = parse_action(decision);
        
        // Validate action is reasonable
        if !is_valid_action(action) {
            log::error("ai", "Invalid AI decision");
            return;
        }
        if !is_within_risk_limits(action) {
            log::error("ai", "Action exceeds risk limits");
            return;
        }
        
        // Execute with additional safety checks
        execute_with_safety(action);
    }
    
    fn parse_action(decision: string) -> string {
        return decision; // Simplified
    }
    
    fn is_valid_action(action: string) -> bool {
        return action != "";
    }
    
    fn is_within_risk_limits(action: string) -> bool {
        return true; // Simplified
    }
    
    fn execute_with_safety(action: string) {
        log::info("ai", "Executing: " + action);
    }
}

2. Oracle + AI Validation

// Combine oracle data with AI validation
service SecureOracle {
    current_price: int = 0;
    
    fn update_price_secure() {
        // Get oracle price
        let oracle_result = oracle::fetch("chainlink", "ETH/USD");
        
        if oracle_result.is_ok() {
            let oracle_data = oracle_result.unwrap();
            let oracle_price = oracle_data.data;
            
            // AI sanity check
            let validation = ai::classify("price_validator", oracle_price.to_string());
            
            if validation != "valid" {
                log::error("oracle", "AI detected suspicious price");
                return;
            }
            
            // Additional checks
            if oracle_price <= 0 {
                log::error("oracle", "Invalid price");
                return;
            }
            
            current_price = oracle_price;
            log::info("oracle", "Price updated: " + current_price.to_string());
        }
    }
}

3. Rate Limiting AI Calls

service RateLimitedAI {
    last_ai_call: map<string, int> = {};
    ai_cooldown: int = 60; // 60 seconds
    
    fn make_ai_decision(user_id: string, input: string) {
        let current_time = time::now();
        let last_call = last_ai_call.get(user_id, 0);
        
        if current_time - last_call < ai_cooldown {
            log::error("ai", "AI call rate limit exceeded");
            return;
        }
        
        last_ai_call[user_id] = current_time;
        
        // AI operations
        let result = ai::classify("model", input);
        if result.is_ok() {
            process_result(result.unwrap());
        }
    }
    
    fn process_result(result: string) {
        log::info("ai", "Result: " + result);
    }
}

🌟 Real-World Use Cases

1. AI-Powered Trading Bot

2. Intelligent DAO Governance

3. Dynamic NFT Marketplace

4. AI Credit Scoring for DeFi

5. Conversational DeFi


📝 Important Notes

Current Implementation Status

The AI module (src/stdlib/ai.rs) currently provides:

Fully Implemented:

Configuration

AI providers can be configured via environment variables or runtime configuration:

# OpenAI
export OPENAI_API_KEY=your_key_here
export OPENAI_BASE_URL=https://api.openai.com/v1  # Optional

# Anthropic
export ANTHROPIC_API_KEY=your_key_here

# Custom providers
export AI_PROVIDER=custom
export AI_ENDPOINT=https://your-endpoint.com
export AI_API_KEY=your_key_here

Or programmatically:

// Configure OpenAI
ai::configure_openai("your-api-key", Some("gpt-4"));

// Configure Anthropic
ai::configure_anthropic("your-api-key", Some("claude-3"));

// Configure custom provider
ai::configure_custom("provider-name", "https://endpoint.com", "api-key", Some("model"));

How to Use

When building with the AI framework:

  1. Configure AI provider (environment variables or ai::configure_* functions)
  2. Use simplified API for quick operations (ai::classify, ai::generate, ai::embed)
  3. For complex agent systems, use the full agent framework (spawn_agent, create_task, etc.)
  4. Use built-in analysis functions (analyze_text, analyze_image, generate_text)

For multi-agent systems:

  1. Create coordinator with ai::create_coordinator()
  2. Spawn agents with ai::spawn_agent()
  3. Create tasks with ai::create_task()
  4. Execute tasks with ai::execute_task()
  5. Define workflows with ai::create_workflow()
  6. Execute workflows with ai::execute_workflow()

📚 Next Steps

  1. AI Best Practices - Security and optimization
  2. Standard Library Reference - Complete AI API docs
  3. Full AI Implementation - See the actual code
  4. CLI Commands - Command-line AI tools

Ready to build intelligent contracts? Start with AI Best Practices →