| 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 |
* Chatgpt integration |
| 13 |
*/ |
| 14 |
class Chatgpt extends BaseAiIntegration { |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Generate Text |
| 19 |
* |
| 20 |
* @see https://platform.openai.com/docs/api-reference/chat/create |
| 21 |
* |
| 22 |
* @param ModelInput $input model input. |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function generate_text( ModelInput $input ) { |
| 26 |
$endpoint = 'https://api.openai.com/v1/chat/completions'; |
| 27 |
|
| 28 |
$prompt = $this->get_processed_user_prompt( $input ); |
| 29 |
|
| 30 |
if ( is_null( $prompt ) ) { |
| 31 |
return new WP_Error( 'generate_text_error', 'Invalid prompt' ); |
| 32 |
} |
| 33 |
|
| 34 |
$body = wp_json_encode( |
| 35 |
array( |
| 36 |
'model' => $this->settings['model'], |
| 37 |
'messages' => array( |
| 38 |
array( |
| 39 |
'role' => 'developer', |
| 40 |
'content' => $this->system_prompt, |
| 41 |
), |
| 42 |
array( |
| 43 |
'role' => 'user', |
| 44 |
'content' => $prompt, |
| 45 |
), |
| 46 |
), |
| 47 |
) |
| 48 |
); |
| 49 |
|
| 50 |
$response = wp_remote_post( |
| 51 |
$endpoint, |
| 52 |
array( |
| 53 |
'headers' => array( |
| 54 |
'Content-Type' => 'application/json', |
| 55 |
'Authorization' => 'Bearer ' . $this->settings['apiKey'], |
| 56 |
), |
| 57 |
'body' => $body, |
| 58 |
'timeout' => 60, |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
if ( is_wp_error( $response ) ) { |
| 63 |
return $response; |
| 64 |
} |
| 65 |
|
| 66 |
$code = wp_remote_retrieve_response_code( $response ); |
| 67 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 68 |
|
| 69 |
if ( 200 !== $code || empty( $data['choices'][0]['message']['content'] ) ) { |
| 70 |
return new WP_Error( 'generate_text_error', 'Unexpected API response.', $data ); |
| 71 |
} |
| 72 |
|
| 73 |
$result = trim( $data['choices'][0]['message']['content'] ); |
| 74 |
|
| 75 |
$result = $this->maybe_remove_quotes( $result ); |
| 76 |
|
| 77 |
return $result; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get models. |
| 82 |
* |
| 83 |
* @see https://platform.openai.com/docs/models |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public static function get_models() { |
| 88 |
|
| 89 |
return array( |
| 90 |
array( |
| 91 |
'value' => 'gpt-5', |
| 92 |
'label' => 'GPT-5', |
| 93 |
'input' => array( 'text', 'image' ), |
| 94 |
'output' => array( 'text' ), |
| 95 |
), |
| 96 |
array( |
| 97 |
'value' => 'gpt-5-mini', |
| 98 |
'label' => 'GPT-5 mini', |
| 99 |
'input' => array( 'text', 'image' ), |
| 100 |
'output' => array( 'text' ), |
| 101 |
), |
| 102 |
array( |
| 103 |
'value' => 'gpt-5-nano', |
| 104 |
'label' => 'GPT-5 nano', |
| 105 |
'input' => array( 'text', 'image' ), |
| 106 |
'output' => array( 'text' ), |
| 107 |
), |
| 108 |
array( |
| 109 |
'value' => 'gpt-4.1', |
| 110 |
'label' => 'GPT-4.1', |
| 111 |
'input' => array( 'text', 'image' ), |
| 112 |
'output' => array( 'text' ), |
| 113 |
), |
| 114 |
array( |
| 115 |
'value' => 'gpt-4.1-mini', |
| 116 |
'label' => 'GPT-4.1 mini', |
| 117 |
'input' => array( 'text', 'image' ), |
| 118 |
'output' => array( 'text' ), |
| 119 |
), |
| 120 |
array( |
| 121 |
'value' => 'gpt-4.1-nano', |
| 122 |
'label' => 'GPT-4.1 nano', |
| 123 |
'input' => array( 'text', 'image' ), |
| 124 |
'output' => array( 'text' ), |
| 125 |
), |
| 126 |
array( |
| 127 |
'value' => 'gpt-4o', |
| 128 |
'label' => 'GPT-4o', |
| 129 |
'input' => array( 'text', 'image' ), |
| 130 |
'output' => array( 'text' ), |
| 131 |
), |
| 132 |
array( |
| 133 |
'value' => 'gpt-3.5-turbo', |
| 134 |
'label' => 'GPT-3.5 Turbo', |
| 135 |
'input' => array( 'text' ), |
| 136 |
'output' => array( 'text' ), |
| 137 |
), |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
|