大模型应用发展与Agent前沿技术趋势(下)

Agent技术的行业应用与实践案例

金融领域的Agent应用

金融行业是大模型Agent技术应用最为广泛的领域之一,涵盖了风险评估、投资决策、客户服务等多个方面。在金融风控领域,Agent系统通过结合大模型的语义理解能力和强化学习的决策优化能力,能够实现更精准的风险评估和欺诈检测。

以下是一个基于大模型的金融风控Agent实现,展示了如何结合多源数据进行风险评估:

python 复制代码
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from typing import Dict, List, Tuple, Any, Optional
import time

class FinancialDataProcessor:
    """金融数据处理器"""
  
    def __init__(self):
        self.numeric_scaler = StandardScaler()
        self.categorical_encoders = {}
        self.fitted = False
  
    def fit(self, data: pd.DataFrame):
        """拟合数据处理器"""
        # 处理数值特征
        numeric_cols = ['transaction_amount', 'account_balance', 'transaction_frequency', 'avg_transaction']
        self.numeric_scaler.fit(data[numeric_cols])
      
        # 处理分类特征
        categorical_cols = ['transaction_type', 'merchant_category', 'device_type', 'location']
        for col in categorical_cols:
            unique_values = data[col].unique()
            self.categorical_encoders[col] = {val: i for i, val in enumerate(unique_values)}
      
        self.fitted = True
  
    def transform(self, data: pd.DataFrame) -> Dict[str, torch.Tensor]:
        """转换数据为模型输入"""
        if not self.fitted:
            raise ValueError("Processor not fitted. Call fit() first.")
      
        # 处理数值特征
        numeric_cols = ['transaction_amount', 'account_balance', 'transaction_frequency', 'avg_transaction']
        numeric_data = self.numeric_scaler.transform(data[numeric_cols])
      
        # 处理分类特征
        categorical_cols = ['transaction_type', 'merchant_category', 'device_type', 'location']
        categorical_data = {}
        for col in categorical_cols:
            encoded = data[col].map(self.categorical_encoders[col]).fillna(-1).astype(int)
            categorical_data[col] = encoded.values
      
        # 处理时间特征
        time_data = self._process_time_features(data['transaction_time'])
      
        # 转换为张量
        numeric_tensor = torch.FloatTensor(numeric_data)
        categorical_tensors = {col: torch.LongTensor(vals) for col, vals in categorical_data.items()}
        time_tensor = torch.FloatTensor(time_data)
      
        return {
            'numeric': numeric_tensor,
            'categorical': categorical_tensors,
            'time': time_tensor
        }
  
    def _process_time_features(self, time_series) -> np.ndarray:
        """处理时间特征"""
        # 提取小时、星期几等特征
        hours = time_series.dt.hour
        day_of_week = time_series.dt.dayofweek
        is_weekend = (day_of_week >= 5).astype(int)
      
        # 组合成特征矩阵
        features = np.vstack([
            hours,
            day_of_week,
            is_weekend
        ]).T
      
        return features

class TransactionEncoder(nn.Module):
    """交易编码器"""
  
    def __init__(self, 
                 numeric_dim: int,
                 categorical_dims: Dict[str, int],
                 time_dim: int,
                 embedding_dim: int = 32,
                 hidden_dim: int = 64):
        super(TransactionEncoder, self).__init__()
      
        # 数值特征处理
        self.numeric_processor = nn.Sequential(
            nn.Linear(numeric_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, embedding_dim)
        )
      
        # 分类特征嵌入
        self.categorical_embeddings = nn.ModuleDict()
        for col, num_categories in categorical_dims.items():
            self.categorical_embeddings[col] = nn.Embedding(num_categories + 1, embedding_dim)
      
        # 时间特征处理
        self.time_processor = nn.Sequential(
            nn.Linear(time_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, embedding_dim)
        )
      
        # 特征融合
        self.fusion = nn.Sequential(
            nn.Linear(embedding_dim * (2 + len(categorical_dims)), hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, embedding_dim)
        )
  
    def forward(self, inputs: Dict[str, Any]) -> torch.Tensor:
        """前向传播"""
        # 处理数值特征
        numeric_features = self.numeric_processor(inputs['numeric'])
      
        # 处理分类特征
        categorical_features = []
        for col, tensor in inputs['categorical'].items():
            embedding = self.categorical_embeddings[col](tensor)
            categorical_features.append(embedding)
      
        # 处理时间特征
        time_features = self.time_processor(inputs['time'])
      
        # 融合所有特征
        all_features = [numeric_features, time_features] + categorical_features
        combined = torch.cat(all_features, dim=-1)
        transaction_embedding = self.fusion(combined)
      
        return transaction_embedding

class UserBehaviorModel(nn.Module):
    """用户行为模型"""
  
    def __init__(self, 
                 transaction_dim: int,
                 lstm_hidden_dim: int = 64,
                 num_layers: int = 1):
        super(UserBehaviorModel, self).__init__()
      
        # LSTM用于建模用户行为序列
        self.lstm = nn.LSTM(
            input_size=transaction_dim,
            hidden_size=lstm_hidden_dim,
            num_layers=num_layers,
            batch_first=True,
            bidirectional=True
        )
      
        # 用户表示生成
        self.user_representation = nn.Sequential(
            nn.Linear(lstm_hidden_dim * 2, 64),
            nn.ReLU(),
            nn.Linear(64, 32)
        )
  
    def forward(self, transaction_sequence: torch.Tensor) -> torch.Tensor:
        """生成用户行为表示"""
        # LSTM处理序列
        lstm_out, _ = self.lstm(transaction_sequence)
      
        # 取最后一个时间步的输出
        last_output = lstm_out[:, -1, :]
      
        # 生成用户表示
        user_representation = self.user_representation(last_output)
      
        return user_representation

class RiskAssessmentAgent:
    """风险评估Agent"""
  
    def __init__(self,
                 transaction_encoder: nn.Module,
                 user_behavior_model: nn.Module,
                 risk_predictor: nn.Module,
                 data_processor: FinancialDataProcessor):
        self.transaction_encoder = transaction_encoder
        self.user_behavior_model = user_behavior_model
        self.risk_predictor = risk_predictor
        self.data_processor = data_processor
        self.user_transaction_history = {}
        self.risk_threshold = 0.7
  
    def update_user_history(self, user_id: str, transactions: pd.DataFrame):
        """更新用户交易历史"""
        if user_id not in self.user_transaction_history:
            self.user_transaction_history[user_id] = []
      
        # 处理新交易
        for _, transaction in transactions.iterrows():
            processed = self.data_processor.transform(pd.DataFrame([transaction]))
            self.user_transaction_history[user_id].append(processed)
          
            # 限制历史长度
            if len(self.user_transaction_history[user_id]) > 100:
                self.user_transaction_history[user_id].pop(0)
  
    def assess_risk(self, user_id: str, new_transaction: pd.DataFrame) -> Dict[str, Any]:
        """评估新交易的风险"""
        # 处理新交易
        new_transaction_data = self.data_processor.transform(new_transaction)
      
        # 获取用户历史交易序列
        if user_id in self.user_transaction_history and self.user_transaction_history[user_id]:
            history_sequence = torch.stack([
                self.transaction_encoder(hist_data) 
                for hist_data in self.user_transaction_history[user_id]
            ], dim=1)  # (batch_size, seq_len, feature_dim)
        else:
            # 新用户,使用默认历史
            history_sequence = torch.zeros(1, 10, self.transaction_encoder.embedding_dim)
      
        # 生成用户行为表示
        user_representation = self.user_behavior_model(history_sequence)
      
        # 编码新交易
        new_transaction_embedding = self.transaction_encoder(new_transaction_data)
      
        # 风险评估
        with torch.no_grad():
            risk_score = self.risk_predictor(
                torch.cat([user_representation, new_transaction_embedding], dim=-1)
            ).item()
      
        # 生成解释
        explanation = self._generate_explanation(risk_score, new_transaction)
      
        # 决策
        is_suspicious = risk_score > self.risk_threshold
      
        # 记录评估结果
        assessment = {
            "risk_score": risk_score,
            "is_suspicious": is_suspicious,
            "explanation": explanation,
            "timestamp": time.time()
        }
      
        return assessment
  
    def _generate_explanation(self, risk_score: float, transaction: pd.DataFrame) -> str:
        """生成风险评估解释"""
        if risk_score > 0.9:
            return "High risk: Unusual transaction amount and location compared to user history."
        elif risk_score > 0.7:
            return "Medium risk: Slightly unusual transaction pattern detected."
        elif risk_score > 0.5:
            return "Low risk: Minor deviation from normal behavior."
        else:
            return "Normal transaction: Consistent with user's historical behavior."

class RiskPredictor(nn.Module):
    """风险预测器"""
  
    def __init__(self, input_dim: int):
        super(RiskPredictor, self).__init__()
        self.network = nn.Sequential(
            nn.Linear(input_dim, 32),
            nn.ReLU(),
            nn.Linear(32, 16),
            nn.ReLU(),
            nn.Linear(16, 1),
            nn.Sigmoid()
        )
  
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.network(x)

# 示例使用金融风控Agent
if __name__ == "__main__":
    # 创建模拟数据处理器
    processor = FinancialDataProcessor()
  
    # 模拟训练数据(简化)
    train_data = pd.DataFrame({
        'transaction_amount': np.random.lognormal(mean=5, sigma=1, size=1000),
        'account_balance': np.random.lognormal(mean=8, sigma=1, size=1000),
        'transaction_frequency': np.random.poisson(lam=5, size=1000),
        'avg_transaction': np.random.lognormal(mean=4, sigma=0.5, size=1000),
        'transaction_type': np.random.choice(['purchase', 'transfer', 'withdrawal'], size=1000),
        'merchant_category': np.random.choice(['retail', 'food', 'travel', 'online'], size=1000),
        'device_type': np.random.choice(['mobile', 'desktop', 'tablet'], size=1000),
        'location': np.random.choice(['home_city', 'nearby_city', 'foreign_country'], size=1000),
        'transaction_time': pd.date_range(start='2023-01-01', periods=1000, freq='H'),
        'is_fraud': np.random.choice([0, 1], size=1000, p=[0.95, 0.05])
    })
  
    # 拟合数据处理器
    processor.fit(train_data)
  
    # 创建模型组件
    transaction_encoder = TransactionEncoder(
        numeric_dim=4,
        categorical_dims={
            'transaction_type': 3,
            'merchant_category': 4,
            'device_type': 3,
            'location': 3
        },
        time_dim=3
    )
  
    user_behavior_model = UserBehaviorModel(
        transaction_dim=transaction_encoder.embedding_dim
    )
  
    risk_predictor = RiskPredictor(
        input_dim=32 + transaction_encoder.embedding_dim  # user_repr + transaction
    )
  
    # 创建风险评估Agent
    agent = RiskAssessmentAgent(
        transaction_encoder,
        user_behavior_model,
        risk_predictor,
        processor
    )
  
    # 模拟用户交易历史
    user_id = "user_123"
    user_history = train_data[train_data.index < 50].copy()
    user_history['is_fraud'] = 0  # 正常历史
    agent.update_user_history(user_id, user_history)
  
    # 评估新交易
    print("Assessing normal transaction...")
    normal_transaction = pd.DataFrame([{
        'transaction_amount': 200,
        'account_balance': 5000,
        'transaction_frequency': 4,
        'avg_transaction': 150,
        'transaction_type': 'purchase',
        'merchant_category': 'retail',
        'device_type': 'mobile',
        'location': 'home_city',
        'transaction_time': pd.Timestamp.now()
    }])
    normal_assessment = agent.assess_risk(user_id, normal_transaction)
    print(f"Risk score: {normal_assessment['risk_score']:.4f}")
    print(f"Assessment: {'Suspicious' if normal_assessment['is_suspicious'] else 'Normal'}")
    print(f"Explanation: {normal_assessment['explanation']}\n")
  
    # 评估可疑交易
    print("Assessing suspicious transaction...")
    suspicious_transaction = pd.DataFrame([{
        'transaction_amount': 50000,
        'account_balance': 5000,
        'transaction_frequency': 4,
        'avg_transaction': 150,
        'transaction_type': 'transfer',
        'merchant_category': 'online',
        'device_type': 'desktop',
        'location': 'foreign_country',
        'transaction_time': pd.Timestamp.now()
    }])
    suspicious_assessment = agent.assess_risk(user_id, suspicious_transaction)
    print(f"Risk score: {suspicious_assessment['risk_score']:.4f}")
    print(f"Assessment: {'Suspicious' if suspicious_assessment['is_suspicious'] else 'Normal'}")
    print(f"Explanation: {suspicious_assessment['explanation']}")

在金融投资领域,Agent系统可以结合大模型的市场分析能力和强化学习的投资策略优化能力,实现智能投顾和量化交易。以下是一个基于大模型的投资决策Agent实现:

python 复制代码
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import yfinance as yf
from datetime import datetime, timedelta
import time
from typing import Dict, List, Tuple, Any, Optional

class MarketDataProcessor:
    """市场数据处理器"""
  
    def __init__(self):
        self.scalers = {}
        self.fitted = False
  
    def fit(self, data: Dict[str, pd.DataFrame]):
        """拟合数据处理器"""
        for symbol, df in data.items():
            # 为每个股票创建标准化器
            self.scalers[symbol] = {}
          
            # 数值特征
            numeric_cols = ['Open', 'High', 'Low', 'Close', 'Volume', 'MA5', 'MA20', 'RSI', 'MACD']
            self.scalers[symbol]['numeric'] = StandardScaler()
            self.scalers[symbol]['numeric'].fit(df[numeric_cols])
      
        self.fitted = True
  
    def transform(self, symbol: str, data: pd.DataFrame) -> torch.Tensor:
        """转换市场数据为模型输入"""
        if not self.fitted:
            raise ValueError("Processor not fitted. Call fit() first.")
      
        # 数值特征
        numeric_cols = ['Open', 'High', 'Low', 'Close', 'Volume', 'MA5', 'MA20', 'RSI', 'MACD']
        numeric_data = self.scalers[symbol]['numeric'].transform(data[numeric_cols])
      
        # 转换为张量
        return torch.FloatTensor(numeric_data)

class MarketTrendEncoder(nn.Module):
    """市场趋势编码器"""
  
    def __init__(self, input_dim: int, hidden_dim: int = 64, num_layers: int = 2):
        super(MarketTrendEncoder, self).__init__()
      
        # LSTM用于编码市场趋势
        self.lstm = nn.LSTM(
            input_size=input_dim,
            hidden_size=hidden_dim,
            num_layers=num_layers,
            batch_first=True,
            bidirectional=True
        )
      
        # 趋势表示
        self.trend_representation = nn.Sequential(
            nn.Linear(hidden_dim * 2, 64),
            nn.ReLU(),
            nn.Linear(64, 32)
        )
  
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """编码市场趋势"""
        # LSTM处理
        lstm_out, _ = self.lstm(x)
      
        # 取最后一个时间步
        last_output = lstm_out[:, -1, :]
      
        # 生成趋势表示
        trend_repr = self.trend_representation(last_output)
      
        return trend_repr

class NewsSentimentAnalyzer:
    """新闻情感分析器(模拟)"""
  
    def __init__(self):
        # 实际应用中应使用预训练的情感分析模型
        pass
  
    def analyze(self, news_articles: List[Dict[str, str]]) -> float:
        """分析新闻情感"""
        # 简化实现:随机生成情感分数
        if not news_articles:
            return 0.0
      
        # 模拟情感分析
        return np.random.uniform(-1.0, 1.0)

class InvestmentDecisionAgent:
    """投资决策Agent"""
  
    def __init__(self,
                 trend_encoder: nn.Module,
                 data_processor: MarketDataProcessor,
                 news_analyzer: NewsSentimentAnalyzer,
                 portfolio_manager: 'PortfolioManager',
                 risk_tolerance: float = 0.5):
        self.trend_encoder = trend_encoder
        self.data_processor = data_processor
        self.news_analyzer = news_analyzer
        self.portfolio_manager = portfolio_manager
        self.risk_tolerance = risk_tolerance
        self.action_space = ['buy', 'sell', 'hold']
        self.decision_history = []
  
    def make_decision(self, 
                     symbol: str,
                     market_data: pd.DataFrame,
                     news_articles: List[Dict[str, str]]) -> Dict[str, Any]:
        """做出投资决策"""
        # 处理市场数据
        processed_data = self.data_processor.transform(symbol, market_data)
      
        # 编码市场趋势
        with torch.no_grad():
            trend_repr = self.trend_encoder(processed_data.unsqueeze(0))
      
        # 分析新闻情感
        sentiment_score = self.news_analyzer.analyze(news_articles)
      
        # 获取当前持仓
        current_position = self.portfolio_manager.get_position(symbol)
      
        # 决策逻辑
        decision, confidence = self._generate_decision(
            trend_repr, 
            sentiment_score, 
            current_position
        )
      
        # 生成解释
        explanation = self._generate_explanation(
            symbol, 
            decision, 
            confidence, 
            sentiment_score
        )
      
        # 记录决策
        decision_record = {
            "symbol": symbol,
            "decision": decision,
            "confidence": confidence,
            "sentiment": sentiment_score,
            "timestamp": time.time(),
            "explanation": explanation
        }
        self.decision_history.append(decision_record)
      
        return decision_record
  
    def _generate_decision(self, 
                          trend_repr: torch.Tensor,
                          sentiment_score: float,
                          current_position: float) -> Tuple[str, float]:
        """生成投资决策"""
        # 简化决策逻辑
        trend_score = trend_repr[0, 0].item()  # 使用第一个维度作为趋势指标
      
        # 综合趋势和新闻
        combined_score = 0.7 * trend_score + 0.3 * sentiment_score
      
        # 根据风险偏好调整
        if self.risk_tolerance < 0.3:
            # 保守型:降低买卖意愿
            combined_score *= 0.7
        elif self.risk_tolerance > 0.7:
            # 激进型:增强买卖意愿
            combined_score *= 1.3
      
        # 决策阈值
        buy_threshold = 0.3
        sell_threshold = -0.2
      
        # 生成决策
        if combined_score > buy_threshold and current_position == 0:
            return 'buy', min(0.5 + combined_score, 1.0)
        elif combined_score < sell_threshold and current_position > 0:
            return 'sell', min(0.5 - combined_score, 1.0)
        else:
            return 'hold', 1.0 - abs(combined_score)
  
    def _generate_explanation(self, 
                             symbol: str,
                             decision: str,
                             confidence: float,
                             sentiment_score: float) -> str:
        """生成决策解释"""
        if decision == 'buy':
            return f"Recommend buying {symbol}. Market trend and news sentiment indicate potential upside (confidence: {confidence:.2f})."
        elif decision == 'sell':
            return f"Recommend selling {symbol}. Market trend and news sentiment suggest potential downside (confidence: {confidence:.2f})."
        else:
            return f"Recommend holding {symbol}. Market signals are inconclusive (confidence: {confidence:.2f})."

