@@ -278,34 +278,38 @@ actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI
278278 }
279279
280280 func callAsFunction( ) async throws -> ChatCompletionResponseBody {
281- requestBody. stream = false
282- var request = URLRequest ( url: endpoint)
283- request. httpMethod = " POST "
284- let encoder = JSONEncoder ( )
285- request. httpBody = try encoder. encode ( requestBody)
286- request. setValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
287-
288- Self . setupAppInformation ( & request)
289- Self . setupAPIKey ( & request, model: model, apiKey: apiKey)
290-
291- let ( result, response) = try await URLSession . shared. data ( for: request)
292- guard let response = response as? HTTPURLResponse else {
293- throw ChatGPTServiceError . responseInvalid
294- }
295-
296- guard response. statusCode == 200 else {
297- let error = try ? JSONDecoder ( ) . decode ( CompletionAPIError . self, from: result)
298- throw error ?? ChatGPTServiceError
299- . otherError ( String ( data: result, encoding: . utf8) ?? " Unknown Error " )
300- }
301-
302- do {
303- let body = try JSONDecoder ( ) . decode ( ResponseBody . self, from: result)
304- return body. formalized ( )
305- } catch {
306- dump ( error)
307- throw error
281+ let stream : AsyncThrowingStream < ChatCompletionsStreamDataChunk , Error > =
282+ try await callAsFunction ( )
283+
284+ var body = ChatCompletionResponseBody (
285+ id: nil ,
286+ object: " " ,
287+ model: " " ,
288+ message: . init( role: . assistant, content: " " ) ,
289+ otherChoices: [ ] ,
290+ finishReason: " "
291+ )
292+ for try await chunk in stream {
293+ if let id = chunk. id {
294+ body. id = id
295+ }
296+ if let finishReason = chunk. finishReason {
297+ body. finishReason = finishReason
298+ }
299+ if let model = chunk. model {
300+ body. model = model
301+ }
302+ if let object = chunk. object {
303+ body. object = object
304+ }
305+ if let role = chunk. message? . role {
306+ body. message. role = role
307+ }
308+ if let text = chunk. message? . content {
309+ body. message. content += text
310+ }
308311 }
312+ return body
309313 }
310314
311315 static func setupAppInformation( _ request: inout URLRequest ) {
0 commit comments