| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\AI\AIServices; |
| 4 |
|
| 5 |
use RadiusTheme\SB\AI\AIFns; |
| 6 |
use RadiusTheme\SB\AI\AIServices\Interface\AIServiceInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class OpenAIAdapter |
| 10 |
* |
| 11 |
* Providing standardized methods for generating AI-based responses. |
| 12 |
*/ |
| 13 |
class OpenAIAdapter implements AIServiceInterface { |
| 14 |
|
| 15 |
/** |
| 16 |
* The OpenAI client instance. |
| 17 |
* |
| 18 |
* @var object |
| 19 |
*/ |
| 20 |
private $openAIClient; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor. |
| 24 |
* |
| 25 |
* Initializes the adapter with the provided OpenAI client instance. |
| 26 |
* |
| 27 |
* @param object $openAIClient An instance of the OpenAI client. |
| 28 |
*/ |
| 29 |
public function __construct( $openAIClient ) { |
| 30 |
$this->openAIClient = $openAIClient; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Generate a general AI response using the OpenAI client. |
| 35 |
* |
| 36 |
* @param string $content_type The type of content to generate (title, description, short_description). |
| 37 |
* @param string $instruction The product information or user instruction. |
| 38 |
* |
| 39 |
* @return string The generated AI response. |
| 40 |
*/ |
| 41 |
public function generateResponse( string $content_type, string $instruction ): string { |
| 42 |
$system_prompt = AIFns::build_prompt( $content_type, $instruction ); |
| 43 |
return $this->openAIClient->ask( $instruction, $system_prompt ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Generate keyword suggestions or responses using the OpenAI client. |
| 48 |
* |
| 49 |
* @param mixed $data Input data used to generate keyword-based output. |
| 50 |
* |
| 51 |
* @return mixed The AI-generated keyword response. |
| 52 |
*/ |
| 53 |
public function generateKeywordResponse( $data ) { |
| 54 |
return $this->openAIClient->askKeyword( $data ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Generate text embeddings using the OpenAI client. |
| 59 |
* |
| 60 |
* @param mixed $data Input text or data for which to generate embeddings. |
| 61 |
* |
| 62 |
* @return mixed The embedding vector or API response from OpenAI. |
| 63 |
*/ |
| 64 |
public function generateEmbedding( $data ) { |
| 65 |
return $this->openAIClient->generateEmbedding( $data ); |
| 66 |
} |
| 67 |
} |
| 68 |
|