-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
28 lines (23 loc) · 968 Bytes
/
test.py
File metadata and controls
28 lines (23 loc) · 968 Bytes
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
# test.py
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from chainlit.utils import mount_chainlit
from chainlit.context import init_ws_context
from chainlit.session import WebsocketSession
import chainlit as cl
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello World from the main FastAPI app"}
@app.get("/hello/{session_id}")
async def hello(request: Request, session_id: str):
ws_session = WebsocketSession.get_by_id(session_id=session_id)
if not ws_session:
raise HTTPException(status_code=404, detail="Websocket session not found.")
init_ws_context(ws_session)
await cl.Message(content="Hello from custom endpoint!").send()
return {"message": "Data sent to websocket client"}
mount_chainlit(app=app, target="ws_test.py", path="/chainlit")
if __name__ == "__main__":
import uvicorn
uvicorn.run("test:app", host="0.0.0.0", port=5555, reload=True)