swift 接收URL请求时出现数据不完整错误

yftpprvb  于 2023-11-16  发布在  Swift
关注(0)|答案(1)|浏览(152)

我正在用Swift做一个简单的ChatGPT应用程序,它从流中的API请求数据。API返回正确的数据,但当我收到它时,单词和字符丢失。我试着调试,但我不确定为什么URL请求没有返回完整的数据。
我将在下面包括2个示例。第一个使用基本的HTTP请求来获取响应。这是我遇到的错误。我想保留这种方法,因为它使我能够取消任务。我不知道如何取消任务时,使用第二种方法。第二种方法使用Almofire库来请求数据。这种方法工作,所有数据都返回完整。现实地说,我想使用Almofire为我的主要方法(第一),因为它是更强大的,但我不知道如何与取消流中途的能力。我欣赏一些见解的方式,数据不返回全部。
第一种方法(bug)

func sendMessageStream(Question_To_Be_Asked: String) async throws -> AsyncThrowingStream<String, Error> {
    var urlRequest = self.urlRequest
    urlRequest.httpBody = try jsonBody(text: Question_To_Be_Asked)
    
    let (result, response) = try await urlSession.bytes(for: urlRequest)
    try Task.checkCancellation()
    
    guard let httpResponse = response as? HTTPURLResponse else {
        throw "Invalid response"
    }
    
    guard 200...299 ~= httpResponse.statusCode else {
        var errorText = ""
        for try await line in result.lines {
            try Task.checkCancellation()
            errorText += line
        }
        
        if let data = errorText.data(using: .utf8), let errorResponse = try? jsonDecoder.decode(ErrorRootResponse.self, from: data).error {
            errorText = "\n\(errorResponse.message)"
        }
        
        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 {
            //print(line) <- incomplete data
            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 nil
    }
}

字符串
第二种方法(工作)

func sendStreamMessage(messages: [Message]) -> DataStreamRequest{
    let openAIMessages = messages.map({OpenAIChatMessage(role: $0.role, content: $0.content)})
    let body = OpenAIChatBody(model: "gpt-4", messages: openAIMessages, stream: true)
    let headers: HTTPHeaders = [
        "Authorization": "Bearer \(Constants.openAIApiKey)"
    ]
    
    return AF.streamRequest(endpointUrl, method: .post, parameters: body, encoder: .json, headers: headers)
}

func sendMessage(question: String)  {
    let messages = [Message(id: UUID().uuidString, role: .user, content: question, createAt: Date())]
    currentInput = ""
    
    sendStreamMessage(messages: messages).responseStreamString { [weak self] stream in
        guard let self = self else { return }
        switch stream.event {
        case .stream(let response):
            switch response {
            case .success(let string):
                let streamResponse = self.parseStreamData(string)
                
                streamResponse.forEach { newMessageResponse in
                    guard let messageContent = newMessageResponse.choices.first?.delta.content else {
                        return
                    }
                    //here messageContent is final complete string from stream
                }
            case .failure(_):
                print("Something failes")
            }
            print(response)
        case .complete(_):
            print("COMPLETE")
        }
    }
}

func parseStreamData(_ data: String) ->[ChatStreamCompletionResponse] {
    let responseStrings = data.split(separator: "data:").map({$0.trimmingCharacters(in: .whitespacesAndNewlines)}).filter({!$0.isEmpty})
    let jsonDecoder = JSONDecoder()
    
    return responseStrings.compactMap { jsonString in
        guard let jsonData = jsonString.data(using: .utf8), let streamResponse = try? jsonDecoder.decode(ChatStreamCompletionResponse.self, from: jsonData) else {
            return nil
        }
        return streamResponse
    }
}

struct ChatStreamCompletionResponse: Decodable {
    let id: String
    let choices: [ChatStreamChoice]
}

struct ChatStreamChoice: Decodable {
    let delta: ChatStreamContent
}

struct ChatStreamContent: Decodable {
    let content: String
}

struct Message: Decodable, Hashable {
    let id: String
    let role: SenderRole
    let content: String
    let createAt: Date
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

struct OpenAIChatBody: Encodable {
    let model: String
    let messages: [OpenAIChatMessage]
    let stream: Bool
}

struct OpenAIChatMessage: Codable {
    let role: SenderRole
    let content: String
}

enum SenderRole: String, Codable {
    case system
    case user
    case assistant
}

sshcrbum

sshcrbum1#

第二种方法使用Alamofire库来请求数据。这种方法有效,所有数据都完整返回。实际上,我想使用Almofire作为我的主要方法(第一种),因为它更健壮,但我不知道如何使用中途取消流的能力
我同意Alamofire使用指南中关于“取消和恢复下载“的内容更多的是关于取消下载,而不是流。
然而,基本的cancel()方法可用于Alamofire中的所有请求类型,包括流请求。
您可以使用此方法取消正在进行的流请求。
修改sendStreamMessage函数以返回DataStreamRequest对象。
并实现一种方法来取消存储的DataStreamRequest

class ChatService {
    private var currentStreamRequest: DataStreamRequest?

    func sendStreamMessage(messages: [Message]) -> DataStreamRequest {
        // Existing code to set up and start the stream request

        let streamRequest = AF.streamRequest(endpointUrl, method: .post, parameters: body, encoder: .json, headers: headers)
        currentStreamRequest = streamRequest
        return streamRequest
    }

    func cancelStream() {
        currentStreamRequest?.cancel()
        currentStreamRequest = nil
    }

    // other methods
}

字符串
sendStreamMessage现在将DataStreamRequest存储在currentStreamRequest中。然后cancelStream方法取消此请求。
你可以这样使用它:

let chatService = ChatService()
// Start streaming
let streamRequest = chatService.sendStreamMessage(messages: messages)

// Cancel the stream when needed
chatService.cancelStream()


请注意,它不涉及恢复数据,因为它与文件下载更相关,其中部分数据可以保存并在以后恢复。在来自API的数据流的情况下,恢复通常不可行,除非API本身支持这种机制。

相关问题