class PortfolioManager:
    """投资组合管理器"""
  
    def __init__(self, initial_cash: float = 100000.0):
        self.cash = initial_cash
        self.positions = {}  # symbol: quantity
        self.trade_history = []
        self.initial_value = initial_cash
  
    def get_position(self, symbol: str) -> float:
        """获取持仓"""
        return self.positions.get(symbol, 0.0)
  
    def execute_trade(self, symbol: str, decision: str, price: float, quantity: Optional[int] = None):
        """执行交易"""
        current_position = self.get_position(symbol)
      
        if decision == 'buy':
            # 计算可购买数量(使用50%现金)
            if quantity is None:
                quantity = int((self.cash * 0.5) // price)
          
            # 检查是否有足够现金
            cost = quantity * price
            if cost > self.cash:
                quantity = self.cash // price
                cost = quantity * price
          
            if quantity > 0:
                # 更新持仓
                self.positions[symbol] = self.positions.get(symbol, 0) + quantity
                self.cash -= cost
              
                # 记录交易
                self._record_trade(symbol, 'buy', quantity, price)
      
        elif decision == 'sell' and current_position > 0:
            # 计算可卖出数量(全部持仓)
            if quantity is None:
                quantity = current_position
          
            # 更新持仓
            self.positions[symbol] = max(0, self.positions.get(symbol, 0) - quantity)
            self.cash += quantity * price
          
            # 记录交易
            self._record_trade(symbol, 'sell', quantity, price)
  
    def _record_trade(self, symbol: str, action: str, quantity: int, price: float):
        """记录交易"""
        self.trade_history.append({
            "symbol": symbol,
            "action": action,
            "quantity": quantity,
            "price": price,
            "timestamp": time.time(),
            "value": quantity * price
        })
  
    def get_portfolio_value(self, current_prices: Dict[str, float]) -> float:
        """获取投资组合当前价值"""
        value = self.cash
        for symbol, quantity in self.positions.items():
            if symbol in current_prices:
                value += quantity * current_prices[symbol]
        return value
  
    def get_performance(self, current_prices: Dict[str, float]) -> float:
        """获取投资组合表现"""
        current_value = self.get_portfolio_value(current_prices)
        return (current_value - self.initial_value) / self.initial_value

# 示例使用投资决策Agent
if __name__ == "__main__":
    # 模拟获取市场数据
    def fetch_market_data(symbol: str, days: int = 30) -> pd.DataFrame:
        """获取市场数据(模拟)"""
        end_date = datetime.now()
        start_date = end_date - timedelta(days=days)
      
        # 实际应用中应使用真实数据源
        dates = pd.date_range(start=start_date, end=end_date, freq='D')
        prices = np.cumprod(1 + np.random.normal(0, 0.02, len(dates)))
        volumes = np.random.lognormal(mean=10, sigma=1, size=len(dates))
      
        df = pd.DataFrame({
            'Date': dates,
            'Open': prices * 0.99,
            'High': prices * 1.02,
            'Low': prices * 0.98,
            'Close': prices,
            'Volume': volumes
        })
      
        # 添加技术指标
        df['MA5'] = df['Close'].rolling(window=5).mean()
        df['MA20'] = df['Close'].rolling(window=20).mean()
        df['RSI'] = 50 + np.random.randn(len(df)) * 10  # 简化RSI
        df['MACD'] = np.random.randn(len(df))  # 简化MACD
      
        return df
  
    # 模拟获取新闻
    def fetch_news(symbol: str, days: int = 7) -> List[Dict[str, str]]:
        """获取相关新闻(模拟)"""
        return [
            {"title": f"News about {symbol}", "content": "Positive news content..."},
            {"title": f"Another article on {symbol}", "content": "Neutral news content..."}
        ]
  
    # 创建组件
    trend_encoder = MarketTrendEncoder(input_dim=9)  # 9个特征
    data_processor = MarketDataProcessor()
    news_analyzer = NewsSentimentAnalyzer()
    portfolio_manager = PortfolioManager(initial_cash=100000.0)
  
    # 准备训练数据(简化)
    symbols = ['AAPL', 'MSFT', 'GOOG']
    train_data = {}
    for symbol in symbols:
        train_data[symbol] = fetch_market_data(symbol, days=90)
    data_processor.fit(train_data)
  
    # 创建投资决策Agent
    agent = InvestmentDecisionAgent(
        trend_encoder=trend_encoder,
        data_processor=data_processor,
        news_analyzer=news_analyzer,
        portfolio_manager=portfolio_manager,
        risk_tolerance=0.6
    )
  
    # 模拟交易日
    print("=== Investment Decision Simulation ===")
    for day in range(5):
        print(f"\nDay {day+1}:")
      
        for symbol in symbols:
            # 获取最新市场数据
            market_data = fetch_market_data(symbol, days=30)
          
            # 获取相关新闻
            news = fetch_news(symbol)
          
            # 做出决策
            decision = agent.make_decision(symbol, market_data, news)
            print(f"{symbol} decision: {decision['decision'].upper()} (confidence: {decision['confidence']:.2f})")
            print(f"  Explanation: {decision['explanation']}")
          
            # 执行交易(简化)
            if decision['decision'] != 'hold':
                # 模拟当前价格
                current_price = market_data['Close'].iloc[-1]
              
                # 执行交易
                portfolio_manager.execute_trade(
                    symbol=symbol,
                    decision=decision['decision'],
                    price=current_price
                )
      
        # 评估投资组合
        current_prices = {symbol: fetch_market_data(symbol)['Close'].iloc[-1] for symbol in symbols}
        portfolio_value = portfolio_manager.get_portfolio_value(current_prices)
        performance = portfolio_manager.get_performance(current_prices)
      
        print(f"\nPortfolio value: ${portfolio_value:,.2f}")
        print(f"Performance: {performance:.2%}")
      
        # 暂停模拟
        time.sleep(1)
  
    # 显示交易历史
    print("\nTrade History:")
    for i, trade in enumerate(portfolio_manager.trade_history):
        print(f"{i+1}. {trade['action'].upper()} {trade['quantity']} {trade['symbol']} @ ${trade['price']:.2f}")

# 高级投资Agent:结合强化学习的优化
class RLInvestmentAgent(InvestmentDecisionAgent):
    """基于强化学习的投资Agent"""
  
    def __init__(self,
                 trend_encoder: nn.Module,
                 data_processor: MarketDataProcessor,
                 news_analyzer: NewsSentimentAnalyzer,
                 portfolio_manager: PortfolioManager,
                 risk_tolerance: float = 0.5,
                 lr: float = 0.001,
                 gamma: float = 0.99):
        super().__init__(
            trend_encoder, 
            data_processor, 
            news_analyzer, 
            portfolio_manager,
            risk_tolerance
        )
      
        # DQN组件
        self.state_dim = 32 + 1  # trend_repr + sentiment
        self.action_dim = len(self.action_space)
      
        self.policy_net = nn.Sequential(
            nn.Linear(self.state_dim, 64),
            nn.ReLU(),
            nn.Linear(64, 64),
            nn.ReLU(),
            nn.Linear(64, self.action_dim)
        )
      
        self.target_net = nn.Sequential(
            nn.Linear(self.state_dim, 64),
            nn.ReLU(),
            nn.Linear(64, 64),
            nn.ReLU(),
            nn.Linear(64, self.action_dim)
        )
        self.target_net.load_state_dict(self.policy_net.state_dict())
        self.target_net.eval()
      
        self.optimizer = torch.optim.Adam(self.policy_net.parameters(), lr=lr)
        self.gamma = gamma
        self.memory = []
        self.batch_size = 32
        self.epsilon = 1.0
        self.epsilon_min = 0.1
        self.epsilon_decay = 0.995
  
    def get_state(self, trend_repr: torch.Tensor, sentiment_score: float) -> torch.Tensor:
        """获取状态表示"""
        # 合并趋势表示和情感分数
        state = torch.cat([
            trend_repr.squeeze(0),
            torch.tensor([sentiment_score])
        ])
        return state
  
    def select_action(self, state: torch.Tensor) -> str:
        """选择动作"""
        if np.random.rand() < self.epsilon:
            # 探索:随机选择
            return np.random.choice(self.action_space)
        else:
            # 利用:选择Q值最高的动作
            with torch.no_grad():
                q_values = self.policy_net(state)
                action_idx = q_values.argmax().item()
                return self.action_space[action_idx]
  
    def store_experience(self, 
                        state: torch.Tensor, 
                        action: str, 
                        reward: float, 
                        next_state: torch.Tensor, 
                        done: bool):
        """存储经验"""
        action_idx = self.action_space.index(action)
        self.memory.append((state, action_idx, reward, next_state, done))
      
        # 限制记忆大小
        if len(self.memory) > 10000:
            self.memory.pop(0)
  
    def train(self):
        """训练DQN"""
        if len(self.memory) < self.batch_size:
            return
      
        # 采样批次
        batch = random.sample(self.memory, self.batch_size)
        states, actions, rewards, next_states, dones = zip(*batch)
      
        # 转换为张量
        states = torch.stack(states)
        actions = torch.LongTensor(actions)
        rewards = torch.FloatTensor(rewards)
        next_states = torch.stack(next_states)
        dones = torch.FloatTensor(dones)
      
        # 计算当前Q值
        current_q = self.policy_net(states).gather(1, actions.unsqueeze(1))
      
        # 计算目标Q值
        with torch.no_grad():
            next_q = self.target_net(next_states).max(1)[0]
            target_q = rewards + self.gamma * next_q * (1 - dones)
      
        # 计算损失
        loss = nn.MSELoss()(current_q.squeeze(), target_q)
      
        # 优化模型
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()
      
        # 更新epsilon
        self.epsilon = max(self.epsilon_min, self.epsilon * self.epsilon_decay)
  
    def make_decision(self, 
                     symbol: str,
                     market_data: pd.DataFrame,
                     news_articles: List[Dict[str, str]]) -> Dict[str, Any]:
        """强化学习增强的决策"""
        # 处理市场数据
        processed_data = self.data_processor.transform(symbol, market_data)
      
        # 编码市场趋势
        with torch.no_grad():
            trend_repr = self.trend_encoder(processed_data.unsqueeze(0))
      
        # 分析新闻情感
        sentiment_score = self.news_analyzer.analyze(news_articles)
      
        # 获取当前持仓
        current_position = self.portfolio_manager.get_position(symbol)
      
        # 获取状态
        state = self.get_state(trend_repr, sentiment_score)
      
        # 选择动作
        action = self.select_action(state)
      
        # 生成解释
        explanation = self._generate_rl_explanation(
            symbol, 
            action, 
            sentiment_score
        )
      
        # 记录决策(用于训练)
        decision_record = {
            "symbol": symbol,
            "decision": action,
            "state": state,
            "timestamp": time.time(),
            "explanation": explanation,
            "current_position": current_position
        }
        self.decision_history.append(decision_record)
      
        return decision_record
  
    def _generate_rl_explanation(self, 
                                symbol: str,
                                decision: str,
                                sentiment_score: float) -> str:
        """生成强化学习决策解释"""
        base_explanation = super()._generate_explanation(symbol, decision, 0.0, sentiment_score)
      
        # 添加强化学习特定信息
        if self.epsilon > 0.5:
            return f"[Exploration] {base_explanation}"
        else:
            return f"[Exploitation] {base_explanation}"
  
    def update_policy(self, 
                     symbol: str,
                     reward: float,
                     done: bool = False):
        """更新策略"""
        if len(self.decision_history) < 2:
            return
      
        # 获取最近两次决策
        current_decision = self.decision_history[-1]
        previous_decision = self.decision_history[-2]
      
        # 存储经验
        self.store_experience(
            previous_decision["state"],
            previous_decision["decision"],
            reward,
            current_decision["state"],
            done
        )
      
        # 训练
        self.train()

医疗健康领域的Agent应用

在医疗健康领域,大模型Agent可以辅助医生进行诊断、制定治疗方案、管理患者健康等任务。以下是一个医疗诊断Agent的实现,展示了如何结合医学知识库和患者数据进行智能诊断:

python 复制代码
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from typing import Dict, List, Tuple, Any, Optional
import re
import json
import time

class MedicalKnowledgeBase:
    """医学知识库"""
  
    def __init__(self, conditions_data: List[Dict]):
        """
        初始化医学知识库
      
        Args:
            conditions_data: 疾病数据列表,每个元素包含疾病名称、症状、治疗方法等信息
        """
        self.conditions = conditions_data
        self.symptom_index = self._build_symptom_index()
        self.vectorizer = TfidfVectorizer()
        self.condition_vectors = self._vectorize_conditions()
  
    def _build_symptom_index(self) -> Dict[str, List[int]]:
        """构建症状索引"""
        symptom_index = {}
        for idx, condition in enumerate(self.conditions):
            for symptom in condition['symptoms']:
                # 标准化症状名称
                normalized = self._normalize_symptom(symptom)
                if normalized not in symptom_index:
                    symptom_index[normalized] = []
                symptom_index[normalized].append(idx)
        return symptom_index
  
    def _normalize_symptom(self, symptom: str) -> str:
        """标准化症状名称"""
        # 转换为小写
        symptom = symptom.lower()
        # 移除标点
        symptom = re.sub(r'[^\w\s]', '', symptom)
        # 移除多余空格
        symptom = re.sub(r'\s+', ' ', symptom).strip()
        return symptom
  
    def _vectorize_conditions(self) -> np.ndarray:
        """向量化疾病描述"""
        descriptions = [
            f"{cond['name']} {cond['description']} {' '.join(cond['symptoms'])}"
            for cond in self.conditions
        ]
        return self.vectorizer.fit_transform(descriptions).toarray()
  
    def search_by_symptoms(self, symptoms: List[str], top_k: int = 5) -> List[Dict]:
        """根据症状搜索可能的疾病"""
        # 标准化输入症状
        normalized_symptoms = [self._normalize_symptom(s) for s in symptoms]
      
        # 收集匹配的疾病ID
        condition_ids = set()
        for symptom in normalized_symptoms:
            if symptom in self.symptom_index:
                condition_ids.update(self.symptom_index[symptom])
      
        # 如果没有匹配,使用向量相似度搜索
        if not condition_ids:
            return self._semantic_search(symptoms, top_k)
      
        # 获取匹配的疾病
        matches = [
            {
                "condition": self.conditions[idx],
                "match_count": self._count_matching_symptoms(idx, normalized_symptoms)
            }
            for idx in condition_ids
        ]
      
        # 按匹配症状数量排序
        matches.sort(key=lambda x: x["match_count"], reverse=True)
      
        # 返回top_k结果
        return [
            {
                "name": m["condition"]["name"],
                "probability": min(m["match_count"] / len(m["condition"]["symptoms"]), 1.0),
                "symptoms": m["condition"]["symptoms"],
                "treatment": m["condition"]["treatment"]
            }
            for m in matches[:top_k]
        ]
  
    def _count_matching_symptoms(self, condition_idx: int, symptoms: List[str]) -> int:
        """计算匹配的症状数量"""
        condition = self.conditions[condition_idx]
        normalized_condition_symptoms = [
            self._normalize_symptom(s) for s in condition["symptoms"]
        ]
        return sum(1 for s in symptoms if s in normalized_condition_symptoms)
  
    def _semantic_search(self, symptoms: List[str], top_k: int) -> List[Dict]:
        """语义搜索"""
        query = " ".join(symptoms)
        query_vec = self.vectorizer.transform([query]).toarray()
      
        # 计算余弦相似度
        similarities = cosine_similarity(query_vec, self.condition_vectors)[0]
      
        # 获取top_k结果
        top_indices = np.argsort(similarities)[::-1][:top_k]
      
        return [
            {
                "name": self.conditions[idx]["name"],
                "probability": float(similarities[idx]),
                "symptoms": self.conditions[idx]["symptoms"],
                "treatment": self.conditions[idx]["treatment"]
            }
            for idx in top_indices
        ]

class PatientProfile:
    """患者档案"""
  
    def __init__(self, 
                 patient_id: str,
                 age: int,
                 gender: str,
                 medical_history: List[str],
                 current_symptoms: List[str],
                 medications: List[str]):
        self.patient_id = patient_id
        self.age = age
        self.gender = gender
        self.medical_history = medical_history
        self.current_symptoms = current_symptoms
        self.medications = medications
        self.diagnosis_history = []
        self.treatment_plan = None
  
    def update_symptoms(self, new_symptoms: List[str]):
        """更新症状"""
        self.current_symptoms = list(set(self.current_symptoms + new_symptoms))
  
    def add_diagnosis(self, diagnosis: Dict):
        """添加诊断记录"""
        diagnosis["timestamp"] = time.time()
        self.diagnosis_history.append(diagnosis)
  
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            "patient_id": self.patient_id,
            "age": self.age,
            "gender": self.gender,
            "medical_history": self.medical_history,
            "current_symptoms": self.current_symptoms,
            "medications": self.medications,
            "diagnosis_history": self.diagnosis_history,
            "treatment_plan": self.treatment_plan
        }

