From 748b130a82fee761cc1a4c12f9062f13fac80f60 Mon Sep 17 00:00:00 2001 From: snow Date: Sat, 15 Jul 2023 18:45:51 +0800 Subject: [PATCH] Fix the problem of missing messages when streaming messages --- Shared/ChatGPTAPI.swift | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Shared/ChatGPTAPI.swift b/Shared/ChatGPTAPI.swift index ddbfbcf..1436283 100644 --- a/Shared/ChatGPTAPI.swift +++ b/Shared/ChatGPTAPI.swift @@ -98,22 +98,23 @@ class ChatGPTAPI: LLMClient, @unchecked Sendable { throw "Bad Response: \(httpResponse.statusCode), \(errorText)" } - - var responseText = "" - return AsyncThrowingStream { [weak self] in - guard let self else { return nil } - for try await line in result.lines { - try Task.checkCancellation() - if line.hasPrefix("data: "), - let data = line.dropFirst(6).data(using: .utf8), - let response = try? self.jsonDecoder.decode(StreamCompletionResponse.self, from: data), - let text = response.choices.first?.delta.content { - responseText += text - return text + + return AsyncThrowingStream { continuation in + Task { + var responseText = "" + for try await line in result.lines { + try Task.checkCancellation() + if line.hasPrefix("data: "), + let data = line.dropFirst(6).data(using: .utf8), + let response = try? self.jsonDecoder.decode(StreamCompletionResponse.self, from: data), + let text = response.choices.first?.delta.content { + responseText += text + continuation.yield(text) + } } + self.appendToHistoryList(userText: text, responseText: responseText) + continuation.finish() } - self.appendToHistoryList(userText: text, responseText: responseText) - return nil } }