Skip to content

Commit

Permalink
LLMOpenAI: filter out "[DONE]" event in streamed responses
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhdk committed Sep 13, 2024
1 parent 83799e6 commit 39ae836
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Sources/SpeziLLMOpenAI/LLMOpenAISession+Generation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ extension LLMOpenAISession {
let response = try await chatGPTClient.createChatCompletion(openAIChatQuery)

// FIXME: does not handle the "[DONE]" message
let chatStream = try response.ok.body.text_event_hyphen_stream
// let chatStream = try response.ok.body.text_event_hyphen_stream
// .asDecodedServerSentEventsWithJSONData(of: Components.Schemas.CreateChatCompletionStreamResponse
// .self)
let chatStream = try await response.ok.body.text_event_hyphen_stream.asDecodedServerSentEvents()
.filter { $0.data != "[DONE]" }
.asEncodedServerSentEvents()
.asDecodedServerSentEventsWithJSONData(of: Components.Schemas.CreateChatCompletionStreamResponse
.self)

Expand Down Expand Up @@ -99,8 +104,6 @@ extension LLMOpenAISession {
Self.logger.error("SpeziLLMOpenAI: Connectivity Issues with the OpenAI API: \(error)")
await finishGenerationWithError(LLMOpenAIError.connectivityIssues(error), on: continuation)
return
} catch let Swift.DecodingError.dataCorrupted(context) {
// FIXME: The API sends a "[DONE]" message to conclude the stream. As it does not conform to the Components.Schemas.CreateChatCompletionStreamResponse schema, it will cause a crash. This is a hacky workaround to avoid the crash.
} catch {
Self.logger.error("SpeziLLMOpenAI: Generation error occurred - \(error)")
await finishGenerationWithError(LLMOpenAIError.generationError, on: continuation)
Expand Down

0 comments on commit 39ae836

Please sign in to comment.