-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
249 lines (203 loc) · 8.23 KB
/
server.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
import hashlib
from typing import Optional
from fastapi import FastAPI
from fastapi.exceptions import WebSocketException
from fastapi import WebSocket, WebSocketDisconnect
from networking import ClientConnectionManager
import fastapi as _fastapi
import fastapi.security as _security
import services as _services
import schemas as _schemas
import sqlalchemy.orm as _orm
import models
import websockets
import json
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from gemini_handler import GeminiClient
import os
from dotenv import load_dotenv
from fastapi.security import APIKeyHeader
api_key_header = APIKeyHeader(name="API_KEY")
app = FastAPI()
# Define the allowed origins
origins = [
'https://carecompanionbot.netlify.app',
'https://carecompanion.netlify.app',
]
# Configure CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
websocket_manager = ClientConnectionManager()
# Load the API key from an environment variable
load_dotenv()
# Create a Gemini client instance
gemini_api_key = os.getenv('GEMINI_API_KEY', 'default_key_if_not_set')
gemini_client = GeminiClient(gemini_api_key)
@app.get('/')
async def root():
return {"Message":"CareCompanion API "}
# User authentication endpoints
@app.post("/api/users")
async def create_user(
user: _schemas.UserCreate,
db: _orm.Session = _fastapi.Depends(_services.get_db),
register_api_key: Optional[str] = _fastapi.Depends(api_key_header)
):
""" Endpoint responsible for creating users"""
# Replace "thetechnicalhackers321" with the hashed key
hashed_key = hashlib.sha256("thetechnicalhackers321".encode()).hexdigest()
if register_api_key != hashed_key:
raise _fastapi.HTTPException(status_code=401, detail="Invalid API key")
db_user = await _services.get_user_by_email(db, user.email)
if db_user:
raise _fastapi.HTTPException(status_code=400, detail="Email already in use")
return {await _services.create_user(db, user)
}
@app.post("/api/token")
async def generate_token(
form_data: _security.OAuth2PasswordRequestForm = _fastapi.Depends(),
db: _orm.Session = _fastapi.Depends(_services.get_db),
):
""" Endpoint responsible for generating token """
user = await _services.authenticate_user(form_data.username, form_data.password, db)
if not user:
raise _fastapi.HTTPException(status_code=401, detail="Invalid Credentials")
return await _services.create_token(user)
@app.get("/api/users/me", response_model=_schemas.User)
async def get_user(
user:_schemas.User = _fastapi.Depends(_services.get_current_user),
):
""" Endpoint responsible to get the user"""
return user
# Endpoint to create a conversation
@app.post("/api/create_convo/", response_model=dict)
async def create_conversation(
conversation: _schemas.ConversationCreate,
db: _orm.Session = _fastapi.Depends(_services.get_db),
token: str = _fastapi.Depends(_services.authenticate_token),
):
"""Endpoint used to create a conversation """
user = await _services.get_current_user(db, token)
convo = await _services.create_conversation_service(user, conversation, db)
return {
"conversation_id":convo.id,
'conversation_date_created':convo.date_created ,
"message": "Conversation created successfully"
}
# Endpoint to get all conversation user has
@app.get('/api/convos/{user_id}')
async def get_user_conversation(
user_id:int,
db: _orm.Session = _fastapi.Depends(_services.get_db),
token: str = _fastapi.Depends(_services.authenticate_token),
):
""" Get al the user conversations"""
user = await _services.get_current_user(db, token)
user_conversations = await _services.get_user_conversations(db, user.id)
if not user:
raise _fastapi.HTTPException(status_code=404, detail="Conversation not found")
return user_conversations
#Endpoint to delete a conversation a user has
@app.delete('/api/delete_convos/{room_id}')
async def delete_conversation(
room_id:str,
db:_orm.Session = _fastapi.Depends(_services.get_db),
token: str = _fastapi.Depends(_services.authenticate_token)
):
conversation = await _services.get_conversation_by_id(db, room_id)
# Check if the conversation exists
if not conversation:
raise _fastapi.HTTPException(status_code=404, detail="Conversation not found")
try :
#delete the conversation in database
db.delete(conversation)
db.commit()
return {'Message': "Conversation Deleted Sucessfully"}
except Exception as e:
raise _fastapi.HTTPException(status_code=500, detail=f"Fail to delete conversation {str(e)}")
#Endpoint to get all messages from a conversation
@app.get('/api/chat_history/{room_id}')
async def get_messages_from_conversation(
room_id:str,
db: _orm.Session = _fastapi.Depends(_services.get_db),
token: str = _fastapi.Depends(_services.authenticate_token),
):
""" Get all the messages from conversations"""
conversation = await _services.get_conversation_by_id(db, room_id)
if not conversation:
_fastapi.HTTPException(status_code=404, detail="Conversation not found")
messages = await _services.get_all_messages_from_conversation(db, conversation.id)
message_payload_schema = [_schemas.MessageSchema(**message.__dict__) for message in messages]
gemini_client.set_chat_history(message_payload_schema)
return message_payload_schema
@app.websocket("/api/chat/{room_id}/{token}")
async def chat_endpoint(
room_id:str,
token:str,
websocket:WebSocket,
db:_orm.Session = _fastapi.Depends(_services.get_db)
):
await websocket.accept()
client_ip = websocket.client.host
print(f"New connection from IP: {client_ip}")
user = await _services.verify_socket_connection(token, db=db)
conversation = await _services.check_conversation_exists(db, room_id)
if not conversation:
await websocket.send_json({"message":'conversation does not exist'})
await websocket_manager.connect(room_id, websocket)
while True:
try:
# Receive JSON data containing the message payload
user_input = await websocket.receive_text()
websocket_conn = websocket_manager.active_connections[room_id]
await websocket_conn.send_json({
"text_content": user_input,
"is_bot_message": False,
})
# Get the response from the Gemini API
gemini_client.set_location(client_ip)
response = gemini_client.get_response(user_input)
# Send the AI's response back to the client via WebSocket
bot_response = {
"text_content": response,
"is_bot_message": True,
}
await websocket_conn.send_json(bot_response)
new_message = models.Message(
text_content=user_input,
author_id=user.id,
conversation_id=room_id
)
db.add(new_message)
db.commit()
# add the message record in db
bot_message = models.Message(
text_content=bot_response["text_content"],
conversation_id=room_id,
is_bot_message=True,
)
db.add(bot_message)
db.commit()
except websockets.exceptions.ConnectionClosedOK as e:
websocket_manager.disconnect(room_id)
except websockets.exceptions.ConnectionClosedError as error:
websocket_manager.disconnect(room_id)
except json.decoder.JSONDecodeError:
# if user does not put in the format of json
websocket_manager.disconnect(room_id)
raise WebSocketException(code=_fastapi.status.WS_1008_POLICY_VIOLATION, reason="Unable to parse JSON")
except WebSocketDisconnect:
websocket_manager.disconnect(room_id)
except Exception as e:
websocket_manager.disconnect(room_id)
break
if __name__ == "__main__":
import uvicorn
# Run the server using uvicorn when this script is executed directly
uvicorn.run(app, host="0.0.0.0", port=8000)