# =========================================== # System Knowledge Base - Document Upload Test # =========================================== # 测试文档上传功能,包含 OSS 存储和 RAG 向量化 $baseUrl = "http://localhost:3001" $phone = "13800000001" # SUPER_ADMIN $password = "123456" $testFile = "D:\MyCursor\AIclinicalresearch\docs\06-测试文档\近红外光谱(NIRS)队列研究举例.pdf" Write-Host "=============================================" -ForegroundColor Cyan Write-Host "System KB - Document Upload Test" -ForegroundColor Cyan Write-Host "=============================================" -ForegroundColor Cyan # 检查测试文件是否存在 if (-not (Test-Path $testFile)) { Write-Host "[ERROR] Test file not found: $testFile" -ForegroundColor Red exit 1 } $fileInfo = Get-Item $testFile Write-Host "Test file: $($fileInfo.Name)" -ForegroundColor Gray Write-Host "File size: $([math]::Round($fileInfo.Length / 1024, 2)) KB" -ForegroundColor Gray # 1. Login Write-Host "`n[1/6] Login..." -ForegroundColor Yellow $loginBody = @{ phone = $phone password = $password } | ConvertTo-Json try { $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/auth/login/password" ` -Method POST ` -Body $loginBody ` -ContentType "application/json" $token = $loginResponse.data.tokens.accessToken Write-Host " [OK] Login success!" -ForegroundColor Green } catch { Write-Host " [FAIL] Login failed: $($_.Exception.Message)" -ForegroundColor Red exit 1 } $headers = @{ "Authorization" = "Bearer $token" } # 2. Create Knowledge Base Write-Host "`n[2/6] Create Knowledge Base..." -ForegroundColor Yellow $testCode = "TEST_UPLOAD_$(Get-Date -Format 'yyyyMMddHHmmss')" $createBody = @{ code = $testCode name = "文档上传测试知识库" description = "用于测试文档上传和 RAG 向量化功能" category = "test" } | ConvertTo-Json -Depth 10 try { $createResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb" ` -Method POST ` -Body $createBody ` -ContentType "application/json; charset=utf-8" ` -Headers $headers $kbId = $createResponse.data.id Write-Host " [OK] Created! ID: $kbId" -ForegroundColor Green Write-Host " Code: $($createResponse.data.code)" -ForegroundColor Gray } catch { Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red exit 1 } # 3. Upload Document Write-Host "`n[3/6] Upload Document (may take a while for RAG processing)..." -ForegroundColor Yellow try { # 使用 curl 上传文件(PowerShell 的 Invoke-RestMethod 对 multipart 支持不好) $uploadResult = curl.exe -s -X POST "$baseUrl/api/v1/admin/system-kb/$kbId/documents" ` -H "Authorization: Bearer $token" ` -F "file=@$testFile" | ConvertFrom-Json if ($uploadResult.success) { Write-Host " [OK] Upload success!" -ForegroundColor Green Write-Host " ----------------------------" -ForegroundColor Gray Write-Host " Doc ID: $($uploadResult.data.docId)" -ForegroundColor White Write-Host " Filename: $($uploadResult.data.filename)" -ForegroundColor White Write-Host " Chunks: $($uploadResult.data.chunkCount)" -ForegroundColor White Write-Host " Tokens: $($uploadResult.data.tokenCount)" -ForegroundColor White Write-Host " Duration: $($uploadResult.data.duration) ms" -ForegroundColor White $docId = $uploadResult.data.docId } else { Write-Host " [FAIL]: $($uploadResult.error)" -ForegroundColor Red $docId = $null } } catch { Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red $docId = $null } # 4. List Documents Write-Host "`n[4/6] List Documents..." -ForegroundColor Yellow try { $docsResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb/$kbId/documents" ` -Method GET ` -Headers $headers $docCount = $docsResponse.data.Count Write-Host " [OK] Total: $docCount documents" -ForegroundColor Green if ($docCount -gt 0) { Write-Host " ----------------------------" -ForegroundColor Gray $docsResponse.data | ForEach-Object { Write-Host " - $($_.filename) | Status: $($_.status) | Tokens: $($_.tokenCount)" -ForegroundColor Gray Write-Host " Path: $($_.filePath)" -ForegroundColor DarkGray } } } catch { Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red } # 5. Get KB Detail (check updated stats) Write-Host "`n[5/6] Get KB Detail (verify stats updated)..." -ForegroundColor Yellow try { $detailResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb/$kbId" ` -Method GET ` -Headers $headers Write-Host " [OK] Success!" -ForegroundColor Green Write-Host " ----------------------------" -ForegroundColor Gray Write-Host " Document Count: $($detailResponse.data.documentCount)" -ForegroundColor White Write-Host " Total Tokens: $($detailResponse.data.totalTokens)" -ForegroundColor White } catch { Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red } # 6. Cleanup - Delete KB Write-Host "`n[6/6] Cleanup - Delete Knowledge Base..." -ForegroundColor Yellow try { $deleteResponse = Invoke-RestMethod -Uri "$baseUrl/api/v1/admin/system-kb/$kbId" ` -Method DELETE ` -Headers $headers Write-Host " [OK] Deleted!" -ForegroundColor Green } catch { Write-Host " [FAIL]: $($_.Exception.Message)" -ForegroundColor Red } Write-Host "`n=============================================" -ForegroundColor Cyan Write-Host "Test Complete!" -ForegroundColor Cyan Write-Host "=============================================" -ForegroundColor Cyan if ($docId) { Write-Host "`n[Summary]" -ForegroundColor Green Write-Host " - Document uploaded successfully" -ForegroundColor White Write-Host " - RAG vectorization completed" -ForegroundColor White Write-Host " - OSS path: system/knowledge-bases/$kbId/$docId.pdf" -ForegroundColor White } else { Write-Host "`n[Summary]" -ForegroundColor Yellow Write-Host " - Document upload may have failed" -ForegroundColor White Write-Host " - Check backend logs for details" -ForegroundColor White }