diff --git a/api.php b/api.php index 779a7e7..bb1c030 100644 --- a/api.php +++ b/api.php @@ -5,11 +5,10 @@ require_once 'config.php'; // Check if a POST request is sent if ($_SERVER['REQUEST_METHOD'] === 'POST') { $input = json_decode(file_get_contents('php://input'), true); - $user_message = $input['message']; + $conversation = $input['conversation']; // Prepare the command to execute the Python script - // Note: Make sure `python` or `python3` is properly configured in your system's PATH. - $command = escapeshellcmd("python3 assistant.py '$user_message'"); + $command = escapeshellcmd("python3 assistant.py '" . json_encode($conversation) . "'"); $output = shell_exec($command); // Decode the response from Python script diff --git a/assistant.py b/assistant.py index 4904e11..94ba29f 100644 --- a/assistant.py +++ b/assistant.py @@ -1,20 +1,21 @@ import sys import json -from openai import OpenAI import time +from openai import OpenAI -def get_assistant_response(user_message): +def get_assistant_response(conversation): client = OpenAI(api_key='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') # Thread erstellen thread = client.beta.threads.create() - # Nachricht zum Thread hinzufügen - message = client.beta.threads.messages.create( - thread_id=thread.id, - role="user", - content=user_message - ) + # Alle Nachrichten zum Thread hinzufügen + for message in conversation: + client.beta.threads.messages.create( + thread_id=thread.id, + role=message['role'], + content=message['content'] + ) # Run mit dem Assistant starten run = client.beta.threads.runs.create( @@ -39,7 +40,6 @@ def get_assistant_response(user_message): return {"response": assistant_message} if __name__ == "__main__": - if len(sys.argv) > 1: - user_message = sys.argv[1] - response = get_assistant_response(user_message) - print(json.dumps(response)) \ No newline at end of file + conversation = json.loads(sys.argv[1]) + response = get_assistant_response(conversation) + print(json.dumps(response)) \ No newline at end of file diff --git a/index.html b/index.html index 4fd4f7e..a27680f 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,7 @@ + diff --git a/script.js b/script.js index 4f5675a..2451b3f 100644 --- a/script.js +++ b/script.js @@ -2,88 +2,45 @@ document.addEventListener('DOMContentLoaded', () => { const chatForm = document.getElementById('chat-form'); const userInput = document.getElementById('user-input'); const chatMessages = document.getElementById('chat-messages'); - - let threadId; // Variable to store the thread ID + const resetButton = document.getElementById('reset-button'); + let conversation = []; chatForm.addEventListener('submit', async (e) => { e.preventDefault(); const message = userInput.value.trim(); if (message) { addMessage('User', message); + conversation.push({ role: 'user', content: message }); userInput.value = ''; try { - if (!threadId) { - // Create a new thread - threadId = await createThread(message); + const response = await fetch('api.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ conversation: conversation }), + }); + + if (response.ok) { + const data = await response.json(); + addMessage('Assistant', data.response); + conversation.push({ role: 'assistant', content: data.response }); } else { - // Post a message to the existing thread - await postMessageToThread(threadId, message); + throw new Error('Fehler bei der API-Anfrage'); } } catch (error) { - console.error('Error:', error); + console.error('Fehler:', error); addMessage('System', 'Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.'); } } }); - async function createThread(message) { - try { - const response = await fetch('https://api.openai.com/v1/threads', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${OPENAI_API_KEY}` - }, - body: JSON.stringify({ - "messages": [ - { - "role": "user", - "content": message - } - ] - }) - }); - - if (response.ok) { - const data = await response.json(); - addMessage('Assistant', data.choices[0].message.content); - return data.id; - } else { - throw new Error('Fehler bei der Erstellung des Threads'); - } - } catch (error) { - throw error; - } - } - async function postMessageToThread(threadId, message) { - try { - const response = await fetch(`https://api.openai.com/v1/threads/${threadId}/messages`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${OPENAI_API_KEY}` - }, - body: JSON.stringify({ - "messages": [ - { - "role": "user", - "content": message - } - ] - }) - }); - - if (response.ok) { - const data = await response.json(); - addMessage('Assistant', data.choices[0].message.content); - } else { - throw new Error('Fehler beim Posten der Nachricht'); - } - } catch (error) { - throw error; - } - } + resetButton.addEventListener('click', () => { + conversation = []; + chatMessages.innerHTML = ''; + addMessage('System', 'Die Konversation wurde zurückgesetzt.'); + }); function addMessage(sender, message) { const messageElement = document.createElement('div'); @@ -91,4 +48,4 @@ document.addEventListener('DOMContentLoaded', () => { chatMessages.appendChild(messageElement); chatMessages.scrollTop = chatMessages.scrollHeight; } -}); +}); \ No newline at end of file diff --git a/styles.css b/styles.css index 6e6b460..974a8ad 100644 --- a/styles.css +++ b/styles.css @@ -51,4 +51,19 @@ button { button:hover { background-color: #0056b3; +} + +#reset-button { + margin-top: 10px; + padding: 10px 20px; + font-size: 16px; + background-color: #dc3545; + color: white; + border: none; + border-radius: 5px; + cursor: pointer; +} + +#reset-button:hover { + background-color: #c82333; } \ No newline at end of file