-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (48 loc) · 1.67 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from flask import Flask, render_template, request, Response, jsonify
from openai import OpenAI
import logging
# Load environment variables from a .env file
from dotenv import load_dotenv
load_dotenv()
# Initialize the OpenAI client
assistant_id = os.getenv('ASSISTANT_ID')
api_key = os.getenv('OPENAI_API_KEY')
client = OpenAI(api_key=api_key)
app = Flask(__name__)
# Configure logging
logging.basicConfig(level=logging.INFO)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/answer", methods=["POST"])
def answer():
data = request.get_json()
if not data or "message" not in data:
return jsonify({"error": "Invalid request"}), 400
message = data["message"]
def generate():
try:
stream = client.beta.threads.create_and_run(
assistant_id=assistant_id,
thread={
"messages": [
{
"role": "user",
"content": message,
}
]
},
stream=True
)
for event in stream:
if event.data.object == "thread.message.delta":
for content in event.data.delta.content:
if content.type == "text":
yield content.text.value
except Exception as e:
logging.error(f"Error generating response: {e}")
yield "An error occurred while generating the response."
return Response(generate(), content_type="text/plain")
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8004, debug=False)