Spaces:
Running
Running
File size: 3,963 Bytes
907b200 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Http;
class AiServicesIntegrationController extends Controller
{
public function health(): JsonResponse
{
$checks = [
'admet' => config('services.admet.url'),
'chemical_rag' => config('services.chemical_ai.url'),
'drug_repurposing' => config('services.drug_repurposing.url'),
];
$results = [];
foreach ($checks as $name => $baseUrl) {
$results[$name] = $this->probeHealth($baseUrl);
usleep(200_000);
}
$allHealthy = collect($results)->every(
fn (array $result) => ($result['status'] ?? '') === 'healthy'
);
return response()->json([
'success' => $allHealthy,
'services' => $results,
], $allHealthy ? 200 : 503);
}
public function testAdmet(Request $request): JsonResponse
{
$baseUrl = rtrim((string) config('services.admet.url'), '/');
$smiles = (string) $request->input('smiles', 'c1ccccc1');
$response = Http::timeout(180)->post("{$baseUrl}/predict", [
'smiles' => $smiles,
]);
return $this->proxyResponse('admet', $response);
}
public function testChemicalSearch(Request $request): JsonResponse
{
$baseUrl = rtrim((string) config('services.chemical_ai.url'), '/');
$smiles = (string) $request->input('smiles', 'CCO');
$topK = (int) $request->input('top_k', 3);
$response = Http::timeout(180)->post("{$baseUrl}/search/retrieval-only", [
'smiles' => $smiles,
'top_k' => $topK,
]);
return $this->proxyResponse('chemical_rag', $response);
}
public function testDrugRepurposing(): JsonResponse
{
$baseUrl = rtrim((string) config('services.drug_repurposing.url'), '/');
$health = Http::timeout(60)->retry(3, 500)->withUserAgent('AILIXIR-Internal/1.0')->get("{$baseUrl}/health");
usleep(200_000);
$modelStatus = Http::timeout(60)->retry(3, 500)->withUserAgent('AILIXIR-Internal/1.0')->get("{$baseUrl}/api/v1/model-status");
$success = $health->successful() && $modelStatus->successful();
return response()->json([
'success' => $success,
'service' => 'drug_repurposing',
'health' => [
'status' => $health->status(),
'body' => $health->json(),
],
'model_status' => [
'status' => $modelStatus->status(),
'body' => $modelStatus->json(),
],
], $success ? 200 : 502);
}
private function probeHealth(?string $baseUrl): array
{
if (empty($baseUrl)) {
return [
'status' => 'misconfigured',
'error' => 'URL not configured',
];
}
try {
$response = Http::timeout(60)->retry(3, 500)->withUserAgent('AILIXIR-Internal/1.0')->get(rtrim($baseUrl, '/').'/health');
return [
'status' => $response->successful() ? 'healthy' : 'unhealthy',
'http_status' => $response->status(),
'body' => $response->json() ?? $response->body(),
];
} catch (\Throwable $e) {
return [
'status' => 'unreachable',
'error' => $e->getMessage(),
];
}
}
private function proxyResponse(string $service, \Illuminate\Http\Client\Response $response): JsonResponse
{
return response()->json([
'success' => $response->successful(),
'service' => $service,
'upstream_status' => $response->status(),
'data' => $response->json(),
], $response->successful() ? 200 : 502);
}
}
|