阿里百炼(DashScope)是阿里云推出的大模型服务平台,提供通义千问系列模型。本指南详细介绍如何在 TradingAgents 中配置和使用阿里百炼大模型。
TradingAgents现在提供了全新的阿里百炼OpenAI兼容适配器,解决了之前的工具调用问题:
ChatDashScopeOpenAI
兼容适配器# 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
# 复制示例文件
cp .env.example .env
# 编辑 .env 文件,填入真实的API密钥
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FINNHUB_API_KEY=your_finnhub_api_key_here
# 使用专门的阿里百炼演示脚本
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
)
max_debate_rounds
参数Error: Invalid API key
解决方案: 检查API密钥是否正确,确认已开通百炼服务
Error: Insufficient quota
解决方案: 在百炼控制台充值或升级套餐
Error: Connection timeout
解决方案: 检查网络连接,确认可以访问阿里云服务
Error: Model not found
解决方案: 检查模型名称是否正确,确认模型已开通
ta = TradingAgentsGraph(debug=True, config=config)
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兼容适配器
from tradingagents.llm_adapters import ChatDashScopeOpenAI
# 继承关系
ChatDashScopeOpenAI -> ChatOpenAI -> BaseChatModel
用户请求 → LLM分析 → 尝试工具调用
↓
如果工具调用失败 → 强制调用数据工具 → 重新生成分析
↓
返回完整的基于真实数据的分析报告
特性 | 旧版本 (ReAct模式) | 新版本 (OpenAI兼容) |
---|---|---|
架构复杂度 | 复杂的ReAct循环 | 简单的标准模式 |
API调用次数 | 多次调用 | 单次调用 |
工具调用稳定性 | 不稳定 | 稳定 |
报告长度 | 30字符 | 完整报告 |
维护难度 | 高 | 低 |
性能 | 较慢 | 快速 |
# 推荐配置
config = {
"llm_provider": "dashscope",
"deep_think_llm": "qwen-plus-latest", # 复杂分析
"quick_think_llm": "qwen-turbo", # 快速响应
}
# 最佳参数设置
llm = ChatDashScopeOpenAI(
model="qwen-plus-latest",
temperature=0.1, # 降低随机性
max_tokens=2000, # 确保完整输出
)
系统自动处理以下情况:
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
)
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在功能上完全一致,为用户提供了更好的选择和体验。