| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utilities |
| 4 |
* |
| 5 |
* @package GamiPress\AI_Assistant\Utilities |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get the list of available models |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
function gamipress_ai_assistant_get_models() { |
| 20 |
|
| 21 |
$models = array(); |
| 22 |
|
| 23 |
if( class_exists( 'WordPress\AiClient\AiClient' ) ) { |
| 24 |
$registry = WordPress\AiClient\AiClient::defaultRegistry(); |
| 25 |
|
| 26 |
$requirements = new WordPress\AiClient\Providers\Models\DTO\ModelRequirements( |
| 27 |
array( WordPress\AiClient\Providers\Models\Enums\CapabilityEnum::textGeneration() ), |
| 28 |
array() |
| 29 |
); |
| 30 |
|
| 31 |
foreach( $registry->findModelsMetadataForSupport( $requirements ) as $result ) { |
| 32 |
$provider = $result->getProvider(); |
| 33 |
$provider_id = $provider->getId(); |
| 34 |
|
| 35 |
$models[$provider_id] = array( |
| 36 |
'name' => $provider->getName(), |
| 37 |
'models' => array(), |
| 38 |
); |
| 39 |
|
| 40 |
foreach( $result->getModels() as $model ) { |
| 41 |
$models[$provider_id]['models'][$model->getId()] = $model->getName(); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $models; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get models IDs |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
function gamipress_ai_assistant_get_models_ids() { |
| 58 |
|
| 59 |
$providers = gamipress_ai_assistant_get_models(); |
| 60 |
$models_ids = array(); |
| 61 |
|
| 62 |
foreach( $providers as $provider ) { |
| 63 |
|
| 64 |
|
| 65 |
foreach( $provider['models'] as $id => $name ) { |
| 66 |
$models_ids[] = $id; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
return $models_ids; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get a function response from a message |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param \WordPress\AiClient\Messages\DTO\Message $message |
| 81 |
* |
| 82 |
* @return mixed|false |
| 83 |
*/ |
| 84 |
function gamipress_ai_assistant_get_function_response( $message ) { |
| 85 |
$array = $message->toArray(); |
| 86 |
$response = false; |
| 87 |
|
| 88 |
if( |
| 89 |
isset( $array['parts'] ) |
| 90 |
&& isset( $array['parts'][0] ) |
| 91 |
&& isset( $array['parts'][0]['functionResponse'] ) |
| 92 |
&& isset( $array['parts'][0]['functionResponse']['response'] ) |
| 93 |
) { |
| 94 |
$response = $array['parts'][0]['functionResponse']['response']; |
| 95 |
} |
| 96 |
|
| 97 |
return $response; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get all function responses from a message |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param \WordPress\AiClient\Messages\DTO\Message $message |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
function gamipress_ai_assistant_get_all_function_responses( $message ) { |
| 111 |
$array = $message->toArray(); |
| 112 |
$responses = array(); |
| 113 |
|
| 114 |
if( isset( $array['parts'] ) && is_array( $array['parts'] ) ) { |
| 115 |
foreach( $array['parts'] as $part ) { |
| 116 |
if( isset( $part['functionResponse'] ) |
| 117 |
&& isset( $part['functionResponse']['response'] ) ) { |
| 118 |
$responses[] = $part['functionResponse']['response']; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $responses; |
| 124 |
|
| 125 |
} |