- Introduced `Groq_AI_Provider_Google` and `Groq_AI_Provider_Groq` classes for handling AI interactions with Google and Groq respectively. - Enhanced `Groq_AI_Provider_Manager` to register and manage multiple AI providers. - Implemented `Groq_AI_Conversation_Manager` for managing conversation IDs and context hashes. - Added `Groq_AI_Generation_Logger` for logging AI generation events and managing log tables. - Developed `Groq_AI_Prompt_Builder` for constructing prompts and processing AI responses. - Established `Groq_AI_Settings_Manager` for managing plugin settings, including context fields and module configurations.
142 lines
3.8 KiB
PHP
142 lines
3.8 KiB
PHP
<?php
|
|
|
|
abstract class Groq_AI_Abstract_OpenAI_Provider implements Groq_AI_Provider_Interface {
|
|
public function get_available_models() {
|
|
return [];
|
|
}
|
|
|
|
public function supports_response_format() {
|
|
return true;
|
|
}
|
|
|
|
public function supports_live_models() {
|
|
return true;
|
|
}
|
|
|
|
public function fetch_live_models( $api_key ) {
|
|
$endpoint = $this->get_models_endpoint();
|
|
if ( empty( $endpoint ) ) {
|
|
return new WP_Error( 'groq_ai_models_endpoint_missing', __( 'Geen model-endpoint beschikbaar voor deze aanbieder.', 'groq-ai-product-text' ) );
|
|
}
|
|
|
|
$response = wp_remote_get(
|
|
$endpoint,
|
|
[
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $api_key,
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'timeout' => 20,
|
|
]
|
|
);
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
return $response;
|
|
}
|
|
|
|
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
|
|
|
if ( isset( $body['error']['message'] ) ) {
|
|
return new WP_Error( 'groq_ai_provider_error', (string) $body['error']['message'] );
|
|
}
|
|
|
|
if ( empty( $body['data'] ) || ! is_array( $body['data'] ) ) {
|
|
return new WP_Error( 'groq_ai_empty_response', __( 'Geen modeldata ontvangen.', 'groq-ai-product-text' ) );
|
|
}
|
|
|
|
$models = [];
|
|
foreach ( $body['data'] as $model ) {
|
|
if ( ! empty( $model['id'] ) ) {
|
|
$models[] = sanitize_text_field( $model['id'] );
|
|
}
|
|
}
|
|
|
|
if ( empty( $models ) ) {
|
|
return new WP_Error( 'groq_ai_empty_response', __( 'Geen modeldata ontvangen.', 'groq-ai-product-text' ) );
|
|
}
|
|
|
|
return $models;
|
|
}
|
|
|
|
public function generate_content( array $args ) {
|
|
$settings = isset( $args['settings'] ) ? (array) $args['settings'] : [];
|
|
$prompt = isset( $args['prompt'] ) ? $args['prompt'] : '';
|
|
$system_prompt = isset( $args['system_prompt'] ) ? $args['system_prompt'] : '';
|
|
$model = ! empty( $args['model'] ) ? $args['model'] : $this->get_default_model();
|
|
$api_key = $this->get_api_key( $settings );
|
|
|
|
if ( empty( $api_key ) ) {
|
|
return new WP_Error( 'groq_ai_missing_api_key', sprintf( __( 'Stel eerst de API-sleutel voor %s in.', 'groq-ai-product-text' ), $this->get_label() ) );
|
|
}
|
|
|
|
$messages = [
|
|
[
|
|
'role' => 'system',
|
|
'content' => $system_prompt,
|
|
],
|
|
[
|
|
'role' => 'user',
|
|
'content' => $prompt,
|
|
],
|
|
];
|
|
|
|
$request_body = [
|
|
'model' => $model,
|
|
'messages' => $messages,
|
|
'temperature' => isset( $args['temperature'] ) ? (float) $args['temperature'] : 0.7,
|
|
'max_tokens' => 1024,
|
|
];
|
|
|
|
if ( ! empty( $args['response_format'] ) ) {
|
|
$request_body['response_format'] = $args['response_format'];
|
|
}
|
|
|
|
$response = wp_remote_post(
|
|
$this->get_endpoint(),
|
|
[
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $api_key,
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
'body' => wp_json_encode( $request_body ),
|
|
'timeout' => isset( $args['timeout'] ) ? (int) $args['timeout'] : 60,
|
|
]
|
|
);
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
return $response;
|
|
}
|
|
|
|
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
|
|
|
if ( isset( $body['error']['message'] ) ) {
|
|
return new WP_Error( 'groq_ai_provider_error', (string) $body['error']['message'] );
|
|
}
|
|
|
|
if ( empty( $body['choices'][0]['message']['content'] ) ) {
|
|
return new WP_Error(
|
|
'groq_ai_empty_response',
|
|
sprintf( __( 'Geen antwoord ontvangen van %s.', 'groq-ai-product-text' ), $this->get_label() )
|
|
);
|
|
}
|
|
|
|
$content = trim( $body['choices'][0]['message']['content'] );
|
|
$usage = isset( $body['usage'] ) && is_array( $body['usage'] ) ? $body['usage'] : [];
|
|
|
|
return [
|
|
'content' => $content,
|
|
'usage' => $usage,
|
|
'raw_response' => $body,
|
|
];
|
|
}
|
|
|
|
abstract protected function get_endpoint();
|
|
|
|
abstract protected function get_models_endpoint();
|
|
|
|
protected function get_api_key( $settings ) {
|
|
$field = $this->get_option_key();
|
|
return isset( $settings[ $field ] ) ? $settings[ $field ] : '';
|
|
}
|
|
}
|