| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Implementations; |
| 4 |
|
| 5 |
use OPTN\Includes\Dto\ModelInput; |
| 6 |
use OPTN\Includes\Integrations\Base\BaseAiIntegration; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Gemini integration |
| 13 |
*/ |
| 14 |
class Gemini extends BaseAiIntegration { |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Generate Text |
| 19 |
* |
| 20 |
* @see https://ai.google.dev/gemini-api/docs/text-generation |
| 21 |
* |
| 22 |
* @param ModelInput $input model input. |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function generate_text( ModelInput $input ) { |
| 26 |
|
| 27 |
$prompt = $this->get_processed_user_prompt( $input ); |
| 28 |
|
| 29 |
if ( is_null( $prompt ) ) { |
| 30 |
return new WP_Error( 'generate_text_error', 'Invalid prompt' ); |
| 31 |
} |
| 32 |
|
| 33 |
$endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . |
| 34 |
$this->settings['model'] . |
| 35 |
':generateContent?key=' . |
| 36 |
$this->settings['apiKey']; |
| 37 |
|
| 38 |
$body = wp_json_encode( |
| 39 |
array( |
| 40 |
'system_instruction' => array( |
| 41 |
'parts' => array( |
| 42 |
'text' => $this->system_prompt, |
| 43 |
), |
| 44 |
), |
| 45 |
'contents' => array( |
| 46 |
'role' => 'user', |
| 47 |
'parts' => array( |
| 48 |
'text' => $prompt, |
| 49 |
), |
| 50 |
), |
| 51 |
) |
| 52 |
); |
| 53 |
|
| 54 |
$response = wp_remote_post( |
| 55 |
$endpoint, |
| 56 |
array( |
| 57 |
'headers' => array( |
| 58 |
'Content-Type' => 'application/json', |
| 59 |
), |
| 60 |
'body' => $body, |
| 61 |
'timeout' => 60, |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
if ( is_wp_error( $response ) ) { |
| 66 |
return $response; |
| 67 |
} |
| 68 |
|
| 69 |
$code = wp_remote_retrieve_response_code( $response ); |
| 70 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 71 |
|
| 72 |
if ( 200 !== $code || |
| 73 |
empty( $data['candidates'][0]['content']['parts'][0]['text'] ) || |
| 74 |
isset( $data['error'] ) |
| 75 |
) { |
| 76 |
return new WP_Error( 'generate_text_error', 'Unexpected API response.', $data ); |
| 77 |
} |
| 78 |
|
| 79 |
$result = trim( $data['candidates'][0]['content']['parts'][0]['text'] ); |
| 80 |
|
| 81 |
$result = $this->maybe_remove_quotes( $result ); |
| 82 |
|
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get models. |
| 88 |
* |
| 89 |
* @see https://ai.google.dev/gemini-api/docs/models |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public static function get_models() { |
| 94 |
|
| 95 |
return array( |
| 96 |
array( |
| 97 |
'value' => 'gemini-2.5-pro', |
| 98 |
'label' => 'Gemini 2.5 Pro', |
| 99 |
'input' => array( 'text', 'image', 'video', 'audio', 'pdf' ), |
| 100 |
'output' => array( 'text' ), |
| 101 |
), |
| 102 |
array( |
| 103 |
'value' => 'gemini-2.5-flash', |
| 104 |
'label' => 'Gemini 2.5 Flash', |
| 105 |
'input' => array( 'text', 'image', 'video', 'audio', 'pdf' ), |
| 106 |
'output' => array( 'text' ), |
| 107 |
), |
| 108 |
array( |
| 109 |
'value' => 'gemini-2.0-flash', |
| 110 |
'label' => 'Gemini 2.0 Flash', |
| 111 |
'input' => array( 'text', 'image', 'video', 'audio' ), |
| 112 |
'output' => array( 'text' ), |
| 113 |
), |
| 114 |
array( |
| 115 |
'value' => 'gemini-2.0-flash-lite', |
| 116 |
'label' => 'Gemini 2.0 Flash-Lite', |
| 117 |
'input' => array( 'text', 'image', 'video', 'audio' ), |
| 118 |
'output' => array( 'text' ), |
| 119 |
), |
| 120 |
array( |
| 121 |
'value' => 'gemini-1.5-pro', |
| 122 |
'label' => 'Gemini 1.5 Pro', |
| 123 |
'input' => array( 'text', 'image', 'video', 'audio' ), |
| 124 |
'output' => array( 'text' ), |
| 125 |
), |
| 126 |
array( |
| 127 |
'value' => 'gemini-1.5-flash', |
| 128 |
'label' => 'Gemini 1.5 Flash', |
| 129 |
'input' => array( 'text', 'image', 'video', 'audio' ), |
| 130 |
'output' => array( 'text' ), |
| 131 |
), |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|