Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamableQuery parity #157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}

Loading