Skip to content

Commit

Permalink
Revert injection of HTTPClient (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanshine committed Apr 7, 2023
1 parent 127b5a9 commit a25c282
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,24 @@ var organization: String {

...

let configuration = Configuration(apiKey: apiKey, organization: organization)

let openAIClient = OpenAIKit.Client(configuration: configuration)
~~~~
// Generally we would advise on creating a single HTTPClient for the lifecycle of your application and recommend shutting it down on application close.

By default, a `HTTPClient` will be internally managed by the framework. For more granular control, you may also instantiate the `OpenAIKit.Client` by passing in your own `HTTPClient`.
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

~~~~swift
...
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))

let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
defer {
// it's important to shutdown the httpClient after all requests are done, even if one failed. See: https://github.com/swift-server/async-http-client
try? httpClient.syncShutdown()
}

let configuration = Configuration(apiKey: apiKey, organization: organization)

let openAIClient = OpenAIKit.Client(httpClient: httpClient, configuration: configuration)

~~~~


## Using the API

The OpenAIKit.Client implements a handful of methods to interact with the OpenAI API:
Expand Down
25 changes: 4 additions & 21 deletions Sources/OpenAIKit/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIO
import NIOHTTP1
import Foundation

public final class Client {
public struct Client {

public let audio: AudioProvider
public let chats: ChatProvider
Expand All @@ -15,20 +15,13 @@ public final class Client {
public let models: ModelProvider
public let moderations: ModerationProvider

// Hold onto reference of internally created HTTPClient to perform appropriate shutdowns.
private let _httpClient: HTTPClient?

public init(
// If an HTTPClient is not provided, an internal one will be created, and will be shutdown after the lifecycle of the Client
httpClient: HTTPClient? = nil,
httpClient: HTTPClient,
configuration: Configuration
) {

// If an httpClient is provided, don't hold reference to it.
self._httpClient = httpClient == nil ? HTTPClient(eventLoopGroupProvider: .createNew) : nil


let requestHandler = RequestHandler(
httpClient: httpClient ?? _httpClient!,
httpClient: httpClient,
configuration: configuration
)

Expand All @@ -43,15 +36,5 @@ public final class Client {
self.moderations = ModerationProvider(requestHandler: requestHandler)

}

deinit {
/**
syncShutdown() must not be called when on an EventLoop.
Calling syncShutdown() on any EventLoop can lead to deadlocks.
*/
guard MultiThreadedEventLoopGroup.currentEventLoop == nil else { return }

try? _httpClient?.syncShutdown()
}

}
17 changes: 5 additions & 12 deletions Tests/OpenAIKitTests/OpenAIKitTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XCTest
import NIOPosix
import AsyncHTTPClient
@testable import OpenAIKit

Expand All @@ -8,7 +9,10 @@ final class OpenAIKitTests: XCTestCase {
private var client: Client!

override func setUp() {
httpClient = HTTPClient(eventLoopGroupProvider: .createNew)

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))

let configuration = Configuration(apiKey: "YOUR-API-KEY")

Expand All @@ -31,17 +35,6 @@ final class OpenAIKitTests: XCTestCase {

}

func test_usingInternalHTTPClient() async throws {

let client = Client(
configuration: .init(apiKey: "YOUR-API-KEY")
)

let models = try await client.models.list()
print(models)

}

func test_listModels() async throws {
let models = try await client.models.list()
print(models)
Expand Down

0 comments on commit a25c282

Please sign in to comment.