class DiagnosisAgent:
    """诊断Agent"""
  
    def __init__(self, knowledge_base: MedicalKnowledgeBase):
        self.knowledge_base = knowledge_base
        self.diagnosis_rules = self._load_diagnosis_rules()
        self.confidence_threshold = 0.3
  
    def _load_diagnosis_rules(self) -> List[Dict]:
        """加载诊断规则"""
        # 实际应用中应从知识库加载
        return [
            {
                "name": "Fever with cough",
                "symptoms": ["fever", "cough"],
                "min_symptoms": 2,
                "conditions": ["Common Cold", "Influenza", "Pneumonia"]
            },
            {
                "name": "Chest pain",
                "symptoms": ["chest pain", "shortness of breath"],
                "min_symptoms": 1,
                "conditions": ["Heart Attack", "Angina", "Pneumonia"]
            }
        ]
  
    def diagnose(self, patient: PatientProfile) -> Dict[str, Any]:
        """诊断患者"""
        # 获取可能的疾病
        possible_conditions = self.knowledge_base.search_by_symptoms(
            patient.current_symptoms, 
            top_k=10
        )
      
        # 应用诊断规则
        rule_matches = self._apply_diagnosis_rules(patient.current_symptoms)
      
        # 合并结果
        final_diagnosis = self._combine_results(
            possible_conditions, 
            rule_matches,
            patient
        )
      
        # 排序并筛选
        final_diagnosis = [
            d for d in final_diagnosis 
            if d["probability"] >= self.confidence_threshold
        ]
        final_diagnosis.sort(key=lambda x: x["probability"], reverse=True)
      
        # 生成诊断报告
        report = {
            "patient_id": patient.patient_id,
            "diagnosis": final_diagnosis[:3],  # 只返回top 3
            "symptoms": patient.current_symptoms,
            "timestamp": time.time(),
            "confidence_threshold": self.confidence_threshold
        }
      
        # 更新患者档案
        patient.add_diagnosis(report)
      
        return report
  
    def _apply_diagnosis_rules(self, symptoms: List[str]) -> List[Dict]:
        """应用诊断规则"""
        matches = []
      
        for rule in self.diagnosis_rules:
            # 检查匹配的症状数量
            matched_symptoms = [
                s for s in symptoms 
                if any(rule_symptom in s or s in rule_symptom for rule_symptom in rule["symptoms"])
            ]
          
            if len(matched_symptoms) >= rule["min_symptoms"]:
                matches.append({
                    "rule": rule["name"],
                    "matched_symptoms": matched_symptoms,
                    "possible_conditions": rule["conditions"]
                })
      
        return matches
  
    def _combine_results(self, 
                        possible_conditions: List[Dict],
                        rule_matches: List[Dict],
                        patient: PatientProfile) -> List[Dict]:
        """合并诊断结果"""
        # 创建条件到概率的映射
        condition_probs = {}
      
        # 添加知识库搜索结果
        for cond in possible_conditions:
            condition_probs[cond["name"]] = cond["probability"]
      
        # 添加规则匹配结果
        for match in rule_matches:
            for cond in match["possible_conditions"]:
                if cond in condition_probs:
                    condition_probs[cond] += 0.2  # 规则匹配增加概率
                else:
                    condition_probs[cond] = 0.3
      
        # 考虑患者历史(简化)
        for cond, prob in condition_probs.items():
            if any(cond.lower() in history.lower() for history in patient.medical_history):
                condition_probs[cond] = min(prob * 1.5, 1.0)
      
        # 归一化概率
        max_prob = max(condition_probs.values()) if condition_probs else 1.0
        for cond in condition_probs:
            condition_probs[cond] /= max_prob
      
        # 转换为列表
        results = [
            {
                "name": cond,
                "probability": prob,
                "treatment": self._get_treatment(cond)
            }
            for cond, prob in condition_probs.items()
        ]
      
        return results
  
    def _get_treatment(self, condition_name: str) -> str:
        """获取治疗方法"""
        for cond in self.knowledge_base.conditions:
            if cond["name"].lower() == condition_name.lower():
                return cond["treatment"]
        return "Consult a healthcare professional for appropriate treatment."

class TreatmentPlanner:
    """治疗规划器"""
  
    def __init__(self, knowledge_base: MedicalKnowledgeBase):
        self.knowledge_base = knowledge_base
  
    def create_treatment_plan(self, 
                            patient: PatientProfile, 
                            diagnosis: List[Dict]) -> Dict[str, Any]:
        """创建治疗计划"""
        if not diagnosis:
            return {"error": "No diagnosis provided"}
      
        # 选择最可能的诊断
        primary_diagnosis = diagnosis[0]
      
        # 获取疾病信息
        condition_info = self._get_condition_info(primary_diagnosis["name"])
        if not condition_info:
            return {"error": "Condition not found in knowledge base"}
      
        # 创建治疗计划
        treatment_plan = {
            "diagnosis": primary_diagnosis["name"],
            "confidence": primary_diagnosis["probability"],
            "recommended_treatments": self._select_treatments(
                condition_info, 
                patient
            ),
            "lifestyle_recommendations": self._generate_lifestyle_recommendations(
                condition_info
            ),
            "follow_up": self._determine_follow_up(condition_info),
            "warnings": self._check_for_warnings(condition_info, patient),
            "timestamp": time.time()
        }
      
        # 更新患者档案
        patient.treatment_plan = treatment_plan
      
        return treatment_plan
  
    def _get_condition_info(self, condition_name: str) -> Optional[Dict]:
        """获取疾病信息"""
        for cond in self.knowledge_base.conditions:
            if cond["name"].lower() == condition_name.lower():
                return cond
        return None
  
    def _select_treatments(self, 
                          condition_info: Dict, 
                          patient: PatientProfile) -> List[Dict]:
        """选择治疗方法"""
        # 基本治疗方法
        treatments = [{
            "name": treatment["name"],
            "description": treatment["description"],
            "dosage": treatment.get("dosage", ""),
            "duration": treatment.get("duration", ""),
            "priority": treatment.get("priority", 1)
        } for treatment in condition_info["treatments"]]
      
        # 考虑患者因素调整治疗
        for treatment in treatments:
            # 检查药物冲突
            if "medications" in treatment and treatment["medications"]:
                for med in treatment["medications"]:
                    if med in patient.medications:
                        treatment["warnings"] = "Potential drug interaction detected"
          
            # 调整剂量基于年龄
            if patient.age > 65 and "elderly_adjustment" in treatment:
                treatment["dosage"] = treatment["elderly_adjustment"]
      
        # 按优先级排序
        treatments.sort(key=lambda x: x["priority"])
      
        return treatments
  
    def _generate_lifestyle_recommendations(self, condition_info: Dict) -> List[str]:
        """生成生活方式建议"""
        return condition_info.get("lifestyle_recommendations", [])
  
    def _determine_follow_up(self, condition_info: Dict) -> str:
        """确定随访计划"""
        severity = condition_info.get("severity", "moderate")
      
        if severity == "critical":
            return "Immediate follow-up required within 24 hours"
        elif severity == "severe":
            return "Follow-up within 3 days"
        else:
            return "Follow-up in 1-2 weeks"
  
    def _check_for_warnings(self, condition_info: Dict, patient: PatientProfile) -> List[str]:
        """检查警告"""
        warnings = []
      
        # 检查既往病史冲突
        for history in patient.medical_history:
            if any(conflict in history.lower() for conflict in condition_info.get("contraindications", [])):
                warnings.append(f"Contraindicated due to history of {history}")
      
        # 检查药物冲突
        for med in patient.medications:
            if any(conflict in med.lower() for conflict in condition_info.get("drug_conflicts", [])):
                warnings.append(f"Potential conflict with medication: {med}")
      
        return warnings

# 示例使用医疗诊断Agent
if __name__ == "__main__":
    # 创建模拟医学知识库
    medical_conditions = [
        {
            "name": "Common Cold",
            "description": "Viral infection of the upper respiratory tract",
            "symptoms": ["runny nose", "sneezing", "sore throat", "mild fever", "cough"],
            "treatments": [
                {
                    "name": "Rest",
                    "description": "Get plenty of rest to help your body fight the infection",
                    "priority": 1
                },
                {
                    "name": "Hydration",
                    "description": "Drink plenty of fluids to stay hydrated",
                    "priority": 1
                },
                {
                    "name": "OTC Pain Relievers",
                    "description": "Such as acetaminophen or ibuprofen for symptom relief",
                    "dosage": "As directed on package",
                    "priority": 2
                }
            ],
            "lifestyle_recommendations": [
                "Use saline nasal drops",
                "Gargle with warm salt water for sore throat",
                "Use a humidifier"
            ],
            "severity": "mild",
            "contraindications": [],
            "drug_conflicts": []
        },
        {
            "name": "Influenza",
            "description": "Contagious respiratory illness caused by influenza viruses",
            "symptoms": ["fever", "chills", "muscle aches", "fatigue", "cough", "sore throat"],
            "treatments": [
                {
                    "name": "Antiviral Medication",
                    "description": "Such as oseltamivir (Tamiflu) if started early",
                    "dosage": "75mg twice daily for 5 days",
                    "priority": 1
                },
                {
                    "name": "Rest",
                    "description": "Get plenty of rest to help your body recover",
                    "priority": 1
                },
                {
                    "name": "Hydration",
                    "description": "Drink plenty of fluids to stay hydrated",
                    "priority": 1
                }
            ],
            "lifestyle_recommendations": [
                "Stay home to avoid spreading the virus",
                "Cover your mouth when coughing or sneezing",
                "Wash hands frequently"
            ],
            "severity": "moderate",
            "contraindications": [],
            "drug_conflicts": ["warfarin"]
        },
        {
            "name": "Pneumonia",
            "description": "Infection that inflames air sacs in one or both lungs",
            "symptoms": ["fever", "chills", "cough with phlegm", "shortness of breath", "chest pain"],
            "treatments": [
                {
                    "name": "Antibiotics",
                    "description": "For bacterial pneumonia",
                    "dosage": "As prescribed by physician",
                    "priority": 1
                },
                {
                    "name": "Oxygen Therapy",
                    "description": "If oxygen levels are low",
                    "priority": 1
                },
                {
                    "name": "Rest",
                    "description": "Get plenty of rest to help recovery",
                    "priority": 2
                }
            ],
            "lifestyle_recommendations": [
                "Use a humidifier to loosen mucus",
                "Stay hydrated",
                "Avoid smoke and air pollutants"
            ],
            "severity": "severe",
            "contraindications": ["asthma", "COPD"],
            "drug_conflicts": []
        }
    ]
  
    # 创建知识库和Agent
    knowledge_base = MedicalKnowledgeBase(medical_conditions)
    diagnosis_agent = DiagnosisAgent(knowledge_base)
    treatment_planner = TreatmentPlanner(knowledge_base)
  
    # 创建模拟患者
    patient = PatientProfile(
        patient_id="P12345",
        age=35,
        gender="female",
        medical_history=["seasonal allergies"],
        current_symptoms=["fever", "cough", "sore throat"],
        medications=["antihistamines"]
    )
  
    # 诊断患者
    print("=== Medical Diagnosis ===")
    diagnosis = diagnosis_agent.diagnose(patient)
  
    # 显示诊断结果
    print(f"Patient: {patient.patient_id}, Age: {patient.age}, Gender: {patient.gender}")
    print(f"Symptoms: {', '.join(patient.current_symptoms)}")
    print("\nDiagnosis Results:")
    for i, cond in enumerate(diagnosis["diagnosis"]):
        print(f"{i+1}. {cond['name']} (Probability: {cond['probability']:.2%})")
        print(f"   Treatment: {cond['treatment']}")
  
    # 创建治疗计划
    print("\n=== Treatment Plan ===")
    treatment_plan = treatment_planner.create_treatment_plan(patient, diagnosis["diagnosis"])
  
    # 显示治疗计划
    print(f"Primary Diagnosis: {treatment_plan['diagnosis']} (Confidence: {treatment_plan['confidence']:.2%})")
  
    print("\nRecommended Treatments:")
    for i, treatment in enumerate(treatment_plan["recommended_treatments"]):
        print(f"{i+1}. {treatment['name']}")
        print(f"   {treatment['description']}")
        if treatment.get('dosage'):
            print(f"   Dosage: {treatment['dosage']}")
        if treatment.get('warnings'):
            print(f"   WARNING: {treatment['warnings']}")
  
    print("\nLifestyle Recommendations:")
    for i, rec in enumerate(treatment_plan["lifestyle_recommendations"]):
        print(f"{i+1}. {rec}")
  
    print(f"\nFollow-up: {treatment_plan['follow_up']}")
  
    if treatment_plan["warnings"]:
        print("\nWarnings:")
        for warning in treatment_plan["warnings"]:
            print(f"- {warning}")

# 高级医疗Agent:结合患者监测数据
class PatientMonitoringAgent:
    """患者监测Agent"""
  
    def __init__(self, 
                 diagnosis_agent: DiagnosisAgent,
                 treatment_planner: TreatmentPlanner,
                 alert_threshold: float = 0.7):
        self.diagnosis_agent = diagnosis_agent
        self.treatment_planner = treatment_planner
        self.alert_threshold = alert_threshold
        self.patient_monitoring = {}
  
    def register_patient(self, patient: PatientProfile, monitoring_frequency: str = "daily"):
        """注册患者进行监测"""
        self.patient_monitoring[patient.patient_id] = {
            "patient": patient,
            "frequency": monitoring_frequency,
            "last_assessment": None,
            "alert_history": []
        }
  
    def monitor_patient(self, patient_id: str, new_symptoms: List[str], vitals: Dict[str, float]):
        """监测患者状态"""
        if patient_id not in self.patient_monitoring:
            raise ValueError(f"Patient {patient_id} not registered for monitoring")
      
        monitoring_data = self.patient_monitoring[patient_id]
        patient = monitoring_data["patient"]
      
        # 更新症状
        patient.update_symptoms(new_symptoms)
      
        # 诊断评估
        diagnosis = self.diagnosis_agent.diagnose(patient)
      
        # 检查是否需要警报
        needs_alert = self._check_for_alerts(diagnosis, vitals)
      
        # 生成监测报告
        report = {
            "patient_id": patient_id,
            "timestamp": time.time(),
            "symptoms": patient.current_symptoms,
            "vitals": vitals,
            "diagnosis": diagnosis["diagnosis"],
            "needs_alert": needs_alert,
            "alert_reasons": []
        }
      
        # 如果需要警报,记录原因
        if needs_alert:
            report["alert_reasons"] = self._get_alert_reasons(diagnosis, vitals)
            monitoring_data["alert_history"].append(report)
      
        # 更新最后评估
        monitoring_data["last_assessment"] = report
      
        return report
  
    def _check_for_alerts(self, diagnosis: Dict, vitals: Dict[str, float]) -> bool:
        """检查是否需要警报"""
        # 检查高概率诊断
        if diagnosis["diagnosis"] and diagnosis["diagnosis"][0]["probability"] > self.alert_threshold:
            return True
      
        # 检查关键生命体征
        if vitals.get("temperature", 0) > 39.0:  # 高烧
            return True
        if 0 < vitals.get("heart_rate", 0) < 50 or vitals.get("heart_rate", 0) > 120:  # 心率异常
            return True
        if vitals.get("oxygen_saturation", 0) < 90:  # 低血氧
            return True
      
        return False
  
    def _get_alert_reasons(self, diagnosis: Dict, vitals: Dict[str, float]) -> List[str]:
        """获取警报原因"""
        reasons = []
      
        # 诊断相关原因
        if diagnosis["diagnosis"] and diagnosis["diagnosis"][0]["probability"] > self.alert_threshold:
            reasons.append(f"High probability diagnosis: {diagnosis['diagnosis'][0]['name']}")
      
        # 生命体征相关原因
        if vitals.get("temperature", 0) > 39.0:
            reasons.append(f"High fever: {vitals['temperature']}°C")
        if 0 < vitals.get("heart_rate", 0) < 50:
            reasons.append(f"Low heart rate: {vitals['heart_rate']} bpm")
        elif vitals.get("heart_rate", 0) > 120:
            reasons.append(f"High heart rate: {vitals['heart_rate']} bpm")
        if vitals.get("oxygen_saturation", 0) < 90:
            reasons.append(f"Low oxygen saturation: {vitals['oxygen_saturation']}%")
      
        return reasons

# 示例使用患者监测Agent
if __name__ == "__main__":
    print("\n\n=== Patient Monitoring Example ===")
  
    # 创建监测Agent
    monitoring_agent = PatientMonitoringAgent(diagnosis_agent, treatment_planner)
  
    # 注册患者
    monitoring_agent.register_patient(patient, "daily")
  
    # 模拟监测
    print("\nFirst monitoring (stable condition):")
    report1 = monitoring_agent.monitor_patient(
        patient.patient_id,
        new_symptoms=["mild headache"],
        vitals={
            "temperature": 37.5,
            "heart_rate": 85,
            "oxygen_saturation": 98
        }
    )
    print(f"Needs alert: {report1['needs_alert']}")
    if report1["alert_reasons"]:
        print("Alert reasons:", report1["alert_reasons"])
  
    # 模拟病情恶化
    print("\nSecond monitoring (worsening condition):")
    report2 = monitoring_agent.monitor_patient(
        patient.patient_id,
        new_symptoms=["difficulty breathing", "chest pain"],
        vitals={
            "temperature": 39.2,
            "heart_rate": 125,
            "oxygen_saturation": 87
        }
    )
    print(f"Needs alert: {report2['needs_alert']}")
    if report2["alert_reasons"]:
        print("Alert reasons:", report2["alert_reasons"])

智能制造领域的Agent应用

在智能制造领域,大模型Agent可以优化生产流程、预测设备故障、管理供应链等。以下是一个生产优化Agent的实现,展示了如何结合生产数据和大模型进行智能决策:

python 复制代码
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from typing import Dict, List, Tuple, Any, Optional
import time
import random

