Fehler beim Ausführen des Phyton-Skriptes behoben

This commit is contained in:
2024-11-19 12:41:48 +01:00
parent 2a31ef1c19
commit 91279fa711
3 changed files with 47 additions and 2 deletions

View File

@@ -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

45
assistant_v1.py Normal file
View File

@@ -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))

View File

@@ -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);
}