feat: 重构项目结构并添加新功能
- 新增Cloudflare AutoRAG/Vectorize集成文档 - 实现Vertex AI记忆银行功能 - 重构项目目录结构,清理无用文件 - 更新README以反映最新架构 - 添加Google ADK集成测试脚本 - 完善需求文档和设计规范
This commit is contained in:
122
tests/test_alpha_vantage_meta.py
Normal file
122
tests/test_alpha_vantage_meta.py
Normal file
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Alpha Vantage API 测试脚本 - Meta (META) 财报和分析师评级
|
||||
"""
|
||||
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
def get_alpha_vantage_key():
|
||||
"""从环境变量获取 Alpha Vantage API Key"""
|
||||
api_key = os.getenv('ALPHA_VANTAGE_API_KEY')
|
||||
if not api_key:
|
||||
raise ValueError("未找到 ALPHA_VANTAGE_API_KEY 环境变量")
|
||||
return api_key
|
||||
|
||||
def get_company_overview(symbol, api_key):
|
||||
"""获取公司基本信息和财务概览"""
|
||||
url = f"https://www.alphavantage.co/query"
|
||||
params = {
|
||||
'function': 'OVERVIEW',
|
||||
'symbol': symbol,
|
||||
'apikey': api_key
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
return response.json()
|
||||
|
||||
def get_earnings_data(symbol, api_key):
|
||||
"""获取财报数据"""
|
||||
url = f"https://www.alphavantage.co/query"
|
||||
params = {
|
||||
'function': 'EARNINGS',
|
||||
'symbol': symbol,
|
||||
'apikey': api_key
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
return response.json()
|
||||
|
||||
def get_analyst_ratings(symbol, api_key):
|
||||
"""获取分析师评级(需要付费版本,这里尝试调用看是否有数据)"""
|
||||
url = f"https://www.alphavantage.co/query"
|
||||
params = {
|
||||
'function': 'ANALYST_RECOMMENDATIONS',
|
||||
'symbol': symbol,
|
||||
'apikey': api_key
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
return response.json()
|
||||
|
||||
def get_income_statement(symbol, api_key):
|
||||
"""获取损益表"""
|
||||
url = f"https://www.alphavantage.co/query"
|
||||
params = {
|
||||
'function': 'INCOME_STATEMENT',
|
||||
'symbol': symbol,
|
||||
'apikey': api_key
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
return response.json()
|
||||
|
||||
def format_financial_data(data, title):
|
||||
"""格式化财务数据输出"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f"{title}")
|
||||
print(f"{'='*60}")
|
||||
|
||||
if isinstance(data, dict):
|
||||
if 'Error Message' in data:
|
||||
print(f"❌ 错误: {data['Error Message']}")
|
||||
elif 'Note' in data:
|
||||
print(f"⚠️ 注意: {data['Note']}")
|
||||
else:
|
||||
print(json.dumps(data, indent=2, ensure_ascii=False))
|
||||
else:
|
||||
print(data)
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
try:
|
||||
# 获取 API Key
|
||||
api_key = get_alpha_vantage_key()
|
||||
print(f"✅ 成功获取 Alpha Vantage API Key: {api_key[:8]}...")
|
||||
|
||||
symbol = "META" # Meta Platforms Inc.
|
||||
print(f"\n🔍 正在获取 {symbol} 的财务数据...")
|
||||
|
||||
# 1. 公司概览
|
||||
print("\n📊 获取公司概览...")
|
||||
overview = get_company_overview(symbol, api_key)
|
||||
format_financial_data(overview, f"{symbol} - 公司概览")
|
||||
|
||||
# 2. 财报数据
|
||||
print("\n📈 获取财报数据...")
|
||||
earnings = get_earnings_data(symbol, api_key)
|
||||
format_financial_data(earnings, f"{symbol} - 财报数据")
|
||||
|
||||
# 3. 分析师评级
|
||||
print("\n⭐ 获取分析师评级...")
|
||||
ratings = get_analyst_ratings(symbol, api_key)
|
||||
format_financial_data(ratings, f"{symbol} - 分析师评级")
|
||||
|
||||
# 4. 损益表
|
||||
print("\n💰 获取损益表...")
|
||||
income_statement = get_income_statement(symbol, api_key)
|
||||
format_financial_data(income_statement, f"{symbol} - 损益表")
|
||||
|
||||
print(f"\n✅ {symbol} 数据获取完成!")
|
||||
print(f"⏰ 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 错误: {str(e)}")
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit(main())
|
||||
44
tests/test_google_adk.py
Normal file
44
tests/test_google_adk.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试 Google ADK 安装和基本功能
|
||||
"""
|
||||
|
||||
import os
|
||||
from google.adk import Agent
|
||||
|
||||
def test_adk_installation():
|
||||
"""测试 ADK 安装是否成功"""
|
||||
try:
|
||||
# 创建一个简单的测试智能体
|
||||
test_agent = Agent(
|
||||
name="测试智能体",
|
||||
model="gemini-2.0-flash-exp"
|
||||
)
|
||||
|
||||
print("✅ Google ADK 安装成功!")
|
||||
print(f"智能体名称: {test_agent.name}")
|
||||
print(f"使用模型: {test_agent.model}")
|
||||
print(f"描述: {test_agent.description}")
|
||||
|
||||
# 检查环境变量
|
||||
google_api_key = os.getenv('GOOGLE_API_KEY')
|
||||
if google_api_key:
|
||||
print(f"✅ GOOGLE_API_KEY 已配置 (长度: {len(google_api_key)} 字符)")
|
||||
else:
|
||||
print("⚠️ GOOGLE_API_KEY 未配置,需要设置 API 密钥")
|
||||
print("请访问 https://aistudio.google.com/ 获取 API 密钥")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ ADK 安装测试失败: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🚀 开始测试 Google ADK 安装...")
|
||||
test_adk_installation()
|
||||
print("\n📝 下一步: 配置 GOOGLE_API_KEY 环境变量")
|
||||
print(" 1. 访问 https://aistudio.google.com/")
|
||||
print(" 2. 获取 API 密钥")
|
||||
print(" 3. 在 Doppler 中设置: doppler secrets set GOOGLE_API_KEY=your_key")
|
||||
147
tests/test_memory_bank.py
Normal file
147
tests/test_memory_bank.py
Normal file
@@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
测试 Google ADK Memory Bank 功能
|
||||
"""
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
from google.adk import Agent
|
||||
from google.adk.memory import MemoryBank, MemoryItem
|
||||
from datetime import datetime
|
||||
|
||||
async def test_memory_bank():
|
||||
"""测试Memory Bank基本功能"""
|
||||
print("🧠 测试 Google ADK Memory Bank...")
|
||||
|
||||
try:
|
||||
# 创建记忆银行
|
||||
memory_bank = MemoryBank(
|
||||
name="test_memory_bank",
|
||||
description="测试用的记忆银行"
|
||||
)
|
||||
|
||||
print("✅ Memory Bank 创建成功")
|
||||
|
||||
# 添加记忆项
|
||||
memory_item = MemoryItem(
|
||||
content="这是一个测试记忆:比特币在2021年达到历史最高点69000美元",
|
||||
metadata={
|
||||
"type": "market_data",
|
||||
"asset": "bitcoin",
|
||||
"timestamp": datetime.now().isoformat()
|
||||
}
|
||||
)
|
||||
|
||||
await memory_bank.add_memory(memory_item)
|
||||
print("✅ 记忆添加成功")
|
||||
|
||||
# 搜索记忆
|
||||
search_results = await memory_bank.search("比特币", limit=5)
|
||||
print(f"✅ 记忆搜索成功,找到 {len(search_results)} 条相关记忆")
|
||||
|
||||
for i, memory in enumerate(search_results):
|
||||
print(f" {i+1}. {memory.content}")
|
||||
|
||||
# 创建带记忆银行的智能体
|
||||
agent = Agent(
|
||||
name="测试智能体",
|
||||
model="gemini-2.0-flash-exp",
|
||||
instruction="你是一个测试智能体,请使用你的记忆银行来回答问题。",
|
||||
memory_bank=memory_bank
|
||||
)
|
||||
|
||||
print("✅ 带记忆银行的智能体创建成功")
|
||||
|
||||
return True
|
||||
|
||||
except ImportError as e:
|
||||
print(f"❌ Memory Bank 模块导入失败: {e}")
|
||||
print("💡 可能需要更新 Google ADK 版本或启用 Memory Bank 功能")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"❌ Memory Bank 测试失败: {e}")
|
||||
return False
|
||||
|
||||
async def test_simple_memory_simulation():
|
||||
"""模拟Memory Bank功能的简单实现"""
|
||||
print("\n🔄 使用简单模拟实现...")
|
||||
|
||||
class SimpleMemoryBank:
|
||||
def __init__(self, name: str, description: str):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.memories = []
|
||||
|
||||
async def add_memory(self, content: str, metadata: dict = None):
|
||||
memory = {
|
||||
"content": content,
|
||||
"metadata": metadata or {},
|
||||
"timestamp": datetime.now().isoformat()
|
||||
}
|
||||
self.memories.append(memory)
|
||||
|
||||
async def search(self, query: str, limit: int = 5):
|
||||
# 简单的关键词匹配
|
||||
results = []
|
||||
query_lower = query.lower()
|
||||
|
||||
for memory in self.memories:
|
||||
if query_lower in memory["content"].lower():
|
||||
results.append(memory)
|
||||
if len(results) >= limit:
|
||||
break
|
||||
|
||||
return results
|
||||
|
||||
# 测试简单实现
|
||||
memory_bank = SimpleMemoryBank(
|
||||
name="铁拐李记忆银行",
|
||||
description="铁拐李的逆向投资记忆"
|
||||
)
|
||||
|
||||
# 添加一些记忆
|
||||
memories = [
|
||||
"2000年互联网泡沫破裂,纳斯达克指数从5048点跌到1114点",
|
||||
"2008年金融危机,雷曼兄弟破产引发全球恐慌",
|
||||
"2020年3月疫情恐慌,美股熔断4次,但随后强劲反弹",
|
||||
"比特币从2017年的2万美元跌到2018年的3200美元"
|
||||
]
|
||||
|
||||
for memory in memories:
|
||||
await memory_bank.add_memory(memory, {"type": "historical_event"})
|
||||
|
||||
print(f"✅ 已添加 {len(memories)} 条记忆")
|
||||
|
||||
# 搜索测试
|
||||
search_queries = ["泡沫", "比特币", "金融危机"]
|
||||
|
||||
for query in search_queries:
|
||||
results = await memory_bank.search(query)
|
||||
print(f"\n🔍 搜索 '{query}' 找到 {len(results)} 条记忆:")
|
||||
for i, result in enumerate(results):
|
||||
print(f" {i+1}. {result['content']}")
|
||||
|
||||
return True
|
||||
|
||||
async def main():
|
||||
"""主测试函数"""
|
||||
print("🚀 Google ADK Memory Bank 功能测试")
|
||||
|
||||
# 检查API密钥
|
||||
api_key = os.getenv('GOOGLE_API_KEY')
|
||||
if not api_key:
|
||||
print("❌ 未找到 GOOGLE_API_KEY 环境变量")
|
||||
return
|
||||
|
||||
print(f"✅ API密钥已配置")
|
||||
|
||||
# 尝试真实的Memory Bank
|
||||
success = await test_memory_bank()
|
||||
|
||||
if not success:
|
||||
# 如果真实的Memory Bank不可用,使用模拟实现
|
||||
await test_simple_memory_simulation()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
257
tests/test_vertex_memory_bank.py
Normal file
257
tests/test_vertex_memory_bank.py
Normal file
@@ -0,0 +1,257 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Vertex AI Memory Bank 测试脚本
|
||||
验证稷下学宫记忆银行功能
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到路径
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from src.jixia.memory.vertex_memory_bank import VertexMemoryBank, initialize_baxian_memory_banks
|
||||
from src.jixia.agents.memory_enhanced_agent import MemoryEnhancedAgent, create_memory_enhanced_council
|
||||
from config.doppler_config import get_google_genai_config, validate_config
|
||||
|
||||
|
||||
async def test_memory_bank_basic():
|
||||
"""测试Memory Bank基础功能"""
|
||||
print("🧪 测试 Memory Bank 基础功能...")
|
||||
|
||||
try:
|
||||
# 验证配置
|
||||
if not validate_config("google_adk"):
|
||||
print("❌ Google ADK 配置验证失败")
|
||||
return False
|
||||
|
||||
config = get_google_genai_config()
|
||||
if not config.get('project_id'):
|
||||
print("❌ Google Cloud Project ID 未配置")
|
||||
print("请设置环境变量: GOOGLE_CLOUD_PROJECT_ID")
|
||||
return False
|
||||
|
||||
# 创建Memory Bank实例
|
||||
memory_bank = VertexMemoryBank.from_config()
|
||||
print(f"✅ Memory Bank 实例创建成功")
|
||||
print(f" 项目ID: {config['project_id']}")
|
||||
print(f" 区域: {config['location']}")
|
||||
|
||||
# 测试创建记忆银行
|
||||
bank_id = await memory_bank.create_memory_bank(
|
||||
agent_name="tieguaili",
|
||||
display_name="铁拐李测试记忆银行"
|
||||
)
|
||||
print(f"✅ 创建记忆银行成功: {bank_id}")
|
||||
|
||||
# 测试添加记忆
|
||||
memory_id = await memory_bank.add_memory(
|
||||
agent_name="tieguaili",
|
||||
content="测试记忆:在分析NVIDIA时,我倾向于关注潜在的市场风险和估值泡沫。",
|
||||
memory_type="preference",
|
||||
debate_topic="NVIDIA投资分析",
|
||||
metadata={"test": True, "priority": "high"}
|
||||
)
|
||||
print(f"✅ 添加记忆成功: {memory_id}")
|
||||
|
||||
# 测试搜索记忆
|
||||
results = await memory_bank.search_memories(
|
||||
agent_name="tieguaili",
|
||||
query="NVIDIA 风险",
|
||||
limit=5
|
||||
)
|
||||
print(f"✅ 搜索记忆成功,找到 {len(results)} 条结果")
|
||||
|
||||
for i, result in enumerate(results, 1):
|
||||
print(f" {i}. {result['content'][:50]}... (相关度: {result.get('relevance_score', 'N/A')})")
|
||||
|
||||
# 测试获取上下文
|
||||
context = await memory_bank.get_agent_context("tieguaili", "NVIDIA投资分析")
|
||||
print(f"✅ 获取上下文成功,长度: {len(context)} 字符")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Memory Bank 基础测试失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
async def test_memory_enhanced_agent():
|
||||
"""测试记忆增强智能体"""
|
||||
print("\n🧪 测试记忆增强智能体...")
|
||||
|
||||
try:
|
||||
# 创建记忆银行
|
||||
memory_bank = VertexMemoryBank.from_config()
|
||||
|
||||
# 创建记忆增强智能体
|
||||
agent = MemoryEnhancedAgent("tieguaili", memory_bank)
|
||||
print(f"✅ 创建记忆增强智能体: {agent.personality.chinese_name}")
|
||||
|
||||
# 测试基于记忆的响应
|
||||
response = await agent.respond_with_memory(
|
||||
message="你对NVIDIA的最新财报有什么看法?",
|
||||
topic="NVIDIA投资分析"
|
||||
)
|
||||
print(f"✅ 智能体响应成功")
|
||||
print(f" 响应长度: {len(response)} 字符")
|
||||
print(f" 响应预览: {response[:100]}...")
|
||||
|
||||
# 测试学习偏好
|
||||
await agent.learn_preference(
|
||||
preference="用户偏好保守的投资策略,关注风险控制",
|
||||
topic="投资偏好"
|
||||
)
|
||||
print("✅ 学习用户偏好成功")
|
||||
|
||||
# 测试保存策略洞察
|
||||
await agent.save_strategy_insight(
|
||||
insight="在高估值环境下,应该更加关注基本面分析和风险管理",
|
||||
topic="投资策略"
|
||||
)
|
||||
print("✅ 保存策略洞察成功")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 记忆增强智能体测试失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
async def test_baxian_memory_council():
|
||||
"""测试八仙记忆议会"""
|
||||
print("\n🧪 测试八仙记忆议会...")
|
||||
|
||||
try:
|
||||
# 创建记忆增强议会
|
||||
council = await create_memory_enhanced_council()
|
||||
print(f"✅ 创建八仙记忆议会成功,智能体数量: {len(council.agents)}")
|
||||
|
||||
# 列出所有智能体
|
||||
for agent_name, agent in council.agents.items():
|
||||
print(f" - {agent.personality.chinese_name} ({agent_name})")
|
||||
|
||||
# 进行简短的记忆辩论测试
|
||||
print("\n🏛️ 开始记忆增强辩论测试...")
|
||||
result = await council.conduct_memory_debate(
|
||||
topic="比特币投资价值分析",
|
||||
participants=["tieguaili", "lvdongbin"], # 只选择两个智能体进行快速测试
|
||||
rounds=1
|
||||
)
|
||||
|
||||
print(f"✅ 辩论完成")
|
||||
print(f" 主题: {result['topic']}")
|
||||
print(f" 参与者: {len(result['participants'])} 位")
|
||||
print(f" 发言次数: {result['total_exchanges']}")
|
||||
|
||||
# 显示辩论内容
|
||||
for exchange in result['conversation_history']:
|
||||
print(f" {exchange['chinese_name']}: {exchange['content'][:80]}...")
|
||||
|
||||
# 获取集体记忆摘要
|
||||
summary = await council.get_collective_memory_summary("比特币投资价值分析")
|
||||
print(f"\n📚 集体记忆摘要长度: {len(summary)} 字符")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 八仙记忆议会测试失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
async def test_memory_bank_initialization():
|
||||
"""测试Memory Bank初始化"""
|
||||
print("\n🧪 测试 Memory Bank 初始化...")
|
||||
|
||||
try:
|
||||
config = get_google_genai_config()
|
||||
project_id = config.get('project_id')
|
||||
location = config.get('location', 'us-central1')
|
||||
|
||||
if not project_id:
|
||||
print("❌ 项目ID未配置,跳过初始化测试")
|
||||
return False
|
||||
|
||||
# 初始化所有八仙记忆银行
|
||||
memory_bank = await initialize_baxian_memory_banks(project_id, location)
|
||||
print(f"✅ 八仙记忆银行初始化成功")
|
||||
print(f" 记忆银行数量: {len(memory_bank.memory_banks)}")
|
||||
|
||||
for agent_name, bank_name in memory_bank.memory_banks.items():
|
||||
chinese_name = memory_bank.baxian_agents.get(agent_name, agent_name)
|
||||
print(f" - {chinese_name}: {bank_name}")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Memory Bank 初始化测试失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
async def main():
|
||||
"""主测试函数"""
|
||||
print("🏛️ 稷下学宫 Vertex AI Memory Bank 测试")
|
||||
print("=" * 50)
|
||||
|
||||
# 检查配置
|
||||
print("🔧 检查配置...")
|
||||
config = get_google_genai_config()
|
||||
|
||||
print(f"Google API Key: {'已配置' if config.get('api_key') else '未配置'}")
|
||||
print(f"Project ID: {config.get('project_id', '未配置')}")
|
||||
print(f"Location: {config.get('location', 'us-central1')}")
|
||||
print(f"Memory Bank: {'启用' if config.get('memory_bank_enabled', 'TRUE') == 'TRUE' else '禁用'}")
|
||||
|
||||
if not config.get('project_id'):
|
||||
print("\n❌ 测试需要 Google Cloud Project ID")
|
||||
print("请设置环境变量: GOOGLE_CLOUD_PROJECT_ID=your-project-id")
|
||||
return
|
||||
|
||||
# 运行测试
|
||||
tests = [
|
||||
("Memory Bank 基础功能", test_memory_bank_basic),
|
||||
("记忆增强智能体", test_memory_enhanced_agent),
|
||||
("八仙记忆议会", test_baxian_memory_council),
|
||||
("Memory Bank 初始化", test_memory_bank_initialization)
|
||||
]
|
||||
|
||||
results = []
|
||||
|
||||
for test_name, test_func in tests:
|
||||
print(f"\n{'='*20}")
|
||||
print(f"测试: {test_name}")
|
||||
print(f"{'='*20}")
|
||||
|
||||
try:
|
||||
result = await test_func()
|
||||
results.append((test_name, result))
|
||||
except Exception as e:
|
||||
print(f"❌ 测试 {test_name} 出现异常: {e}")
|
||||
results.append((test_name, False))
|
||||
|
||||
# 显示测试结果摘要
|
||||
print(f"\n{'='*50}")
|
||||
print("🏛️ 测试结果摘要")
|
||||
print(f"{'='*50}")
|
||||
|
||||
passed = 0
|
||||
total = len(results)
|
||||
|
||||
for test_name, result in results:
|
||||
status = "✅ 通过" if result else "❌ 失败"
|
||||
print(f"{status} {test_name}")
|
||||
if result:
|
||||
passed += 1
|
||||
|
||||
print(f"\n📊 总体结果: {passed}/{total} 测试通过")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 所有测试通过!Vertex AI Memory Bank 集成成功!")
|
||||
else:
|
||||
print("⚠️ 部分测试失败,请检查配置和网络连接")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 运行测试
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user