11from __future__ import annotations
22
33from collections .abc import AsyncIterator , Iterator
4+ from typing import Any
45
56from pawa_ai ._http import AsyncStream , Stream
6- from pawa_ai .models .chat import ChatCompletion , ChatStreamChunk
7+ from pawa_ai .models .chat import ChatStreamChunk
8+
9+
10+ def _collected_payload (text : str ) -> dict [str , Any ]:
11+ return {
12+ "success" : True ,
13+ "message" : "Stream collected" ,
14+ "data" : {
15+ "request" : [
16+ {
17+ "finish_reason" : "stop" ,
18+ "message" : {"role" : "assistant" , "content" : text },
19+ }
20+ ],
21+ "created" : "" ,
22+ "model" : "" ,
23+ "object" : "chat.request" ,
24+ },
25+ }
726
827
928class ChatCompletionStream :
@@ -26,8 +45,8 @@ def __exit__(self, *_: object) -> None:
2645
2746 def chunks (self ) -> Iterator [ChatStreamChunk ]:
2847 """Yield typed stream chunks."""
29- for raw in self ._stream :
30- yield ChatStreamChunk .from_dict (raw )
48+ for payload in self ._stream :
49+ yield ChatStreamChunk .from_dict (payload )
3150
3251 def text_deltas (self ) -> Iterator [str ]:
3352 """Yield only the text delta from each chunk."""
@@ -39,26 +58,9 @@ def collect_text(self) -> str:
3958 """Collect all text deltas into a single string."""
4059 return "" .join (self .text_deltas ())
4160
42- def collect (self ) -> ChatCompletion :
43- """Build a :class:`ChatCompletion` from the full streamed text."""
44- text = self .collect_text ()
45- return ChatCompletion .from_dict (
46- {
47- "success" : True ,
48- "message" : "Stream collected" ,
49- "data" : {
50- "request" : [
51- {
52- "finish_reason" : "stop" ,
53- "message" : {"role" : "assistant" , "content" : text },
54- }
55- ],
56- "created" : "" ,
57- "model" : "" ,
58- "object" : "chat.request" ,
59- },
60- }
61- )
61+ def collect (self ) -> dict [str , Any ]:
62+ """Build an API-shaped dict from the full streamed text."""
63+ return _collected_payload (self .collect_text ())
6264
6365 def close (self ) -> None :
6466 self ._stream .close ()
@@ -74,8 +76,8 @@ def __aiter__(self) -> AsyncIterator[ChatStreamChunk]:
7476 return self .chunks ()
7577
7678 async def chunks (self ) -> AsyncIterator [ChatStreamChunk ]:
77- async for raw in self ._stream :
78- yield ChatStreamChunk .from_dict (raw )
79+ async for payload in self ._stream :
80+ yield ChatStreamChunk .from_dict (payload )
7981
8082 async def text_deltas (self ) -> AsyncIterator [str ]:
8183 async for chunk in self .chunks ():
@@ -88,25 +90,8 @@ async def collect_text(self) -> str:
8890 parts .append (delta )
8991 return "" .join (parts )
9092
91- async def collect (self ) -> ChatCompletion :
92- text = await self .collect_text ()
93- return ChatCompletion .from_dict (
94- {
95- "success" : True ,
96- "message" : "Stream collected" ,
97- "data" : {
98- "request" : [
99- {
100- "finish_reason" : "stop" ,
101- "message" : {"role" : "assistant" , "content" : text },
102- }
103- ],
104- "created" : "" ,
105- "model" : "" ,
106- "object" : "chat.request" ,
107- },
108- }
109- )
93+ async def collect (self ) -> dict [str , Any ]:
94+ return _collected_payload (await self .collect_text ())
11095
11196 async def close (self ) -> None :
11297 await self ._stream .close ()
0 commit comments