MCPcopy Create free account
hub / github.com/pywebio/PyWebIO / ChatGPTStreamResponse

Class ChatGPTStreamResponse

demos/chatgpt.py:15–36  ·  view source on GitHub ↗

A wrapper to Stream[ChatCompletionChunk], add a `result()` method to get the final result.

Source from the content-addressed store, hash-verified

13
14
15class ChatGPTStreamResponse:
16 """
17 A wrapper to Stream[ChatCompletionChunk], add a `result()` method to get the final result.
18 """
19 def __init__(self, response: Stream[ChatCompletionChunk]):
20 self.response = response
21 self.yielded = []
22 self.finish_reason = None
23
24 def __next__(self):
25 chunk = next(self.response)
26 self.finish_reason = chunk.choices[0].finish_reason
27 delta = chunk.choices[0].delta
28 if delta.content:
29 self.yielded.append(delta.content)
30 return delta.content
31
32 def __iter__(self):
33 return self
34
35 def result(self):
36 return ''.join(self.yielded)
37
38
39class ChatGPT:

Callers 1

ask_streamMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…