mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-23 00:40:59 +00:00
Poxy OpenAI calls and remove OpenAI secrets from the app
This commit is contained in:
parent
da6c5ed76c
commit
52208ab20e
2 changed files with 2 additions and 24 deletions
|
@ -2,8 +2,6 @@
|
|||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>OPENAI_SECRET</key>
|
||||
<string>NICE_TRY</string>
|
||||
<key>DEEPL_SECRET</key>
|
||||
<string>NICE_TRY_AGAIN</string>
|
||||
</dict>
|
||||
|
|
|
@ -1,30 +1,11 @@
|
|||
import Foundation
|
||||
|
||||
protocol OpenAIRequest: Encodable {
|
||||
var path: String { get }
|
||||
var model: String { get }
|
||||
}
|
||||
|
||||
extension OpenAIRequest {
|
||||
var path: String {
|
||||
"chat/completions"
|
||||
}
|
||||
}
|
||||
|
||||
public struct OpenAIClient {
|
||||
private let endpoint: URL = .init(string: "https://api.openai.com/v1/")!
|
||||
|
||||
private var APIKey: String {
|
||||
if let path = Bundle.main.path(forResource: "Secret", ofType: "plist") {
|
||||
let secret = NSDictionary(contentsOfFile: path)
|
||||
return secret?["OPENAI_SECRET"] as? String ?? ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
private var authorizationHeaderValue: String {
|
||||
"Bearer \(APIKey)"
|
||||
}
|
||||
private let endpoint: URL = .init(string: "https://icecubesrelay.fly.dev/openai")!
|
||||
|
||||
private var encoder: JSONEncoder {
|
||||
let encoder = JSONEncoder()
|
||||
|
@ -130,9 +111,8 @@ public struct OpenAIClient {
|
|||
public func request(_ prompt: Prompt) async throws -> Response {
|
||||
do {
|
||||
let jsonData = try encoder.encode(prompt.request)
|
||||
var request = URLRequest(url: endpoint.appending(path: prompt.request.path))
|
||||
var request = URLRequest(url: endpoint)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue(authorizationHeaderValue, forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = jsonData
|
||||
let (result, _) = try await URLSession.shared.data(for: request)
|
||||
|
|
Loading…
Reference in a new issue