assessment-artific/back-end/src/generators.py

33 lines
865 B
Python

import asyncio
from src.schema.response import Item, ResponseItem
import random
async def conversation_streamer():
for l in "Hello, nice to meet you!":
yield l
await asyncio.sleep(0.2)
async def json_streamer():
response_item: ResponseItem = ResponseItem(id=1, title="Test Title", items=[])
for i in range(9):
response_item.items.append(
Item(
title=f"Item {i}",
description=f"Description {i}",
type=random.choice(['A', 'B']),
)
)
text = response_item.model_dump_json()
index = 0
while True:
next_index = random.randint(1, 3) + index
yield text[index : min(len(text), next_index)]
index = next_index
if index >= len(text):
break
else:
await asyncio.sleep(0.2)