pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
937 B
38 lines
937 B
import time
|
|
from typing import Annotated
|
|
|
|
from fastapi import Depends, FastAPI, HTTPException
|
|
from fastapi.responses import StreamingResponse
|
|
from sqlmodel import Field, Session, SQLModel, create_engine
|
|
|
|
engine = create_engine("postgresql+psycopg://postgres:postgres@localhost/db")
|
|
|
|
|
|
class User(SQLModel, table=True):
|
|
id: int | None = Field(default=None, primary_key=True)
|
|
name: str
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def get_session():
|
|
with Session(engine) as session:
|
|
yield session
|
|
|
|
|
|
def get_user(user_id: int, session: Annotated[Session, Depends(get_session)]):
|
|
user = session.get(User, user_id)
|
|
if not user:
|
|
raise HTTPException(status_code=403, detail="Not authorized")
|
|
|
|
|
|
def generate_stream(query: str):
|
|
for ch in query:
|
|
yield ch
|
|
time.sleep(0.1)
|
|
|
|
|
|
@app.get("/generate", dependencies=[Depends(get_user)])
|
|
def generate(query: str):
|
|
return StreamingResponse(content=generate_stream(query))
|
|
|