From 53538a4d09ea2a48af4a39d7c008d338dfc17b30 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 22 Feb 2026 10:08:22 +0000 Subject: [PATCH] =?UTF-8?q?Dateien=20nach=20=E2=80=9Ebavarian-rank-engine/?= =?UTF-8?q?includes/Providers=E2=80=9C=20hochladen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../includes/Providers/AnthropicProvider.php | 74 ++++++++++++++++++ .../includes/Providers/GeminiProvider.php | 68 ++++++++++++++++ .../includes/Providers/GrokProvider.php | 72 +++++++++++++++++ .../includes/Providers/OpenAIProvider.php | 78 +++++++++++++++++++ .../includes/Providers/ProviderInterface.php | 34 ++++++++ 5 files changed, 326 insertions(+) create mode 100644 bavarian-rank-engine/includes/Providers/AnthropicProvider.php create mode 100644 bavarian-rank-engine/includes/Providers/GeminiProvider.php create mode 100644 bavarian-rank-engine/includes/Providers/GrokProvider.php create mode 100644 bavarian-rank-engine/includes/Providers/OpenAIProvider.php create mode 100644 bavarian-rank-engine/includes/Providers/ProviderInterface.php diff --git a/bavarian-rank-engine/includes/Providers/AnthropicProvider.php b/bavarian-rank-engine/includes/Providers/AnthropicProvider.php new file mode 100644 index 0000000..50901d7 --- /dev/null +++ b/bavarian-rank-engine/includes/Providers/AnthropicProvider.php @@ -0,0 +1,74 @@ + 'Claude Sonnet 4.6 (Empfohlen)', + 'claude-opus-4-6' => 'Claude Opus 4.6 (Leistungsstark)', + 'claude-haiku-4-5-20251001' => 'Claude Haiku 4.5 (Schnell & günstig)', + ); + } + + public function testConnection( string $api_key ): array { + try { + $this->generateText( 'Say "ok"', $api_key, 'claude-haiku-4-5-20251001', 5 ); + return array( + 'success' => true, + 'message' => 'Verbindung erfolgreich', + ); + } catch ( \RuntimeException $e ) { + return array( + 'success' => false, + 'message' => $e->getMessage(), + ); + } + } + + public function generateText( string $prompt, string $api_key, string $model, int $max_tokens = 300 ): string { + $response = wp_remote_post( + self::API_URL, + array( + 'timeout' => 30, + 'headers' => array( + 'x-api-key' => $api_key, + 'anthropic-version' => '2023-06-01', + 'Content-Type' => 'application/json', + ), + 'body' => wp_json_encode( + array( + 'model' => $model, + 'max_tokens' => $max_tokens, + 'messages' => array( + array( + 'role' => 'user', + 'content' => $prompt, + ), + ), + ) + ), + ) + ); + + if ( is_wp_error( $response ) ) { + throw new \RuntimeException( esc_html( $response->get_error_message() ) ); + } + + $code = wp_remote_retrieve_response_code( $response ); + $body = json_decode( wp_remote_retrieve_body( $response ), true ); + + if ( $code !== 200 ) { + $msg = $body['error']['message'] ?? "HTTP $code"; + throw new \RuntimeException( esc_html( $msg ) ); + } + + return trim( $body['content'][0]['text'] ?? '' ); + } +} diff --git a/bavarian-rank-engine/includes/Providers/GeminiProvider.php b/bavarian-rank-engine/includes/Providers/GeminiProvider.php new file mode 100644 index 0000000..3124455 --- /dev/null +++ b/bavarian-rank-engine/includes/Providers/GeminiProvider.php @@ -0,0 +1,68 @@ + 'Gemini 2.0 Flash (Empfohlen)', + 'gemini-2.0-flash-lite' => 'Gemini 2.0 Flash Lite (Günstig)', + 'gemini-1.5-pro' => 'Gemini 1.5 Pro', + ); + } + + public function testConnection( string $api_key ): array { + try { + $this->generateText( 'Say "ok"', $api_key, 'gemini-2.0-flash-lite', 5 ); + return array( + 'success' => true, + 'message' => 'Verbindung erfolgreich', + ); + } catch ( \RuntimeException $e ) { + return array( + 'success' => false, + 'message' => $e->getMessage(), + ); + } + } + + public function generateText( string $prompt, string $api_key, string $model, int $max_tokens = 300 ): string { + $url = self::API_BASE . $model . ':generateContent'; + $response = wp_remote_post( + $url, + array( + 'timeout' => 30, + 'headers' => array( + 'Content-Type' => 'application/json', + 'x-goog-api-key' => $api_key, + ), + 'body' => wp_json_encode( + array( + 'contents' => array( array( 'parts' => array( array( 'text' => $prompt ) ) ) ), + 'generationConfig' => array( 'maxOutputTokens' => $max_tokens ), + ) + ), + ) + ); + + if ( is_wp_error( $response ) ) { + throw new \RuntimeException( esc_html( $response->get_error_message() ) ); + } + + $code = wp_remote_retrieve_response_code( $response ); + $body = json_decode( wp_remote_retrieve_body( $response ), true ); + + if ( $code !== 200 ) { + $msg = $body['error']['message'] ?? "HTTP $code"; + throw new \RuntimeException( esc_html( $msg ) ); + } + + return trim( $body['candidates'][0]['content']['parts'][0]['text'] ?? '' ); + } +} diff --git a/bavarian-rank-engine/includes/Providers/GrokProvider.php b/bavarian-rank-engine/includes/Providers/GrokProvider.php new file mode 100644 index 0000000..a2b1349 --- /dev/null +++ b/bavarian-rank-engine/includes/Providers/GrokProvider.php @@ -0,0 +1,72 @@ + 'Grok 3 (Empfohlen)', + 'grok-3-mini' => 'Grok 3 Mini (Günstig)', + ); + } + + public function testConnection( string $api_key ): array { + try { + $this->generateText( 'Say "ok"', $api_key, 'grok-3-mini', 5 ); + return array( + 'success' => true, + 'message' => 'Verbindung erfolgreich', + ); + } catch ( \RuntimeException $e ) { + return array( + 'success' => false, + 'message' => $e->getMessage(), + ); + } + } + + public function generateText( string $prompt, string $api_key, string $model, int $max_tokens = 300 ): string { + $response = wp_remote_post( + self::API_URL, + array( + 'timeout' => 30, + 'headers' => array( + 'Authorization' => 'Bearer ' . $api_key, + 'Content-Type' => 'application/json', + ), + 'body' => wp_json_encode( + array( + 'model' => $model, + 'messages' => array( + array( + 'role' => 'user', + 'content' => $prompt, + ), + ), + 'max_tokens' => $max_tokens, + ) + ), + ) + ); + + if ( is_wp_error( $response ) ) { + throw new \RuntimeException( esc_html( $response->get_error_message() ) ); + } + + $code = wp_remote_retrieve_response_code( $response ); + $body = json_decode( wp_remote_retrieve_body( $response ), true ); + + if ( $code !== 200 ) { + $msg = $body['error']['message'] ?? "HTTP $code"; + throw new \RuntimeException( esc_html( $msg ) ); + } + + return trim( $body['choices'][0]['message']['content'] ?? '' ); + } +} diff --git a/bavarian-rank-engine/includes/Providers/OpenAIProvider.php b/bavarian-rank-engine/includes/Providers/OpenAIProvider.php new file mode 100644 index 0000000..7cb95a0 --- /dev/null +++ b/bavarian-rank-engine/includes/Providers/OpenAIProvider.php @@ -0,0 +1,78 @@ + 'GPT-4.1 (Empfohlen)', + 'gpt-4o' => 'GPT-4o', + 'gpt-4o-mini' => 'GPT-4o Mini (Günstig)', + 'gpt-3.5-turbo' => 'GPT-3.5 Turbo (Sehr günstig)', + ); + } + + public function testConnection( string $api_key ): array { + try { + $this->generateText( 'Say "ok"', $api_key, 'gpt-4o-mini', 5 ); + return array( + 'success' => true, + 'message' => 'Verbindung erfolgreich', + ); + } catch ( \RuntimeException $e ) { + return array( + 'success' => false, + 'message' => $e->getMessage(), + ); + } + } + + public function generateText( string $prompt, string $api_key, string $model, int $max_tokens = 300 ): string { + $response = wp_remote_post( + self::API_URL, + array( + 'timeout' => 30, + 'headers' => array( + 'Authorization' => 'Bearer ' . $api_key, + 'Content-Type' => 'application/json', + ), + 'body' => wp_json_encode( + array( + 'model' => $model, + 'messages' => array( + array( + 'role' => 'user', + 'content' => $prompt, + ), + ), + 'max_tokens' => $max_tokens, + ) + ), + ) + ); + + return $this->parseResponse( $response ); + } + + private function parseResponse( $response ): string { + if ( is_wp_error( $response ) ) { + throw new \RuntimeException( esc_html( $response->get_error_message() ) ); + } + + $code = wp_remote_retrieve_response_code( $response ); + $body = json_decode( wp_remote_retrieve_body( $response ), true ); + + if ( $code !== 200 ) { + $msg = $body['error']['message'] ?? "HTTP $code"; + throw new \RuntimeException( esc_html( $msg ) ); + } + + return trim( $body['choices'][0]['message']['content'] ?? '' ); + } +} diff --git a/bavarian-rank-engine/includes/Providers/ProviderInterface.php b/bavarian-rank-engine/includes/Providers/ProviderInterface.php new file mode 100644 index 0000000..e381d7a --- /dev/null +++ b/bavarian-rank-engine/includes/Providers/ProviderInterface.php @@ -0,0 +1,34 @@ + 'Human Label'] + * Ordered from most capable to least expensive + */ + public function getModels(): array; + + /** + * Test API connectivity with minimal cost. + * Returns ['success' => bool, 'message' => string] + */ + public function testConnection( string $api_key ): array; + + /** + * Generate text from a prompt. + * + * @param string $prompt The full prompt to send + * @param string $api_key Provider API key + * @param string $model Model ID from getModels() + * @param int $max_tokens Maximum tokens in response (0 = provider default) + * @return string Generated text or empty string on failure + * @throws \RuntimeException on API error + */ + public function generateText( string $prompt, string $api_key, string $model, int $max_tokens = 300 ): string; +}