| 48 | |
| 49 | |
| 50 | async def fetch_streaming( |
| 51 | session: aiohttp.ClientSession, |
| 52 | url: str, |
| 53 | params: dict[str, Any], |
| 54 | payload: dict[str, Any] | None, |
| 55 | ) -> list[dict[str, Any]]: |
| 56 | messages: list[dict[str, Any]] = [] |
| 57 | |
| 58 | request_kwargs: dict[str, Any] = {"params": params} |
| 59 | if payload is not None: |
| 60 | request_kwargs["json"] = payload |
| 61 | |
| 62 | http_method = session.get if payload is None else session.post |
| 63 | |
| 64 | async with http_method(url, **request_kwargs) as response: |
| 65 | response.raise_for_status() |
| 66 | async for raw_line in response.content: |
| 67 | line = raw_line.decode("utf-8", errors="ignore").strip() |
| 68 | if not line or line.startswith(":") or not line.startswith("data:"): |
| 69 | continue |
| 70 | data_str = line[5:].strip() |
| 71 | if data_str == "[DONE]": |
| 72 | break |
| 73 | try: |
| 74 | message = json.loads(data_str) |
| 75 | messages.append(message) |
| 76 | except json.JSONDecodeError: |
| 77 | continue |
| 78 | return messages |
| 79 | |
| 80 | |
| 81 | async def fetch_non_streaming( |