-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from WP-Autoplugin/version-1-2
Version 1.2
- Loading branch information
Showing
37 changed files
with
6,254 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
/** | ||
* Custom API class. | ||
* | ||
* @package WP-Autoplugin | ||
* @since 1.2 | ||
* @version 1.2 | ||
* @link https://wp-autoplugin.com | ||
* @license GPL-2.0+ | ||
* @license https://www.gnu.org/licenses/gpl-2.0.html | ||
*/ | ||
|
||
namespace WP_Autoplugin; | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Custom API class that connects to user-defined OpenAI-compatible endpoints. | ||
*/ | ||
class Custom_API extends OpenAI_API { | ||
|
||
/** | ||
* Additional headers specified by the user. | ||
* | ||
* @var array | ||
*/ | ||
protected $extra_headers = array(); | ||
|
||
/** | ||
* Configure the custom API with the user-defined settings. | ||
* | ||
* @param string $endpoint The custom API endpoint (url). | ||
* @param string $api_key The API key for authentication. | ||
* @param string $model The model parameter sent to the API. | ||
* @param array $headers Additional headers (key/value pairs). | ||
*/ | ||
public function set_custom_config( $endpoint, $api_key, $model, $headers = array() ) { | ||
$this->api_url = $endpoint; | ||
$this->api_key = $api_key; | ||
$this->model = $model; | ||
$this->extra_headers = $this->parse_extra_headers( $headers ); | ||
} | ||
|
||
/** | ||
* Override the send_prompt to include user-defined headers. | ||
* | ||
* @param string $prompt The user prompt. | ||
* @param string $system_message Optional system message. | ||
* @param array $override_body Optional parameters to override in the request body. | ||
* | ||
* @return string|\WP_Error The response or a WP_Error object on failure. | ||
*/ | ||
public function send_prompt( $prompt, $system_message = '', $override_body = array() ) { | ||
$prompt = $this->trim_prompt( $prompt ); | ||
|
||
$messages = array(); | ||
if ( $system_message ) { | ||
$messages[] = array( | ||
'role' => 'system', | ||
'content' => $system_message, | ||
); | ||
} | ||
|
||
$messages[] = array( | ||
'role' => 'user', | ||
'content' => $prompt, | ||
); | ||
|
||
$body = array( | ||
'model' => $this->model, | ||
'temperature' => $this->temperature, | ||
'max_tokens' => $this->max_tokens, | ||
'messages' => $messages, | ||
); | ||
|
||
// Only keep valid keys from $override_body. | ||
$allowed_keys = $this->get_allowed_parameters(); | ||
$override_body = array_intersect_key( $override_body, array_flip( $allowed_keys ) ); | ||
$body = array_merge( $body, $override_body ); | ||
|
||
// Merge default auth header with any extra headers. | ||
$headers = array_merge( | ||
array( | ||
'Authorization' => 'Bearer ' . $this->api_key, | ||
'Content-Type' => 'application/json', | ||
), | ||
$this->extra_headers | ||
); | ||
|
||
$response = wp_remote_post( | ||
$this->api_url, | ||
array( | ||
'timeout' => 60, | ||
'headers' => $headers, | ||
'body' => wp_json_encode( $body ), | ||
) | ||
); | ||
|
||
if ( is_wp_error( $response ) ) { | ||
return $response; | ||
} | ||
|
||
$data = json_decode( wp_remote_retrieve_body( $response ), true ); | ||
|
||
if ( empty( $data['choices'][0]['message']['content'] ) ) { | ||
return new \WP_Error( | ||
'api_error', | ||
__( 'Error communicating with the API.', 'wp-autoplugin' ) . "\n" . print_r( $data, true ) | ||
); | ||
} | ||
|
||
return $data['choices'][0]['message']['content']; | ||
} | ||
|
||
/** | ||
* Convert the user’s header lines into an associative array. | ||
* | ||
* @param array $headers Array of lines like ["X-Test=Value", "Accept=application/json"]. | ||
* @return array Key-value pairs for use in wp_remote_post header. | ||
*/ | ||
protected function parse_extra_headers( $headers ) { | ||
$parsed = array(); | ||
foreach ( $headers as $header_line ) { | ||
if ( strpos( $header_line, '=' ) !== false ) { | ||
list( $key, $value ) = explode( '=', $header_line, 2 ); | ||
$key = trim( $key ); | ||
$value = trim( $value ); | ||
if ( $key && $value ) { | ||
$parsed[ $key ] = $value; | ||
} | ||
} | ||
} | ||
return $parsed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.