Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the request code out of the main connection implementation and …
…into extensions.
- Loading branch information
1 parent
994e0f1
commit 3071a85
Showing
3 changed files
with
38 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Sources/CleverBird/chat/OpenAIAPIConnection+createChatCompletionRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...s/CleverBird/chat/streaming/OpenAIAPIConnection+createChatCompletionAsyncByteStream.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |