Chat Bot für Chat GPT mit Chat Historie via Session. Gibt Assi Deutsch aus. Einfach als zB. Assi-Chat.php abspeichern.
<?php
session_start();
// Dein API-Schlüssel von OpenAI
$apiKey = "DEIN_OPENAI_API_SCHLUESSEL";
// Falls es noch keine Chat-Historie gibt, initialisieren
if (!isset($_SESSION["chat_history"])) {
$_SESSION["chat_history"] = [
[
"role" => "system",
"content" => "Du bist ein Chatbot, der in Assi-Deutsch antwortet. Sprich locker, direkt, frech, mit Straßen-Slang, aber ohne beleidigend zu werden."
]
];
}
$responseText = "";
// Wenn Nachricht gesendet wurde
if (!empty($_POST["userMessage"])) {
$userMessage = trim($_POST["userMessage"]);
// Nachricht des Users speichern
$_SESSION["chat_history"][] = [
"role" => "user",
"content" => $userMessage
];
// Anfrage-Daten
$data = [
"model" => "gpt-4o-mini",
"messages" => $_SESSION["chat_history"]
];
// API-Anfrage
$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)) {
$responseText = "Fehler: " . curl_error($ch);
} else {
$result = json_decode($response, true);
$responseText = $result["choices"][0]["message"]["content"];
// Bot-Antwort auch in der Session speichern
$_SESSION["chat_history"][] = [
"role" => "assistant",
"content" => $responseText
];
}
curl_close($ch);
}
// Chat leeren
if (isset($_POST["reset"])) {
session_destroy();
header("Location: " . $_SERVER["PHP_SELF"]);
exit;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Assi-Chat mit ChatGPT</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: auto; background: #f4f4f4; padding: 20px; }
h1 { text-align: center; }
.chat-box { background: white; padding: 15px; border-radius: 8px; }
.message { padding: 10px; margin-bottom: 10px; border-radius: 5px; }
.user { background: #d0e6ff; }
.assistant { background: #e2ffe2; }
textarea { width: 100%; height: 80px; padding: 10px; border-radius: 5px; border: 1px solid #ccc; }
button { padding: 10px 15px; border: none; background: #333; color: white; border-radius: 5px; cursor: pointer; }
button:hover { background: #555; }
.btn-reset { background: #a00; }
.btn-reset:hover { background: #c00; }
</style>
</head>
<body>
<h1>Assi-Chat mit ChatGPT</h1>
<div class="chat-box">
<?php
// Verlauf anzeigen (ohne system-Prompt)
foreach ($_SESSION["chat_history"] as $msg) {
if ($msg["role"] === "user") {
echo '<div class="message user"><strong>Du:</strong> ' . nl2br(htmlspecialchars($msg["content"])) . '</div>';
} elseif ($msg["role"] === "assistant") {
echo '<div class="message assistant"><strong>Assi-Bot:</strong> ' . nl2br(htmlspecialchars($msg["content"])) . '</div>';
}
}
?>
<form method="post">
<textarea name="userMessage" placeholder="Schreib was..." required></textarea>
<br><br>
<button type="submit">Abschicken</button>
<button type="submit" name="reset" class="btn-reset">Chat löschen</button>
</form>
</div>
</body>
</html>
Funktionen in diesem Codeschnipsel
- Komplette Chat-Historie bleibt erhalten, solange die Session aktiv ist
- „Chat löschen“-Button setzt den Verlauf zurück
- Slang/Assi-Deutsch ist durch den Systemprompt fest im Ton verankert
- Benutzer- und Bot-Nachrichten werden farblich unterschieden