class ProductionDataProcessor:
    """生产数据处理器"""
  
    def __init__(self):
        self.scalers = {}
        self.fitted = False
  
    def fit(self, data: Dict[str, pd.DataFrame]):
        """拟合数据处理器"""
        for machine_id, df in data.items():
            # 为每台机器创建标准化器
            self.scalers[machine_id] = {}
          
            # 数值特征
            numeric_cols = ['temperature', 'vibration', 'pressure', 'speed', 'power_consumption', 'output_rate']
            self.scalers[machine_id]['numeric'] = StandardScaler()
            self.scalers[machine_id]['numeric'].fit(df[numeric_cols])
      
        self.fitted = True
  
    def transform(self, machine_id: str, data: pd.DataFrame) -> torch.Tensor:
        """转换生产数据为模型输入"""
        if not self.fitted:
            raise ValueError("Processor not fitted. Call fit() first.")
      
        # 数值特征
        numeric_cols = ['temperature', 'vibration', 'pressure', 'speed', 'power_consumption', 'output_rate']
        numeric_data = self.scalers[machine_id]['numeric'].transform(data[numeric_cols])
      
        # 转换为张量
        return torch.FloatTensor(numeric_data)

class MachineStateEncoder(nn.Module):
    """机器状态编码器"""
  
    def __init__(self, input_dim: int, hidden_dim: int = 64, num_layers: int = 2):
        super(MachineStateEncoder, self).__init__()
      
        # LSTM用于编码机器状态序列
        self.lstm = nn.LSTM(
            input_size=input_dim,
            hidden_size=hidden_dim,
            num_layers=num_layers,
            batch_first=True,
            bidirectional=True
        )
      
        # 状态表示
        self.state_representation = nn.Sequential(
            nn.Linear(hidden_dim * 2, 64),
            nn.ReLU(),
            nn.Linear(64, 32)
        )
  
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """编码机器状态"""
        # LSTM处理
        lstm_out, _ = self.lstm(x)
      
        # 取最后一个时间步
        last_output = lstm_out[:, -1, :]
      
        # 生成状态表示
        state_repr = self.state_representation(last_output)
      
        return state_repr

class ProductionOptimizer:
    """生产优化器"""
  
    def __init__(self, 
                 state_encoder: nn.Module,
                 data_processor: ProductionDataProcessor,
                 target_output: float,
                 max_power: float):
        self.state_encoder = state_encoder
        self.data_processor = data_processor
        self.target_output = target_output
        self.max_power = max_power
        self.machine_states = {}
        self.optimization_history = []
  
    def update_machine_state(self, machine_id: str, sensor_data: pd.DataFrame):
        """更新机器状态"""
        # 处理传感器数据
        processed_data = self.data_processor.transform(machine_id, sensor_data)
      
        # 编码状态
        with torch.no_grad():
            state_repr = self.state_encoder(processed_data.unsqueeze(0))
      
        # 存储状态
        self.machine_states[machine_id] = {
            "state_repr": state_repr,
            "last_data": sensor_data.iloc[-1].to_dict(),
            "timestamp": time.time()
        }
  
    def optimize_production(self, machine_id: str) -> Dict[str, Any]:
        """优化生产参数"""
        if machine_id not in self.machine_states:
            raise ValueError(f"Machine {machine_id} state not available")
      
        machine_state = self.machine_states[machine_id]
        current_data = machine_state["last_data"]
      
        # 优化逻辑
        optimization = self._calculate_optimization(current_data)
      
        # 生成优化建议
        recommendation = self._generate_recommendation(optimization, current_data)
      
        # 记录优化
        optimization_record = {
            "machine_id": machine_id,
            "current_state": current_data,
            "optimization": optimization,
            "recommendation": recommendation,
            "timestamp": time.time()
        }
        self.optimization_history.append(optimization_record)
      
        return optimization_record
  
    def _calculate_optimization(self, current_data: Dict[str, float]) -> Dict[str, float]:
        """计算优化参数"""
        # 简化优化逻辑
        current_output = current_data['output_rate']
        current_power = current_data['power_consumption']
      
        # 计算与目标的差距
        output_gap = self.target_output - current_output
      
        # 优化建议
        optimization = {}
      
        # 调整速度
        if output_gap > 5:
            # 需要提高产量
            speed_increase = min(output_gap / 10, 0.2)  # 最多提高20%
            optimization['speed'] = current_data['speed'] * (1 + speed_increase)
        elif output_gap < -5:
            # 需要降低产量
            speed_decrease = min(-output_gap / 10, 0.1)  # 最多降低10%
            optimization['speed'] = current_data['speed'] * (1 - speed_decrease)
        else:
            optimization['speed'] = current_data['speed']
      
        # 调整压力
        if current_data['vibration'] > 80:
            # 振动过高,降低压力
            optimization['pressure'] = max(current_data['pressure'] * 0.9, 50)
        elif current_data['vibration'] < 30:
            # 振动过低,可适当增加压力
            optimization['pressure'] = min(current_data['pressure'] * 1.1, 100)
        else:
            optimization['pressure'] = current_data['pressure']
      
        # 优化能耗
        efficiency = current_output / current_power
        target_efficiency = self.target_output / self.max_power * 1.2  # 目标效率略高于理论值
      
        if efficiency < target_efficiency * 0.9:
            # 能效低,调整参数
            optimization['power_target'] = min(
                current_power * (target_efficiency / efficiency),
                self.max_power
            )
        else:
            optimization['power_target'] = current_power
      
        return optimization
  
    def _generate_recommendation(self, 
                               optimization: Dict[str, float], 
                               current_data: Dict[str, float]) -> str:
        """生成优化建议"""
        recommendations = []
      
        # 速度建议
        speed_change = (optimization['speed'] - current_data['speed']) / current_data['speed']
        if abs(speed_change) > 0.05:
            if speed_change > 0:
                recommendations.append(f"Increase speed by {speed_change:.1%} to improve output")
            else:
                recommendations.append(f"Decrease speed by {abs(speed_change):.1%} to reduce vibration")
      
        # 压力建议
        pressure_change = (optimization['pressure'] - current_data['pressure']) / current_data['pressure']
        if abs(pressure_change) > 0.05:
            if pressure_change > 0:
                recommendations.append(f"Increase pressure by {pressure_change:.1%} for better output")
            else:
                recommendations.append(f"Decrease pressure by {abs(pressure_change):.1%} to reduce vibration")
      
        # 能耗建议
        power_change = (optimization['power_target'] - current_data['power_consumption']) / current_data['power_consumption']
        if abs(power_change) > 0.05:
            if power_change < 0:
                savings = current_data['power_consumption'] - optimization['power_target']
                recommendations.append(f"Reduce power to {optimization['power_target']:.1f}kW, saving {savings:.1f}kW")
            else:
                recommendations.append(f"Increase power to {optimization['power_target']:.1f}kW for higher output")
      
        # 故障预警
        if current_data['temperature'] > 85:
            recommendations.append("WARNING: High temperature detected. Consider reducing load.")
        if current_data['vibration'] > 90:
            recommendations.append("CRITICAL: Excessive vibration. Immediate inspection required.")
      
        if not recommendations:
            return "Current settings are optimal for target output."
      
        return "Recommendations: " + "; ".join(recommendations)

class PredictiveMaintenanceAgent:
    """预测性维护Agent"""
  
    def __init__(self, 
                 state_encoder: nn.Module,
                 data_processor: ProductionDataProcessor,
                 failure_threshold: float = 0.8):
        self.state_encoder = state_encoder
        self.data_processor = data_processor
        self.failure_threshold = failure_threshold
        self.machine_states = {}
        self.maintenance_history = []
  
    def update_machine_state(self, machine_id: str, sensor_data: pd.DataFrame):
        """更新机器状态"""
        # 处理传感器数据
        processed_data = self.data_processor.transform(machine_id, sensor_data)
      
        # 编码状态
        with torch.no_grad():
            state_repr = self.state_encoder(processed_data.unsqueeze(0))
      
        # 计算故障概率
        failure_prob = self._calculate_failure_probability(sensor_data)
      
        # 存储状态
        self.machine_states[machine_id] = {
            "state_repr": state_repr,
            "last_data": sensor_data.iloc[-1].to_dict(),
            "failure_prob": failure_prob,
            "timestamp": time.time()
        }
  
    def _calculate_failure_probability(self, sensor_data: pd.DataFrame) -> float:
        """计算故障概率"""
        # 简化实现:基于关键指标计算
        last_data = sensor_data.iloc[-1]
      
        # 基于温度、振动等指标计算故障概率
        temp_factor = min(max((last_data['temperature'] - 60) / 40, 0), 1)
        vibration_factor = min(last_data['vibration'] / 100, 1)
        pressure_factor = min(max((last_data['pressure'] - 80) / 20, 0), 1)
      
        # 组合因素
        failure_prob = (0.4 * temp_factor + 
                        0.3 * vibration_factor + 
                        0.2 * pressure_factor + 
                        0.1 * np.random.rand())  # 随机因素
      
        return min(failure_prob, 1.0)
  
    def assess_failure_risk(self, machine_id: str) -> Dict[str, Any]:
        """评估故障风险"""
        if machine_id not in self.machine_states:
            raise ValueError(f"Machine {machine_id} state not available")
      
        machine_state = self.machine_states[machine_id]
        current_data = machine_state["last_data"]
        failure_prob = machine_state["failure_prob"]
      
        # 生成维护建议
        recommendation = self._generate_maintenance_recommendation(
            failure_prob, 
            current_data
        )
      
        # 记录评估
        assessment = {
            "machine_id": machine_id,
            "failure_probability": failure_prob,
            "is_critical": failure_prob > self.failure_threshold,
            "recommendation": recommendation,
            "timestamp": time.time(),
            "current_state": current_data
        }
        self.maintenance_history.append(assessment)
      
        return assessment
  
    def _generate_maintenance_recommendation(self, 
                                           failure_prob: float, 
                                           current_data: Dict[str, float]) -> str:
        """生成维护建议"""
        if failure_prob > self.failure_threshold:
            return ("CRITICAL: High risk of failure. Immediate maintenance required. "
                    "Recommendation: Shut down machine for inspection.")
        elif failure_prob > 0.5:
            return ("WARNING: Elevated risk of failure. Schedule maintenance within 24-48 hours. "
                    "Monitor temperature and vibration closely.")
        elif failure_prob > 0.3:
            return ("NOTE: Slightly elevated risk. Consider scheduling maintenance in the next week. "
                    "Check oil levels and filter conditions.")
        else:
            return "Normal operation. No immediate maintenance required."

# 示例使用生产优化和预测维护Agent
if __name__ == "__main__":
    # 模拟获取机器传感器数据
    def generate_sensor_data(hours: int = 24, initial_state: Dict = None) -> pd.DataFrame:
        """生成模拟传感器数据"""
        if initial_state is None:
            initial_state = {
                'temperature': 70,
                'vibration': 50,
                'pressure': 75,
                'speed': 1000,
                'power_consumption': 50,
                'output_rate': 80
            }
      
        timestamps = pd.date_range(start=pd.Timestamp.now(), periods=hours, freq='H')
        data = []
      
        state = initial_state.copy()
        for _ in range(hours):
            # 模拟状态变化
            state['temperature'] += random.uniform(-1, 2)
            state['vibration'] += random.uniform(-5, 10)
            state['pressure'] += random.uniform(-2, 3)
            state['speed'] += random.uniform(-50, 50)
            state['power_consumption'] += random.uniform(-5, 10)
            state['output_rate'] = max(0, min(100, state['speed'] / 12))
          
            # 限制范围
            state['temperature'] = max(40, min(100, state['temperature']))
            state['vibration'] = max(0, min(100, state['vibration']))
            state['pressure'] = max(50, min(100, state['pressure']))
            state['speed'] = max(500, min(1500, state['speed']))
            state['power_consumption'] = max(30, min(80, state['power_consumption']))
          
            data.append(state.copy())
      
        return pd.DataFrame(data, index=timestamps)
  
    # 创建组件
    state_encoder = MachineStateEncoder(input_dim=6)  # 6个传感器特征
    data_processor = ProductionDataProcessor()
  
    # 准备训练数据
    machine_ids = ['M1001', 'M1002', 'M1003']
    train_data = {}
    for machine_id in machine_ids:
        train_data[machine_id] = generate_sensor_data(hours=168)  # 一周数据
    data_processor.fit(train_data)
  
    # 创建生产优化Agent
    production_optimizer = ProductionOptimizer(
        state_encoder=state_encoder,
        data_processor=data_processor,
        target_output=85.0,  # 目标产出率85%
        max_power=70.0       # 最大功率70kW
    )
  
    # 创建预测维护Agent
    maintenance_agent = PredictiveMaintenanceAgent(
        state_encoder=state_encoder,
        data_processor=data_processor,
        failure_threshold=0.75
    )
  
    # 模拟运行
    print("=== Production Optimization & Predictive Maintenance Simulation ===")
    for hour in range(12):
        print(f"\nHour {hour+1}:")
      
        for machine_id in machine_ids:
            # 生成新的传感器数据
            new_data = generate_sensor_data(
                hours=1, 
                initial_state=production_optimizer.machine_states.get(
                    machine_id, 
                    {'temperature': 70, 'vibration': 50}
                )["last_data"] if machine_id in production_optimizer.machine_states else None
            )
          
            # 更新机器状态
            production_optimizer.update_machine_state(machine_id, new_data)
            maintenance_agent.update_machine_state(machine_id, new_data)
          
            # 优化生产
            optimization = production_optimizer.optimize_production(machine_id)
            print(f"{machine_id} production optimization:")
            print(f"  Current output: {optimization['current_state']['output_rate']:.1f}%")
            print(f"  Recommendation: {optimization['recommendation']}")
          
            # 评估故障风险
            maintenance = maintenance_agent.assess_failure_risk(machine_id)
            print(f"{machine_id} maintenance assessment:")
            print(f"  Failure probability: {maintenance['failure_probability']:.1%}")
            print(f"  Recommendation: {maintenance['recommendation']}")
      
        # 暂停模拟
        time.sleep(0.5)
  
    # 显示维护历史
    print("\nMaintenance History for M1001:")
    for i, record in enumerate(maintenance_agent.maintenance_history):
        if record["machine_id"] == "M1001":
            print(f"{i+1}. Failure prob: {record['failure_probability']:.1%}, Recommendation: {record['recommendation']}")

