The frequency_penalty parameter helps reduce repetition in the AI's explanations by discouraging the repeated use of the same words or phrases. This encourages more varied and engaging educational content. A low frequency_penalty value, such as 0.0, allows for more repetition, which can be useful for reinforcing key concepts. Conversely, a high frequency_penalty value, such as 1.0, reduces repetition, promoting more dynamic and varied explanations.
While presence_penalty and frequency_penalty serve different functions in controlling repetition:
-
Presence penalty: Encourages the AI to bring up new concepts by penalizing the model for using words that have already appeared in the conversation history.
-
Frequency penalty: Reduces repetition by penalizing the model for using the same words or phrases multiple times within a single explanation.
This distinction allows you to manage both the range of topics and the variety of language in the AI tutor's output.
In the following example, we set the frequency_penalty to 0.2:
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
// Initialize the HTTP client
$client = new Client([
'base_uri' => getenv('OPENAI_BASE_URL'),
'headers' => [
'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'),
'Content-Type' => 'application/json',
]
]);
// Define a query
$prompt = "Tell me a fun fact about space.";
// Send a request with frequency_penalty parameter
$response = $client->post('v1/chat/completions', [
'json' => [
'model' => 'deepseek-ai/DeepSeek-V3',
'messages' => [['role' => 'user', 'content' => $prompt]],
'frequency_penalty' => 0.2,
],
]);
// Process the response
$body = json_decode($response->getBody(), true);
$reply = trim($body['choices'][0]['message']['content']);
echo "Answer: " . $reply;
?>
By applying a frequency_penalty of 0.2, you can reduce redundancy in the tutor's explanations while still allowing for some repetition when necessary for educational purposes. This results in more dynamic and engaging educational content, striking a balance between variation and reinforcement of important concepts.