Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| AnthropicApiService | |
0.00% |
0 / 26 |
|
0.00% |
0 / 13 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| getHeaders | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| generateTagSeo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateArticleSeo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateGallerySeo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateArticleTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| analyzeImage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| analyzeComment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateTextSummary | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| translateStrings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parseResponse | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| isConfigured | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getProviderName | |
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 App\Model\Table\AipromptsTable; |
| 7 | use App\Service\Api\AbstractApiService; |
| 8 | use App\Service\Api\AiProviderInterface; |
| 9 | use App\Utility\SettingsManager; |
| 10 | use Cake\Http\Client; |
| 11 | use Cake\Http\Client\Response; |
| 12 | use Cake\ORM\TableRegistry; |
| 13 | |
| 14 | /** |
| 15 | * AnthropicApiService Class |
| 16 | * |
| 17 | * This service class provides an interface to interact with the Anthropic API, |
| 18 | * handling various AI-related tasks such as SEO content generation, image analysis, |
| 19 | * comment moderation, and text summarization. |
| 20 | */ |
| 21 | class AnthropicApiService extends AbstractApiService implements AiProviderInterface |
| 22 | { |
| 23 | /** |
| 24 | * The base URL for the Anthropic API. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | private const API_URL = 'https://api.anthropic.com/v1/messages'; |
| 29 | |
| 30 | /** |
| 31 | * The version of the Anthropic API being used. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | private const API_VERSION = '2023-06-01'; |
| 36 | |
| 37 | /** |
| 38 | * @var \App\Model\Table\AipromptsTable The table instance for AI prompts. |
| 39 | */ |
| 40 | private AipromptsTable $aipromptsTable; |
| 41 | |
| 42 | /** |
| 43 | * @var \App\Service\Api\Anthropic\SeoContentGenerator The SEO content generator service. |
| 44 | */ |
| 45 | private SeoContentGenerator $seoContentGenerator; |
| 46 | |
| 47 | /** |
| 48 | * @var \App\Service\Api\Anthropic\ImageAnalyzer The image analyzer service. |
| 49 | */ |
| 50 | private ImageAnalyzer $imageAnalyzer; |
| 51 | |
| 52 | /** |
| 53 | * @var \App\Service\Api\Anthropic\CommentAnalyzer The comment analyzer service. |
| 54 | */ |
| 55 | private CommentAnalyzer $commentAnalyzer; |
| 56 | |
| 57 | /** |
| 58 | * @var \App\Service\Api\Anthropic\ArticleTagsGenerator The article tags generator service. |
| 59 | */ |
| 60 | private ArticleTagsGenerator $articleTagsGenerator; |
| 61 | |
| 62 | /** |
| 63 | * @var \App\Service\Api\Anthropic\TextSummaryGenerator The text summary generator service. |
| 64 | */ |
| 65 | private TextSummaryGenerator $textSummaryGenerator; |
| 66 | |
| 67 | /** |
| 68 | * @var \App\Service\Api\Anthropic\TranslationGenerator The text summary generator service. |
| 69 | */ |
| 70 | private TranslationGenerator $translationGenerator; |
| 71 | |
| 72 | /** |
| 73 | * AnthropicApiService constructor. |
| 74 | * |
| 75 | * Initializes the service with necessary dependencies and configurations. |
| 76 | */ |
| 77 | public function __construct() |
| 78 | { |
| 79 | $apiKey = SettingsManager::read('Anthropic.apiKey'); |
| 80 | parent::__construct(new Client(), $apiKey, self::API_URL, self::API_VERSION); |
| 81 | |
| 82 | $this->aipromptsTable = TableRegistry::getTableLocator()->get('Aiprompts'); |
| 83 | $this->seoContentGenerator = new SeoContentGenerator($this, $this->aipromptsTable); |
| 84 | $this->imageAnalyzer = new ImageAnalyzer($this, $this->aipromptsTable); |
| 85 | $this->commentAnalyzer = new CommentAnalyzer($this, $this->aipromptsTable); |
| 86 | $this->articleTagsGenerator = new ArticleTagsGenerator($this, $this->aipromptsTable); |
| 87 | $this->textSummaryGenerator = new TextSummaryGenerator($this, $this->aipromptsTable); |
| 88 | $this->translationGenerator = new TranslationGenerator($this, $this->aipromptsTable); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Gets the headers for the API request. |
| 93 | * |
| 94 | * @return array An associative array of headers. |
| 95 | */ |
| 96 | protected function getHeaders(): array |
| 97 | { |
| 98 | return [ |
| 99 | 'x-api-key' => $this->apiKey, |
| 100 | 'anthropic-version' => $this->apiVersion, |
| 101 | 'Content-Type' => 'application/json', |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Generates SEO content for a tag. |
| 107 | * |
| 108 | * @param string $tagTitle The title of the tag. |
| 109 | * @param string $tagDescription The description of the tag. |
| 110 | * @return array The generated SEO content. |
| 111 | */ |
| 112 | public function generateTagSeo(string $tagTitle, string $tagDescription): array |
| 113 | { |
| 114 | return $this->seoContentGenerator->generateTagSeo($tagTitle, $tagDescription); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Generates SEO content for an article. |
| 119 | * |
| 120 | * @param string $title The title of the article. |
| 121 | * @param string $body The body content of the article. |
| 122 | * @return array The generated SEO content. |
| 123 | */ |
| 124 | public function generateArticleSeo(string $title, string $body): array |
| 125 | { |
| 126 | return $this->seoContentGenerator->generateArticleSeo($title, $body); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Generates SEO content for an image gallery. |
| 131 | * |
| 132 | * @param string $name The name of the gallery. |
| 133 | * @param string $context Additional context about the gallery content and images. |
| 134 | * @return array The generated SEO content. |
| 135 | */ |
| 136 | public function generateGallerySeo(string $name, string $context): array |
| 137 | { |
| 138 | return $this->seoContentGenerator->generateGallerySeo($name, $context); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Generates tags for an article. |
| 143 | * |
| 144 | * @param array $allTags All available tags. |
| 145 | * @param string $title The title of the article. |
| 146 | * @param string $body The body content of the article. |
| 147 | * @return array The generated article tags. |
| 148 | */ |
| 149 | public function generateArticleTags(array $allTags, string $title, string $body): array |
| 150 | { |
| 151 | return $this->articleTagsGenerator->generateArticleTags($allTags, $title, $body); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Analyzes an image. |
| 156 | * |
| 157 | * @param string $imagePath The path to the image file. |
| 158 | * @return array The analysis results. |
| 159 | */ |
| 160 | public function analyzeImage(string $imagePath): array |
| 161 | { |
| 162 | return $this->imageAnalyzer->analyze($imagePath); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Analyzes a comment. |
| 167 | * |
| 168 | * @param string $comment The comment text to analyze. |
| 169 | * @return array The analysis results. |
| 170 | */ |
| 171 | public function analyzeComment(string $comment): array |
| 172 | { |
| 173 | return $this->commentAnalyzer->analyze($comment); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Generates a summary for a given text. |
| 178 | * |
| 179 | * @param string $context The context of the text (e.g., 'article', 'page', 'report'). |
| 180 | * @param string $text The text to summarize. |
| 181 | * @return array The generated summary. |
| 182 | */ |
| 183 | public function generateTextSummary(string $context, string $text): array |
| 184 | { |
| 185 | return $this->textSummaryGenerator->generateTextSummary($context, $text); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Translates an array of strings from one locale to another. |
| 190 | * |
| 191 | * This method utilizes the TranslationGenerator service to perform translations |
| 192 | * of the provided strings from the specified source locale to the target locale. |
| 193 | * |
| 194 | * @param array $strings The array of strings to be translated. |
| 195 | * @param string $localeFrom The locale code of the source language (e.g., 'en_US'). |
| 196 | * @param string $localeTo The locale code of the target language (e.g., 'fr_FR'). |
| 197 | * @return array The translated strings. |
| 198 | */ |
| 199 | public function translateStrings(array $strings, string $localeFrom, string $localeTo): array |
| 200 | { |
| 201 | return $this->translationGenerator->generateTranslation($strings, $localeFrom, $localeTo); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Parses the response from the API. |
| 206 | * |
| 207 | * @param \Cake\Http\Client\Response $response The HTTP response from the API. |
| 208 | * @return array The parsed response data. |
| 209 | */ |
| 210 | public function parseResponse(Response $response): array |
| 211 | { |
| 212 | $responseData = $response->getJson(); |
| 213 | |
| 214 | return json_decode($responseData['content'][0]['text'], true); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Checks if the provider is properly configured. |
| 219 | * |
| 220 | * @return bool True if the Anthropic API key is set and not a placeholder. |
| 221 | */ |
| 222 | public function isConfigured(): bool |
| 223 | { |
| 224 | return !empty($this->apiKey) && $this->apiKey !== 'your-api-key-here'; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Gets the provider name for logging purposes. |
| 229 | * |
| 230 | * @return string The provider identifier. |
| 231 | */ |
| 232 | public function getProviderName(): string |
| 233 | { |
| 234 | return 'anthropic'; |
| 235 | } |
| 236 | } |