Fehler beim Ausführen des Phyton-Skriptes behoben
This commit is contained in:
2
api.php
2
api.php
@@ -8,7 +8,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$conversation = $input['conversation'];
|
$conversation = $input['conversation'];
|
||||||
|
|
||||||
// Prepare the command to execute the Python script
|
// 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);
|
$output = shell_exec($command);
|
||||||
|
|
||||||
// Decode the response from Python script
|
// Decode the response from Python script
|
||||||
|
|||||||
45
assistant_v1.py
Normal file
45
assistant_v1.py
Normal 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))
|
||||||
@@ -7,7 +7,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
|
|
||||||
if (!empty($userMessage)) {
|
if (!empty($userMessage)) {
|
||||||
// Python-Skript aufrufen
|
// Python-Skript aufrufen
|
||||||
$command = escapeshellcmd("python3 assistant.py " . escapeshellarg($userMessage));
|
$command = escapeshellcmd("python3 assistant_v1.py " . escapeshellarg($userMessage));
|
||||||
$response = shell_exec($command);
|
$response = shell_exec($command);
|
||||||
$assistantResponse = json_decode($response, true);
|
$assistantResponse = json_decode($response, true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user