refactor: 重构项目结构和文件组织
- 将文档文件移动到docs目录下分类存放 - 将测试文件移动到tests目录 - 将工具脚本移动到tools目录 - 更新README中的文件路径说明 - 删除重复和过时的文件 - 优化项目目录结构,提升可维护性
This commit is contained in:
150
tools/memory_bank/detailed_memory_bank_info.py
Normal file
150
tools/memory_bank/detailed_memory_bank_info.py
Normal file
@@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
详细查看和测试Vertex AI Memory Bank功能
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import asyncio
|
||||
import json
|
||||
from datetime import datetime
|
||||
sys.path.append('src')
|
||||
|
||||
from jixia.memory.factory import get_memory_backend
|
||||
from config.doppler_config import get_google_genai_config
|
||||
|
||||
async def test_memory_bank_functionality():
|
||||
print("🧠 详细测试Memory Bank功能")
|
||||
print("=" * 60)
|
||||
|
||||
# 获取配置
|
||||
config = get_google_genai_config()
|
||||
project_id = config.get('project_id')
|
||||
location = config.get('location', 'us-central1')
|
||||
|
||||
print(f"📊 项目ID: {project_id}")
|
||||
print(f"📍 位置: {location}")
|
||||
print(f"🕐 测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print()
|
||||
|
||||
try:
|
||||
# 获取Memory Bank后端
|
||||
memory_backend = get_memory_backend()
|
||||
print(f"✅ Memory Bank后端: {type(memory_backend).__name__}")
|
||||
print()
|
||||
|
||||
# 选择一个智能体进行详细测试
|
||||
test_agent = "lvdongbin"
|
||||
print(f"🧙♂️ 测试智能体: {test_agent} (吕洞宾)")
|
||||
print("-" * 40)
|
||||
|
||||
# 1. 创建/获取Memory Bank
|
||||
print("1️⃣ 创建Memory Bank...")
|
||||
memory_bank_id = await memory_backend.create_memory_bank(
|
||||
agent_name=test_agent,
|
||||
display_name=f"测试Memory Bank - {test_agent}"
|
||||
)
|
||||
print(f" ✅ Memory Bank ID: {memory_bank_id}")
|
||||
print()
|
||||
|
||||
# 2. 添加不同类型的记忆
|
||||
print("2️⃣ 添加测试记忆...")
|
||||
|
||||
# 添加对话记忆
|
||||
conversation_memory = await memory_backend.add_memory(
|
||||
agent_name=test_agent,
|
||||
content="在关于AI伦理的辩论中,我强调了技术发展应该以人为本,不能忽视道德考量。",
|
||||
memory_type="conversation",
|
||||
debate_topic="AI伦理与技术发展",
|
||||
metadata={"opponent": "铁拐李", "stance": "支持伦理优先"}
|
||||
)
|
||||
print(f" 📝 对话记忆: {conversation_memory}")
|
||||
|
||||
# 添加偏好记忆
|
||||
preference_memory = await memory_backend.add_memory(
|
||||
agent_name=test_agent,
|
||||
content="我偏好使用古典哲学的智慧来论证现代问题,特别是道家思想。",
|
||||
memory_type="preference",
|
||||
metadata={"philosophy": "道家", "style": "古典智慧"}
|
||||
)
|
||||
print(f" ⚙️ 偏好记忆: {preference_memory}")
|
||||
|
||||
# 添加知识记忆
|
||||
knowledge_memory = await memory_backend.add_memory(
|
||||
agent_name=test_agent,
|
||||
content="区块链技术的核心是去中心化和不可篡改性,这与道家'无为而治'的理念有相通之处。",
|
||||
memory_type="knowledge",
|
||||
debate_topic="区块链技术应用",
|
||||
metadata={"domain": "技术", "connection": "哲学"}
|
||||
)
|
||||
print(f" 📚 知识记忆: {knowledge_memory}")
|
||||
|
||||
# 添加策略记忆
|
||||
strategy_memory = await memory_backend.add_memory(
|
||||
agent_name=test_agent,
|
||||
content="在辩论中,当对手使用激进论点时,我会用温和的反问来引导思考,而不是直接对抗。",
|
||||
memory_type="strategy",
|
||||
metadata={"tactic": "温和引导", "effectiveness": "高"}
|
||||
)
|
||||
print(f" 🎯 策略记忆: {strategy_memory}")
|
||||
print()
|
||||
|
||||
# 3. 测试记忆搜索
|
||||
print("3️⃣ 测试记忆搜索...")
|
||||
|
||||
# 搜索关于AI的记忆
|
||||
ai_memories = await memory_backend.search_memories(
|
||||
agent_name=test_agent,
|
||||
query="AI 人工智能 伦理",
|
||||
limit=5
|
||||
)
|
||||
print(f" 🔍 搜索'AI 人工智能 伦理': 找到 {len(ai_memories)} 条记忆")
|
||||
for i, memory in enumerate(ai_memories, 1):
|
||||
print(f" {i}. {memory.get('content', '')[:50]}...")
|
||||
print()
|
||||
|
||||
# 搜索策略相关记忆
|
||||
strategy_memories = await memory_backend.search_memories(
|
||||
agent_name=test_agent,
|
||||
query="辩论 策略",
|
||||
memory_type="strategy",
|
||||
limit=3
|
||||
)
|
||||
print(f" 🎯 搜索策略记忆: 找到 {len(strategy_memories)} 条记忆")
|
||||
for i, memory in enumerate(strategy_memories, 1):
|
||||
print(f" {i}. {memory.get('content', '')[:50]}...")
|
||||
print()
|
||||
|
||||
# 4. 获取智能体上下文
|
||||
print("4️⃣ 获取智能体上下文...")
|
||||
context = await memory_backend.get_agent_context(
|
||||
agent_name=test_agent,
|
||||
debate_topic="AI伦理与技术发展"
|
||||
)
|
||||
print(f" 📋 上下文长度: {len(context)} 字符")
|
||||
print(f" 📋 上下文预览: {context[:200]}...")
|
||||
print()
|
||||
|
||||
# 5. 显示所有记忆类型的统计
|
||||
print("5️⃣ 记忆统计...")
|
||||
memory_types = ["conversation", "preference", "knowledge", "strategy"]
|
||||
for mem_type in memory_types:
|
||||
memories = await memory_backend.search_memories(
|
||||
agent_name=test_agent,
|
||||
query="",
|
||||
memory_type=mem_type,
|
||||
limit=100
|
||||
)
|
||||
print(f" 📊 {mem_type}: {len(memories)} 条记忆")
|
||||
|
||||
print()
|
||||
print("🎉 Memory Bank功能测试完成!")
|
||||
print("=" * 60)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 测试失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(test_memory_bank_functionality())
|
||||
83
tools/memory_bank/list_memory_banks.py
Normal file
83
tools/memory_bank/list_memory_banks.py
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
使用项目现有的Memory Bank代码来查看实例
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import asyncio
|
||||
sys.path.append('src')
|
||||
|
||||
from jixia.memory.factory import get_memory_backend
|
||||
from config.doppler_config import get_google_genai_config
|
||||
|
||||
async def list_memory_banks():
|
||||
"""使用项目的Memory Bank工厂来查看实例"""
|
||||
|
||||
print("🧠 使用项目Memory Bank工厂查看实例")
|
||||
print("="*50)
|
||||
|
||||
try:
|
||||
# 获取配置
|
||||
config = get_google_genai_config()
|
||||
print(f"📊 项目ID: {config.get('project_id')}")
|
||||
print(f"📍 位置: {config.get('location')}")
|
||||
print(f"🔑 Memory Bank启用: {config.get('memory_bank_enabled')}")
|
||||
|
||||
# 获取Memory Bank后端
|
||||
print("\n🔍 正在获取Memory Bank后端...")
|
||||
memory_backend = get_memory_backend()
|
||||
print(f"✅ 成功获取Memory Bank后端: {type(memory_backend).__name__}")
|
||||
|
||||
# 八仙列表
|
||||
immortals = [
|
||||
"tieguaili", "zhongliquan", "lvdongbin", "hehe_erxian",
|
||||
"lantsaihe", "hanxiangzi", "caoguo_jiu", "hexiangu"
|
||||
]
|
||||
|
||||
print(f"\n🔍 正在检查八仙的Memory Bank实例...")
|
||||
print("="*50)
|
||||
|
||||
for immortal in immortals:
|
||||
try:
|
||||
print(f"\n🧙♂️ {immortal}:")
|
||||
|
||||
# 尝试创建Memory Bank
|
||||
memory_bank_id = await memory_backend.create_memory_bank(immortal)
|
||||
print(f" ✅ Memory Bank ID: {memory_bank_id}")
|
||||
|
||||
# 尝试搜索一些记忆(如果有的话)
|
||||
try:
|
||||
memories = await memory_backend.search_memories(immortal, "投资", limit=3)
|
||||
if memories:
|
||||
print(f" 📝 找到 {len(memories)} 条记忆")
|
||||
for i, memory in enumerate(memories[:2], 1):
|
||||
content = memory.get('content', '无内容')[:50]
|
||||
print(f" {i}. {content}...")
|
||||
else:
|
||||
print(f" 📭 暂无记忆")
|
||||
except Exception as e:
|
||||
print(f" ⚠️ 无法搜索记忆: {str(e)[:50]}...")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ 错误: {str(e)[:50]}...")
|
||||
|
||||
print(f"\n🎉 Memory Bank检查完成!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ 主要错误: {str(e)}")
|
||||
print(f"🔧 错误类型: {type(e).__name__}")
|
||||
|
||||
# 显示一些调试信息
|
||||
print("\n🔍 调试信息:")
|
||||
print(f" Python路径: {sys.path[:3]}...")
|
||||
print(f" 当前目录: {os.getcwd()}")
|
||||
print(f" 环境变量:")
|
||||
for key in ['GOOGLE_API_KEY', 'GOOGLE_CLOUD_PROJECT_ID', 'VERTEX_MEMORY_BANK_ENABLED']:
|
||||
value = os.getenv(key, '未设置')
|
||||
if 'API_KEY' in key and value != '未设置':
|
||||
value = value[:10] + '...' if len(value) > 10 else value
|
||||
print(f" {key}: {value}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(list_memory_banks())
|
||||
299
tools/memory_bank/memory_bank_web_interface.py
Normal file
299
tools/memory_bank/memory_bank_web_interface.py
Normal file
@@ -0,0 +1,299 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Vertex AI Memory Bank Web界面
|
||||
一个简单的Streamlit应用,用于通过Web界面访问和管理Memory Bank
|
||||
"""
|
||||
|
||||
import streamlit as st
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
# 添加项目路径
|
||||
sys.path.append('/Users/ben/liurenchaxin/src')
|
||||
|
||||
try:
|
||||
from jixia.memory.factory import get_memory_backend
|
||||
except ImportError as e:
|
||||
st.error(f"无法导入jixia模块: {e}")
|
||||
st.info("请确保已激活虚拟环境并安装了所需依赖")
|
||||
st.stop()
|
||||
|
||||
# 页面配置
|
||||
st.set_page_config(
|
||||
page_title="Memory Bank 管理界面",
|
||||
page_icon="🧠",
|
||||
layout="wide"
|
||||
)
|
||||
|
||||
# 标题
|
||||
st.title("🧠 Vertex AI Memory Bank 管理界面")
|
||||
st.markdown("---")
|
||||
|
||||
# 侧边栏配置
|
||||
st.sidebar.header("配置")
|
||||
project_id = st.sidebar.text_input("项目ID", value="inner-radius-469712-e9")
|
||||
location = st.sidebar.text_input("区域", value="us-central1")
|
||||
|
||||
# 八仙列表
|
||||
EIGHT_IMMORTALS = [
|
||||
"lvdongbin", "tieguaili", "hanxiangzi", "lanzaihe",
|
||||
"hesengu", "zhonghanli", "caogujiu", "hanzhongli"
|
||||
]
|
||||
|
||||
# 缓存Memory Bank后端
|
||||
@st.cache_resource
|
||||
def get_memory_backend_cached():
|
||||
"""获取Memory Bank后端(缓存)"""
|
||||
try:
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
backend = loop.run_until_complete(get_memory_backend("vertex"))
|
||||
return backend, loop
|
||||
except Exception as e:
|
||||
st.error(f"初始化Memory Bank失败: {e}")
|
||||
return None, None
|
||||
|
||||
# 异步函数包装器
|
||||
def run_async(coro):
|
||||
"""运行异步函数"""
|
||||
backend, loop = get_memory_backend_cached()
|
||||
if backend is None:
|
||||
return None
|
||||
try:
|
||||
return loop.run_until_complete(coro)
|
||||
except Exception as e:
|
||||
st.error(f"操作失败: {e}")
|
||||
return None
|
||||
|
||||
# 主界面
|
||||
tab1, tab2, tab3, tab4 = st.tabs(["📋 Memory Bank列表", "🔍 搜索记忆", "➕ 添加记忆", "📊 统计信息"])
|
||||
|
||||
with tab1:
|
||||
st.header("Memory Bank 实例列表")
|
||||
|
||||
if st.button("🔄 刷新列表", key="refresh_list"):
|
||||
st.rerun()
|
||||
|
||||
# 显示八仙Memory Bank状态
|
||||
cols = st.columns(4)
|
||||
for i, immortal in enumerate(EIGHT_IMMORTALS):
|
||||
with cols[i % 4]:
|
||||
with st.container():
|
||||
st.subheader(f"🧙♂️ {immortal}")
|
||||
|
||||
# 检查Memory Bank状态
|
||||
backend, _ = get_memory_backend_cached()
|
||||
if backend:
|
||||
try:
|
||||
# 尝试获取agent context来验证Memory Bank存在
|
||||
context = run_async(backend.get_agent_context(immortal))
|
||||
if context is not None:
|
||||
st.success("✅ 活跃")
|
||||
|
||||
# 显示记忆数量
|
||||
memories = run_async(backend.search_memories(immortal, "", limit=100))
|
||||
if memories:
|
||||
st.info(f"📝 记忆数量: {len(memories)}")
|
||||
else:
|
||||
st.info("📝 记忆数量: 0")
|
||||
else:
|
||||
st.warning("⚠️ 未初始化")
|
||||
except Exception as e:
|
||||
st.error(f"❌ 错误: {str(e)[:50]}...")
|
||||
else:
|
||||
st.error("❌ 连接失败")
|
||||
|
||||
with tab2:
|
||||
st.header("🔍 搜索记忆")
|
||||
|
||||
col1, col2 = st.columns([1, 2])
|
||||
|
||||
with col1:
|
||||
selected_agent = st.selectbox("选择Agent", EIGHT_IMMORTALS, key="search_agent")
|
||||
search_query = st.text_input("搜索关键词", placeholder="输入要搜索的内容...", key="search_query")
|
||||
search_limit = st.slider("结果数量", 1, 50, 10, key="search_limit")
|
||||
|
||||
if st.button("🔍 搜索", key="search_button"):
|
||||
if search_query:
|
||||
with st.spinner("搜索中..."):
|
||||
backend, _ = get_memory_backend_cached()
|
||||
if backend:
|
||||
memories = run_async(backend.search_memories(selected_agent, search_query, limit=search_limit))
|
||||
st.session_state['search_results'] = memories
|
||||
st.session_state['search_agent'] = selected_agent
|
||||
st.session_state['search_query'] = search_query
|
||||
else:
|
||||
st.warning("请输入搜索关键词")
|
||||
|
||||
with col2:
|
||||
st.subheader("搜索结果")
|
||||
|
||||
if 'search_results' in st.session_state and st.session_state['search_results']:
|
||||
st.success(f"找到 {len(st.session_state['search_results'])} 条记忆")
|
||||
|
||||
for i, memory in enumerate(st.session_state['search_results']):
|
||||
with st.expander(f"记忆 {i+1}: {memory.get('content', 'N/A')[:50]}..."):
|
||||
st.write(f"**内容**: {memory.get('content', 'N/A')}")
|
||||
st.write(f"**类型**: {memory.get('memory_type', 'N/A')}")
|
||||
st.write(f"**时间**: {memory.get('timestamp', 'N/A')}")
|
||||
if 'metadata' in memory:
|
||||
st.write(f"**元数据**: {memory['metadata']}")
|
||||
elif 'search_results' in st.session_state:
|
||||
st.info("未找到匹配的记忆")
|
||||
else:
|
||||
st.info("请执行搜索以查看结果")
|
||||
|
||||
with tab3:
|
||||
st.header("➕ 添加记忆")
|
||||
|
||||
col1, col2 = st.columns([1, 1])
|
||||
|
||||
with col1:
|
||||
add_agent = st.selectbox("选择Agent", EIGHT_IMMORTALS, key="add_agent")
|
||||
memory_type = st.selectbox("记忆类型", ["conversation", "preference", "knowledge", "strategy"], key="memory_type")
|
||||
memory_content = st.text_area("记忆内容", placeholder="输入要添加的记忆内容...", height=150, key="memory_content")
|
||||
|
||||
# 可选的元数据
|
||||
st.subheader("元数据(可选)")
|
||||
importance = st.slider("重要性", 1, 10, 5, key="importance")
|
||||
tags = st.text_input("标签(用逗号分隔)", placeholder="标签1, 标签2, 标签3", key="tags")
|
||||
|
||||
if st.button("➕ 添加记忆", key="add_memory_button"):
|
||||
if memory_content:
|
||||
with st.spinner("添加记忆中..."):
|
||||
backend, _ = get_memory_backend_cached()
|
||||
if backend:
|
||||
# 准备元数据
|
||||
metadata = {
|
||||
"importance": importance,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"source": "web_interface"
|
||||
}
|
||||
if tags:
|
||||
metadata["tags"] = [tag.strip() for tag in tags.split(",")]
|
||||
|
||||
# 添加记忆
|
||||
success = run_async(backend.add_memory(
|
||||
agent_id=add_agent,
|
||||
content=memory_content,
|
||||
memory_type=memory_type,
|
||||
metadata=metadata
|
||||
))
|
||||
|
||||
if success:
|
||||
st.success("✅ 记忆添加成功!")
|
||||
# 清空输入
|
||||
st.session_state['memory_content'] = ""
|
||||
st.session_state['tags'] = ""
|
||||
else:
|
||||
st.error("❌ 添加记忆失败")
|
||||
else:
|
||||
st.warning("请输入记忆内容")
|
||||
|
||||
with col2:
|
||||
st.subheader("添加记忆预览")
|
||||
if memory_content:
|
||||
st.info(f"**Agent**: {add_agent}")
|
||||
st.info(f"**类型**: {memory_type}")
|
||||
st.info(f"**内容**: {memory_content}")
|
||||
st.info(f"**重要性**: {importance}/10")
|
||||
if tags:
|
||||
st.info(f"**标签**: {tags}")
|
||||
else:
|
||||
st.info("输入记忆内容以查看预览")
|
||||
|
||||
with tab4:
|
||||
st.header("📊 统计信息")
|
||||
|
||||
if st.button("🔄 刷新统计", key="refresh_stats"):
|
||||
st.rerun()
|
||||
|
||||
# 获取统计信息
|
||||
backend, _ = get_memory_backend_cached()
|
||||
if backend:
|
||||
stats_data = []
|
||||
|
||||
for immortal in EIGHT_IMMORTALS:
|
||||
try:
|
||||
# 获取记忆数量
|
||||
memories = run_async(backend.search_memories(immortal, "", limit=1000))
|
||||
memory_count = len(memories) if memories else 0
|
||||
|
||||
# 获取agent context
|
||||
context = run_async(backend.get_agent_context(immortal))
|
||||
status = "活跃" if context else "未初始化"
|
||||
|
||||
stats_data.append({
|
||||
"Agent": immortal,
|
||||
"状态": status,
|
||||
"记忆数量": memory_count,
|
||||
"最后更新": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
})
|
||||
except Exception as e:
|
||||
stats_data.append({
|
||||
"Agent": immortal,
|
||||
"状态": "错误",
|
||||
"记忆数量": 0,
|
||||
"最后更新": f"错误: {str(e)[:30]}..."
|
||||
})
|
||||
|
||||
# 显示统计表格
|
||||
st.dataframe(stats_data, use_container_width=True)
|
||||
|
||||
# 显示汇总信息
|
||||
col1, col2, col3, col4 = st.columns(4)
|
||||
|
||||
total_agents = len(EIGHT_IMMORTALS)
|
||||
active_agents = sum(1 for item in stats_data if item["状态"] == "活跃")
|
||||
total_memories = sum(item["记忆数量"] for item in stats_data)
|
||||
avg_memories = total_memories / total_agents if total_agents > 0 else 0
|
||||
|
||||
with col1:
|
||||
st.metric("总Agent数", total_agents)
|
||||
|
||||
with col2:
|
||||
st.metric("活跃Agent数", active_agents)
|
||||
|
||||
with col3:
|
||||
st.metric("总记忆数", total_memories)
|
||||
|
||||
with col4:
|
||||
st.metric("平均记忆数", f"{avg_memories:.1f}")
|
||||
|
||||
# 页脚
|
||||
st.markdown("---")
|
||||
st.markdown(
|
||||
"""
|
||||
<div style='text-align: center; color: #666;'>
|
||||
🧠 Vertex AI Memory Bank Web界面 |
|
||||
<a href='https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/memory-bank/overview' target='_blank'>官方文档</a>
|
||||
</div>
|
||||
""",
|
||||
unsafe_allow_html=True
|
||||
)
|
||||
|
||||
# 使用说明
|
||||
with st.expander("📖 使用说明"):
|
||||
st.markdown("""
|
||||
### 功能说明
|
||||
|
||||
1. **Memory Bank列表**: 查看所有八仙角色的Memory Bank状态和记忆数量
|
||||
2. **搜索记忆**: 在指定Agent的记忆中搜索特定内容
|
||||
3. **添加记忆**: 为Agent添加新的记忆,支持不同类型和元数据
|
||||
4. **统计信息**: 查看所有Agent的统计数据和汇总信息
|
||||
|
||||
### 使用前准备
|
||||
|
||||
1. 确保已激活虚拟环境: `source venv/bin/activate`
|
||||
2. 确保已设置Google Cloud认证: `gcloud auth application-default login`
|
||||
3. 运行此界面: `streamlit run memory_bank_web_interface.py`
|
||||
|
||||
### 注意事项
|
||||
|
||||
- Memory Bank目前仅在us-central1区域可用
|
||||
- 搜索功能支持模糊匹配
|
||||
- 添加的记忆会立即生效
|
||||
- 统计信息实时更新
|
||||
""")
|
||||
154
tools/memory_bank/view_memory_banks_gcp.py
Normal file
154
tools/memory_bank/view_memory_banks_gcp.py
Normal file
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
通过Google Cloud Console查看Memory Bank资源
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import asyncio
|
||||
import json
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
sys.path.append('src')
|
||||
|
||||
from config.doppler_config import get_google_genai_config
|
||||
|
||||
def get_access_token():
|
||||
"""获取Google Cloud访问令牌"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
['gcloud', 'auth', 'print-access-token'],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"❌ 获取访问令牌失败: {e}")
|
||||
return None
|
||||
|
||||
def make_api_request(url, token):
|
||||
"""发起API请求"""
|
||||
import requests
|
||||
|
||||
headers = {
|
||||
'Authorization': f'Bearer {token}',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.get(url, headers=headers)
|
||||
return response.status_code, response.json() if response.content else {}
|
||||
except Exception as e:
|
||||
return None, str(e)
|
||||
|
||||
def main():
|
||||
print("🔍 通过GCP API查看Memory Bank资源")
|
||||
print("=" * 60)
|
||||
|
||||
# 获取配置
|
||||
config = get_google_genai_config()
|
||||
project_id = config.get('project_id')
|
||||
location = config.get('location', 'us-central1')
|
||||
|
||||
print(f"📊 项目ID: {project_id}")
|
||||
print(f"📍 位置: {location}")
|
||||
print(f"🕐 查询时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print()
|
||||
|
||||
# 获取访问令牌
|
||||
print("🔑 获取访问令牌...")
|
||||
token = get_access_token()
|
||||
if not token:
|
||||
print("❌ 无法获取访问令牌")
|
||||
return
|
||||
print(f"✅ 访问令牌: {token[:20]}...")
|
||||
print()
|
||||
|
||||
# 尝试不同的API端点
|
||||
api_endpoints = [
|
||||
# Vertex AI API
|
||||
f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/operations",
|
||||
f"https://aiplatform.googleapis.com/v1beta1/projects/{project_id}/locations/{location}/operations",
|
||||
|
||||
# Generative Language API
|
||||
f"https://generativelanguage.googleapis.com/v1beta/projects/{project_id}/locations/{location}/operations",
|
||||
|
||||
# 尝试Memory Bank相关端点
|
||||
f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/memoryBanks",
|
||||
f"https://aiplatform.googleapis.com/v1beta1/projects/{project_id}/locations/{location}/memoryBanks",
|
||||
|
||||
# 尝试其他可能的端点
|
||||
f"https://generativelanguage.googleapis.com/v1beta/projects/{project_id}/locations/{location}/memoryBanks",
|
||||
f"https://generativelanguage.googleapis.com/v1/projects/{project_id}/locations/{location}/memoryBanks",
|
||||
]
|
||||
|
||||
print("🌐 测试API端点...")
|
||||
print("-" * 40)
|
||||
|
||||
for i, endpoint in enumerate(api_endpoints, 1):
|
||||
print(f"{i}. 测试: {endpoint.split('/')[-2]}/{endpoint.split('/')[-1]}")
|
||||
|
||||
status_code, response = make_api_request(endpoint, token)
|
||||
|
||||
if status_code == 200:
|
||||
print(f" ✅ 成功 (200): 找到 {len(response.get('operations', response.get('memoryBanks', [])))} 个资源")
|
||||
if response:
|
||||
print(f" 📄 响应预览: {str(response)[:100]}...")
|
||||
elif status_code == 404:
|
||||
print(f" ⚠️ 未找到 (404): 端点不存在")
|
||||
elif status_code == 403:
|
||||
print(f" 🚫 权限不足 (403): 需要更多权限")
|
||||
elif status_code:
|
||||
print(f" ❌ 错误 ({status_code}): {str(response)[:50]}...")
|
||||
else:
|
||||
print(f" 💥 请求失败: {response}")
|
||||
print()
|
||||
|
||||
# 查看项目信息
|
||||
print("📋 项目信息...")
|
||||
project_url = f"https://cloudresourcemanager.googleapis.com/v1/projects/{project_id}"
|
||||
status_code, response = make_api_request(project_url, token)
|
||||
|
||||
if status_code == 200:
|
||||
print(f" ✅ 项目名称: {response.get('name', 'N/A')}")
|
||||
print(f" 📊 项目编号: {response.get('projectNumber', 'N/A')}")
|
||||
print(f" 🏷️ 项目ID: {response.get('projectId', 'N/A')}")
|
||||
print(f" 📅 创建时间: {response.get('createTime', 'N/A')}")
|
||||
print(f" 🔄 生命周期: {response.get('lifecycleState', 'N/A')}")
|
||||
else:
|
||||
print(f" ❌ 无法获取项目信息: {status_code}")
|
||||
print()
|
||||
|
||||
# 查看启用的服务
|
||||
print("🔧 查看启用的AI相关服务...")
|
||||
services_url = f"https://serviceusage.googleapis.com/v1/projects/{project_id}/services"
|
||||
status_code, response = make_api_request(services_url, token)
|
||||
|
||||
if status_code == 200:
|
||||
services = response.get('services', [])
|
||||
ai_services = [s for s in services if 'ai' in s.get('config', {}).get('name', '').lower() or 'generative' in s.get('config', {}).get('name', '').lower()]
|
||||
|
||||
print(f" 📊 总服务数: {len(services)}")
|
||||
print(f" 🤖 AI相关服务: {len(ai_services)}")
|
||||
|
||||
for service in ai_services[:10]: # 显示前10个
|
||||
name = service.get('config', {}).get('name', 'Unknown')
|
||||
state = service.get('state', 'Unknown')
|
||||
print(f" • {name}: {state}")
|
||||
else:
|
||||
print(f" ❌ 无法获取服务信息: {status_code}")
|
||||
|
||||
print()
|
||||
print("🎯 Memory Bank访问建议:")
|
||||
print(" 1. 在Google Cloud Console中访问:")
|
||||
print(f" https://console.cloud.google.com/vertex-ai/generative/memory-banks?project={project_id}")
|
||||
print(" 2. 或者访问Vertex AI主页:")
|
||||
print(f" https://console.cloud.google.com/vertex-ai?project={project_id}")
|
||||
print(" 3. Memory Bank功能可能在'生成式AI'或'实验性功能'部分")
|
||||
print()
|
||||
print("🎉 GCP API查询完成!")
|
||||
print("=" * 60)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user