mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-04-23 10:39:45 -05:00
Feat: Streaming Docs + Examples (#1912)
* feat: streaming examples + gen * feat: more examples * chore: gen again * feat: fastapi example * chore: gen * feat: callout * fix: typos * fix: whoops - how did this work? Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from examples.streaming.worker import stream_task
|
||||
from hatchet_sdk import RunEventListener, StepRunEventType
|
||||
|
||||
# > FastAPI Proxy
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
async def generate_stream(stream: RunEventListener) -> AsyncGenerator[str, None]:
|
||||
async for chunk in stream:
|
||||
if chunk.type == StepRunEventType.STEP_RUN_EVENT_TYPE_STREAM:
|
||||
yield chunk.payload
|
||||
|
||||
|
||||
@app.get("/stream")
|
||||
async def stream() -> StreamingResponse:
|
||||
ref = await stream_task.aio_run_no_wait()
|
||||
|
||||
return StreamingResponse(generate_stream(ref.stream()), media_type="text/plain")
|
||||
|
||||
|
||||
# !!
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
Reference in New Issue
Block a user