diff --git a/api.php b/api.php index bb1c030..c6b9963 100644 --- a/api.php +++ b/api.php @@ -8,7 +8,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $conversation = $input['conversation']; // Prepare the command to execute the Python script - $command = escapeshellcmd("python3 assistant.py '" . json_encode($conversation) . "'"); + $command = escapeshellcmd("python3 assistant_v1.py '" . json_encode($conversation) . "'"); $output = shell_exec($command); // Decode the response from Python script diff --git a/assistant_v1.py b/assistant_v1.py new file mode 100644 index 0000000..4904e11 --- /dev/null +++ b/assistant_v1.py @@ -0,0 +1,45 @@ +import sys +import json +from openai import OpenAI +import time + +def get_assistant_response(user_message): + 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 + ) + + # Run mit dem Assistant starten + run = client.beta.threads.runs.create( + thread_id=thread.id, + assistant_id="asst_0LyJEWQS90Sx0oEIYBVWE47C" + ) + + # Auf Antwort warten + while True: + run_status = client.beta.threads.runs.retrieve( + thread_id=thread.id, + run_id=run.id + ) + if run_status.status == 'completed': + break + time.sleep(1) + + # Antwort abrufen + messages = client.beta.threads.messages.list(thread_id=thread.id) + assistant_message = messages.data[0].content[0].text.value + + 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 diff --git a/index.php b/index.php index b81f856..0a5af1c 100644 --- a/index.php +++ b/index.php @@ -7,7 +7,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!empty($userMessage)) { // Python-Skript aufrufen - $command = escapeshellcmd("python3 assistant.py " . escapeshellarg($userMessage)); + $command = escapeshellcmd("python3 assistant_v1.py " . escapeshellarg($userMessage)); $response = shell_exec($command); $assistantResponse = json_decode($response, true); }