Skip to content

Commit

Permalink
Added new "createAsyncByteStream" method to the OpenAIConnection, as …
Browse files Browse the repository at this point in the history
…well as a "stream" parameter to the Parameters type, setting the stage for future support of streaming responses.
  • Loading branch information
btfranklin committed May 6, 2023
1 parent 516bab8 commit 0b27339
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions Sources/CleverBird/OpenAIAPIConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ public class OpenAIAPIConnection {
body: body,
headers: self.requestHeaders)
}

func createAsyncByteStream(for body: Encodable) async throws -> URLSession.AsyncBytes {

let request = try await createRequest(for: body)
let urlRequest = try await client.makeURLRequest(for: request)
let (asyncByteStream, response) = try await client.session.bytes(for: urlRequest)

guard let response = response as? HTTPURLResponse else {
throw CleverBirdError.responseParsingFailed
}

guard (200...299).contains(response.statusCode) else {
throw CleverBirdError.requestFailed(message: "Response status code: \(response.statusCode)")
}

return asyncByteStream
}
}

3 changes: 3 additions & 0 deletions Sources/CleverBird/chat/ChatCompletionRequestParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public struct ChatCompletionRequestParameters: Codable {
public let model: Model
public let temperature: Percentage
public let topP: Percentage?
public let stream: Bool
public let stop: [String]?
public let presencePenalty: Penalty?
public let frequencyPenalty: Penalty?
Expand All @@ -13,6 +14,7 @@ public struct ChatCompletionRequestParameters: Codable {
public init(model: Model,
temperature: Percentage,
topP: Percentage? = nil,
stream: Bool = false,
stop: [String]? = nil,
presencePenalty: Penalty? = nil,
frequencyPenalty: Penalty? = nil,
Expand All @@ -21,6 +23,7 @@ public struct ChatCompletionRequestParameters: Codable {
self.model = model
self.temperature = temperature
self.topP = topP
self.stream = stream
self.stop = stop
self.presencePenalty = presencePenalty
self.frequencyPenalty = frequencyPenalty
Expand Down
2 changes: 0 additions & 2 deletions Sources/CleverBird/chat/ChatThread+complete.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Created by B.T. Franklin on 5/5/23

import Get

extension ChatThread {
public func complete() async -> ChatMessage? {

Expand Down

0 comments on commit 0b27339

Please sign in to comment.