# 高级生产优化:结合强化学习
class RLProductionOptimizer(ProductionOptimizer):
    """基于强化学习的生产优化器"""
  
    def __init__(self,
                 state_encoder: nn.Module,
                 data_processor: ProductionDataProcessor,
                 target_output: float,
                 max_power: float,
                 lr: float = 0.001,
                 gamma: float = 0.95):
        super().__init__(
            state_encoder, 
            data_processor, 
            target_output, 
            max_power
        )
      
        # DQN组件
        self.state_dim = 32  # state_encoder输出维度
        self.action_dim = 3  # 速度、压力、功率三个可调参数
      
        self.policy_net = nn.Sequential(
            nn.Linear(self.state_dim, 64),
            nn.ReLU(),
            nn.Linear(64, 64),
            nn.ReLU(),
            nn.Linear(64, self.action_dim * 3)  # 每个动作的三个选项
        )
      
        self.target_net = nn.Sequential(
            nn.Linear(self.state_dim, 64),
            nn.ReLU(),
            nn.Linear(64, 64),
            nn.ReLU(),
            nn.Linear(64, self.action_dim * 3)
        )
        self.target_net.load_state_dict(self.policy_net.state_dict())
        self.target_net.eval()
      
        self.optimizer = torch.optim.Adam(self.policy_net.parameters(), lr=lr)
        self.gamma = gamma
        self.memory = []
        self.batch_size = 32
        self.epsilon = 1.0
        self.epsilon_min = 0.1
        self.epsilon_decay = 0.995
        self.action_options = [
            [-0.1, 0.0, 0.1],  # 速度调整:-10%, 0, +10%
            [-0.1, 0.0, 0.1],  # 压力调整:-10%, 0, +10%
            [-0.1, 0.0, 0.1]   # 功率调整:-10%, 0, +10%
        ]
  
    def select_action(self, state_repr: torch.Tensor) -> List[float]:
        """选择优化动作"""
        if np.random.rand() < self.epsilon:
            # 探索:随机选择
            return [random.choice(options) for options in self.action_options]
        else:
            # 利用:选择Q值最高的动作组合
            with torch.no_grad():
                q_values = self.policy_net(state_repr)
                q_values = q_values.view(self.action_dim, 3)
              
                # 为每个维度选择最佳动作
                action_indices = q_values.argmax(dim=1)
                actions = [
                    self.action_options[i][idx.item()]
                    for i, idx in enumerate(action_indices)
                ]
                return actions
  
    def store_experience(self, 
                        state: torch.Tensor, 
                        action: List[float], 
                        reward: float, 
                        next_state: torch.Tensor, 
                        done: bool):
        """存储经验"""
        # 将动作转换为索引
        action_indices = []
        for i, a in enumerate(action):
            idx = self.action_options[i].index(min(self.action_options[i], 
                                                key=lambda x: abs(x-a)))
            action_indices.append(idx)
      
        self.memory.append((state, action_indices, reward, next_state, done))
      
        # 限制记忆大小
        if len(self.memory) > 10000:
            self.memory.pop(0)
  
    def train(self):
        """训练DQN"""
        if len(self.memory) < self.batch_size:
            return
      
        # 采样批次
        batch = random.sample(self.memory, self.batch_size)
        states, actions, rewards, next_states, dones = zip(*batch)
      
        # 转换为张量
        states = torch.cat(states)
        actions = torch.LongTensor(actions)
        rewards = torch.FloatTensor(rewards)
        next_states = torch.cat(next_states)
        dones = torch.FloatTensor(dones)
      
        # 计算当前Q值
        current_q = self.policy_net(states)
        current_q = current_q.view(self.batch_size, self.action_dim, 3)
      
        # 选择动作对应的Q值
        action_mask = torch.zeros_like(current_q, dtype=torch.bool)
        for i in range(self.batch_size):
            for j in range(self.action_dim):
                action_mask[i, j, actions[i, j]] = True
        selected_q = current_q[action_mask].view(self.batch_size, self.action_dim)
      
        # 计算目标Q值
        with torch.no_grad():
            next_q = self.target_net(next_states)
            next_q = next_q.view(self.batch_size, self.action_dim, 3)
            max_next_q = next_q.max(dim=2)[0]
            target_q = rewards.unsqueeze(1) + self.gamma * max_next_q * (1 - dones.unsqueeze(1))
      
        # 计算损失
        loss = nn.MSELoss()(selected_q, target_q)
      
        # 优化模型
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()
      
        # 更新epsilon
        self.epsilon = max(self.epsilon_min, self.epsilon * self.epsilon_decay)
  
    def optimize_production(self, machine_id: str) -> Dict[str, Any]:
        """强化学习增强的生产优化"""
        if machine_id not in self.machine_states:
            raise ValueError(f"Machine {machine_id} state not available")
      
        machine_state = self.machine_states[machine_id]
        current_data = machine_state["last_data"]
        state_repr = machine_state["state_repr"]
      
        # 选择动作
        action = self.select_action(state_repr)
      
        # 计算优化参数
        optimization = self._apply_rl_action(current_data, action)
      
        # 生成优化建议
        recommendation = self._generate_rl_recommendation(optimization, current_data, action)
      
        # 记录优化
        optimization_record = {
            "machine_id": machine_id,
            "current_state": current_data,
            "optimization": optimization,
            "recommendation": recommendation,
            "timestamp": time.time(),
            "rl_action": action
        }
        self.optimization_history.append(optimization_record)
      
        return optimization_record
  
    def _apply_rl_action(self, current_data: Dict[str, float], action: List[float]) -> Dict[str, float]:
        """应用强化学习动作"""
        optimization = {}
      
        # 应用速度调整
        speed_change = action[0]
        optimization['speed'] = current_data['speed'] * (1 + speed_change)
      
        # 应用压力调整
        pressure_change = action[1]
        optimization['pressure'] = current_data['pressure'] * (1 + pressure_change)
      
        # 应用功率调整
        power_change = action[2]
        optimization['power_target'] = current_data['power_consumption'] * (1 + power_change)
      
        return optimization
  
    def _generate_rl_recommendation(self, 
                                  optimization: Dict[str, float], 
                                  current_data: Dict[str, float],
                                  action: List[float]) -> str:
        """生成强化学习优化建议"""
        base_recommendation = super()._generate_recommendation(optimization, current_data)
      
        # 添加RL特定信息
        action_descriptions = []
        if abs(action[0]) > 0.05:
            direction = "increase" if action[0] > 0 else "decrease"
            action_descriptions.append(f"{direction} speed by {abs(action[0]):.0%}")
        if abs(action[1]) > 0.05:
            direction = "increase" if action[1] > 0 else "decrease"
            action_descriptions.append(f"{direction} pressure by {abs(action[1]):.0%}")
        if abs(action[2]) > 0.05:
            direction = "increase" if action[2] > 0 else "decrease"
            action_descriptions.append(f"{direction} power by {abs(action[2]):.0%}")
      
        rl_info = f"[RL Decision] {' and '.join(action_descriptions)} based on historical performance."
      
        return f"{rl_info}\n{base_recommendation}"
  
    def update_policy(self, machine_id: str, reward: float, done: bool = False):
        """更新策略"""
        if len(self.optimization_history) < 2:
            return
      
        # 获取最近两次优化
        current_opt = self.optimization_history[-1]
        previous_opt = self.optimization_history[-2]
      
        if machine_id != previous_opt["machine_id"]:
            return
      
        # 存储经验
        self.store_experience(
            previous_opt["state_repr"],
            previous_opt["rl_action"],
            reward,
            current_opt["state_repr"],
            done
        )
      
        # 训练
        self.train()

Agent技术的伦理与社会责任

偏见检测与缓解

大模型Agent可能继承训练数据中的偏见,导致不公平的决策。以下是一个偏见检测与缓解框架的实现,展示了如何识别和减轻Agent决策中的偏见:

python 复制代码
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.metrics import roc_auc_score
from typing import Dict, List, Tuple, Any, Optional
import re

class BiasDetector:
    """偏见检测器"""
  
    def __init__(self, 
                 sensitive_attributes: List[str],
                 protected_groups: Dict[str, List[str]],
                 threshold: float = 0.1):
        """
        初始化偏见检测器
      
        Args:
            sensitive_attributes: 敏感属性列表(如性别、种族)
            protected_groups: 受保护群体定义
            threshold: 偏见检测阈值
        """
        self.sensitive_attributes = sensitive_attributes
        self.protected_groups = protected_groups
        self.threshold = threshold
  
    def detect_bias(self, 
                   inputs: pd.DataFrame,
                   predictions: np.ndarray,
                   labels: Optional[np.ndarray] = None) -> Dict[str, Dict[str, float]]:
        """
        检测决策中的偏见
      
        Args:
            inputs: 输入数据
            predictions: 模型预测
            labels: 真实标签(可选,用于检测训练数据偏见)
          
        Returns:
            偏见检测结果
        """
        results = {}
      
        for attr in self.sensitive_attributes:
            if attr not in inputs.columns:
                continue
          
            # 获取受保护群体
            protected_values = self.protected_groups.get(attr, [])
          
            # 检查每个受保护群体
            for group in protected_values:
                # 计算群体指标
                group_mask = inputs[attr] == group
                non_group_mask = inputs[attr] != group
              
                if labels is not None:
                    # 训练数据偏见检测
                    group_positive_rate = np.mean(labels[group_mask])
                    non_group_positive_rate = np.mean(labels[non_group_mask])
                  
                    disparity = abs(group_positive_rate - non_group_positive_rate)
                    results.setdefault(attr, {})[group] = {
                        "disparity": disparity,
                        "group_rate": group_positive_rate,
                        "non_group_rate": non_group_positive_rate,
                        "bias_detected": disparity > self.threshold
                    }
                else:
                    # 预测偏见检测
                    group_prediction_rate = np.mean(predictions[group_mask])
                    non_group_prediction_rate = np.mean(predictions[non_group_mask])
                  
                    disparity = abs(group_prediction_rate - non_group_prediction_rate)
                    results.setdefault(attr, {})[group] = {
                        "disparity": disparity,
                        "group_rate": group_prediction_rate,
                        "non_group_rate": non_group_prediction_rate,
                        "bias_detected": disparity > self.threshold
                    }
      
        return results
  
    def explain_bias(self, bias_results: Dict) -> List[str]:
        """解释偏见检测结果"""
        explanations = []
      
        for attr, groups in bias_results.items():
            for group, metrics in groups.items():
                if metrics["bias_detected"]:
                    explanation = (
                        f"Potential bias detected for {attr}='{group}'. "
                        f"Difference in decision rate: {metrics['disparity']:.2%} "
                        f"(group: {metrics['group_rate']:.2%}, others: {metrics['non_group_rate']:.2%})."
                    )
                    explanations.append(explanation)
      
        return explanations

class AdversarialDebiaser(nn.Module):
    """对抗去偏模块"""
  
    def __init__(self, input_dim: int, sensitive_dim: int, hidden_dim: int = 64):
        """
        初始化对抗去偏模块
      
        Args:
            input_dim: 主任务输入维度
            sensitive_dim: 敏感属性维度
            hidden_dim: 隐藏层维度
        """
        super(AdversarialDebiaser, self).__init__()
      
        # 主任务分类器
        self.classifier = nn.Sequential(
            nn.Linear(input_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, 1),
            nn.Sigmoid()
        )
      
        # 对抗分类器(用于去除敏感信息)
        self.adversary = nn.Sequential(
            nn.Linear(input_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, sensitive_dim)
        )
  
    def forward(self, x: torch.Tensor, sensitive: torch.Tensor, lambda_param: float = 0.5):
        """
        前向传播
      
        Args:
            x: 输入特征
            sensitive: 敏感属性
            lambda_param: 对抗损失权重
          
        Returns:
            主任务预测, 对抗任务预测, 损失
        """
        # 主任务预测
        pred = self.classifier(x)
      
        # 对抗任务预测(尝试从表示中预测敏感属性)
        sensitive_pred = self.adversary(x)
      
        # 计算损失
        main_loss = nn.BCELoss()(pred, sensitive.float().view(-1, 1))
        adversary_loss = nn.CrossEntropyLoss()(sensitive_pred, sensitive)
      
        # 总损失(对抗训练)
        total_loss = main_loss - lambda_param * adversary_loss
      
        return pred, sensitive_pred, total_loss

class BiasMitigationAgent:
    """偏见缓解Agent"""
  
    def __init__(self,
                 model: nn.Module,
                 bias_detector: BiasDetector,
                 debiaser: Optional[AdversarialDebiaser] = None,
                 mitigation_strategy: str = "reweighting"):
        """
        初始化偏见缓解Agent
      
        Args:
            model: 要缓解偏见的模型
            bias_detector: 偏见检测器
            debiaser: 对抗去偏模块
            mitigation_strategy: 缓解策略
        """
        self.model = model
        self.bias_detector = bias_detector
        self.debiaser = debiaser
        self.mitigation_strategy = mitigation_strategy
        self.mitigation_history = []
  
    def detect_and_mitigate(self, 
                           inputs: pd.DataFrame,
                           predictions: np.ndarray,
                           labels: Optional[np.ndarray] = None) -> Dict[str, Any]:
        """
        检测并缓解偏见
      
        Args:
            inputs: 输入数据
            predictions: 模型预测
            labels: 真实标签(可选)
          
        Returns:
            缓解结果
        """
        # 检测偏见
        bias_results = self.bias_detector.detect_bias(inputs, predictions, labels)
      
        # 检查是否需要缓解
        needs_mitigation = any(
            metrics["bias_detected"] 
            for attr_groups in bias_results.values() 
            for metrics in attr_groups.values()
        )
      
        # 生成解释
        explanations = self.bias_detector.explain_bias(bias_results)
      
        # 执行缓解(如果需要)
        mitigation = None
        if needs_mitigation and self.mitigation_strategy != "none":
            mitigation = self._apply_mitigation(inputs, predictions, bias_results)
      
        # 记录历史
        mitigation_record = {
            "bias_results": bias_results,
            "explanations": explanations,
            "mitigation_applied": mitigation is not None,
            "mitigation": mitigation,
            "timestamp": time.time()
        }
        self.mitigation_history.append(mitigation_record)
      
        return mitigation_record
  
    def _apply_mitigation(self, 
                         inputs: pd.DataFrame,
                         predictions: np.ndarray,
                         bias_results: Dict) -> Dict[str, Any]:
        """应用偏见缓解"""
        if self.mitigation_strategy == "reweighting":
            return self._reweighting_mitigation(inputs, predictions, bias_results)
        elif self.mitigation_strategy == "adversarial":
            return self._adversarial_mitigation(inputs, predictions, bias_results)
        elif self.mitigation_strategy == "calibration":
            return self._calibration_mitigation(inputs, predictions, bias_results)
        else:
            raise ValueError(f"Unknown mitigation strategy: {self.mitigation_strategy}")
  
    def _reweighting_mitigation(self, 
                               inputs: pd.DataFrame,
                               predictions: np.ndarray,
                               bias_results: Dict) -> Dict[str, Any]:
        """重新加权缓解"""
        weights = np.ones(len(inputs))
      
        # 为受偏见影响的群体增加权重
        for attr, groups in bias_results.items():
            for group, metrics in groups.items():
                if metrics["bias_detected"]:
                    group_mask = inputs[attr] == group
                    weights[group_mask] *= 1.5  # 增加受影响群体的权重
      
        # 归一化权重
        weights /= np.sum(weights)
      
        return {
            "strategy": "reweighting",
            "weights": weights.tolist(),
            "description": "Increased weights for potentially biased groups to balance training"
        }
  
    def _adversarial_mitigation(self, 
                               inputs: pd.DataFrame,
                               predictions: np.ndarray,
                               bias_results: Dict) -> Dict[str, Any]:
        """对抗训练缓解"""
        if not self.debiaser:
            return {
                "strategy": "adversarial",
                "status": "failed",
                "error": "Adversarial debiaser not initialized"
            }
      
        # 实际应用中应执行对抗训练
        # 这里简化为返回配置
        return {
            "strategy": "adversarial",
            "lambda_param": 0.5,
            "description": "Adversarial training to remove sensitive attribute information from representations"
        }
  
    def _calibration_mitigation(self, 
                               inputs: pd.DataFrame,
                               predictions: np.ndarray,
                               bias_results: Dict) -> Dict[str, Any]:
        """校准缓解"""
        adjustments = {}
      
        # 为每个受偏见影响的群体计算校准因子
        for attr, groups in bias_results.items():
            for group, metrics in groups.items():
                if metrics["bias_detected"]:
                    group_mask = inputs[attr] == group
                  
                    # 计算当前校准误差
                    current_rate = np.mean(predictions[group_mask])
                    target_rate = metrics["non_group_rate"]
                  
                    # 计算调整因子
                    if current_rate > 0:
                        adjustment = target_rate / current_rate
                        adjustments[f"{attr}_{group}"] = float(adjustment)
      
        return {
            "strategy": "calibration",
            "adjustments": adjustments,
            "description": "Probability adjustments to achieve equal decision rates across groups"
        }
  
    def apply_mitigation(self, 
                        inputs: pd.DataFrame,
                        predictions: np.ndarray,
                        mitigation: Dict) -> np.ndarray:
        """应用具体的缓解措施"""
        if not mitigation["mitigation_applied"]:
            return predictions
      
        strategy = mitigation["mitigation"]["strategy"]
      
        if strategy == "calibration" and "adjustments" in mitigation["mitigation"]:
            # 应用校准调整
            adjusted_preds = predictions.copy()
          
            for attr_group, factor in mitigation["mitigation"]["adjustments"].items():
                attr, group = attr_group.split("_", 1)
                group_mask = inputs[attr] == group
                adjusted_preds[group_mask] = np.clip(
                    adjusted_preds[group_mask] * factor, 
                    0, 1
                )
          
            return adjusted_preds
      
        return predictions

# 示例使用偏见检测与缓解Agent
if __name__ == "__main__":
    # 创建模拟数据
    np.random.seed(42)
  
    # 生成包含敏感属性的数据
    n_samples = 1000
    data = {
        "gender": np.random.choice(["male", "female"], n_samples, p=[0.6, 0.4]),
        "race": np.random.choice(["white", "black", "hispanic"], n_samples, p=[0.5, 0.3, 0.2]),
        "age": np.random.normal(40, 10, n_samples).astype(int),
        "income": np.random.lognormal(10, 1, n_samples)
    }
  
    # 添加一些偏见:女性收入预测偏低
    data["income"] = [
        inc * 0.8 if gender == "female" else inc
        for gender, inc in zip(data["gender"], data["income"])
    ]
  
    # 生成二元结果(例如贷款批准)
    data["loan_approved"] = (
        (data["income"] > np.percentile(data["income"], 50)) & 
        (np.random.random(n_samples) > 0.2)  # 20%拒绝率
    ).astype(int)
  
    # 女性额外20%拒绝率(模拟偏见)
    for i in range(n_samples):
        if data["gender"][i] == "female" and np.random.random() < 0.2:
            data["loan_approved"][i] = 0
  
    df = pd.DataFrame(data)
  
    # 创建偏见检测器
    bias_detector = BiasDetector(
        sensitive_attributes=["gender", "race"],
        protected_groups={
            "gender": ["female"],
            "race": ["black", "hispanic"]
        },
        threshold=0.1
    )
  
    # 模拟模型预测(带偏见)
    predictions = df["loan_approved"].values * 0.9  # 模拟预测概率
  
    # 创建偏见缓解Agent
    bias_agent = BiasMitigationAgent(
        model=None,  # 实际应用中应传入模型
        bias_detector=bias_detector,
        mitigation_strategy="calibration"
    )
  
    # 检测并缓解偏见
    print("=== Bias Detection and Mitigation ===")
    result = bias_agent.detect_and_mitigate(
        inputs=df,
        predictions=predictions,
        labels=df["loan_approved"].values
    )
  
    # 显示结果
    print("\nBias Detection Results:")
    for attr, groups in result["bias_results"].items():
        for group, metrics in groups.items():
            print(f"{attr}='{group}': disparity={metrics['disparity']:.2%}, "
                  f"bias detected={metrics['bias_detected']}")
  
    print("\nExplanations:")
    for explanation in result["explanations"]:
        print(f"- {explanation}")
  
    if result["mitigation_applied"]:
        print("\nMitigation Applied:")
        mitigation = result["mitigation"]
        print(f"Strategy: {mitigation['strategy']}")
        print(f"Description: {mitigation['description']}")
      
        # 应用缓解
        adjusted_predictions = bias_agent.apply_mitigation(df, predictions, result)
      
        # 检查缓解效果
        print("\nChecking mitigation effect:")
        for attr in ["gender"]:
            for group in ["female"]:
                group_mask = df[attr] == group
                original_rate = np.mean(predictions[group_mask])
                adjusted_rate = np.mean(adjusted_predictions[group_mask])
                print(f"{attr}='{group}': original={original_rate:.2%}, adjusted={adjusted_rate:.2%}")

