Skip to content

Commit

Permalink
Moved the request code out of the main connection implementation and …
Browse files Browse the repository at this point in the history
…into extensions.
  • Loading branch information
btfranklin committed Jul 28, 2023
1 parent 994e0f1 commit 3071a85
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
31 changes: 1 addition & 30 deletions Sources/CleverBird/OpenAIAPIConnection.swift
Expand Up @@ -5,14 +5,10 @@ import Get

public class OpenAIAPIConnection {

private let chatCompletionPath = "/v1/chat/completions"
private let embeddingsPath = "/v1/embeddings"

let apiKey: String
let organization: String?
let client: APIClient

private let requestHeaders: [String:String]
let requestHeaders: [String:String]

public init(apiKey: String, organization: String? = nil) {
self.apiKey = apiKey
Expand All @@ -38,29 +34,4 @@ public class OpenAIAPIConnection {
}
self.requestHeaders = requestHeaders
}

func createChatCompletionRequest(for body: Encodable) async throws -> Request<ChatCompletionResponse> {
Request<ChatCompletionResponse>(
path: self.chatCompletionPath,
method: .post,
body: body,
headers: self.requestHeaders)
}

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

let request = try await createChatCompletionRequest(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(message: "Expected response of type HTTPURLResponse, but received: \(response)")
}

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

return asyncByteStream
}
}
@@ -0,0 +1,15 @@
// Created by B.T. Franklin on 7/27/23

import Get

private let chatCompletionPath = "/v1/chat/completions"

extension OpenAIAPIConnection {
func createChatCompletionRequest(for body: Encodable) async throws -> Request<ChatCompletionResponse> {
Request<ChatCompletionResponse>(
path: chatCompletionPath,
method: .post,
body: body,
headers: self.requestHeaders)
}
}
@@ -0,0 +1,22 @@
// Created by B.T. Franklin on 7/27/23

import Foundation

extension OpenAIAPIConnection {
func createChatCompletionAsyncByteStream(for body: Encodable) async throws -> URLSession.AsyncBytes {

let request = try await createChatCompletionRequest(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(message: "Expected response of type HTTPURLResponse, but received: \(response)")
}

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

return asyncByteStream
}
}

0 comments on commit 3071a85

Please sign in to comment.