1. Version zum Testen

This commit is contained in:
Martin Rattensberger
2024-11-18 10:42:12 +01:00
parent 53a7cb9954
commit 98095814eb
5 changed files with 143 additions and 0 deletions

23
api.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
// Einbinden der Konfigurationsdatei
require_once 'config.php';
// Überprüfen, ob eine POST-Anfrage gesendet wurde
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Lesen der Eingabe aus dem POST-Body
$input = json_decode(file_get_contents('php://input'), true);
$user_message = $input['message'];
// Hier würden Sie den API-Aufruf an OpenAI mit den Konfigurationsvariablen durchführen
// Dies ist ein Platzhalter für die tatsächliche API-Implementierung
$response = "Dies ist eine Beispielantwort vom ChatGPT-Assistenten.";
// Senden der Antwort zurück an das Frontend
header('Content-Type: application/json');
echo json_encode(['response' => $response]);
} else {
// Wenn keine POST-Anfrage, senden Sie einen Fehler
header('HTTP/1.1 405 Method Not Allowed');
echo 'Nur POST-Anfragen sind erlaubt.';
}
?>

5
config.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
// API-Schlüssel für OpenAI und den Assistenten
$openai_api_key = 'Ihr_OpenAI_API_Schlüssel';
$assistant_id = 'Ihr_Assistant_ID';
?>

20
index.html Normal file
View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChatGPT Assistent</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="chat-container">
<h1>ChatGPT Assistent</h1>
<div id="chat-messages"></div>
<form id="chat-form">
<input type="text" id="user-input" placeholder="Geben Sie Ihre Nachricht ein..." required>
<button type="submit">Senden</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>

41
script.js Normal file
View File

@@ -0,0 +1,41 @@
document.addEventListener('DOMContentLoaded', () => {
const chatForm = document.getElementById('chat-form');
const userInput = document.getElementById('user-input');
const chatMessages = document.getElementById('chat-messages');
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const message = userInput.value.trim();
if (message) {
addMessage('User', message);
userInput.value = '';
try {
const response = await fetch('api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: message }),
});
if (response.ok) {
const data = await response.json();
addMessage('Assistant', data.response);
} else {
throw new Error('Fehler bei der API-Anfrage');
}
} catch (error) {
console.error('Fehler:', error);
addMessage('System', 'Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.');
}
}
});
function addMessage(sender, message) {
const messageElement = document.createElement('div');
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
chatMessages.appendChild(messageElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
});

54
styles.css Normal file
View File

@@ -0,0 +1,54 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.chat-container {
max-width: 800px;
margin: 20px auto;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
h1 {
text-align: center;
color: #333;
}
#chat-messages {
height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 20px;
}
#chat-form {
display: flex;
}
#user-input {
flex-grow: 1;
padding: 10px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 5px 0 0 5px;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}