# 高级偏见缓解:动态公平性约束
class DynamicFairnessConstraint:
    """动态公平性约束"""
  
    def __init__(self, 
                 sensitive_attributes: List[str],
                 target_disparity: float = 0.05,
                 constraint_type: str = "demographic_parity"):
        """
        初始化动态公平性约束
      
        Args:
            sensitive_attributes: 敏感属性
            target_disparity: 目标差异
            constraint_type: 约束类型
        """
        self.sensitive_attributes = sensitive_attributes
        self.target_disparity = target_disparity
        self.constraint_type = constraint_type
        self.history = []
  
    def calculate_constraint_loss(self, 
                                predictions: torch.Tensor,
                                sensitive: torch.Tensor,
                                labels: Optional[torch.Tensor] = None) -> torch.Tensor:
        """
        计算公平性约束损失
      
        Args:
            predictions: 模型预测
            sensitive: 敏感属性
            labels: 真实标签(用于某些约束类型)
          
        Returns:
            约束损失
        """
        if self.constraint_type == "demographic_parity":
            return self._demographic_parity_loss(predictions, sensitive)
        elif self.constraint_type == "equalized_odds":
            if labels is None:
                raise ValueError("Labels required for equalized odds constraint")
            return self._equalized_odds_loss(predictions, sensitive, labels)
        else:
            raise ValueError(f"Unknown constraint type: {self.constraint_type}")
  
    def _demographic_parity_loss(self, 
                                predictions: torch.Tensor,
                                sensitive: torch.Tensor) -> torch.Tensor:
        """人口均等损失"""
        unique_vals = torch.unique(sensitive)
        rates = []
      
        for val in unique_vals:
            mask = (sensitive == val)
            if torch.sum(mask) > 0:
                rate = torch.mean(predictions[mask])
                rates.append(rate)
      
        if len(rates) < 2:
            return torch.tensor(0.0)
      
        # 计算最大差异
        max_diff = torch.max(torch.stack(rates)) - torch.min(torch.stack(rates))
      
        # 惩罚超过目标差异的部分
        violation = torch.relu(max_diff - self.target_disparity)
      
        return violation
  
    def _equalized_odds_loss(self, 
                            predictions: torch.Tensor,
                            sensitive: torch.Tensor,
                            labels: torch.Tensor) -> torch.Tensor:
        """均等机会损失"""
        unique_vals = torch.unique(sensitive)
        tpr_rates = []
        fpr_rates = []
      
        for val in unique_vals:
            mask = (sensitive == val)
            if torch.sum(mask) > 0:
                # 真阳性率
                pos_mask = mask & (labels == 1)
                if torch.sum(pos_mask) > 0:
                    tpr = torch.mean(predictions[pos_mask])
                    tpr_rates.append(tpr)
              
                # 假阳性率
                neg_mask = mask & (labels == 0)
                if torch.sum(neg_mask) > 0:
                    fpr = torch.mean(predictions[neg_mask])
                    fpr_rates.append(fpr)
      
        # 计算TPR差异
        tpr_violation = 0.0
        if len(tpr_rates) >= 2:
            tpr_max_diff = torch.max(torch.stack(tpr_rates)) - torch.min(torch.stack(tpr_rates))
            tpr_violation = torch.relu(tpr_max_diff - self.target_disparity)
      
        # 计算FPR差异
        fpr_violation = 0.0
        if len(fpr_rates) >= 2:
            fpr_max_diff = torch.max(torch.stack(fpr_rates)) - torch.min(torch.stack(fpr_rates))
            fpr_violation = torch.relu(fpr_max_diff - self.target_disparity)
      
        return (tpr_violation + fpr_violation) / 2

class FairnessAwareTrainer:
    """公平性感知训练器"""
  
    def __init__(self,
                 model: nn.Module,
                 optimizer: torch.optim.Optimizer,
                 fairness_constraint: DynamicFairnessConstraint,
                 main_loss_weight: float = 1.0,
                 fairness_loss_weight: float = 0.5):
        """
        初始化公平性感知训练器
      
        Args:
            model: 模型
            optimizer: 优化器
            fairness_constraint: 公平性约束
            main_loss_weight: 主任务损失权重
            fairness_loss_weight: 公平性损失权重
        """
        self.model = model
        self.optimizer = optimizer
        self.fairness_constraint = fairness_constraint
        self.main_loss_weight = main_loss_weight
        self.fairness_loss_weight = fairness_loss_weight
        self.criterion = nn.BCELoss()
  
    def train_step(self, 
                  inputs: torch.Tensor,
                  labels: torch.Tensor,
                  sensitive: torch.Tensor) -> Dict[str, float]:
        """
        训练步骤
      
        Args:
            inputs: 输入数据
            labels: 标签
            sensitive: 敏感属性
          
        Returns:
            损失统计
        """
        self.optimizer.zero_grad()
      
        # 前向传播
        outputs = self.model(inputs)
        outputs = torch.sigmoid(outputs)
      
        # 计算主任务损失
        main_loss = self.criterion(outputs, labels)
      
        # 计算公平性约束损失
        fairness_loss = self.fairness_constraint.calculate_constraint_loss(
            outputs, sensitive, labels
        )
      
        # 总损失
        total_loss = (
            self.main_loss_weight * main_loss + 
            self.fairness_loss_weight * fairness_loss
        )
      
        # 反向传播
        total_loss.backward()
        self.optimizer.step()
      
        return {
            "total_loss": total_loss.item(),
            "main_loss": main_loss.item(),
            "fairness_loss": fairness_loss.item()
        }

# 示例使用公平性感知训练
if __name__ == "__main__":
    print("\n\n=== Fairness-Aware Training Example ===")
  
    # 创建模拟模型
    class SimpleClassifier(nn.Module):
        def __init__(self, input_dim: int):
            super().__init__()
            self.fc = nn.Linear(input_dim, 1)
      
        def forward(self, x):
            return self.fc(x)
  
    # 创建组件
    model = SimpleClassifier(input_dim=5)
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
  
    # 创建公平性约束
    fairness_constraint = DynamicFairnessConstraint(
        sensitive_attributes=["gender"],
        target_disparity=0.05,
        constraint_type="demographic_parity"
    )
  
    # 创建公平性感知训练器
    trainer = FairnessAwareTrainer(
        model=model,
        optimizer=optimizer,
        fairness_constraint=fairness_constraint
    )
  
    # 模拟训练数据
    X = torch.randn(100, 5)
    y = torch.rand(100) > 0.3  # 二元标签
    y = y.float()
  
    # 模拟敏感属性(0=male, 1=female)
    sensitive = torch.randint(0, 2, (100,))
  
    # 训练步骤
    print("Training with fairness constraints...")
    for epoch in range(5):
        losses = trainer.train_step(X, y, sensitive)
        print(f"Epoch {epoch+1}, Total Loss: {losses['total_loss']:.4f}, "
              f"Main Loss: {losses['main_loss']:.4f}, "
              f"Fairness Loss: {losses['fairness_loss']:.4f}")
  
    # 评估公平性
    with torch.no_grad():
        outputs = torch.sigmoid(model(X))
  
    # 计算不同群体的预测率
    male_mask = (sensitive == 0)
    female_mask = (sensitive == 1)
  
    male_rate = torch.mean(outputs[male_mask]).item()
    female_rate = torch.mean(outputs[female_mask]).item()
  
    print(f"\nPost-training disparity: {abs(male_rate - female_rate):.4f}")
    print(f"Male prediction rate: {male_rate:.4f}")
    print(f"Female prediction rate: {female_rate:.4f}")

透明度与可解释性增强

大模型Agent的决策过程往往缺乏透明度,影响用户信任。以下是一个可解释性增强框架的实现,展示了如何为Agent决策提供清晰解释:

python 复制代码
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from typing import Dict, List, Tuple, Any, Optional
import re
import json

class ExplanationGenerator:
    """解释生成器"""
  
    def __init__(self, 
                 model: nn.Module,
                 feature_names: List[str],
                 class_names: List[str]):
        """
        初始化解释生成器
      
        Args:
            model: 被解释的模型
            feature_names: 特征名称
            class_names: 类别名称
        """
        self.model = model
        self.feature_names = feature_names
        self.class_names = class_names
  
    def generate_explanation(self, 
                            input_data: np.ndarray,
                            prediction: int,
                            prediction_prob: float) -> Dict[str, Any]:
        """
        生成决策解释
      
        Args:
            input_data: 输入数据
            prediction: 预测类别
            prediction_prob: 预测概率
          
        Returns:
            解释结果
        """
        # 生成特征重要性
        feature_importance = self._calculate_feature_importance(input_data)
      
        # 生成对比案例
        contrastive_example = self._generate_contrastive_example(input_data, prediction)
      
        # 生成自然语言解释
        natural_language = self._generate_natural_language_explanation(
            input_data, 
            prediction, 
            prediction_prob,
            feature_importance,
            contrastive_example
        )
      
        return {
            "feature_importance": feature_importance,
            "contrastive_example": contrastive_example,
            "natural_language": natural_language,
            "prediction": prediction,
            "prediction_prob": prediction_prob
        }
  
    def _calculate_feature_importance(self, input_data: np.ndarray) -> List[Dict[str, float]]:
        """计算特征重要性"""
        # 简化实现:使用梯度计算
        input_tensor = torch.FloatTensor(input_data).requires_grad_(True)
      
        # 前向传播
        output = self.model(input_tensor)
        _, predicted = torch.max(output.data, 1)
      
        # 反向传播获取梯度
        self.model.zero_grad()
        output[0, predicted.item()].backward()
      
        # 计算特征重要性
        gradients = input_tensor.grad.data.numpy()[0]
        abs_gradients = np.abs(gradients)
        normalized = abs_gradients / np.sum(abs_gradients)
      
        # 创建特征重要性列表
        feature_importance = [
            {"feature": self.feature_names[i], "importance": float(normalized[i])}
            for i in range(len(self.feature_names))
        ]
      
        # 按重要性排序
        feature_importance.sort(key=lambda x: x["importance"], reverse=True)
      
        return feature_importance
  
    def _generate_contrastive_example(self, 
                                     input_data: np.ndarray,
                                     prediction: int) -> Dict[str, Any]:
        """生成对比案例"""
        # 简化实现:随机修改特征
        contrastive_data = input_data.copy()
      
        # 找到最重要的特征并修改
        feature_importance = self._calculate_feature_importance(input_data)
        most_important_idx = self.feature_names.index(feature_importance[0]["feature"])
      
        # 修改特征值(假设数值特征)
        contrastive_data[0, most_important_idx] *= 0.5
      
        # 获取对比预测
        with torch.no_grad():
            contrastive_output = self.model(torch.FloatTensor(contrastive_data))
            _, contrastive_pred = torch.max(contrastive_output.data, 1)
            contrastive_prob = torch.softmax(contrastive_output, dim=1)[0, contrastive_pred.item()].item()
      
        return {
            "modified_feature": self.feature_names[most_important_idx],
            "original_value": float(input_data[0, most_important_idx]),
            "modified_value": float(contrastive_data[0, most_important_idx]),
            "original_prediction": prediction,
            "contrastive_prediction": contrastive_pred.item(),
            "contrastive_prob": contrastive_prob
        }
  
    def _generate_natural_language_explanation(self,
                                             input_data: np.ndarray,
                                             prediction: int,
                                             prediction_prob: float,
                                             feature_importance: List[Dict],
                                             contrastive_example: Dict) -> str:
        """生成自然语言解释"""
        # 基本决策陈述
        explanation = (
            f"The model predicted '{self.class_names[prediction]}' "
            f"with {prediction_prob:.1%} confidence. "
        )
      
        # 添加关键特征
        top_features = feature_importance[:3]
        feature_descriptions = []
        for feat in top_features:
            value = input_data[0, self.feature_names.index(feat["feature"])]
            feature_descriptions.append(
                f"{feat['feature']} ({value:.2f}, importance: {feat['importance']:.0%})"
            )
      
        explanation += "This decision was primarily influenced by: " + ", ".join(feature_descriptions) + ". "
      
        # 添加对比案例
        if contrastive_example["original_prediction"] != contrastive_example["contrastive_prediction"]:
            explanation += (
                f"If {contrastive_example['modified_feature']} were lower "
                f"({contrastive_example['modified_value']:.2f} instead of {contrastive_example['original_value']:.2f}), "
                f"the model would have predicted '{self.class_names[contrastive_example['contrastive_prediction']]}'."
            )
      
        return explanation

class LocalInterpretableModel:
    """局部可解释模型(LIME的简化实现)"""
  
    def __init__(self,
                 model: nn.Module,
                 feature_names: List[str],
                 class_names: List[str],
                 kernel_width: float = 0.25):
        """
        初始化局部可解释模型
      
        Args:
            model: 被解释的模型
            feature_names: 特征名称
            class_names: 类别名称
            kernel_width: 核宽度
        """
        self.model = model
        self.feature_names = feature_names
        self.class_names = class_names
        self.kernel_width = kernel_width
  
    def explain_instance(self, 
                        instance: np.ndarray,
                        num_samples: int = 5000,
                        top_labels: int = 1) -> Dict[str, Any]:
        """
        解释单个实例
      
        Args:
            instance: 要解释的实例
            num_samples: 采样数量
            top_labels: 要解释的标签数量
          
        Returns:
            解释结果
        """
        # 生成扰动样本
        perturbed_samples, distances, labels = self._generate_perturbed_samples(
            instance, num_samples
        )
      
        # 训练可解释模型
        explanation = self._train_explainable_model(
            instance, perturbed_samples, distances, labels, top_labels
        )
      
        return explanation
  
    def _generate_perturbed_samples(self, 
                                   instance: np.ndarray,
                                   num_samples: int) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
        """生成扰动样本"""
        n_features = instance.shape[1]
        samples = []
      
        # 生成扰动
        for _ in range(num_samples):
            # 随机选择要扰动的特征
            mask = np.random.binomial(1, 0.5, n_features)
          
            # 扰动特征值
            perturbation = np.random.normal(0, 0.1, n_features)
            perturbed = instance.copy()
            perturbed[0, mask == 1] += perturbation[mask == 1]
          
            samples.append(perturbed[0])
      
        samples = np.array(samples)
      
        # 获取预测
        with torch.no_grad():
            inputs = torch.FloatTensor(samples)
            outputs = self.model(inputs)
            probabilities = torch.softmax(outputs, dim=1).numpy()
            labels = np.argmax(probabilities, axis=1)
      
        # 计算距离
        distances = np.sqrt(np.sum((samples - instance)**2, axis=1))
      
        return samples, distances, labels
  
    def _train_explainable_model(self,
                                instance: np.ndarray,
                                samples: np.ndarray,
                                distances: np.ndarray,
                                labels: np.ndarray,
                                top_labels: int) -> Dict[str, Any]:
        """训练可解释模型"""
        # 简化实现:使用决策树
        explanations = {}
      
        for label in range(top_labels):
            # 选择目标类别的样本
            label_mask = (labels == label)
            if not np.any(label_mask):
                continue
          
            # 计算权重(基于距离)
            weights = np.exp(-(distances**2) / (2 * self.kernel_width**2))
          
            # 训练决策树
            tree = DecisionTreeClassifier(max_depth=3)
            tree.fit(samples, labels == label, sample_weight=weights)
          
            # 提取规则
            rules = self._extract_rules(tree)
          
            explanations[label] = {
                "label": self.class_names[label],
                "rules": rules,
                "feature_importance": self._get_feature_importance(tree)
            }
      
        return explanations
  
    def _extract_rules(self, tree: DecisionTreeClassifier) -> List[str]:
        """提取决策树规则"""
        # 简化实现
        return ["Rule 1", "Rule 2"]
  
    def _get_feature_importance(self, tree: DecisionTreeClassifier) -> List[Dict[str, float]]:
        """获取特征重要性"""
        importance = tree.feature_importances_
        feature_importance = [
            {"feature": self.feature_names[i], "importance": float(importance[i])}
            for i in range(len(self.feature_names))
        ]
        feature_importance.sort(key=lambda x: x["importance"], reverse=True)
        return feature_importance

class ExplainableAgent:
    """可解释Agent"""
  
    def __init__(self,
                 model: nn.Module,
                 feature_names: List[str],
                 class_names: List[str],
                 explanation_style: str = "detailed"):
        """
        初始化可解释Agent
      
        Args:
            model: 被包装的模型
            feature_names: 特征名称
            class_names: 类别名称
            explanation_style: 解释风格
        """
        self.model = model
        self.feature_names = feature_names
        self.class_names = class_names
        self.explanation_style = explanation_style
        self.explanation_generator = ExplanationGenerator(
            model, feature_names, class_names
        )
        self.local_interpretability = LocalInterpretableModel(
            model, feature_names, class_names
        )
        self.explanation_history = []
  
    def predict_with_explanation(self, input_data: np.ndarray) -> Dict[str, Any]:
        """
        预测并提供解释
      
        Args:
            input_data: 输入数据
          
        Returns:
            预测结果和解释
        """
        # 获取模型预测
        with torch.no_grad():
            inputs = torch.FloatTensor(input_data)
            outputs = self.model(inputs)
            probabilities = torch.softmax(outputs, dim=1)
            _, predicted = torch.max(probabilities, 1)
      
        # 生成解释
        explanation = self.explanation_generator.generate_explanation(
            input_data,
            predicted.item(),
            probabilities[0, predicted.item()].item()
        )
      
        # 本地可解释性(可选)
        local_explanation = None
        if self.explanation_style == "detailed":
            local_explanation = self.local_interpretability.explain_instance(
                input_data, top_labels=1
            )
      
        # 创建完整结果
        result = {
            "prediction": predicted.item(),
            "prediction_label": self.class_names[predicted.item()],
            "prediction_prob": probabilities[0, predicted.item()].item(),
            "explanation": explanation,
            "local_explanation": local_explanation,
            "timestamp": time.time()
        }
      
        # 记录历史
        self.explanation_history.append(result)
      
        return result
  
    def get_explanation_history(self) -> List[Dict]:
        """获取解释历史"""
        return self.explanation_history

