Files
sam-manage/app/Http/Controllers/System/AiConfigController.php
pro d824b45fc0 feat:AI 설정에 Vertex AI 서비스 계정 인증 방식 추가
- AiConfig 모델에 Vertex AI 헬퍼 메소드 추가
- AI 설정 UI에 인증 방식 선택 (API 키 / Vertex AI)
- Vertex AI 선택 시 프로젝트 ID, 리전, 서비스 계정 경로 입력
- BusinessCardOcrService가 DB 설정 기반으로 동작
- Google AI Studio와 Vertex AI 모두 지원

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 08:08:30 +09:00

229 lines
7.1 KiB
PHP

<?php
namespace App\Http\Controllers\System;
use App\Http\Controllers\Controller;
use App\Models\System\AiConfig;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
class AiConfigController extends Controller
{
/**
* AI 설정 목록
*/
public function index(Request $request): View|Response
{
if ($request->header('HX-Request')) {
return response('', 200)->header('HX-Redirect', route('system.ai-config.index'));
}
$configs = AiConfig::orderBy('provider')
->orderByDesc('is_active')
->orderBy('name')
->get();
return view('system.ai-config.index', compact('configs'));
}
/**
* AI 설정 저장
*/
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'name' => 'required|string|max:50',
'provider' => 'required|string|in:gemini,claude,openai',
'api_key' => 'nullable|string|max:255',
'model' => 'required|string|max:100',
'base_url' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
'options' => 'nullable|array',
'options.auth_type' => 'nullable|string|in:api_key,vertex_ai',
'options.project_id' => 'nullable|string|max:100',
'options.region' => 'nullable|string|max:50',
'options.service_account_path' => 'nullable|string|max:500',
]);
// Vertex AI가 아닌 경우 API 키 필수
$authType = $validated['options']['auth_type'] ?? 'api_key';
if ($authType !== 'vertex_ai' && empty($validated['api_key'])) {
return response()->json([
'ok' => false,
'message' => 'API 키를 입력해주세요.',
], 422);
}
// 활성화 시 동일 provider의 다른 설정 비활성화
if ($validated['is_active'] ?? false) {
AiConfig::where('provider', $validated['provider'])
->update(['is_active' => false]);
}
$config = AiConfig::create($validated);
return response()->json([
'ok' => true,
'message' => '저장되었습니다.',
'data' => $config,
]);
}
/**
* AI 설정 수정
*/
public function update(Request $request, int $id): JsonResponse
{
$config = AiConfig::findOrFail($id);
$validated = $request->validate([
'name' => 'required|string|max:50',
'provider' => 'required|string|in:gemini,claude,openai',
'api_key' => 'nullable|string|max:255',
'model' => 'required|string|max:100',
'base_url' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean',
'options' => 'nullable|array',
'options.auth_type' => 'nullable|string|in:api_key,vertex_ai',
'options.project_id' => 'nullable|string|max:100',
'options.region' => 'nullable|string|max:50',
'options.service_account_path' => 'nullable|string|max:500',
]);
// Vertex AI가 아닌 경우 API 키 필수
$authType = $validated['options']['auth_type'] ?? 'api_key';
if ($authType !== 'vertex_ai' && empty($validated['api_key'])) {
return response()->json([
'ok' => false,
'message' => 'API 키를 입력해주세요.',
], 422);
}
// 활성화 시 동일 provider의 다른 설정 비활성화
if ($validated['is_active'] ?? false) {
AiConfig::where('provider', $validated['provider'])
->where('id', '!=', $id)
->update(['is_active' => false]);
}
$config->update($validated);
return response()->json([
'ok' => true,
'message' => '수정되었습니다.',
'data' => $config->fresh(),
]);
}
/**
* AI 설정 삭제
*/
public function destroy(int $id): JsonResponse
{
$config = AiConfig::findOrFail($id);
$config->delete();
return response()->json([
'ok' => true,
'message' => '삭제되었습니다.',
]);
}
/**
* AI 설정 활성화/비활성화 토글
*/
public function toggle(int $id): JsonResponse
{
$config = AiConfig::findOrFail($id);
if (! $config->is_active) {
// 활성화 시 동일 provider의 다른 설정 비활성화
AiConfig::where('provider', $config->provider)
->where('id', '!=', $id)
->update(['is_active' => false]);
}
$config->update(['is_active' => ! $config->is_active]);
return response()->json([
'ok' => true,
'message' => $config->is_active ? '활성화되었습니다.' : '비활성화되었습니다.',
'data' => $config->fresh(),
]);
}
/**
* API 연결 테스트
*/
public function test(Request $request): JsonResponse
{
$validated = $request->validate([
'provider' => 'required|string|in:gemini,claude,openai',
'api_key' => 'required|string',
'model' => 'required|string',
'base_url' => 'nullable|string',
]);
try {
$provider = $validated['provider'];
$apiKey = $validated['api_key'];
$model = $validated['model'];
$baseUrl = $validated['base_url'] ?? AiConfig::DEFAULT_BASE_URLS[$provider];
if ($provider === 'gemini') {
$result = $this->testGemini($baseUrl, $model, $apiKey);
} else {
return response()->json([
'ok' => false,
'error' => '아직 지원하지 않는 provider입니다.',
]);
}
return response()->json($result);
} catch (\Exception $e) {
return response()->json([
'ok' => false,
'error' => $e->getMessage(),
]);
}
}
/**
* Gemini API 테스트
*/
private function testGemini(string $baseUrl, string $model, string $apiKey): array
{
$url = "{$baseUrl}/models/{$model}:generateContent?key={$apiKey}";
$response = \Illuminate\Support\Facades\Http::timeout(10)->post($url, [
'contents' => [
[
'parts' => [
['text' => '안녕하세요. 테스트입니다. "OK"라고만 응답해주세요.'],
],
],
],
'generationConfig' => [
'temperature' => 0,
'maxOutputTokens' => 10,
],
]);
if ($response->successful()) {
return [
'ok' => true,
'message' => '연결 테스트 성공',
];
}
return [
'ok' => false,
'error' => 'API 응답 오류: ' . $response->status(),
];
}
}