新增:AI 接口连通性测试功能;修复:Ollama 超时问题

- 后台 AI 厂商列表新增「 测试」按钮,实时验证接口连通性
- 显示响应耗时(含冷启动)和模型返回内容
- AiChatService 请求超时从 30s 调整为 120s(兼容 Ollama 本地冷启动)
- 测试接口超时设为 60s
This commit is contained in:
2026-03-06 03:29:13 +08:00
parent 6c9db806ae
commit 318eb6f234
4 changed files with 101 additions and 4 deletions
@@ -200,6 +200,67 @@ class AiProviderController extends Controller
]);
}
/**
* 测试指定 AI 厂商的接口连通性
*
* 发送一条简短的测试消息,返回响应结果和耗时,用于验证配置是否正确。
*
* @param int $id 厂商配置 ID
* @return JsonResponse 测试结果
*/
public function testConnection(int $id): JsonResponse
{
$provider = AiProviderConfig::findOrFail($id);
$apiKey = $provider->getDecryptedApiKey();
$base = rtrim($provider->api_endpoint, '/');
$endpoint = str_ends_with($base, '/v1')
? $base.'/chat/completions'
: $base.'/v1/chat/completions';
$startTime = microtime(true);
try {
$response = \Illuminate\Support\Facades\Http::withToken($apiKey)
->timeout(60) // Ollama 本地模型冷启动较慢,给 60s
->post($endpoint, [
'model' => $provider->model,
'messages' => [
['role' => 'user', 'content' => '请用一句话介绍你自己。'],
],
'max_tokens' => 64,
]);
$ms = (int) ((microtime(true) - $startTime) * 1000);
$data = $response->json();
if (! $response->successful()) {
return response()->json([
'ok' => false,
'message' => "HTTP {$response->status()}{$response->body()}",
'ms' => $ms,
]);
}
$reply = $data['choices'][0]['message']['content'] ?? '(无回复内容)';
return response()->json([
'ok' => true,
'message' => trim($reply),
'ms' => $ms,
'model' => $data['model'] ?? $provider->model,
]);
} catch (\Illuminate\Http\Client\ConnectionException $e) {
$ms = (int) ((microtime(true) - $startTime) * 1000);
return response()->json([
'ok' => false,
'message' => '连接失败:'.$e->getMessage(),
'ms' => $ms,
]);
}
}
/**
* 删除 AI 厂商配置
*