TradingAgents-CN

阿里百炼大模型配置指南

概述

阿里百炼(DashScope)是阿里云推出的大模型服务平台,提供通义千问系列模型。本指南详细介绍如何在 TradingAgents 中配置和使用阿里百炼大模型。

🎉 v0.1.6 重大更新

OpenAI兼容适配器

TradingAgents现在提供了全新的阿里百炼OpenAI兼容适配器,解决了之前的工具调用问题:

架构改进

为什么选择阿里百炼?

🇨🇳 国产化优势

💰 成本优势

🧠 技术优势

快速开始

1. 获取API密钥

步骤1: 注册阿里云账号

  1. 访问 阿里云官网
  2. 点击”免费注册”
  3. 完成账号注册和实名认证

步骤2: 开通百炼服务

  1. 访问 百炼控制台
  2. 点击”立即开通”
  3. 选择合适的套餐(建议先选择按量付费)

步骤3: 获取API密钥

  1. 在百炼控制台中,点击”API-KEY管理”
  2. 点击”创建新的API-KEY”
  3. 复制生成的API密钥

2. 配置环境变量

方法1: 使用环境变量

# Windows
set DASHSCOPE_API_KEY=your_dashscope_api_key_here
set FINNHUB_API_KEY=your_finnhub_api_key_here

# Linux/macOS
export DASHSCOPE_API_KEY=your_dashscope_api_key_here
export FINNHUB_API_KEY=your_finnhub_api_key_here

方法2: 使用 .env 文件

# 复制示例文件
cp .env.example .env

# 编辑 .env 文件,填入真实的API密钥
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FINNHUB_API_KEY=your_finnhub_api_key_here

3. 运行演示

# 使用专门的阿里百炼演示脚本
python demo_dashscope.py

支持的模型

通义千问系列模型

模型名称 模型ID 特点 适用场景
通义千问 Turbo qwen-turbo 快速响应,成本低 快速任务、日常对话
通义千问 Plus qwen-plus-latest 平衡性能和成本 复杂分析、专业任务
通义千问 Max qwen-max 最强性能 最复杂任务、高质量输出
通义千问 Max 长文本 qwen-max-longcontext 超长上下文 长文档分析、大量数据处理

推荐配置

经济型配置(成本优先)

config = {
    "llm_provider": "dashscope",
    "deep_think_llm": "qwen-plus-latest",      # 深度思考使用Plus
    "quick_think_llm": "qwen-turbo",    # 快速任务使用Turbo
    "max_debate_rounds": 1,             # 减少辩论轮次
}

性能型配置(质量优先)

config = {
    "llm_provider": "dashscope", 
    "deep_think_llm": "qwen-max",       # 深度思考使用Max
    "quick_think_llm": "qwen-plus",     # 快速任务使用Plus
    "max_debate_rounds": 2,             # 增加辩论轮次
}

长文本配置(处理大量数据)

config = {
    "llm_provider": "dashscope",
    "deep_think_llm": "qwen-max-longcontext",  # 使用长文本版本
    "quick_think_llm": "qwen-plus",
    "max_debate_rounds": 1,
}

配置示例

基础配置

from tradingagents.graph.trading_graph import TradingAgentsGraph
from tradingagents.default_config import DEFAULT_CONFIG

# 创建阿里百炼配置
config = DEFAULT_CONFIG.copy()
config["llm_provider"] = "dashscope"
config["deep_think_llm"] = "qwen-plus-latest"
config["quick_think_llm"] = "qwen-turbo"

# 初始化
ta = TradingAgentsGraph(debug=True, config=config)

# 运行分析
state, decision = ta.propagate("AAPL", "2024-05-10")
print(decision)

高级配置

# 自定义模型参数
config = DEFAULT_CONFIG.copy()
config.update({
    "llm_provider": "dashscope",
    "deep_think_llm": "qwen-max",
    "quick_think_llm": "qwen-plus-latest",
    "max_debate_rounds": 2,
    "max_risk_discuss_rounds": 2,
    "online_tools": True,
})

# 使用自定义参数创建LLM
from tradingagents.llm_adapters import ChatDashScope

