A wrapper to Stream[ChatCompletionChunk], add a `result()` method to get the final result.
| 13 | |
| 14 | |
| 15 | class 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 | |
| 39 | class ChatGPT: |
no outgoing calls
no test coverage detected
searching dependent graphs…