Skip to content

Commit 3e577c1

Browse files
committed
feat: 添加完整的AI聊天API功能
- 新增AIChatController控制器,支持真实AI聊天 - 添加纯文本端点(/chat/text)和JSON端点(/chat) - 新增快速测试端点(/ping, /echo) - 添加完整的日志记录和错误处理 - 创建多种测试脚本(PowerShell, Bash) - 添加权限修复脚本 - 更新配置文件支持DeepSeek API - 添加启动脚本和Docker支持
1 parent 8bcedd9 commit 3e577c1

File tree

17 files changed

+829
-13
lines changed

17 files changed

+829
-13
lines changed

.env.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 测试环境配置
2+
HOST_PORT=8081
3+
AI_API_KEY=demo-mode-test
4+
AI_MODEL=deepseek-chat
5+
AI_BASE_URL=https://api.deepseek.com
6+
AI_ENABLED=true

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ COPY src src
2121
RUN mvn clean package -DskipTests
2222

2323
# 第二阶段:运行阶段
24-
FROM openjdk:17-jre-slim
24+
FROM eclipse-temurin:17-jre
2525

2626
# 设置时区
2727
ENV TZ=Asia/Shanghai

ai-chat.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
# 简单的AI聊天脚本
3+
4+
API_URL="http://localhost:8080/api/v1/chat/text"
5+
6+
# 如果没有参数,显示帮助
7+
if [ $# -eq 0 ]; then
8+
echo "使用方法:"
9+
echo " $0 \"你的问题\" # 提问一个问题"
10+
echo " $0 -i # 交互模式"
11+
echo " $0 -s # 检查API状态"
12+
echo " $0 -h # 显示帮助"
13+
exit 0
14+
fi
15+
16+
case "$1" in
17+
-i|--interactive)
18+
echo "🤖 AI聊天交互模式 (输入'退出'结束)"
19+
echo "================================="
20+
while true; do
21+
echo -n "你: "
22+
read question
23+
if [ "$question" = "退出" ] || [ "$question" = "exit" ]; then
24+
echo "再见!"
25+
break
26+
fi
27+
28+
echo -n "AI: "
29+
curl -s -X POST "$API_URL" \
30+
-H "Content-Type: application/json" \
31+
-d "{\"message\": \"$question\"}"
32+
echo ""
33+
done
34+
;;
35+
36+
-s|--status)
37+
echo "🔍 检查API状态..."
38+
curl -s http://localhost:8080/api/v1/status | python3 -c "
39+
import json, sys
40+
data = json.load(sys.stdin)
41+
print('✅ 服务:', data['service'])
42+
print('📊 状态:', data['status'])
43+
print('🤖 模型:', data['model'])
44+
print('🔑 API配置:', data['api_configured'])
45+
print('🌐 基础URL:', data['base_url'])
46+
"
47+
;;
48+
49+
-h|--help)
50+
echo "AI聊天客户端"
51+
echo "命令:"
52+
echo " -i, --interactive 交互模式"
53+
echo " -s, --status 检查API状态"
54+
echo " -h, --help 显示帮助"
55+
echo " 其他任何文本 提问问题"
56+
;;
57+
58+
*)
59+
# 直接提问
60+
question="$*"
61+
echo "提问: $question"
62+
echo -n "回答: "
63+
curl -s -X POST "$API_URL" \
64+
-H "Content-Type: application/json" \
65+
-d "{\"message\": \"$question\"}"
66+
echo ""
67+
;;
68+
esac

docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export JAVA_OPTS="$JAVA_OPTS -Dmanagement.endpoint.health.show-details=always"
2727
export JAVA_OPTS="$JAVA_OPTS -Dlogging.file.name=/app/logs/application.log"
2828
export JAVA_OPTS="$JAVA_OPTS -Dlogging.file.max-size=10MB"
2929
export JAVA_OPTS="$JAVA_OPTS -Dlogging.file.max-history=10"
30-
export JAVA_OPTS="$JAVA_OPTS -Dlogging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
30+
export JAVA_OPTS="$JAVA_OPTS -Dlogging.pattern.console='%d{yyyy-MM-dd HH:mm:ss} - %msg%n'"
3131

