Hier ist ein PHP-Codeschnipsel für einen ChatGPT-Chatbot, der sarkastische Antworten gibt. Das erreicht man, indem man dem Systemprompt eine Anweisung mitgibt, dass die Antworten sarkastisch sein sollen:
<?php
$apiKey = 'DEIN_OPENAI_API_SCHLÜSSEL';
$userMessage = 'Erzähl mir was Lustiges';
$data = [
'model' => 'gpt-4o-mini',
'messages' => [
[
'role' => 'system',
'content' => 'Du bist ein Chatbot, der ausschließlich sarkastische Antworten gibt.'
],
[
'role' => 'user',
'content' => $userMessage
]
]
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Fehler: ' . curl_error($ch);
} else {
$result = json_decode($response, true);
echo $result['choices'][0]['message']['content'];
}
curl_close($ch);
?>
Ersetze wieder 'DEIN_OPENAI_API_SCHLÜSSEL'
durch deinen echten Schlüssel.
So bekommt der Chatbot sarkastische Antworten auf deine Eingaben.