Skip to content

Commit

Permalink
make StreamableQuery symmetrically convert ChatQuery objects to non-s…
Browse files Browse the repository at this point in the history
…treamable mode on the fly
  • Loading branch information
James J Kalafus committed Feb 4, 2024
1 parent 27ad53b commit 04ff962
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final public class OpenAI: OpenAIProtocol {
}

public func completions(query: CompletionsQuery, completion: @escaping (Result<CompletionsResult, Error>) -> Void) {
performRequest(request: JSONRequest<CompletionsResult>(body: query, url: buildURL(path: .completions)), completion: completion)
performRequest(request: JSONRequest<CompletionsResult>(body: query.makeNonStreamable(), url: buildURL(path: .completions)), completion: completion)
}

public func completionsStream(query: CompletionsQuery, onResult: @escaping (Result<CompletionsResult, Error>) -> Void, completion: ((Error?) -> Void)?) {
Expand All @@ -81,7 +81,7 @@ final public class OpenAI: OpenAIProtocol {
}

public func chats(query: ChatQuery, completion: @escaping (Result<ChatResult, Error>) -> Void) {
performRequest(request: JSONRequest<ChatResult>(body: query, url: buildURL(path: .chats)), completion: completion)
performRequest(request: JSONRequest<ChatResult>(body: query.makeNonStreamable(), url: buildURL(path: .chats)), completion: completion)
}

public func chatsStream(query: ChatQuery, onResult: @escaping (Result<ChatStreamResult, Error>) -> Void, completion: ((Error?) -> Void)?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
//
// File.swift
//
// Streamable.swift
//
//
// Created by Sergii Kryvoblotskyi on 15/05/2023.
//

import Foundation

protocol Streamable {

var stream: Bool { get set }
func makeStreamable() -> Self
func makeNonStreamable() -> Self
}

extension Streamable {

func makeStreamable() -> Self {
guard !stream else {
return self
}
var copy = self
copy.stream = true
return copy
}

func makeNonStreamable() -> Self {
guard stream else {
return self
}
var copy = self
copy.stream = false
return copy
}
}

0 comments on commit 04ff962

Please sign in to comment.