-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
75 lines (62 loc) · 1.65 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
from typing import AsyncGenerator, NoReturn
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse
from openai import AsyncOpenAI
load_dotenv()
app = FastAPI()
client = AsyncOpenAI()
with open("index.html") as f:
html = f.read()
async def get_ai_response(message: str) -> AsyncGenerator[str, None]:
"""
OpenAI Response
"""
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": (
"You are a helpful assistant, skilled in explaining "
"complex concepts in simple terms."
),
},
{
"role": "user",
"content": message,
},
],
stream=True,
)
all_content = ""
async for chunk in response:
content = chunk.choices[0].delta.content
if content:
all_content += content
yield all_content
@app.get("/")
async def web_app() -> HTMLResponse:
"""
Web App
"""
return HTMLResponse(html)
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket) -> NoReturn:
"""
Websocket for AI responses
"""
await websocket.accept()
while True:
message = await websocket.receive_text()
async for text in get_ai_response(message):
await websocket.send_text(text)
if __name__ == "__main__":
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
log_level="debug",
reload=True,
)