feat: Add max output tokens setting and integrate with AI content generation

This commit is contained in:
2026-01-16 18:08:46 +00:00
parent 95f7983e70
commit 1bb10f4b45
5 changed files with 63 additions and 3 deletions

View File

@@ -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 = [
'model' => $model,
'messages' => $messages,
'temperature' => isset( $args['temperature'] ) ? (float) $args['temperature'] : 0.7,
'max_tokens' => 1024,
'max_tokens' => $max_tokens,
];
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'] );
$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 [
'content' => $content,

View File

@@ -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 = [
'contents' => [
[
@@ -153,7 +162,7 @@ class Groq_AI_Provider_Google implements Groq_AI_Provider_Interface {
],
'generationConfig' => [
'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 ) ) );
$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 [
'content' => $content,