|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace tommyknocker\pdodb\ai\providers; |
| 6 | + |
| 7 | +use tommyknocker\pdodb\ai\BaseAiProvider; |
| 8 | +use tommyknocker\pdodb\exceptions\QueryException; |
| 9 | + |
| 10 | +/** |
| 11 | + * DeepSeek provider implementation. |
| 12 | + * API is compatible with OpenAI format. |
| 13 | + * |
| 14 | + * @see https://api-docs.deepseek.com/ |
| 15 | + */ |
| 16 | +class DeepSeekProvider extends BaseAiProvider |
| 17 | +{ |
| 18 | + private const string API_URL = 'https://api.deepseek.com/chat/completions'; |
| 19 | + private const string DEFAULT_MODEL = 'deepseek-chat'; |
| 20 | + private const float DEFAULT_TEMPERATURE = 0.7; |
| 21 | + private const int DEFAULT_MAX_TOKENS = 2000; |
| 22 | + private const string HEADER_AUTHORIZATION = 'Authorization'; |
| 23 | + private const string HEADER_BEARER_PREFIX = 'Bearer '; |
| 24 | + private const string MESSAGE_ROLE_SYSTEM = 'system'; |
| 25 | + private const string MESSAGE_ROLE_USER = 'user'; |
| 26 | + private const string RESPONSE_KEY_CHOICES = 'choices'; |
| 27 | + private const string RESPONSE_KEY_MESSAGE = 'message'; |
| 28 | + private const string RESPONSE_KEY_CONTENT = 'content'; |
| 29 | + |
| 30 | + protected string $apiUrl = self::API_URL; |
| 31 | + |
| 32 | + protected function initializeDefaults(): void |
| 33 | + { |
| 34 | + $this->model = $this->config->getProviderSetting('deepseek', 'model', self::DEFAULT_MODEL); |
| 35 | + $this->temperature = (float)$this->config->getProviderSetting('deepseek', 'temperature', self::DEFAULT_TEMPERATURE); |
| 36 | + $this->maxTokens = (int)$this->config->getProviderSetting('deepseek', 'max_tokens', self::DEFAULT_MAX_TOKENS); |
| 37 | + } |
| 38 | + |
| 39 | + public function getProviderName(): string |
| 40 | + { |
| 41 | + return 'deepseek'; |
| 42 | + } |
| 43 | + |
| 44 | + public function isAvailable(): bool |
| 45 | + { |
| 46 | + return $this->config->hasApiKey('deepseek'); |
| 47 | + } |
| 48 | + |
| 49 | + public function analyzeQuery(string $sql, array $context = []): string |
| 50 | + { |
| 51 | + $this->ensureAvailable(); |
| 52 | + |
| 53 | + $systemPrompt = $this->buildSystemPrompt('query'); |
| 54 | + $userPrompt = $this->buildQueryPrompt($sql, $context); |
| 55 | + |
| 56 | + return $this->callApi($systemPrompt, $userPrompt); |
| 57 | + } |
| 58 | + |
| 59 | + public function analyzeSchema(array $schema, array $context = []): string |
| 60 | + { |
| 61 | + $this->ensureAvailable(); |
| 62 | + |
| 63 | + $systemPrompt = $this->buildSystemPrompt('schema'); |
| 64 | + $userPrompt = $this->buildSchemaPrompt($schema, $context); |
| 65 | + |
| 66 | + return $this->callApi($systemPrompt, $userPrompt); |
| 67 | + } |
| 68 | + |
| 69 | + public function suggestOptimizations(array $analysis, array $context = []): string |
| 70 | + { |
| 71 | + $this->ensureAvailable(); |
| 72 | + |
| 73 | + $systemPrompt = $this->buildSystemPrompt('optimization'); |
| 74 | + $userPrompt = $this->buildOptimizationPrompt($analysis, $context); |
| 75 | + |
| 76 | + return $this->callApi($systemPrompt, $userPrompt); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Call DeepSeek API. |
| 81 | + * |
| 82 | + * @param string $systemPrompt System prompt |
| 83 | + * @param string $userPrompt User prompt |
| 84 | + * |
| 85 | + * @return string AI response |
| 86 | + */ |
| 87 | + protected function callApi(string $systemPrompt, string $userPrompt): string |
| 88 | + { |
| 89 | + $apiKey = $this->config->getApiKey('deepseek'); |
| 90 | + if ($apiKey === null) { |
| 91 | + throw new QueryException('DeepSeek API key not configured', 0); |
| 92 | + } |
| 93 | + |
| 94 | + $data = [ |
| 95 | + 'model' => $this->model, |
| 96 | + 'messages' => [ |
| 97 | + [ |
| 98 | + 'role' => self::MESSAGE_ROLE_SYSTEM, |
| 99 | + 'content' => $systemPrompt, |
| 100 | + ], |
| 101 | + [ |
| 102 | + 'role' => self::MESSAGE_ROLE_USER, |
| 103 | + 'content' => $userPrompt, |
| 104 | + ], |
| 105 | + ], |
| 106 | + 'temperature' => $this->temperature, |
| 107 | + 'max_tokens' => $this->maxTokens, |
| 108 | + ]; |
| 109 | + |
| 110 | + $headers = [ |
| 111 | + self::HEADER_AUTHORIZATION => self::HEADER_BEARER_PREFIX . $apiKey, |
| 112 | + ]; |
| 113 | + |
| 114 | + $response = $this->makeRequest($this->apiUrl, $data, $headers); |
| 115 | + |
| 116 | + if (!isset($response[self::RESPONSE_KEY_CHOICES][0][self::RESPONSE_KEY_MESSAGE][self::RESPONSE_KEY_CONTENT])) { |
| 117 | + throw new QueryException( |
| 118 | + 'Invalid response format from DeepSeek API', |
| 119 | + 0 |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + return (string)$response[self::RESPONSE_KEY_CHOICES][0][self::RESPONSE_KEY_MESSAGE][self::RESPONSE_KEY_CONTENT]; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Build system prompt based on analysis type. |
| 128 | + */ |
| 129 | + protected function buildSystemPrompt(string $type): string |
| 130 | + { |
| 131 | + $basePrompt = 'You are an expert database performance analyst. Provide clear, actionable recommendations for database optimization.'; |
| 132 | + |
| 133 | + $typePrompts = [ |
| 134 | + 'query' => 'Analyze SQL queries and provide optimization suggestions. Focus on index usage, query structure, and performance bottlenecks.', |
| 135 | + 'schema' => 'Analyze database schema and provide recommendations for indexes, constraints, and table structure improvements.', |
| 136 | + 'optimization' => 'Review existing analysis results and provide additional optimization suggestions. Build upon the existing recommendations.', |
| 137 | + ]; |
| 138 | + |
| 139 | + return $basePrompt . ' ' . ($typePrompts[$type] ?? ''); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Build prompt for query analysis. |
| 144 | + * |
| 145 | + * @param array<string, mixed> $context |
| 146 | + */ |
| 147 | + protected function buildQueryPrompt(string $sql, array $context): string |
| 148 | + { |
| 149 | + $prompt = "Analyze the following SQL query and provide optimization recommendations:\n\n"; |
| 150 | + $prompt .= "SQL Query:\n```sql\n{$sql}\n```\n\n"; |
| 151 | + |
| 152 | + if (!empty($context)) { |
| 153 | + $prompt .= $this->formatContext($context); |
| 154 | + } |
| 155 | + |
| 156 | + $prompt .= "\n\nProvide specific, actionable recommendations including:\n"; |
| 157 | + $prompt .= "- Index suggestions\n"; |
| 158 | + $prompt .= "- Query structure improvements\n"; |
| 159 | + $prompt .= "- Performance bottlenecks\n"; |
| 160 | + $prompt .= '- Estimated impact of optimizations'; |
| 161 | + |
| 162 | + return $prompt; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Build prompt for schema analysis. |
| 167 | + * |
| 168 | + * @param array<string, mixed> $schema |
| 169 | + * @param array<string, mixed> $context |
| 170 | + */ |
| 171 | + protected function buildSchemaPrompt(array $schema, array $context): string |
| 172 | + { |
| 173 | + $prompt = "Analyze the following database schema and provide optimization recommendations:\n\n"; |
| 174 | + $prompt .= $this->formatContext(array_merge(['schema' => $schema], $context)); |
| 175 | + |
| 176 | + $prompt .= "\n\nProvide specific recommendations for:\n"; |
| 177 | + $prompt .= "- Missing indexes\n"; |
| 178 | + $prompt .= "- Redundant indexes\n"; |
| 179 | + $prompt .= "- Table structure improvements\n"; |
| 180 | + $prompt .= '- Foreign key optimizations'; |
| 181 | + |
| 182 | + return $prompt; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Build prompt for optimization suggestions. |
| 187 | + * |
| 188 | + * @param array<string, mixed> $analysis |
| 189 | + * @param array<string, mixed> $context |
| 190 | + */ |
| 191 | + protected function buildOptimizationPrompt(array $analysis, array $context): string |
| 192 | + { |
| 193 | + $prompt = "Review the following database analysis and provide additional optimization suggestions:\n\n"; |
| 194 | + $prompt .= $this->formatContext(array_merge(['existing_analysis' => $analysis], $context)); |
| 195 | + |
| 196 | + $prompt .= "\n\nProvide additional recommendations that complement the existing analysis."; |
| 197 | + |
| 198 | + return $prompt; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Ensure provider is available. |
| 203 | + * |
| 204 | + * @throws QueryException If provider is not available |
| 205 | + */ |
| 206 | + protected function ensureAvailable(): void |
| 207 | + { |
| 208 | + if (!$this->isAvailable()) { |
| 209 | + throw new QueryException( |
| 210 | + 'DeepSeek provider is not available. Please configure PDODB_AI_DEEPSEEK_KEY environment variable.', |
| 211 | + 0 |
| 212 | + ); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | + |
0 commit comments