custom_llm = ChatDashScope(
    model="qwen-max",
    temperature=0.1,
    max_tokens=3000,
    top_p=0.9
)

成本控制

典型使用成本

成本优化建议

  1. 合理选择模型: 根据任务复杂度选择合适的模型
  2. 控制辩论轮次: 减少 max_debate_rounds 参数
  3. 使用缓存: 启用数据缓存减少重复调用
  4. 监控使用量: 定期检查API调用量和费用

故障排除

常见问题

1. API密钥错误

Error: Invalid API key

解决方案: 检查API密钥是否正确,确认已开通百炼服务

2. 额度不足

Error: Insufficient quota

解决方案: 在百炼控制台充值或升级套餐

3. 网络连接问题

Error: Connection timeout

解决方案: 检查网络连接,确认可以访问阿里云服务

4. 模型不存在

Error: Model not found

解决方案: 检查模型名称是否正确,确认模型已开通

调试技巧

  1. 启用调试模式:
    ta = TradingAgentsGraph(debug=True, config=config)
    
  2. 检查API连接:
    import dashscope
    dashscope.api_key = "your_api_key"
       
    from dashscope import Generation
    response = Generation.call(
        model="qwen-turbo",
        messages=[{"role": "user", "content": "Hello"}]
    )
    print(response)
    

技术实现详解

OpenAI兼容适配器架构

1. 适配器类层次结构

# 新的OpenAI兼容适配器
from tradingagents.llm_adapters import ChatDashScopeOpenAI

# 继承关系
ChatDashScopeOpenAI -> ChatOpenAI -> BaseChatModel

2. 核心特性

3. 工具调用流程

用户请求 → LLM分析 → 尝试工具调用
    ↓
如果工具调用失败 → 强制调用数据工具 → 重新生成分析
    ↓
返回完整的基于真实数据的分析报告

与旧版本的对比

特性 旧版本 (ReAct模式) 新版本 (OpenAI兼容)
架构复杂度 复杂的ReAct循环 简单的标准模式
API调用次数 多次调用 单次调用
工具调用稳定性 不稳定 稳定
报告长度 30字符 完整报告
维护难度
性能 较慢 快速

最佳实践

1. 模型选择建议

# 推荐配置
config = {
    "llm_provider": "dashscope",
    "deep_think_llm": "qwen-plus-latest",  # 复杂分析
    "quick_think_llm": "qwen-turbo",       # 快速响应
}

2. 参数优化

# 最佳参数设置
llm = ChatDashScopeOpenAI(
    model="qwen-plus-latest",
    temperature=0.1,        # 降低随机性
    max_tokens=2000,        # 确保完整输出
)

3. 错误处理

系统自动处理以下情况:

开发者指南

1. 自定义适配器

from tradingagents.llm_adapters.openai_compatible_base import OpenAICompatibleBase

class CustomDashScopeAdapter(OpenAICompatibleBase):
    def __init__(self, **kwargs):
        super().__init__(
            provider_name="custom_dashscope",
            model=kwargs.get("model", "qwen-turbo"),
            api_key_env_var="DASHSCOPE_API_KEY",
            base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
            **kwargs
        )

2. 工具调用测试

from tradingagents.llm_adapters import ChatDashScopeOpenAI
from langchain_core.tools import tool

@tool
def test_tool(query: str) -> str:
    """测试工具"""
    return f"查询结果: {query}"

llm = ChatDashScopeOpenAI(model="qwen-turbo")
llm_with_tools = llm.bind_tools([test_tool])

# 测试工具调用
response = llm_with_tools.invoke([
    {"role": "user", "content": "请调用test_tool查询股票信息"}
])

总结

阿里百炼OpenAI兼容适配器的引入标志着TradingAgents在LLM集成方面的重大进步:

现在阿里百炼与DeepSeek、OpenAI等其他LLM在功能上完全一致,为用户提供了更好的选择和体验。

最佳实践

  1. 模型选择: 根据任务复杂度选择合适的模型
  2. 参数调优: 根据具体需求调整温度、最大token数等参数
  3. 错误处理: 实现适当的错误处理和重试机制
  4. 监控使用: 定期监控API使用量和成本
  5. 缓存策略: 合理使用缓存减少API调用

相关链接