3232
# 应用配置
3333
export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE:-docker}"

fix-permissions.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
# 修复权限脚本
3+
4+
echo "🔧 修复Java AI Starter项目权限"
5+
echo "==============================="
6+
7+
# 检查当前用户
8+
echo "当前用户: $(whoami)"
9+
echo "当前目录: $(pwd)"
10+
11+
# 检查项目目录
12+
if [ ! -d "logs" ]; then
13+
echo "❌ logs目录不存在"
14+
mkdir -p logs
15+
echo "✅ 已创建logs目录"
16+
fi
17+
18+
# 修复目录权限
19+
echo "修复目录权限..."
20+
find . -type d -exec chmod 755 {} \;
21+
echo "✅ 目录权限已修复"
22+
23+
# 修复文件权限
24+
echo "修复文件权限..."
25+
find . -type f -exec chmod 644 {} \;
26+
27+
# 修复可执行文件
28+
echo "修复可执行文件权限..."
29+
find . -name "*.sh" -type f -exec chmod +x {} \;
30+
find . -name "*.jar" -type f -exec chmod +x {} \;
31+
32+
# 修复特定文件
33+
echo "修复特定文件权限..."
34+
chmod 755 logs 2>/dev/null
35+
chmod 644 logs/application.log 2>/dev/null
36+
37+
# 检查权限
38+
echo ""
39+
echo "📋 权限检查:"
40+
echo "logs目录: $(ls -ld logs)"
41+
echo "日志文件: $(ls -l logs/application.log 2>/dev/null || echo '日志文件不存在')"
42+
43+
# 测试读取
44+
echo ""
45+
echo "🧪 测试读取日志:"
46+
if [ -f "logs/application.log" ]; then
47+
echo "最后3行日志:"
48+
tail -3 logs/application.log
49+
echo "✅ 日志读取成功"
50+
else
51+
echo "⚠️ 日志文件不存在,但目录权限已修复"
52+
fi
53+
54+
echo ""
55+
echo "✅ 权限修复完成"
56+
echo ""
57+
echo "📋 现在可以运行:"
58+
echo " tail -f logs/application.log"
59+
echo " ./view-logs.sh tail"

mvnw

100755100644
File mode changed.

