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.
19 lines
528 B
19 lines
528 B
from collections.abc import AsyncIterable
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.sse import EventSourceResponse, ServerSentEvent
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Prompt(BaseModel):
|
|
text: str
|
|
|
|
|
|
@app.post("/chat/stream", response_class=EventSourceResponse)
|
|
async def stream_chat(prompt: Prompt) -> AsyncIterable[ServerSentEvent]:
|
|
words = prompt.text.split()
|
|
for word in words:
|
|
yield ServerSentEvent(data=word, event="token")
|
|
yield ServerSentEvent(raw_data="[DONE]", event="done")
|
|
|