mirror of
https://github.com/fastapi/fastapi.git
synced 2026-03-01 13:19:09 -05:00
20 lines
528 B
Python
20 lines
528 B
Python
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")
|