Dateien nach „bavarian-rank-engine/includes/Providers“ hochladen

This commit is contained in:
Michael Fuchs 2026-02-22 10:08:22 +00:00
parent 9aec7cd0bd
commit 53538a4d09
5 changed files with 326 additions and 0 deletions

View file

@ -0,0 +1,74 @@
<?php
namespace BavarianRankEngine\Providers;
class AnthropicProvider implements ProviderInterface {
private const API_URL = 'https://api.anthropic.com/v1/messages';
public function getId(): string {
return 'anthropic'; }
public function getName(): string {
return 'Anthropic (Claude)'; }
public function getModels(): array {
return array(
'claude-sonnet-4-6' => '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'] ?? '' );
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace BavarianRankEngine\Providers;
class GeminiProvider implements ProviderInterface {
private const API_BASE = 'https://generativelanguage.googleapis.com/v1beta/models/';
public function getId(): string {
return 'gemini'; }
public function getName(): string {
return 'Google Gemini'; }
public function getModels(): array {
return array(
'gemini-2.0-flash' => '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'] ?? '' );
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace BavarianRankEngine\Providers;
class GrokProvider implements ProviderInterface {
private const API_URL = 'https://api.x.ai/v1/chat/completions';
public function getId(): string {
return 'grok'; }
public function getName(): string {
return 'xAI Grok'; }
public function getModels(): array {
return array(
'grok-3' => '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'] ?? '' );
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace BavarianRankEngine\Providers;
class OpenAIProvider implements ProviderInterface {
private const API_URL = 'https://api.openai.com/v1/chat/completions';
public function getId(): string {
return 'openai'; }
public function getName(): string {
return 'OpenAI'; }
public function getModels(): array {
return array(
'gpt-4.1' => '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'] ?? '' );
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace BavarianRankEngine\Providers;
interface ProviderInterface {
/** Unique machine-readable ID, e.g. 'openai' */
public function getId(): string;
/** Human-readable label for dropdowns */
public function getName(): string;
/**
* Available models as ['model-id' => '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;
}