3.4 Handling Streaming Requests
In this section, we will create a new HTTP trigger that returns a streaming response based on the request body of the POST request.
Stream The Numbers
Create a new file named http_trigger_stream_numbers.py
in the awel_tutorial
directory
from dbgpt._private.pydantic import BaseModel, Field
from dbgpt.core.awel import DAG, HttpTrigger, StreamifyAbsOperator, setup_dev_environment
from typing import AsyncIterator
class TriggerReqBody(BaseModel):
n: int = Field(..., description="The number of integers to be streamed")
class NumberProducerOperator(StreamifyAbsOperator[TriggerReqBody, int]):
"""Create a stream of numbers from 0 to `n-1`"""
async def streamify(self, req: TriggerReqBody) -> AsyncIterator[int]:
for i in range(req.n):
yield str(i) + "\n"
with DAG("awel_stream_numbers") as dag:
trigger_task = HttpTrigger(
endpoint="/awel_tutorial/stream_numbers",
methods="POST",
request_body=TriggerReqBody,
status_code=200,
streaming_predict_func=lambda x: True
)
task = NumberProducerOperator()
trigger_task >> task
setup_dev_environment([dag], port=5555)