Files
dock/ai
2025-11-03 18:18:46 +08:00

156 lines
4.6 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 简单的AI连接测试脚本
# 让你确认是否已经成功接入AI
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# 显示标题
echo -e "${CYAN}"
echo "╔══════════════════════════════════════╗"
echo "║ AI连接测试工具 ║"
echo "║ 确认你是否已接入AI助手 ║"
echo "╚══════════════════════════════════════╝"
echo -e "${NC}"
# 配置API密钥请先修改这里
API_KEY="sk-61d6716fe6b2452d94a0cee3bc5c4e2e"
# 测试函数简单的AI对话
test_ai_connection() {
echo -e "${BLUE}🔄 尝试连接AI助手...${NC}"
echo -e "${YELLOW}📡 发送测试请求到DeepSeek API...${NC}"
# 简单的测试请求
response=$(curl -s -X POST "https://api.deepseek.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "deepseek-coder",
"messages": [
{
"role": "system",
"content": "你是一个AI助手。请用中文简短回复确认连接成功。"
},
{
"role": "user",
"content": "请回复'随机50个字注意一定要50个字或50个字以上,只有50个字以上才能确定连接正常不然一律算连接不生产所以一定要50个字以上'来确认我们的连接正常。"
}
],
"max_tokens": 50,
"temperature": 0.1
}')
# 检查响应
if [ $? -ne 0 ]; then
echo -e "${RED}❌ 网络连接失败${NC}"
return 1
fi
# 提取AI回复
ai_reply=$(echo "$response" | grep -o '"content":"[^"]*"' | cut -d'"' -f4)
if [ -z "$ai_reply" ]; then
error_msg=$(echo "$response" | grep -o '"message":"[^"]*"' | cut -d'"' -f4)
if [ -n "$error_msg" ]; then
echo -e "${RED}❌ API错误: $error_msg${NC}"
else
echo -e "${RED}❌ 无法解析AI响应${NC}"
fi
return 1
fi
echo -e "${GREEN}✅ AI回复: $ai_reply${NC}"
return 0
}
# 检查API密钥
check_api_key() {
echo -e "${YELLOW}🔑 检查API密钥配置...${NC}"
if [ "$API_KEY" = "sk-your-api-key-here" ] || [ -z "$API_KEY" ]; then
echo -e "${RED}❌ 请先设置你的DeepSeek API密钥${NC}"
echo ""
echo -e "${CYAN}修改方法:${NC}"
echo "1. 打开脚本: nano ai_test.sh"
echo "2. 找到 API_KEY= 这一行"
echo "3. 替换为你的真实API密钥"
echo "4. 保存并重新运行脚本"
echo ""
return 1
fi
if [[ "$API_KEY" != sk-* ]]; then
echo -e "${RED}❌ API密钥格式不正确${NC}"
return 1
fi
echo -e "${GREEN}✅ API密钥格式正确${NC}"
return 0
}
# 系统信息显示
show_system_info() {
echo -e "${CYAN}🖥️ 系统信息:${NC}"
echo "主机名: $(hostname)"
echo "系统: $(uname -srm)"
echo "时间: $(date)"
echo "工作目录: $(pwd)"
echo ""
}
# 主测试流程
main_test() {
echo -e "${BLUE}开始AI连接测试...${NC}"
echo ""
# 显示系统信息
show_system_info
# 检查API密钥
if ! check_api_key; then
exit 1
fi
echo -e "${YELLOW}⏳ 正在进行连接测试...${NC}"
echo ""
# 测试AI连接
if test_ai_connection; then
echo ""
echo -e "${GREEN}🎉 恭喜AI连接测试成功${NC}"
echo -e "${GREEN}🤖 你现在已经正式接入AI助手了${NC}"
echo ""
echo -e "${CYAN}接下来你可以:${NC}"
echo "✅ 运行完整的AI修复脚本"
echo "✅ 使用AI助手解决各种问题"
echo "✅ 享受AI驱动的自动化体验"
else
echo ""
echo -e "${RED}😞 AI连接测试失败${NC}"
echo -e "${YELLOW}请检查:${NC}"
echo "🔹 API密钥是否正确"
echo "🔹 网络连接是否正常"
echo "🔹 账户余额是否充足"
echo "🔹 API密钥是否有访问权限"
fi
}
# 交互式选项
case "${1:-}" in
"help")
echo -e "${CYAN}使用方法:${NC}"
echo " ./ai_test.sh - 运行AI连接测试"
echo " ./ai_test.sh help - 显示帮助信息"
;;
*)
main_test
;;
esac