feat: Add max output tokens setting and integrate with AI content generation
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
/**
|
/**
|
||||||
* Plugin Name: SitiAI Product Teksten
|
* Plugin Name: SitiAI Product Teksten
|
||||||
* Description: Genereer productteksten met diverse AI-aanbieders rechtstreeks vanuit WooCommerce.
|
* Description: Genereer productteksten met diverse AI-aanbieders rechtstreeks vanuit WooCommerce.
|
||||||
* Version: 1.4.0
|
* Version: 1.4.1
|
||||||
* Author: SitiAI
|
* Author: SitiAI
|
||||||
* Text Domain: siti-ai-product-content-generator
|
* Text Domain: siti-ai-product-content-generator
|
||||||
* Domain Path: /languages
|
* Domain Path: /languages
|
||||||
|
|||||||
@@ -542,6 +542,14 @@ class Groq_AI_Product_Text_Settings_Page {
|
|||||||
'groq_ai_product_text_prompts'
|
'groq_ai_product_text_prompts'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
add_settings_field(
|
||||||
|
'groq_ai_max_output_tokens',
|
||||||
|
__( 'Max output tokens', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||||
|
[ $this, 'render_max_output_tokens_field' ],
|
||||||
|
'groq-ai-product-text-prompts',
|
||||||
|
'groq_ai_product_text_prompts'
|
||||||
|
);
|
||||||
|
|
||||||
add_settings_field(
|
add_settings_field(
|
||||||
'groq_ai_context_fields',
|
'groq_ai_context_fields',
|
||||||
__( 'Standaard productcontext', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
__( 'Standaard productcontext', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||||
@@ -1455,6 +1463,25 @@ class Groq_AI_Product_Text_Settings_Page {
|
|||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function render_max_output_tokens_field() {
|
||||||
|
$settings = $this->plugin->get_settings();
|
||||||
|
$value = isset( $settings['max_output_tokens'] ) ? absint( $settings['max_output_tokens'] ) : 2048;
|
||||||
|
$value = max( 128, min( 8192, $value ) );
|
||||||
|
?>
|
||||||
|
<input type="number"
|
||||||
|
name="<?php echo esc_attr( $this->plugin->get_option_key() ); ?>[max_output_tokens]"
|
||||||
|
min="128"
|
||||||
|
max="8192"
|
||||||
|
step="128"
|
||||||
|
value="<?php echo esc_attr( (string) $value ); ?>"
|
||||||
|
class="small-text"
|
||||||
|
/>
|
||||||
|
<p class="description">
|
||||||
|
<?php esc_html_e( 'Limiet voor lengte van het AI-antwoord. Als teksten afgekapt worden, zet dit hoger (kost vaak wel meer tokens).', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?>
|
||||||
|
</p>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
public function render_context_fields_field() {
|
public function render_context_fields_field() {
|
||||||
$settings = $this->plugin->get_settings();
|
$settings = $this->plugin->get_settings();
|
||||||
$values = isset( $settings['context_fields'] ) ? $settings['context_fields'] : $this->plugin->get_default_context_fields();
|
$values = isset( $settings['context_fields'] ) ? $settings['context_fields'] : $this->plugin->get_default_context_fields();
|
||||||
|
|||||||
@@ -80,11 +80,20 @@ abstract class Groq_AI_Abstract_OpenAI_Provider implements Groq_AI_Provider_Inte
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$max_tokens = isset( $args['max_tokens'] ) ? absint( $args['max_tokens'] ) : 0;
|
||||||
|
if ( $max_tokens <= 0 ) {
|
||||||
|
$max_tokens = isset( $settings['max_output_tokens'] ) ? absint( $settings['max_output_tokens'] ) : 0;
|
||||||
|
}
|
||||||
|
if ( $max_tokens <= 0 ) {
|
||||||
|
$max_tokens = 2048;
|
||||||
|
}
|
||||||
|
$max_tokens = max( 128, min( 8192, $max_tokens ) );
|
||||||
|
|
||||||
$request_body = [
|
$request_body = [
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
'messages' => $messages,
|
'messages' => $messages,
|
||||||
'temperature' => isset( $args['temperature'] ) ? (float) $args['temperature'] : 0.7,
|
'temperature' => isset( $args['temperature'] ) ? (float) $args['temperature'] : 0.7,
|
||||||
'max_tokens' => 1024,
|
'max_tokens' => $max_tokens,
|
||||||
];
|
];
|
||||||
|
|
||||||
if ( ! empty( $args['response_format'] ) ) {
|
if ( ! empty( $args['response_format'] ) ) {
|
||||||
@@ -122,6 +131,10 @@ abstract class Groq_AI_Abstract_OpenAI_Provider implements Groq_AI_Provider_Inte
|
|||||||
|
|
||||||
$content = trim( $body['choices'][0]['message']['content'] );
|
$content = trim( $body['choices'][0]['message']['content'] );
|
||||||
$usage = isset( $body['usage'] ) && is_array( $body['usage'] ) ? $body['usage'] : [];
|
$usage = isset( $body['usage'] ) && is_array( $body['usage'] ) ? $body['usage'] : [];
|
||||||
|
$finish_reason = isset( $body['choices'][0]['finish_reason'] ) ? sanitize_text_field( (string) $body['choices'][0]['finish_reason'] ) : '';
|
||||||
|
if ( '' !== $finish_reason ) {
|
||||||
|
$usage['finish_reason'] = $finish_reason;
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
|
|||||||
@@ -144,6 +144,15 @@ class Groq_AI_Provider_Google implements Groq_AI_Provider_Interface {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$max_tokens = isset( $args['max_tokens'] ) ? absint( $args['max_tokens'] ) : 0;
|
||||||
|
if ( $max_tokens <= 0 ) {
|
||||||
|
$max_tokens = isset( $settings['max_output_tokens'] ) ? absint( $settings['max_output_tokens'] ) : 0;
|
||||||
|
}
|
||||||
|
if ( $max_tokens <= 0 ) {
|
||||||
|
$max_tokens = 2048;
|
||||||
|
}
|
||||||
|
$max_tokens = max( 128, min( 8192, $max_tokens ) );
|
||||||
|
|
||||||
$payload = [
|
$payload = [
|
||||||
'contents' => [
|
'contents' => [
|
||||||
[
|
[
|
||||||
@@ -153,7 +162,7 @@ class Groq_AI_Provider_Google implements Groq_AI_Provider_Interface {
|
|||||||
],
|
],
|
||||||
'generationConfig' => [
|
'generationConfig' => [
|
||||||
'temperature' => isset( $args['temperature'] ) ? (float) $args['temperature'] : 0.7,
|
'temperature' => isset( $args['temperature'] ) ? (float) $args['temperature'] : 0.7,
|
||||||
'maxOutputTokens' => 1024,
|
'maxOutputTokens' => $max_tokens,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -196,6 +205,10 @@ class Groq_AI_Provider_Google implements Groq_AI_Provider_Interface {
|
|||||||
|
|
||||||
$content = trim( implode( "\n\n", array_filter( $texts ) ) );
|
$content = trim( implode( "\n\n", array_filter( $texts ) ) );
|
||||||
$usage = isset( $body['usageMetadata'] ) && is_array( $body['usageMetadata'] ) ? $body['usageMetadata'] : [];
|
$usage = isset( $body['usageMetadata'] ) && is_array( $body['usageMetadata'] ) ? $body['usageMetadata'] : [];
|
||||||
|
$finish_reason = isset( $body['candidates'][0]['finishReason'] ) ? sanitize_text_field( (string) $body['candidates'][0]['finishReason'] ) : '';
|
||||||
|
if ( '' !== $finish_reason ) {
|
||||||
|
$usage['finish_reason'] = $finish_reason;
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ class Groq_AI_Settings_Manager {
|
|||||||
'model' => '',
|
'model' => '',
|
||||||
'store_context' => '',
|
'store_context' => '',
|
||||||
'default_prompt' => '',
|
'default_prompt' => '',
|
||||||
|
'max_output_tokens' => 2048,
|
||||||
'groq_api_key' => '',
|
'groq_api_key' => '',
|
||||||
'openai_api_key' => '',
|
'openai_api_key' => '',
|
||||||
'google_api_key' => '',
|
'google_api_key' => '',
|
||||||
@@ -87,6 +88,7 @@ class Groq_AI_Settings_Manager {
|
|||||||
'model' => '',
|
'model' => '',
|
||||||
'store_context' => '',
|
'store_context' => '',
|
||||||
'default_prompt' => '',
|
'default_prompt' => '',
|
||||||
|
'max_output_tokens' => 2048,
|
||||||
'groq_api_key' => '',
|
'groq_api_key' => '',
|
||||||
'openai_api_key' => '',
|
'openai_api_key' => '',
|
||||||
'google_api_key' => '',
|
'google_api_key' => '',
|
||||||
@@ -129,6 +131,10 @@ class Groq_AI_Settings_Manager {
|
|||||||
|
|
||||||
$image_limit = isset( $input['image_context_limit'] ) ? $this->sanitize_image_context_limit_value( $input['image_context_limit'] ) : $defaults['image_context_limit'];
|
$image_limit = isset( $input['image_context_limit'] ) ? $this->sanitize_image_context_limit_value( $input['image_context_limit'] ) : $defaults['image_context_limit'];
|
||||||
|
|
||||||
|
$max_output_tokens = isset( $input['max_output_tokens'] ) ? absint( $input['max_output_tokens'] ) : absint( $defaults['max_output_tokens'] );
|
||||||
|
// Keep within sane bounds across providers.
|
||||||
|
$max_output_tokens = max( 128, min( 8192, $max_output_tokens ) );
|
||||||
|
|
||||||
$context_fields = $this->normalize_context_fields( $context_posted ? $raw_input['context_fields'] : $defaults['context_fields'] );
|
$context_fields = $this->normalize_context_fields( $context_posted ? $raw_input['context_fields'] : $defaults['context_fields'] );
|
||||||
|
|
||||||
if ( 'none' === $image_mode ) {
|
if ( 'none' === $image_mode ) {
|
||||||
@@ -142,6 +148,7 @@ class Groq_AI_Settings_Manager {
|
|||||||
'model' => $model,
|
'model' => $model,
|
||||||
'store_context' => sanitize_textarea_field( $input['store_context'] ),
|
'store_context' => sanitize_textarea_field( $input['store_context'] ),
|
||||||
'default_prompt' => sanitize_textarea_field( $input['default_prompt'] ),
|
'default_prompt' => sanitize_textarea_field( $input['default_prompt'] ),
|
||||||
|
'max_output_tokens' => $max_output_tokens,
|
||||||
'groq_api_key' => sanitize_text_field( $input['groq_api_key'] ),
|
'groq_api_key' => sanitize_text_field( $input['groq_api_key'] ),
|
||||||
'openai_api_key' => sanitize_text_field( $input['openai_api_key'] ),
|
'openai_api_key' => sanitize_text_field( $input['openai_api_key'] ),
|
||||||
'google_api_key' => sanitize_text_field( $input['google_api_key'] ),
|
'google_api_key' => sanitize_text_field( $input['google_api_key'] ),
|
||||||
|
|||||||
Reference in New Issue
Block a user