# 示例使用可解释Agent
if __name__ == "__main__":
    # 创建模拟模型
    class SimpleClassifier(nn.Module):
        def __init__(self, input_dim: int, num_classes: int):
            super().__init__()
            self.fc = nn.Linear(input_dim, num_classes)
      
        def forward(self, x):
            return self.fc(x)
  
    # 创建可解释Agent
    feature_names = ["age", "income", "credit_score", "debt_ratio", "employment_years"]
    class_names = ["low_risk", "medium_risk", "high_risk"]
  
    model = SimpleClassifier(input_dim=len(feature_names), num_classes=len(class_names))
    agent = ExplainableAgent(
        model=model,
        feature_names=feature_names,
        class_names=class_names,
        explanation_style="detailed"
    )
  
    # 模拟输入数据
    input_data = np.array([[45, 85000, 720, 0.35, 12]])
  
    # 预测并获取解释
    print("=== Explainable AI Example ===")
    result = agent.predict_with_explanation(input_data)
  
    # 显示结果
    print(f"Prediction: {result['prediction_label']} ({result['prediction_prob']:.1%} confidence)")
  
    print("\nFeature Importance:")
    for i, feat in enumerate(result["explanation"]["feature_importance"][:3]):
        print(f"{i+1}. {feat['feature']}: {feat['importance']:.1%}")
  
    print("\nNatural Language Explanation:")
    print(result["explanation"]["natural_language"])
  
    # 显示对比案例
    contrastive = result["explanation"]["contrastive_example"]
    print("\nContrastive Example:")
    print(f"If {contrastive['modified_feature']} were lower ({contrastive['modified_value']:.2f} instead of {contrastive['original_value']:.2f}), "
          f"the prediction would change to: {class_names[contrastive['contrastive_prediction']]} "
          f"({contrastive['contrastive_prob']:.1%} confidence)")

# 高级可解释性:基于因果推理的解释
class CausalExplanationGenerator:
    """基于因果推理的解释生成器"""
  
    def __init__(self, 
                 model: nn.Module,
                 feature_names: List[str],
                 class_names: List[str],
                 causal_graph: Dict[str, List[str]]):
        """
        初始化因果解释生成器
      
        Args:
            model: 被解释的模型
            feature_names: 特征名称
            class_names: 类别名称
            causal_graph: 因果图(特征之间的因果关系)
        """
        self.model = model
        self.feature_names = feature_names
        self.class_names = class_names
        self.causal_graph = causal_graph
        self.structural_equation_models = self._build_structural_equation_models()
  
    def _build_structural_equation_models(self) -> Dict[str, Any]:
        """构建结构方程模型"""
        # 实际应用中应从数据学习SEM
        # 这里简化为模拟
        sems = {}
        for feature in self.feature_names:
            # 简单线性模型
            sems[feature] = {
                "parents": self.causal_graph.get(feature, []),
                "coefficients": {parent: np.random.uniform(0.1, 0.5) 
                                for parent in self.causal_graph.get(feature, [])},
                "noise_std": 0.1
            }
        return sems
  
    def generate_causal_explanation(self, 
                                  input_data: np.ndarray,
                                  prediction: int) -> Dict[str, Any]:
        """
        生成因果解释
      
        Args:
            input_data: 输入数据
            prediction: 预测类别
          
        Returns:
            因果解释
        """
        # 识别关键因果路径
        causal_paths = self._identify_causal_paths(input_data, prediction)
      
        # 生成干预分析
        intervention_analysis = self._perform_intervention_analysis(input_data, prediction)
      
        # 生成自然语言因果解释
        natural_language = self._generate_causal_natural_language(
            input_data, prediction, causal_paths, intervention_analysis
        )
      
        return {
            "causal_paths": causal_paths,
            "intervention_analysis": intervention_analysis,
            "natural_language": natural_language,
            "prediction": prediction
        }
  
    def _identify_causal_paths(self, 
                              input_data: np.ndarray,
                              prediction: int) -> List[Dict[str, Any]]:
        """识别关键因果路径"""
        # 简化实现
        paths = []
      
        # 检查每个特征对预测的影响
        for i, feature in enumerate(self.feature_names):
            # 模拟干预:将特征设置为均值
            intervened_data = input_data.copy()
            intervened_data[0, i] = 0  # 假设特征已标准化
          
            # 获取干预后的预测
            with torch.no_grad():
                intervened_pred = self.model(torch.FloatTensor(intervened_data))
                _, intervened_class = torch.max(intervened_pred, 1)
          
            # 如果干预改变预测,说明该特征在因果路径上
            if intervened_class.item() != prediction:
                # 估计影响大小
                impact = abs(
                    torch.softmax(intervened_pred, dim=1)[0, prediction].item() - 
                    torch.softmax(self.model(torch.FloatTensor(input_data)), dim=1)[0, prediction].item()
                )
              
                paths.append({
                    "feature": feature,
                    "impact": float(impact),
                    "intervention_effect": "changed_prediction"
                })
      
        # 按影响排序
        paths.sort(key=lambda x: x["impact"], reverse=True)
      
        return paths
  
    def _perform_intervention_analysis(self, 
                                     input_data: np.ndarray,
                                     prediction: int) -> Dict[str, Any]:
        """执行干预分析"""
        # 简化实现
        analysis = {
            "sufficient_conditions": [],
            "necessary_conditions": []
        }
      
        # 检查必要条件
        for i, feature in enumerate(self.feature_names):
            # 检查如果特征值较低,预测是否会改变
            low_value_data = input_data.copy()
            low_value_data[0, i] *= 0.5
          
            with torch.no_grad():
                low_pred = self.model(torch.FloatTensor(low_value_data))
                _, low_class = torch.max(low_pred, 1)
          
            if low_class.item() != prediction:
                analysis["necessary_conditions"].append({
                    "feature": feature,
                    "threshold": float(input_data[0, i] * 0.5),
                    "effect": "prediction changes below this threshold"
                })
      
        return analysis
  
    def _generate_causal_natural_language(self,
                                        input_data: np.ndarray,
                                        prediction: int,
                                        causal_paths: List[Dict],
                                        intervention_analysis: Dict) -> str:
        """生成因果自然语言解释"""
        explanation = (
            f"The model predicted '{self.class_names[prediction]}' primarily because of "
        )
      
        # 添加关键因果路径
        if causal_paths:
            top_paths = causal_paths[:2]
            path_descriptions = []
            for path in top_paths:
                value = input_data[0, self.feature_names.index(path["feature"])]
                path_descriptions.append(
                    f"{path['feature']} is high ({value:.2f}), which directly contributes to this prediction"
                )
            explanation += " and ".join(path_descriptions) + ". "
        else:
            explanation += "multiple factors working together. "
      
        # 添加必要条件
        if intervention_analysis["necessary_conditions"]:
            necessary = intervention_analysis["necessary_conditions"][0]
            explanation += (
                f"For this prediction to hold, {necessary['feature']} must be above {necessary['threshold']:.2f}; "
                f"if it were lower, the prediction would change."
            )
      
        return explanation

# 示例使用因果解释
if __name__ == "__main__":
    print("\n\n=== Causal Explanation Example ===")
  
    # 定义因果图
    causal_graph = {
        "credit_score": ["income", "employment_years"],
        "debt_ratio": ["income", "credit_score"],
        "risk_prediction": ["credit_score", "debt_ratio", "age"]
    }
  
    # 创建因果解释生成器
    causal_explainer = CausalExplanationGenerator(
        model=model,
        feature_names=feature_names,
        class_names=class_names,
        causal_graph=causal_graph
    )
  
    # 生成因果解释
    with torch.no_grad():
        outputs = model(torch.FloatTensor(input_data))
        _, predicted = torch.max(outputs, 1)
  
    causal_explanation = causal_explainer.generate_causal_explanation(
        input_data, predicted.item()
    )
  
    # 显示结果
    print(f"Causal Explanation for {class_names[predicted.item()]} prediction:")
    print(causal_explanation["natural_language"])
  
    print("\nKey Causal Paths:")
    for i, path in enumerate(causal_explanation["causal_paths"][:3]):
        print(f"{i+1}. {path['feature']} (impact: {path['impact']:.2%})")
  
    print("\nNecessary Conditions:")
    for cond in causal_explanation["intervention_analysis"]["necessary_conditions"]:
        print(f"- {cond['feature']} > {cond['threshold']:.2f}")

未来展望与总结

Agent技术的演进路径

大模型Agent技术正处于快速发展阶段,未来几年将沿着以下几个关键方向演进:

  1. 认知能力提升:从当前的模式匹配向真正的推理和问题解决能力发展
  2. 多模态融合:无缝整合文本、图像、音频等多种模态信息
  3. 自主学习能力:减少对人工标注数据的依赖,实现持续自主学习
  4. 人机协作优化:建立更自然、高效的人机协作模式

以下是一个预测性框架,展示了Agent技术的演进路径和关键技术里程碑:

python 复制代码
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from typing import Dict, List, Tuple, Any, Optional
import pandas as pd

class TechnologyRoadmap:
    """技术路线图"""
  
    def __init__(self, start_year: int = 2023):
        self.start_year = start_year
        self.technology_areas = [
            "基础模型能力",
            "推理与规划",
            "记忆与学习",
            "多模态理解",
            "人机协作",
            "安全与伦理"
        ]
        self.milestones = []
  
    def add_milestone(self, 
                     year: int,
                     area: str,
                     title: str,
                     description: str,
                     impact: float,
                     feasibility: float):
        """
        添加技术里程碑
      
        Args:
            year: 预期实现年份
            area: 技术领域
            title: 里程碑标题
            description: 详细描述
            impact: 影响力(0-1)
            feasibility: 可行性(0-1)
        """
        if area not in self.technology_areas:
            raise ValueError(f"Unknown technology area: {area}")
      
        self.milestones.append({
            "year": year,
            "area": area,
            "title": title,
            "description": description,
            "impact": impact,
            "feasibility": feasibility
        })
  
    def generate_roadmap(self) -> pd.DataFrame:
        """生成技术路线图数据"""
        data = {
            "Year": [],
            "Area": [],
            "Title": [],
            "Impact": [],
            "Feasibility": []
        }
      
        for milestone in self.milestones:
            data["Year"].append(milestone["year"])
            data["Area"].append(milestone["area"])
            data["Title"].append(milestone["title"])
            data["Impact"].append(milestone["impact"])
            data["Feasibility"].append(milestone["feasibility"])
      
        return pd.DataFrame(data)
  
    def visualize_roadmap(self, save_path: Optional[str] = None):
        """可视化技术路线图"""
        df = self.generate_roadmap()
      
        # 创建图表
        plt.figure(figsize=(14, 8))
      
        # 为每个技术领域设置颜色
        area_colors = {
            "基础模型能力": "#1f77b4",
            "推理与规划": "#ff7f0e",
            "记忆与学习": "#2ca02c",
            "多模态理解": "#d62728",
            "人机协作": "#9467bd",
            "安全与伦理": "#8c564b"
        }
      
        # 绘制散点图
        for area in self.technology_areas:
            area_data = df[df["Area"] == area]
            plt.scatter(
                area_data["Year"],
                area_data["Impact"],
                s=area_data["Feasibility"] * 1000,
                alpha=0.6,
                color=area_colors[area],
                label=area,
                edgecolor="black"
            )
      
        # 添加标签
        for i, row in df.iterrows():
            plt.annotate(
                row["Title"],
                (row["Year"], row["Impact"]),
                xytext=(5, 5),
                textcoords="offset points"
            )
      
        # 设置图表属性
        plt.title("大模型Agent技术发展路线图 (2023-2030)", fontsize=16)
        plt.xlabel("年份", fontsize=12)
        plt.ylabel("技术影响力", fontsize=12)
        plt.grid(True, linestyle="--", alpha=0.7)
        plt.legend(title="技术领域")
        plt.xticks(range(self.start_year, 2031))
        plt.ylim(0, 1.1)
      
        # 调整布局
        plt.tight_layout()
      
        # 保存或显示
        if save_path:
            plt.savefig(save_path, dpi=300, bbox_inches="tight")
        else:
            plt.show()

# 创建技术路线图
roadmap = TechnologyRoadmap(start_year=2023)

# 添加基础模型能力里程碑
roadmap.add_milestone(
    year=2023,
    area="基础模型能力",
    title="百亿参数模型普及",
    description="百亿参数级大模型成为行业标准,支持基本对话和任务执行",
    impact=0.7,
    feasibility=1.0
)
roadmap.add_milestone(
    year=2024,
    area="基础模型能力",
    title="多任务统一模型",
    description="单一模型支持多种任务类型,减少模型切换开销",
    impact=0.8,
    feasibility=0.9
)
roadmap.add_milestone(
    year=2025,
    area="基础模型能力",
    title="高效推理架构",
    description="新型推理架构使大模型推理成本降低50%",
    impact=0.9,
    feasibility=0.85
)
roadmap.add_milestone(
    year=2027,
    area="基础模型能力",
    title="万亿参数实用化",
    description="万亿参数模型在特定领域实现商业化应用",
    impact=1.0,
    feasibility=0.6
)

# 添加推理与规划里程碑
roadmap.add_milestone(
    year=2023,
    area="推理与规划",
    title="简单任务规划",
    description="Agent能够执行预定义的简单任务序列",
    impact=0.5,
    feasibility=1.0
)
roadmap.add_milestone(
    year=2024,
    area="推理与规划",
    title="动态任务规划",
    description="Agent能够根据环境变化动态调整任务规划",
    impact=0.7,
    feasibility=0.85
)
roadmap.add_milestone(
    year=2026,
    area="推理与规划",
    title="复杂问题分解",
    description="Agent能够将复杂问题分解为可执行的子任务",
    impact=0.9,
    feasibility=0.7
)
roadmap.add_milestone(
    year=2028,
    area="推理与规划",
    title="创造性问题解决",
    description="Agent能够提出创新性解决方案,超越预定义模式",
    impact=1.0,
    feasibility=0.5
)

# 添加记忆与学习里程碑
roadmap.add_milestone(
    year=2023,
    area="记忆与学习",
    title="短期对话记忆",
    description="Agent能够维持短时对话上下文",
    impact=0.4,
    feasibility=1.0
)
roadmap.add_milestone(
    year=2024,
    area="记忆与学习",
    title="长期个性化记忆",
    description="Agent能够学习并应用用户偏好和历史",
    impact=0.7,
    feasibility=0.8
)
roadmap.add_milestone(
    year=2025,
    area="记忆与学习",
    title="跨会话知识迁移",
    description="Agent能够在不同会话间迁移学习到的知识",
    impact=0.8,
    feasibility=0.75
)
roadmap.add_milestone(
    year=2027,
    area="记忆与学习",
    title="自主知识构建",
    description="Agent能够从交互中自主构建知识体系",
    impact=0.95,
    feasibility=0.6
)

# 添加多模态理解里程碑
roadmap.add_milestone(
    year=2023,
    area="多模态理解",
    title="文本-图像基础理解",
    description="Agent能够理解基本的图文关联",
    impact=0.5,
    feasibility=1.0
)
roadmap.add_milestone(
    year=2024,
    area="多模态理解",
    title="跨模态推理",
    description="Agent能够在不同模态间进行推理和转换",
    impact=0.75,
    feasibility=0.85
)
roadmap.add_milestone(
    year=2026,
    area="多模态理解",
    title="实时多模态交互",
    description="Agent能够处理实时视频、音频等流式多模态输入",
    impact=0.9,
    feasibility=0.7
)
roadmap.add_milestone(
    year=2029,
    area="多模态理解",
    title="全感官交互",
    description="Agent能够整合视觉、听觉、触觉等多感官信息",
    impact=1.0,
    feasibility=0.4
)

# 添加人机协作里程碑
roadmap.add_milestone(
    year=2023,
    area="人机协作",
    title="指令跟随",
    description="Agent能够理解和执行明确指令",
    impact=0.4,
    feasibility=1.0
)
roadmap.add_milestone(
    year=2024,
    area="人机协作",
    title="意图理解",
    description="Agent能够推断用户隐含意图",
    impact=0.65,
    feasibility=0.9
)
roadmap.add_milestone(
    year=2025,
    area="人机协作",
    title="主动协作",
    description="Agent能够主动提出建议并参与决策",
    impact=0.8,
    feasibility=0.8
)
roadmap.add_milestone(
    year=2027,
    area="人机协作",
    title="无缝人机团队",
    description="Agent成为高效人机协作团队的自然成员",
    impact=0.95,
    feasibility=0.65
)

# 添加安全与伦理里程碑
roadmap.add_milestone(
    year=2023,
    area="安全与伦理",
    title="基础内容过滤",
    description="Agent具备基本有害内容过滤能力",
    impact=0.3,
    feasibility=1.0
)
roadmap.add_milestone(
    year=2024,
    area="安全与伦理",
    title="偏见检测",
    description="Agent能够检测并报告潜在偏见",
    impact=0.55,
    feasibility=0.85
)
roadmap.add_milestone(
    year=2026,
    area="安全与伦理",
    title="可验证决策",
    description="Agent提供可验证的决策依据和解释",
    impact=0.75,
    feasibility=0.7
)
roadmap.add_milestone(
    year=2028,
    area="安全与伦理",
    title="伦理对齐框架",
    description="Agent内置全面的伦理决策框架",
    impact=0.9,
    feasibility=0.55
)

# 生成并可视化路线图
print("=== 大模型Agent技术发展路线图 ===")
roadmap_df = roadmap.generate_roadmap()
print(roadmap_df.sort_values(['Year', 'Impact'], ascending=[True, False]))

# 可视化(在实际环境中会生成图表)
try:
    roadmap.visualize_roadmap()
    print("\n技术路线图可视化已生成。")
except Exception as e:
    print(f"可视化生成失败: {e}")
    print("在支持matplotlib的环境中运行可查看可视化图表。")

# 高级分析:技术发展预测模型
class TechnologyAdoptionModel:
    """技术采纳预测模型"""
  
    def __init__(self, 
                 innovation_rate: float = 0.1,
                 imitation_rate: float = 0.3,
                 max_adopters: float = 1.0):
        """
        初始化技术采纳模型
      
        Args:
            innovation_rate: 创新率(早期采用者比例)
            imitation_rate: 模仿率(社会影响系数)
            max_adopters: 最大采用者比例
        """
        self.innovation_rate = innovation_rate
        self.imitation_rate = imitation_rate
        self.max_adopters = max_adopters
  
    def bass_model(self, t: float) -> float:
        """
        Bass扩散模型
      
        Args:
            t: 时间(年)
          
        Returns:
            采用者比例
        """
        p = self.innovation_rate
        q = self.imitation_rate
      
        return self.max_adopters * (
            (p + q)**2 / p * np.exp(-(p + q) * t) / 
            (q / p * np.exp(-(p + q) * t) + 1)**2
        )
  
    def cumulative_adoption(self, t: float) -> float:
        """
        累计采纳率
      
        Args:
            t: 时间(年)
          
        Returns:
            累计采纳比例
        """
        p = self.innovation_rate
        q = self.imitation_rate
      
        return self.max_adopters * (
            1 - np.exp(-(p + q) * t)
        ) / (
            1 + (q / p) * np.exp(-(p + q) * t)
        )

