-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
339 lines (277 loc) · 10.3 KB
/
main.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
"""Main entrypoint for the app."""
from datetime import datetime
import logging
import os
from typing import Annotated, List
from functools import lru_cache
from pathlib import Path
from typing import Union
from tempfile import NamedTemporaryFile
import asyncio
from dotenv import load_dotenv
from fastapi import (
BackgroundTasks,
Cookie,
Depends,
FastAPI,
Query,
HTTPException,
WebSocket,
WebSocketDisconnect,
WebSocketException,
status,
)
from fastapi.middleware.cors import CORSMiddleware
from langchain.schema import messages_to_dict
from pydantic import BaseSettings, BaseModel
from sqlalchemy.orm import Session
import crud
from schemas import ChatResponse, Job
from ingest import ingest_docs, ingest_text
from database import get_db_session, test_db_connection
from agent import chat_agent
from task import generate_report, generate_mock
from evaluate_agent import evaluate_agent
from callback import StreamingLLMCallbackHandler
from stt import Deepgram_STT
import openai
from redis import asyncio as aioredis
class Settings(BaseSettings):
class Config:
env_file = ".env"
case_sensitive = True
app = FastAPI()
origins = ["http://localhost:3000", "https://chat-job-in.vercel.app"]
gd_client = Deepgram_STT()
logger = logging.getLogger(__name__)
class ConnectionManager:
def __init__(self) -> None:
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_personal_message(self, message: str, websocket: WebSocket):
await websocket.send_text(message)
async def send_dict_message(self, message: dict, websocket: WebSocket):
await websocket.send_json(message)
async def send_binary_message(self, message: bytes, websocket: WebSocket):
await websocket.send_bytes(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
websocket_manager = ConnectionManager()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@lru_cache()
def get_settings():
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
return Settings()
async def test_redis_connection():
redis_url = os.environ["REDIS_URL"]
connection = aioredis.from_url(redis_url)
try:
await connection.ping()
# await connection.ping()
finally:
await connection.close()
@app.on_event("startup")
async def startup_event():
# setup openai
openai.api_base = os.environ["OPENAI_API_BASE"]
openai.api_type = os.environ["OPENAI_API_TYPE"]
await test_db_connection()
# test redis connection
await test_redis_connection()
# create job duty
# create item
@app.get("/jd/", response_model=List[Job])
def create_jd(
skip: int = 0, limit: int = 100, db_session: Session = Depends(get_db_session)
):
db_jobs = crud.get_job_list(db_session, skip=skip, limit=limit)
if db_jobs is None:
raise HTTPException(status_code=404, detail="Job not found")
return db_jobs
# check valid session
def get_cookie_or_token(
websocket: WebSocket,
session: Annotated[Union[str, None], Cookie()] = None,
token: Annotated[Union[str, None], Query()] = None,
db_session: Session = Depends(get_db_session),
):
# TODO: check if session is valid
# print("session", session)
print("session", session)
print("token", token)
if session is None or token is None or "user" not in session:
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
# TODO session invalid
valid_session = crud.get_session_by_id(db_session, token, session)
if valid_session is None:
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
return valid_session
# add endpoint for setup job role
@app.websocket("/interview/{interview_id}/ws")
async def websocket_endpoint(
*,
websocket: WebSocket,
interview_id: str,
t: str,
db_session: Session = Depends(get_db_session),
): # CHECK VALIDITY
result = crud.get_interview_by_id(db_session, interview_id)
client_host = websocket.client.host
if t is None:
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
# test if have a valid ticket
interviewTicket = crud.get_interview_ticket(db_session, t)
# if interview ticket is not expired
# TODO check host
if (
interviewTicket is None
or interviewTicket.expiredAt < datetime.now()
# or interviewTicket.host != client_host
):
print(interviewTicket)
print(datetime.now())
print(">>>> fail load interview")
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
if (
result is None
or "job" not in result
or "resume" not in result
or "interview" not in result
or result["interview"].result is not None
or result["interview"].history is not None
):
print(">>>> fail load interview")
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
interview_type = result["interview"].type
vs = await ingest_docs(
index="r_" + result["resume"].id, input=result["resume"].resource
)
job_input = f'Job Title: {result["job"].title} Employer: {result["job"].company } Job Description: {result["job"].content}'
job_vs = await ingest_text(index="j_" + result["job"].id, input=job_input)
# create agent specific to this chat
new_chat = chat_agent(
vs,
job_vs,
)
print(">>>> chat agent created")
await websocket_manager.connect(websocket)
start_resp = ChatResponse(sender="bot", message="", type="start")
await websocket.send_json(start_resp.dict())
timeout_count = 0
while True:
try:
print(">>>> waiting for message")
data = await asyncio.wait_for(websocket.receive(), timeout=30)
websocket._raise_on_disconnect(data)
timeout_count = 0
question, transcribed = None, None
if "text" in data:
question = data["text"]
print('">>>> received: ', question)
if question == ">>>>INTERVIEW TIMESUP<<<<":
print(">>>> close here")
end_resp = ChatResponse(
sender="bot", message="<<<<INTERVIEW TIMESUP>>>>", type="end"
)
await websocket.send_json(end_resp.dict())
websocket_manager.disconnect(websocket)
break
elif "bytes" in data:
# in voice
print(">>>> received bytes")
# data = await websocket.receive_bytes()
# create a array buffer
temp = NamedTemporaryFile(suffix=".webm", delete=False)
with temp as audio:
audio.write(data["bytes"])
transcribed = await gd_client.transcribe_from_file(temp.name)
os.remove(temp.name)
input = transcribed if transcribed is not None else question
if input is not None:
result = await new_chat.arun(
input,
callbacks=[
StreamingLLMCallbackHandler(
websocket_manager, websocket, type=interview_type
)
],
)
end_resp = ChatResponse(sender="bot", message="", type="end")
await websocket.send_json(end_resp.dict())
except asyncio.TimeoutError:
timeout_count += 1
# timeout more than 4 times, max 2.5min
if timeout_count > 4:
websocket_manager.disconnect(websocket)
break
except WebSocketDisconnect:
logging.info("websocket disconnect")
websocket_manager.disconnect(websocket)
break
except Exception as e:
logging.error(e)
resp = ChatResponse(
sender="bot",
message="Sorry, something went wrong.",
type="error",
)
await websocket.send_json(resp.dict())
# TODO save chat history
print(new_chat.memory.buffer)
dicts = messages_to_dict(new_chat.memory.buffer)
print(">>>> save interview history")
if dicts is not None and len(dicts) > 0:
crud.add_interview_history(db_session, interview_id, dicts)
@app.post("/evaluate/{interview_id}")
async def evaluate_interview(
interview_id: str,
# cookie_or_token: Annotated[str, Depends(get_cookie_or_token)],
background_tasks: BackgroundTasks,
db_session: Session = Depends(get_db_session),
):
result = crud.get_interview_by_id(db_session, interview_id)
print(result)
if (
result is None
or "job" not in result
or "resume" not in result
or "interview" not in result
or result["interview"].result is not None
or result["interview"].history is None
or len(result["interview"].history) == 0
):
print(">>>> fail load interview")
raise HTTPException(status_code=404, detail="Invalid interview")
background_tasks.add_task(generate_report, interview_id, result, db_session)
background_tasks.add_task(generate_mock, interview_id, result, db_session)
return crud.add_pending_interview_result(db_session, interview_id)
@app.post("/add-history/{interview_id}")
def add_history(
interview_id: str,
# cookie_or_token: Annotated[str, Depends(get_cookie_or_token)],
db_session: Session = Depends(get_db_session),
):
history = ["GG"]
interview = crud.add_interview_history(db_session, interview_id, history)
print(">>>>> finish", interview)
# get the interview history
# llm for evaluation
# get the vectorstore of the jd and resume
# in redis first, if no -> create
# redis id => id the resume and the jd
return interview
# return {**interview, 'resume_resource': interview[1], 'job_content':interview[2]}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9000)