neue Version von Perplexity mit Beispielcode von OpenAI
This commit is contained in:
51
assistant.py
51
assistant.py
@@ -1,28 +1,45 @@
|
|||||||
import sys
|
import sys
|
||||||
import openai
|
|
||||||
import json
|
import json
|
||||||
|
from openai import OpenAI
|
||||||
|
import time
|
||||||
|
|
||||||
def main(user_message):
|
def get_assistant_response(user_message):
|
||||||
# Set your OpenAI API key from the environment, or replace with your key here
|
client = OpenAI(api_key='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')
|
||||||
openai.api_key = "sk-proj-CMVUSsmXIr-Da3a8bpAByG0v2FD1hxEahGs7CqTz7tcegAWGP03OitAAD5LQVf_z5ZUucDWZ10pSHXJVzoWZeGCHueskkC5IMLccUldlvTlsfUA"
|
|
||||||
|
|
||||||
try:
|
# Thread erstellen
|
||||||
# Call the OpenAI ChatCompletion API
|
thread = client.beta.threads.create()
|
||||||
response = openai.ChatCompletion.create(
|
|
||||||
model="gpt-3.5-turbo",
|
# Nachricht zum Thread hinzufügen
|
||||||
messages=[{"role": "user", "content": user_message}],
|
message = client.beta.threads.messages.create(
|
||||||
assistant_id="asst_0LyJEWQS90Sx0oEIYBVWE47C" # Assistant ID
|
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)
|
||||||
|
|
||||||
assistant_message = response['choices'][0]['message']['content']
|
# Antwort abrufen
|
||||||
print(json.dumps({"response": assistant_message}))
|
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
||||||
|
assistant_message = messages.data[0].content[0].text.value
|
||||||
|
|
||||||
except Exception as e:
|
return {"response": assistant_message}
|
||||||
print(json.dumps({"response": f"An error occurred: {str(e)}"}))
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
user_message = sys.argv[1]
|
user_message = sys.argv[1]
|
||||||
main(user_message)
|
response = get_assistant_response(user_message)
|
||||||
else:
|
print(json.dumps(response))
|
||||||
print(json.dumps({"response": "No message provided"}))
|
|
||||||
65
index.php
65
index.php
@@ -1,29 +1,60 @@
|
|||||||
<?php
|
<?php
|
||||||
// Include the configuration file
|
// index.php
|
||||||
require_once 'config.php';
|
session_start();
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$userMessage = $_POST['message'] ?? '';
|
||||||
|
|
||||||
|
if (!empty($userMessage)) {
|
||||||
|
// Python-Skript aufrufen
|
||||||
|
$command = escapeshellcmd("python3 assistant_api.py " . escapeshellarg($userMessage));
|
||||||
|
$response = shell_exec($command);
|
||||||
|
$assistantResponse = json_decode($response, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="de">
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<title>OpenAI Assistant Chat</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<style>
|
||||||
<title>ChatGPT Assistent</title>
|
.chat-container {
|
||||||
<link rel="stylesheet" href="styles.css">
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.message {
|
||||||
|
margin: 10px 0;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
.user-message {
|
||||||
|
background-color: #e3f2fd;
|
||||||
|
}
|
||||||
|
.assistant-message {
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="chat-container">
|
<div class="chat-container">
|
||||||
<h1>ChatGPT Assistent</h1>
|
<h1>Chat mit OpenAI Assistant</h1>
|
||||||
<div id="chat-messages"></div>
|
|
||||||
<form id="chat-form">
|
<form method="POST">
|
||||||
<input type="text" id="user-input" placeholder="Geben Sie Ihre Nachricht ein..." required>
|
<textarea name="message" rows="4" cols="50" placeholder="Ihre Nachricht..."></textarea>
|
||||||
|
<br>
|
||||||
<button type="submit">Senden</button>
|
<button type="submit">Senden</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<?php if (isset($assistantResponse)): ?>
|
||||||
|
<div class="message user-message">
|
||||||
|
<strong>Sie:</strong> <?php echo htmlspecialchars($userMessage); ?>
|
||||||
|
</div>
|
||||||
|
<div class="message assistant-message">
|
||||||
|
<strong>Assistant:</strong> <?php echo htmlspecialchars($assistantResponse['response']); ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
|
||||||
// Pass PHP variables to JavaScript
|
|
||||||
const OPENAI_API_KEY = "<?php echo $openai_api_key; ?>";
|
|
||||||
const ASSISTANT_ID = "<?php echo $assistant_id; ?>";
|
|
||||||
</script>
|
|
||||||
<script src="script.js"></script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user