class AgentTechnologyForecast:
    """Agent技术预测"""
  
    def __init__(self):
        # 为不同技术领域设置Bass模型参数
        self.technology_models = {
            "基础模型能力": TechnologyAdoptionModel(0.03, 0.35, 0.95),
            "推理与规划": TechnologyAdoptionModel(0.02, 0.25, 0.85),
            "记忆与学习": TechnologyAdoptionModel(0.025, 0.3, 0.9),
            "多模态理解": TechnologyAdoptionModel(0.02, 0.28, 0.8),
            "人机协作": TechnologyAdoptionModel(0.015, 0.22, 0.75),
            "安全与伦理": TechnologyAdoptionModel(0.01, 0.18, 0.7)
        }
  
    def forecast_adoption(self, years: List[int]) -> pd.DataFrame:
        """
        预测技术采纳率
      
        Args:
            years: 预测年份列表
          
        Returns:
            采纳率数据框
        """
        data = {"Year": []}
        for area in self.technology_models.keys():
            data[area] = []
      
        for year in years:
            # 相对于2023年的时间
            t = year - 2023
          
            data["Year"].append(year)
            for area, model in self.technology_models.items():
                data[area].append(model.cumulative_adoption(t))
      
        return pd.DataFrame(data)
  
    def identify_inflection_points(self) -> Dict[str, int]:
        """
        识别拐点年份(采纳率增长最快的时间)
      
        Returns:
            拐点年份字典
        """
        inflection_points = {}
      
        for area, model in self.technology_models.items():
            # 找到二阶导数为零的点(近似)
            max_growth = 0
            inflection_year = 2023
          
            for t in range(0, 15):
                # 一阶导数(近似)
                adoption_t = model.bass_model(t)
                adoption_t1 = model.bass_model(t + 0.1)
                growth = (adoption_t1 - adoption_t) / 0.1
              
                if growth > max_growth:
                    max_growth = growth
                    inflection_year = 2023 + t
          
            inflection_points[area] = inflection_year
      
        return inflection_points

# 示例技术预测
if __name__ == "__main__":
    print("\n\n=== Agent技术采纳预测 ===")
  
    # 创建预测模型
    forecast = AgentTechnologyForecast()
  
    # 预测2023-2030年采纳率
    adoption_df = forecast.forecast_adoption(list(range(2023, 2031)))
    print("技术采纳率预测 (%):")
    print(adoption_df.set_index('Year').multiply(100).round(1))
  
    # 识别拐点
    inflection_points = forecast.identify_inflection_points()
    print("\n技术拐点预测(采纳增速最快的年份):")
    for area, year in inflection_points.items():
        print(f"{area}: {year}")
  
    # 可视化预测(在支持matplotlib的环境中)
    try:
        plt.figure(figsize=(12, 8))
        for area in forecast.technology_models.keys():
            plt.plot(adoption_df["Year"], adoption_df[area] * 100, label=area, marker='o')
      
        plt.title("大模型Agent技术采纳率预测 (2023-2030)", fontsize=16)
        plt.xlabel("年份", fontsize=12)
        plt.ylabel("采纳率 (%)", fontsize=12)
        plt.grid(True, linestyle="--", alpha=0.7)
        plt.legend()
        plt.tight_layout()
      
        print("\n技术采纳预测可视化已生成。")
    except Exception as e:
        print(f"可视化生成失败: {e}")

结论与建议

大模型Agent技术正处于从理论研究向实际应用的关键转折点。通过对当前技术发展态势的分析,我们可以得出以下结论和建议:

  1. 技术成熟度评估

    • 基础模型能力已相对成熟,但推理与规划、记忆与学习等领域仍有较大提升空间
    • 多模态理解和人机协作是下一阶段的关键突破口
    • 安全与伦理方面的技术发展相对滞后,需要加速推进
  2. 行业应用建议

    • 金融、医疗、制造等高价值领域应优先部署Agent技术
    • 从辅助决策开始,逐步过渡到自主决策
    • 建立人机协作框架,充分发挥人类和Agent的各自优势
  3. 研发重点建议

    • 加强Agent的记忆管理和长期学习能力
    • 开发更高效的推理和规划算法
    • 构建全面的评估框架,衡量Agent的综合能力
    • 深入研究Agent的安全性和可解释性
  4. 社会影响与责任

    • 建立Agent技术的伦理准则和监管框架
    • 重视技术对就业市场的影响,提前规划转型
    • 促进技术普惠,避免数字鸿沟扩大

以下是一个Agent技术评估框架的实现,可以帮助组织评估和选择适合的Agent解决方案:

python 复制代码
import numpy as np
import pandas as pd
from typing import Dict, List, Tuple, Any, Optional

class AgentCapabilityAssessment:
    """Agent能力评估框架"""
  
    def __init__(self):
        # 评估维度和权重
        self.dimensions = {
            "基础能力": 0.2,
            "推理与规划": 0.25,
            "记忆与学习": 0.15,
            "多模态理解": 0.15,
            "人机协作": 0.15,
            "安全与伦理": 0.1
        }
      
        # 每个维度的具体指标
        self.metrics = {
            "基础能力": [
                ("语言理解", 0.3),
                ("任务执行", 0.4),
                ("响应速度", 0.3)
            ],
            "推理与规划": [
                ("逻辑推理", 0.3),
                ("问题分解", 0.3),
                ("动态规划", 0.4)
            ],
            "记忆与学习": [
                ("短期记忆", 0.3),
                ("长期记忆", 0.4),
                ("持续学习", 0.3)
            ],
            "多模态理解": [
                ("跨模态关联", 0.4),
                ("实时处理", 0.3),
                ("多模态生成", 0.3)
            ],
            "人机协作": [
                ("意图理解", 0.4),
                ("主动协作", 0.3),
                ("自然交互", 0.3)
            ],
            "安全与伦理": [
                ("内容安全", 0.3),
                ("偏见控制", 0.3),
                ("可解释性", 0.4)
            ]
        }
  
    def assess_agent(self, agent_scores: Dict[str, Dict[str, float]]) -> Dict[str, Any]:
        """
        评估Agent能力
      
        Args:
            agent_scores: Agent在各指标上的得分
          
        Returns:
            评估结果
        """
        # 计算各维度得分
        dimension_scores = {}
        for dimension, metrics in self.metrics.items():
            score = 0
            for metric, weight in metrics:
                if dimension in agent_scores and metric in agent_scores[dimension]:
                    score += agent_scores[dimension][metric] * weight
            dimension_scores[dimension] = score
      
        # 计算综合得分
        total_score = sum(
            dimension_scores[dimension] * self.dimensions[dimension]
            for dimension in self.dimensions
        )
      
        # 生成评估报告
        report = {
            "dimension_scores": dimension_scores,
            "total_score": total_score,
            "strengths": self._identify_strengths(dimension_scores),
            "weaknesses": self._identify_weaknesses(dimension_scores),
            "recommendations": self._generate_recommendations(dimension_scores)
        }
      
        return report
  
    def _identify_strengths(self, scores: Dict[str, float]) -> List[str]:
        """识别优势"""
        avg = np.mean(list(scores.values()))
        return [dim for dim, score in scores.items() if score > avg + 0.1]
  
    def _identify_weaknesses(self, scores: Dict[str, float]) -> List[str]:
        """识别劣势"""
        avg = np.mean(list(scores.values()))
        return [dim for dim, score in scores.items() if score < avg - 0.1]
  
    def _generate_recommendations(self, scores: Dict[str, float]) -> List[str]:
        """生成建议"""
        recommendations = []
      
        # 针对薄弱环节提供建议
        weaknesses = self._identify_weaknesses(scores)
        for weakness in weaknesses:
            if weakness == "推理与规划":
                recommendations.append(
                    "加强推理能力训练,引入更多逻辑推理和问题分解任务"
                )
            elif weakness == "记忆与学习":
                recommendations.append(
                    "优化记忆管理系统,增强长期记忆和知识迁移能力"
                )
            elif weakness == "安全与伦理":
                recommendations.append(
                    "强化安全机制,增加偏见检测和可解释性功能"
                )
      
        # 如果综合得分高,建议高级应用场景
        if sum(scores[dim] * self.dimensions[dim] for dim in self.dimensions) > 0.8:
            recommendations.append(
                "该Agent适合部署在高价值、高复杂度场景,如医疗诊断或金融决策"
            )
      
        return recommendations

class AgentSelectionAdvisor:
    """Agent选择顾问"""
  
    def __init__(self):
        # 行业需求配置
        self.industry_requirements = {
            "金融": {
                "基础能力": 0.25,
                "推理与规划": 0.3,
                "安全与伦理": 0.25,
                "记忆与学习": 0.1,
                "多模态理解": 0.05,
                "人机协作": 0.05
            },
            "医疗": {
                "基础能力": 0.2,
                "推理与规划": 0.25,
                "安全与伦理": 0.3,
                "记忆与学习": 0.15,
                "多模态理解": 0.05,
                "人机协作": 0.05
            },
            "制造": {
                "基础能力": 0.2,
                "推理与规划": 0.25,
                "多模态理解": 0.2,
                "记忆与学习": 0.15,
                "人机协作": 0.15,
                "安全与伦理": 0.05
            },
            "客服": {
                "基础能力": 0.25,
                "人机协作": 0.3,
                "多模态理解": 0.2,
                "记忆与学习": 0.15,
                "推理与规划": 0.05,
                "安全与伦理": 0.05
            }
        }
  
    def recommend_agents(self,
                        agents: List[Dict],
                        industry: str,
                        budget: float) -> List[Dict]:
        """
        推荐适合的Agent
      
        Args:
            agents: Agent列表
            industry: 行业
            budget: 预算
          
        Returns:
            推荐结果
        """
        if industry not in self.industry_requirements:
            raise ValueError(f"Unsupported industry: {industry}")
      
        # 计算每个Agent的匹配度
        recommendations = []
        for agent in agents:
            # 获取Agent的评估结果
            assessment = agent["assessment"]
            scores = assessment["dimension_scores"]
          
            # 计算行业匹配度
            industry_weights = self.industry_requirements[industry]
            match_score = sum(
                scores[dimension] * industry_weights[dimension]
                for dimension in industry_weights
            )
          
            # 考虑预算
            within_budget = agent["cost"] <= budget
          
            recommendations.append({
                "agent_id": agent["id"],
                "agent_name": agent["name"],
                "match_score": match_score,
                "within_budget": within_budget,
                "strengths": assessment["strengths"],
                "weaknesses": assessment["weaknesses"],
                "cost": agent["cost"]
            })
      
        # 按匹配度排序
        recommendations.sort(key=lambda x: x["match_score"], reverse=True)
      
        return recommendations

# 示例Agent评估与选择
if __name__ == "__main__":
    # 创建评估框架
    assessment = AgentCapabilityAssessment()
  
    # 模拟Agent1(金融领域)
    agent1_scores = {
        "基础能力": {
            "语言理解": 0.85,
            "任务执行": 0.9,
            "响应速度": 0.8
        },
        "推理与规划": {
            "逻辑推理": 0.92,
            "问题分解": 0.85,
            "动态规划": 0.9
        },
        "记忆与学习": {
            "短期记忆": 0.8,
            "长期记忆": 0.75,
            "持续学习": 0.7
        },
        "多模态理解": {
            "跨模态关联": 0.6,
            "实时处理": 0.5,
            "多模态生成": 0.55
        },
        "人机协作": {
            "意图理解": 0.75,
            "主动协作": 0.7,
            "自然交互": 0.65
        },
        "安全与伦理": {
            "内容安全": 0.95,
            "偏见控制": 0.85,
            "可解释性": 0.8
        }
    }
  
    # 评估Agent1
    print("=== Agent能力评估 ===")
    agent1_assessment = assessment.assess_agent(agent1_scores)
  
    # 显示评估结果
    print(f"综合得分: {agent1_assessment['total_score']:.2%}")
  
    print("\n维度得分:")
    for dimension, score in agent1_assessment["dimension_scores"].items():
        print(f"{dimension}: {score:.2%}")
  
    print("\n优势领域:")
    for strength in agent1_assessment["strengths"]:
        print(f"- {strength}")
  
    print("\n待改进领域:")
    for weakness in agent1_assessment["weaknesses"]:
        print(f"- {weakness}")
  
    print("\n建议:")
    for recommendation in agent1_assessment["recommendations"]:
        print(f"- {recommendation}")
  
    # Agent选择示例
    print("\n\n=== Agent选择建议 ===")
  
    # 创建选择顾问
    advisor = AgentSelectionAdvisor()
  
    # 模拟多个Agent
    agents = [
        {
            "id": "A1",
            "name": "金融智能Agent",
            "assessment": agent1_assessment,
            "cost": 150000
        },
        {
            "id": "A2",
            "name": "全能Agent",
            "assessment": assessment.assess_agent({
                "基础能力": {"语言理解": 0.9, "任务执行": 0.85, "响应速度": 0.85},
                "推理与规划": {"逻辑推理": 0.8, "问题分解": 0.75, "动态规划": 0.8},
                "记忆与学习": {"短期记忆": 0.85, "长期记忆": 0.8, "持续学习": 0.85},
                "多模态理解": {"跨模态关联": 0.9, "实时处理": 0.85, "多模态生成": 0.9},
                "人机协作": {"意图理解": 0.8, "主动协作": 0.85, "自然交互": 0.9},
                "安全与伦理": {"content安全": 0.75, "偏见控制": 0.7, "可解释性": 0.7}
            }),
            "cost": 200000
        },
        {
            "id": "A3",
            "name": "客服专用Agent",
            "assessment": assessment.assess_agent({
                "基础能力": {"语言理解": 0.85, "任务执行": 0.8, "响应速度": 0.9},
                "推理与规划": {"逻辑推理": 0.65, "问题分解": 0.6, "动态规划": 0.65},
                "记忆与学习": {"短期记忆": 0.9, "长期记忆": 0.85, "持续学习": 0.8},
                "多模态理解": {"跨模态关联": 0.85, "实时处理": 0.8, "多模态生成": 0.85},
                "人机协作": {"意图理解": 0.9, "主动协作": 0.85, "自然交互": 0.95},
                "安全与伦理": {"content安全": 0.85, "偏见控制": 0.8, "可解释性": 0.75}
            }),
            "cost": 80000
        }
    ]
  
    # 为金融行业推荐Agent(预算20万)
    recommendations = advisor.recommend_agents(agents, "金融", 200000)
  
    print("金融行业Agent推荐 (预算: $200,000):")
    for i, rec in enumerate(recommendations):
        status = "✓ Within budget" if rec["within_budget"] else "✗ Over budget"
        print(f"{i+1}. {rec['agent_name']} (ID: {rec['agent_id']})")
        print(f"   匹配度: {rec['match_score']:.2%} | 成本: ${rec['cost']:,} | {status}")
        print(f"   优势: {', '.join(rec['strengths'])}")
        print(f"   待改进: {', '.join(rec['weaknesses'])}\n")

结语

大模型Agent技术代表了人工智能发展的新阶段,它不仅继承了大模型的强大语言理解和生成能力,还通过自主决策和任务执行能力,将AI的应用边界推向了新的高度。从金融风控到医疗诊断,从智能制造到客户服务,Agent技术正在各个领域展现其变革性潜力。

然而,技术发展也带来了新的挑战。如何确保Agent决策的公平性、透明度和安全性,如何平衡自动化与人类控制,如何应对技术对就业和社会结构的影响,这些都是我们需要认真思考的问题。

未来,随着技术的不断成熟和应用场景的拓展,我们期待看到更多负责任、可信赖、高效能的Agent系统出现,它们将成为人类工作和生活的重要伙伴,共同创造更智能、更高效、更人性化的未来。

正如我们所展示的,大模型Agent技术已经超越了简单的对话系统,发展成为能够理解、推理、规划和执行的智能体。通过结合先进的机器学习算法、丰富的知识库和精心设计的交互机制,这些Agent正在逐步实现从"工具"到"伙伴"的转变。

我们相信,在研究人员、工程师、开发者等大家的共同努力下,大模型Agent技术将为人类社会带来深远的积极影响,推动我们进入一个人机协作、智能增强的新时代。

码字不易,敬请给个关注或赞,我将持续输出更多高质量AI Agent相关原理与实现方案。

相关推荐
qq_262498117 分钟前
Datawhale AI夏令营---coze空间共学
人工智能
失散1314 分钟前
自然语言处理——04 注意力机制
人工智能·自然语言处理·注意力机制·seq2seq 架构
Freak嵌入式30 分钟前
一文速通 Python 并行计算:教程总结
开发语言·python
MiaoChuAI1 小时前
豆包AI PPT与秒出PPT对比评测:谁更适合你?
人工智能·powerpoint
hweiyu001 小时前
Python办公之Excel(openpyxl)、PPT(python-pptx)、Word(python-docx)
python·powerpoint·excel
java技术之路1 小时前
【免费AI文档助手开发实战系列】基于正则表达式的PDF脱敏python服务构建(一)
python·pdf·pymupdf·免费pdf脱敏
waynaqua1 小时前
FastAPI开发AI应用五:模型新增图片理解
python·openai·fastapi
waynaqua1 小时前
FastAPI开发AI应用四:新增豆包、kimi模型
python·fastapi·ai编程
%KT%1 小时前
简单聊聊多模态大语言模型MLLM
人工智能·语言模型·自然语言处理
唐某人丶1 小时前
教你如何用 JS 实现一个 Agent 系统(1)—— 认识 Agentic System
前端·人工智能