SwiftBeta / SwiftOpenAI

OpenAI API build with Swift ❤️
https://www.swiftbeta.com
MIT License
198 stars 18 forks source link

Streaming requests end with DecodingError #4

Closed randomsnowflake closed 1 year ago

randomsnowflake commented 1 year ago

Sample code (adapted from the example given):

var openAI = SwiftOpenAI(apiKey: "sk-...")

for try await newMessage in try! await openAI.createChatCompletionsStream(model: .gpt3_5(.turbo),
                                                                          messages: [.init(text: "Hello, how are you?", role: "user")],
                                                                          optionalParameters: .init(stream: true))
{
    dump(newMessage)
    let newToken = newMessage.choices.first?.delta?.content
    print("New Message Received: \(newToken) ")
}

While the code works and does stream all responses properly, it always fails with:

Swift/ErrorType.swift:200: Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 1." UserInfo={NSDebugDescription=Invalid value around line 1, column 1., NSJSONSerializationErrorIndex=1})))

Wrapping the calls in do {.. } catch DecodingError.dataCorrupted(let context) works fine however, but should be handled by your framework.

SwiftBeta commented 1 year ago

Thank you @randomsnowflake! It should work now, as I have been introducing some updates to avoid the issues that you are facing (more context). Now that I have better control over what is happening, this error should have disappeared with the new release 0.4.0

{"id":"chatcmpl-6zpBNtBaNTSRAen7AUt7bk1N0uDt8","object":"chat.completion.chunk","created":1680192773,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"?"},"index":0,"finish_reason":null}]}
{"id":"chatcmpl-6zpBNtBaNTSRAen7AUt7bk1N0uDt8","object":"chat.completion.chunk","created":1680192773,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}
[DONE]
Finished streaming messages

Please, if you find any other issues, I will be happy to fix them :)