powershell-test.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# PowerShell AI聊天API测试脚本 - 无乱码版本
2+
3+
Write-Host "🤖 AI聊天API测试" -ForegroundColor Cyan
4+
Write-Host "=================" -ForegroundColor Cyan
5+
6+
# 测试纯文本端点
7+
Write-Host "`n1. 测试纯文本端点 (推荐)" -ForegroundColor Green
8+
$questions = @(
9+
"你好,请用中文介绍一下你自己",
10+
"今天天气怎么样?",
11+
"讲一个简短的笑话",
12+
"用一句话说明什么是人工智能"
13+
)
14+
15+
foreach ($q in $questions) {
16+
Write-Host "`n提问: $q" -ForegroundColor Yellow
17+
$body = @{message = $q} | ConvertTo-Json
18+
try {
19+
$response = Invoke-RestMethod -Uri "http://localhost:8080/api/v1/chat/text" -Method Post -Body $body -ContentType "application/json" -ErrorAction Stop
20+
Write-Host "回答: $response" -ForegroundColor White
21+
} catch {
22+
Write-Host "错误: $_" -ForegroundColor Red
23+
}
24+
Start-Sleep -Milliseconds 500
25+
}
26+
27+
# 测试状态检查
28+
Write-Host "`n2. 测试API状态" -ForegroundColor Green
29+
try {
30+
$status = Invoke-RestMethod -Uri "http://localhost:8080/api/v1/status" -Method Get -ErrorAction Stop
31+
Write-Host "✅ API状态正常" -ForegroundColor Green
32+
Write-Host " 服务: $($status.service)" -ForegroundColor White
33+
Write-Host " 状态: $($status.status)" -ForegroundColor White
34+
Write-Host " 模型: $($status.model)" -ForegroundColor White
35+
Write-Host " API配置: $($status.api_configured)" -ForegroundColor White
36+
} catch {
37+
Write-Host "❌ 状态检查失败: $_" -ForegroundColor Red
38+
}
39+
40+
# 交互模式
41+
Write-Host "`n3. 交互模式 (输入'退出'结束)" -ForegroundColor Green
42+
while ($true) {
43+
$userInput = Read-Host "`n你的问题"
44+
if ($userInput -eq "退出" -or $userInput -eq "exit") {
45+
break
46+
}
47+
48+
$body = @{message = $userInput} | ConvertTo-Json
49+
try {
50+
$response = Invoke-RestMethod -Uri "http://localhost:8080/api/v1/chat/text" -Method Post -Body $body -ContentType "application/json" -ErrorAction Stop
51+
Write-Host "`n🤖 AI: $response" -ForegroundColor Cyan
52+
} catch {
53+
Write-Host "错误: $_" -ForegroundColor Red
54+
}
55+
}
56+
57+
Write-Host "`n✅ 测试完成" -ForegroundColor Green
58+
Write-Host "`n📋 快速命令参考:" -ForegroundColor Yellow
59+
Write-Host " # 一句话测试" -ForegroundColor Gray
60+
Write-Host " Invoke-RestMethod -Uri 'http://localhost:8080/api/v1/chat/text' -Method Post -Body (@{message='你好'} | ConvertTo-Json) -ContentType 'application/json'" -ForegroundColor White
61+
Write-Host "`n # 使用curl.exe" -ForegroundColor Gray
62+
Write-Host " curl.exe -s -X POST http://localhost:8080/api/v1/chat/text -H 'Content-Type: application/json' -d '{\"message\":\"你好\"}'" -ForegroundColor White

run-docker-simple.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
# 简单的Docker运行脚本
3+
4+
echo "🚀 启动Java AI Starter (简单Docker模式)"
5+
6+
# 检查是否已有容器在运行
7+
if docker ps | grep -q "java-ai-simple"; then
8+
echo "停止现有容器..."
9+
docker stop java-ai-simple
10+
docker rm java-ai-simple
11+
fi
12+
13+
# 使用现有jar文件运行
14+
echo "构建Docker镜像..."
15+
docker build -t java-ai-simple:latest .
16+
17+
echo "启动容器..."
18+
docker run -d \
19+
--name java-ai-simple \
20+
-p 8080:8080 \
21+
-e AI_API_KEY=${AI_API_KEY:-} \
22+
-e AI_MODEL=deepseek-chat \
23+
-e AI_BASE_URL=https://api.deepseek.com \
24+
java-ai-simple:latest
25+
26+
echo "✅ 容器已启动"
27+
echo "📊 访问地址: http://localhost:8080"
28+
echo "🔍 查看日志: docker logs -f java-ai-simple"
29+
echo "🛑 停止容器: docker stop java-ai-simple"

simple-test.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 最简单的测试脚本
2+
3+
# 测试聊天
4+
$body = @{message = "你好,请用中文介绍一下你自己"} | ConvertTo-Json
5+
$response = Invoke-RestMethod -Uri "http://localhost:8080/api/v1/chat" -Method Post -Body $body -ContentType "application/json"
6+
7+
Write-Host "=" * 50
8+
Write-Host "🤖 AI回复:" -ForegroundColor Yellow
9+
Write-Host $response.message
10+
Write-Host "=" * 50
11+
Write-Host ""
12+
Write-Host "📊 统计信息:" -ForegroundColor Cyan
13+
Write-Host "模型: $($response.model)"
14+
Write-Host "成功: $($response.success)"
15+
Write-Host "总Token: $($response.tokens_used.total_tokens)"
16+
Write-Host "时间戳: $(Get-Date -Date '1970-01-01').AddMilliseconds($response.timestamp)"

0 commit comments

Comments
 (0)