197 lines
5.7 KiB
Python
197 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Docker 环境问题诊断脚本
|
||
帮助用户诊断和修复 Docker 环境中的问题
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import subprocess
|
||
from datetime import datetime
|
||
|
||
|
||
def check_version():
|
||
"""检查关键组件的版本"""
|
||
print("=" * 80)
|
||
print("版本检查")
|
||
print("=" * 80)
|
||
print()
|
||
|
||
packages = [
|
||
"coze-coding-dev-sdk",
|
||
"langchain",
|
||
"langgraph",
|
||
"coze-coding-utils",
|
||
]
|
||
|
||
for pkg in packages:
|
||
try:
|
||
result = subprocess.run(
|
||
["pip", "show", pkg],
|
||
capture_output=True,
|
||
text=True,
|
||
timeout=10
|
||
)
|
||
|
||
if result.returncode == 0:
|
||
version = None
|
||
for line in result.stdout.split('\n'):
|
||
if line.startswith('Version:'):
|
||
version = line.split(':', 1)[1].strip()
|
||
break
|
||
|
||
if version:
|
||
print(f"✅ {pkg:<30} {version}")
|
||
else:
|
||
print(f"⚠️ {pkg:<30} 版本未知")
|
||
else:
|
||
print(f"❌ {pkg:<30} 未安装")
|
||
except Exception as e:
|
||
print(f"❌ {pkg:<30} 检查失败: {str(e)[:50]}")
|
||
|
||
print()
|
||
|
||
|
||
def check_file_modification():
|
||
"""检查关键文件的修改时间"""
|
||
print("=" * 80)
|
||
print("文件检查")
|
||
print("=" * 80)
|
||
print()
|
||
|
||
critical_files = [
|
||
"src/graphs/nodes/recognize_and_correct_node.py",
|
||
"src/graphs/nodes/doc_extract_node.py",
|
||
"src/main.py",
|
||
]
|
||
|
||
for file_path in critical_files:
|
||
try:
|
||
if os.path.exists(file_path):
|
||
mtime = os.path.getmtime(file_path)
|
||
mtime_str = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')
|
||
print(f"✅ {file_path:<50} {mtime_str}")
|
||
|
||
# 检查是否包含 new_context
|
||
with open(file_path, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
if 'new_context' in content:
|
||
print(f" ✓ 包含 new_context()")
|
||
else:
|
||
print(f" ✗ 不包含 new_context()(需要更新代码)")
|
||
else:
|
||
print(f"❌ {file_path:<50} 文件不存在")
|
||
except Exception as e:
|
||
print(f"❌ {file_path:<50} 检查失败")
|
||
|
||
print()
|
||
|
||
|
||
def test_llm_with_correct_endpoint():
|
||
"""测试使用正确端点的 LLM 调用"""
|
||
print("=" * 80)
|
||
print("LLM 调用测试")
|
||
print("=" * 80)
|
||
print()
|
||
|
||
try:
|
||
from coze_coding_dev_sdk import LLMClient, Config
|
||
from coze_coding_utils.runtime_ctx.context import new_context
|
||
from langchain_core.messages import HumanMessage
|
||
|
||
# 使用正确的端点
|
||
ctx = new_context(method="invoke")
|
||
|
||
# 尝试不同的配置
|
||
configs = [
|
||
("默认配置", None),
|
||
("集成端点", Config(base_url="https://integration.coze.cn/api")),
|
||
("V1 端点", Config(base_url="https://api.coze.cn/v1")),
|
||
]
|
||
|
||
for name, config in configs:
|
||
try:
|
||
if config:
|
||
client = LLMClient(config=config, ctx=ctx)
|
||
else:
|
||
client = LLMClient(ctx=ctx)
|
||
|
||
messages = [HumanMessage(content="测试")]
|
||
|
||
print(f"正在测试: {name}...")
|
||
response = client.invoke(
|
||
messages=messages,
|
||
model="doubao-seed-2-0-lite-260215",
|
||
temperature=0.1,
|
||
max_completion_tokens=10
|
||
)
|
||
|
||
print(f" ✅ 成功!")
|
||
|
||
except Exception as e:
|
||
error_str = str(e)
|
||
if "404" in error_str:
|
||
print(f" ❌ 失败:404 Not Found(端点错误)")
|
||
elif "401" in error_str:
|
||
print(f" ❌ 失败:401 Unauthorized(认证失败)")
|
||
else:
|
||
print(f" ❌ 失败:{error_str[:100]}")
|
||
|
||
except ImportError as e:
|
||
print(f"❌ 无法导入 LLM 客户端: {str(e)}")
|
||
|
||
print()
|
||
|
||
|
||
def provide_fix_suggestions():
|
||
"""提供修复建议"""
|
||
print("=" * 80)
|
||
print("修复建议")
|
||
print("=" * 80)
|
||
print()
|
||
|
||
print("如果 LLM 调用失败,请按以下步骤修复:")
|
||
print()
|
||
print("1. 更新代码(推荐)")
|
||
print(" git pull")
|
||
print()
|
||
print("2. 更新 SDK")
|
||
print(" pip install --upgrade coze-coding-dev-sdk")
|
||
print()
|
||
print("3. 检查代码是否包含 new_context()")
|
||
print(" grep -r 'new_context' src/graphs/nodes/")
|
||
print()
|
||
print("4. 如果代码已更新但仍有问题,尝试重启服务")
|
||
print(" docker restart <container_id>")
|
||
print()
|
||
print("5. 如果仍然失败,请联系技术支持并提供:")
|
||
print(" - SDK 版本号")
|
||
print(" - 完整的错误日志")
|
||
print(" - Docker 镜像信息")
|
||
|
||
|
||
def main():
|
||
print()
|
||
print("=" * 80)
|
||
print("Docker 环境问题诊断")
|
||
print("=" * 80)
|
||
print()
|
||
print("此脚本将诊断 Docker 环境中的问题并提供修复建议")
|
||
print()
|
||
|
||
check_version()
|
||
check_file_modification()
|
||
test_llm_with_correct_endpoint()
|
||
provide_fix_suggestions()
|
||
|
||
print("=" * 80)
|
||
print("诊断完成")
|
||
print("=" * 80)
|
||
print()
|
||
print("请根据上面的诊断结果采取相应的修复措施")
|
||
print()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|