-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
340 lines (282 loc) · 10.6 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from flask import Flask, request, jsonify, make_response
from sqlalchemy.orm import Session
from app.src.db import get_db
from app.models.user import User
from app.models.conversation_thread import ConversationThread
from app.api.auth import token_required, authenticate_user, generate_token
import logging
from typing import Iterator, Dict, Any
from http import HTTPStatus
from app.assistants.openai import OpenAIAssistant
app = Flask(__name__)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("app.log"), # Logs to a file
logging.StreamHandler(), # Also logs to the console
],
)
logger = logging.getLogger(__name__)
openai_client = OpenAIAssistant()
def get_database_session() -> Iterator[Session]:
db = next(get_db())
try:
yield db
finally:
db.close()
@app.route("/register", methods=["POST"])
def register() -> Dict[str, Any]:
data = request.get_json()
if not data:
logger.warning("Empty JSON request body")
return jsonify({"error": "Request body must be JSON"}), HTTPStatus.BAD_REQUEST
email = data.get("email")
password = data.get("password")
if not email or not password:
logger.warning("Missing email or password in JSON data")
return (
jsonify({"error": "Email and password are required"}),
HTTPStatus.BAD_REQUEST,
)
db_session = next(get_database_session())
if db_session.query(User).filter_by(email=email).first() is not None:
logger.info(f"Attempt to register with existing email: {email}")
return (
jsonify({"error": "User with this email already exists."}),
HTTPStatus.BAD_REQUEST,
)
try:
new_user = User(email=email)
new_user.set_password(password)
db_session.add(new_user)
db_session.commit()
logger.info(f"User registered successfully with email: {email}")
return jsonify({"message": "User registered successfully."}), HTTPStatus.CREATED
except Exception as e:
logger.error(f"Exception during registration: {str(e)}")
return (
jsonify({"error": "An error occurred registering the user."}),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
@app.route("/login", methods=["POST"])
def login() -> Dict[str, Any]:
data = request.get_json()
if not data:
logger.warning("Empty JSON request body")
return jsonify({"error": "Request body must be JSON"}), HTTPStatus.BAD_REQUEST
email = data.get("email")
password = data.get("password")
if not email or not password:
logger.warning("Missing email or password in JSON data")
return (
jsonify({"error": "Email and password are required"}),
HTTPStatus.BAD_REQUEST,
)
user = authenticate_user(email, password)
if user:
token = generate_token(user.id)
logger.info(f"User logged in: {email}")
response = make_response(
jsonify({"message": "Login successful"}), HTTPStatus.OK
)
response.set_cookie(
key="token",
value=token,
httponly=True,
secure=False, # TODO: Set to True in production for HTTPS
samesite="Lax",
)
return response
logger.warning(f"Invalid login attempt for email: {email}")
return jsonify({"error": "Invalid credentials"}), HTTPStatus.UNAUTHORIZED
@app.route("/user", methods=["GET"])
@token_required
def get_user_info(current_user: User) -> Dict[str, Any]:
logger.info(f"User info retrieved for user ID: {current_user.id}")
return jsonify({"id": current_user.id, "email": current_user.email}), HTTPStatus.OK
@app.route("/conversations", methods=["POST"])
@token_required
def create_conversation(current_user: User) -> Dict[str, Any]:
thread_id = openai_client.create_thread()
if not thread_id:
logger.error("Error creating conversation thread")
return (
jsonify({"error": "An error occurred creating the conversation thread"}),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
assistant_id = openai_client.get_assistant_id()
if not assistant_id:
logger.error("Error getting assistant ID")
return (
jsonify({"error": "An error occurred getting the assistant ID"}),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
new_conversation = ConversationThread(
user_id=current_user.id,
thread_id=thread_id,
assistant_id=assistant_id,
)
db_session = next(get_database_session())
db_session.add(new_conversation)
db_session.commit()
logger.info(f"Conversation thread created with ID: {new_conversation.id}")
return (
jsonify(
{"message": "Conversation created", "conversation_id": new_conversation.id}
),
HTTPStatus.CREATED,
)
@app.route("/conversations", methods=["GET"])
@token_required
def list_conversations(current_user: User) -> Dict[str, Any]:
db_session = next(get_database_session())
conversations = (
db_session.query(ConversationThread).filter_by(user_id=current_user.id).all()
)
logger.info(f"Conversations listed for user ID: {current_user.id}")
return (
jsonify(
[
{
"id": conversation.id,
"thread_id": conversation.thread_id,
"assistant_id": conversation.assistant_id,
"created_at": conversation.created_at,
"status": conversation.status,
}
for conversation in conversations
]
),
HTTPStatus.OK,
)
@app.route("/conversations/<int:conversation_id>/messages", methods=["POST"])
@token_required
def send_message(current_user: User, conversation_id: int) -> Dict[str, Any]:
data = request.get_json()
if not data:
logger.warning("Empty JSON request body")
return jsonify({"error": "Request body must be JSON"}), HTTPStatus.BAD_REQUEST
message = data.get("message")
if not message:
logger.warning("Message missing in request body")
return jsonify({"error": "Message is required"}), HTTPStatus.BAD_REQUEST
db_session = next(get_database_session())
conversation = (
db_session.query(ConversationThread)
.filter_by(id=conversation_id, user_id=current_user.id)
.first()
)
if not conversation:
logger.warning(
f"Conversation {conversation_id} not found for user {current_user.id}"
)
return jsonify({"error": "Conversation not found"}), HTTPStatus.NOT_FOUND
openai_response = openai_client.send_message(
thread_id=conversation.thread_id,
assistant_id=conversation.assistant_id,
message=message,
)
if not openai_response:
logger.error("Error sending message to OpenAI")
return (
jsonify({"error": "An error occurred sending the message"}),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
logger.info(f"Message sent to conversation {conversation_id}")
return jsonify(openai_response), HTTPStatus.OK
@app.route("/conversations/<int:conversation_id>/messages", methods=["GET"])
@token_required
def get_conversation_messages(
conversation_id: str, current_user: User
) -> Dict[str, Any]:
try:
db_session = next(get_database_session())
conversation = (
db_session.query(ConversationThread)
.filter_by(id=conversation_id, user_id=current_user.id)
.first()
)
if not conversation:
logger.warning(
f"Conversation with ID {conversation_id} not found for user {current_user.id}"
)
return jsonify({"error": "Conversation not found"}), HTTPStatus.NOT_FOUND
# Fetch the thread messages from OpenAI
messages = openai_client.get_thread_messages(conversation.thread_id)
if not messages and not isinstance(messages, list):
logger.error("Error fetching conversation messages")
return (
jsonify(
{"error": "An error occurred fetching the conversation messages"}
),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
return (
jsonify(
{
"conversation_id": conversation_id,
"messages": messages,
}
),
HTTPStatus.OK,
)
except Exception as e:
logger.error(
f"Error fetching conversation messages for {conversation_id}: {str(e)}"
)
return (
jsonify({"error": "Failed to retrieve conversation messages"}),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
@app.route("/conversations/<int:conversation_id>/thread", methods=["GET"])
@token_required
def get_conversation_thread(conversation_id: int, current_user: User) -> Dict[str, Any]:
try:
db_session = next(get_database_session())
conversation = (
db_session.query(ConversationThread)
.filter_by(id=conversation_id, user_id=current_user.id)
.first()
)
if not conversation:
logger.warning(
f"Conversation with ID {conversation_id} not found for user {current_user.id}"
)
return jsonify({"error": "Conversation not found"}), HTTPStatus.NOT_FOUND
# Fetch the thread messages from OpenAI
thread = openai_client.get_thread(conversation.thread_id).to_json()
if not thread:
logger.error("Error fetching conversation thread messages")
return (
jsonify(
{"error": "An error occurred fetching the conversation thread"}
),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
return (
jsonify(
{
"conversation_id": conversation_id,
"thread_id": conversation.thread_id,
"content": thread,
}
),
HTTPStatus.OK,
)
except Exception as e:
logger.error(
f"Error fetching conversation thread for {conversation_id}: {str(e)}"
)
return (
jsonify({"error": "Failed to retrieve conversation thread"}),
HTTPStatus.INTERNAL_SERVER_ERROR,
)
@app.route("/")
def index() -> str:
logger.info("Index page accessed")
return "Welcome to LLM Connect!"
if __name__ == "__main__":
app.run(debug=True)
# fapp = ChatApp()
# fapp.run()