Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ImageAnalyzer | |
0.00% |
0 / 35 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| analyze | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| createImagePayload | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| getExpectedKeys | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getLoggerName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Service\Api\Anthropic; |
| 5 | |
| 6 | use InvalidArgumentException; |
| 7 | |
| 8 | /** |
| 9 | * ImageAnalyzer Class |
| 10 | * |
| 11 | * This class is responsible for analyzing images using the Anthropic API service. |
| 12 | * It interacts with the AI prompts table to retrieve prompt data and uses the AnthropicApiService |
| 13 | * to send requests and parse responses for image analysis. |
| 14 | */ |
| 15 | class ImageAnalyzer extends AbstractAnthropicGenerator |
| 16 | { |
| 17 | /** |
| 18 | * Analyzes an image using the Anthropic API. |
| 19 | * |
| 20 | * This method performs the following steps: |
| 21 | * 1. Validates the existence of the image file. |
| 22 | * 2. Encodes the image file to base64 and determines its MIME type. |
| 23 | * 3. Retrieves the appropriate prompt data for image analysis. |
| 24 | * 4. Creates a payload with the image data and prompt information. |
| 25 | * 5. Sends a request to the Anthropic API and processes the response. |
| 26 | * |
| 27 | * @param string $imagePath The path to the image file to be analyzed. |
| 28 | * @return array The analysis results from the API, containing various aspects of the image analysis. |
| 29 | * @throws \InvalidArgumentException If the image file is not found or the task prompt data is not found. |
| 30 | */ |
| 31 | public function analyze(string $imagePath): array |
| 32 | { |
| 33 | if (!file_exists($imagePath)) { |
| 34 | throw new InvalidArgumentException("Image file not found: {$imagePath}"); |
| 35 | } |
| 36 | |
| 37 | $imageData = base64_encode(file_get_contents($imagePath)); |
| 38 | $mimeType = mime_content_type($imagePath); |
| 39 | |
| 40 | $promptData = $this->getPromptData('image_analysis'); |
| 41 | $payload = $this->createImagePayload($promptData, $imageData, $mimeType); |
| 42 | |
| 43 | $result = $this->sendApiRequest($payload); |
| 44 | |
| 45 | return $this->ensureExpectedKeys($result); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a specialized payload for image analysis requests. |
| 50 | * |
| 51 | * @param array $promptData The prompt data retrieved from the AI prompts table. |
| 52 | * @param string $imageData The base64 encoded image data. |
| 53 | * @param string $mimeType The MIME type of the image. |
| 54 | * @return array The created payload for the API request. |
| 55 | */ |
| 56 | private function createImagePayload(array $promptData, string $imageData, string $mimeType): array |
| 57 | { |
| 58 | return [ |
| 59 | 'model' => $promptData['model'], |
| 60 | 'max_tokens' => $promptData['max_tokens'], |
| 61 | 'temperature' => $promptData['temperature'], |
| 62 | 'system' => $promptData['system_prompt'], |
| 63 | 'messages' => [ |
| 64 | [ |
| 65 | 'role' => 'user', |
| 66 | 'content' => [ |
| 67 | [ |
| 68 | 'type' => 'image', |
| 69 | 'source' => [ |
| 70 | 'type' => 'base64', |
| 71 | 'media_type' => $mimeType, |
| 72 | 'data' => $imageData, |
| 73 | ], |
| 74 | ], |
| 75 | ], |
| 76 | ], |
| 77 | ], |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the expected keys for the API response. |
| 83 | * |
| 84 | * @return array Array of expected response keys. |
| 85 | */ |
| 86 | protected function getExpectedKeys(): array |
| 87 | { |
| 88 | return [ |
| 89 | 'name', |
| 90 | 'alt_text', |
| 91 | 'keywords', |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Gets the logger name for this analyzer. |
| 97 | * |
| 98 | * @return string The logger name. |
| 99 | */ |
| 100 | protected function getLoggerName(): string |
| 101 | { |
| 102 | return 'Image Analyzer'; |
| 103 | } |
| 104 | } |