diff --git a/app/Services/BusinessCardOcrService.php b/app/Services/BusinessCardOcrService.php index d3ff331b..15bf9034 100644 --- a/app/Services/BusinessCardOcrService.php +++ b/app/Services/BusinessCardOcrService.php @@ -53,7 +53,7 @@ private function callVertexAiApi(AiConfig $config, string $base64Image): array return $this->callGeminiApi($url, $base64Image, [ 'Authorization' => 'Bearer ' . $accessToken, 'Content-Type' => 'application/json', - ]); + ], true); // Vertex AI } /** @@ -69,13 +69,13 @@ private function callGoogleAiStudioApi(AiConfig $config, string $base64Image): a return $this->callGeminiApi($url, $base64Image, [ 'Content-Type' => 'application/json', - ]); + ], false); // Google AI Studio } /** * Gemini API 공통 호출 로직 */ - private function callGeminiApi(string $url, string $base64Image, array $headers): array + private function callGeminiApi(string $url, string $base64Image, array $headers, bool $isVertexAi = false): array { // Base64 데이터에서 prefix 제거 $imageData = $base64Image; @@ -88,25 +88,30 @@ private function callGeminiApi(string $url, string $base64Image, array $headers) $prompt = $this->buildPrompt(); + // Vertex AI는 role 필드 필요 + $content = [ + 'parts' => [ + [ + 'inlineData' => [ + 'mimeType' => $mimeType, + 'data' => $imageData, + ], + ], + [ + 'text' => $prompt, + ], + ], + ]; + + if ($isVertexAi) { + $content['role'] = 'user'; + } + try { $response = Http::timeout(30) ->withHeaders($headers) ->post($url, [ - 'contents' => [ - [ - 'parts' => [ - [ - 'inlineData' => [ - 'mimeType' => $mimeType, - 'data' => $imageData, - ], - ], - [ - 'text' => $prompt, - ], - ], - ], - ], + 'contents' => [$content], 'generationConfig' => [ 'temperature' => 0.1, 'topK' => 40,