-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from bluepi0j/AddChatStreamSupport
Add support for chat stream
- Loading branch information
Showing
4 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// ServerSentEventsHandler.swift | ||
// | ||
// | ||
// Created by Vic on 2023-03-25. | ||
// | ||
|
||
import Foundation | ||
|
||
class ServerSentEventsHandler: NSObject { | ||
|
||
var onEventReceived: ((Result<OpenAI<StreamMessageResult>, OpenAIError>) -> Void)? | ||
var onComplete: (() -> Void)? | ||
|
||
private lazy var session: URLSession = URLSession(configuration: .default, delegate: self, delegateQueue: nil) | ||
private var task: URLSessionDataTask? | ||
|
||
func connect(with request: URLRequest) { | ||
task = session.dataTask(with: request) | ||
task?.resume() | ||
} | ||
|
||
func disconnect() { | ||
task?.cancel() | ||
} | ||
|
||
func processEvent(_ eventData: Data) { | ||
do { | ||
let res = try JSONDecoder().decode(OpenAI<StreamMessageResult>.self, from: eventData) | ||
onEventReceived?(.success(res)) | ||
} catch { | ||
onEventReceived?(.failure(.decodingError(error: error))) | ||
} | ||
} | ||
} | ||
|
||
extension ServerSentEventsHandler: URLSessionDataDelegate { | ||
|
||
/// It will be called several times, each time could return one chunk of data or multiple chunk of data | ||
/// The JSON look liks this: | ||
/// `data: {"id":"chatcmpl-6yVTvD6UAXsE9uG2SmW4Tc2iuFnnT","object":"chat.completion.chunk","created":1679878715,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}` | ||
/// `data: {"id":"chatcmpl-6yVTvD6UAXsE9uG2SmW4Tc2iuFnnT","object":"chat.completion.chunk","created":1679878715,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"Once"},"index":0,"finish_reason":null}]}` | ||
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | ||
if let eventString = String(data: data, encoding: .utf8) { | ||
let lines = eventString.split(separator: "\n") | ||
for line in lines { | ||
if line.hasPrefix("data:") && line != "data: [DONE]" { | ||
if let eventData = String(line.dropFirst(5)).data(using: .utf8) { | ||
processEvent(eventData) | ||
} else { | ||
disconnect() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
if let error = error { | ||
onEventReceived?(.failure(.genericError(error: error))) | ||
} else { | ||
onComplete?() | ||
} | ||
} | ||
} |