#How to stream using Swift with ChatGPT API

3 messages · Page 1 of 1 (latest)

rustic linden
#

Currently using "https://github.com/MacPaw/OpenAI/blob/main/README.md#chats"

Example Implementation:

 let query = ChatQuery(model: .gpt3_5Turbo, messages: [
      .init(role: .user, content: promptStatement),
    ], stream: true)
    let result = try await openAI.chats(query: query)
    return result.choices.first?.message.content ?? "ERROR OCCURRED"
  }

Experience:

  • I will occasionally get a response in as a completed response but typically receive an error response:
    Error: 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 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0})))

Question:

  • Is stream supported for iOS app development. It appears it is from the documentation of this api but so far after many hours can't get it to do as i expect, the submission of a prompt will at most respond after 30seconds to a minute the complete response at best. I can never get a stream of tokens responded
GitHub

Swift package for OpenAI public API. Contribute to MacPaw/OpenAI development by creating an account on GitHub.

final moth
#

but there are alternatives listed in the issue