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 openai
|
||||
import json
|
||||
from openai import OpenAI
|
||||
import time
|
||||
|
||||
def main(user_message):
|
||||
# Set your OpenAI API key from the environment, or replace with your key here
|
||||
openai.api_key = "sk-proj-CMVUSsmXIr-Da3a8bpAByG0v2FD1hxEahGs7CqTz7tcegAWGP03OitAAD5LQVf_z5ZUucDWZ10pSHXJVzoWZeGCHueskkC5IMLccUldlvTlsfUA"
|
||||
def get_assistant_response(user_message):
|
||||
client = OpenAI(api_key='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')
|
||||
|
||||
try:
|
||||
# Call the OpenAI ChatCompletion API
|
||||
response = openai.ChatCompletion.create(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": user_message}],
|
||||
assistant_id="asst_0LyJEWQS90Sx0oEIYBVWE47C" # Assistant ID
|
||||
# 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)
|
||||
|
||||
assistant_message = response['choices'][0]['message']['content']
|
||||
print(json.dumps({"response": assistant_message}))
|
||||
# Antwort abrufen
|
||||
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
||||
assistant_message = messages.data[0].content[0].text.value
|
||||
|
||||
except Exception as e:
|
||||
print(json.dumps({"response": f"An error occurred: {str(e)}"}))
|
||||
return {"response": assistant_message}
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1:
|
||||
user_message = sys.argv[1]
|
||||
main(user_message)
|
||||
else:
|
||||
print(json.dumps({"response": "No message provided"}))
|
||||
response = get_assistant_response(user_message)
|
||||
print(json.dumps(response))
|
||||
65
index.php
65
index.php
@@ -1,29 +1,60 @@
|
||||
<?php
|
||||
// Include the configuration file
|
||||
require_once 'config.php';
|
||||
// index.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>
|
||||
<html lang="de">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>ChatGPT Assistent</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
<title>OpenAI Assistant Chat</title>
|
||||
<style>
|
||||
.chat-container {
|
||||
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>
|
||||
<body>
|
||||
<div class="chat-container">
|
||||
<h1>ChatGPT Assistent</h1>
|
||||
<div id="chat-messages"></div>
|
||||
<form id="chat-form">
|
||||
<input type="text" id="user-input" placeholder="Geben Sie Ihre Nachricht ein..." required>
|
||||
<h1>Chat mit OpenAI Assistant</h1>
|
||||
|
||||
<form method="POST">
|
||||
<textarea name="message" rows="4" cols="50" placeholder="Ihre Nachricht..."></textarea>
|
||||
<br>
|
||||
<button type="submit">Senden</button>
|
||||
</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>
|
||||
<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>
|
||||
</html>
|
||||
Reference in New Issue
Block a user