From 52208ab20e81d6b6679c190fe1a94b8b7e714519 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sat, 9 Dec 2023 10:58:42 +0100 Subject: [PATCH] Poxy OpenAI calls and remove OpenAI secrets from the app --- IceCubesApp/Secret.plist | 2 -- .../Sources/Network/OpenAIClient.swift | 24 ++----------------- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/IceCubesApp/Secret.plist b/IceCubesApp/Secret.plist index e05bb39c..0a3d9258 100644 --- a/IceCubesApp/Secret.plist +++ b/IceCubesApp/Secret.plist @@ -2,8 +2,6 @@ - OPENAI_SECRET - NICE_TRY DEEPL_SECRET NICE_TRY_AGAIN diff --git a/Packages/Network/Sources/Network/OpenAIClient.swift b/Packages/Network/Sources/Network/OpenAIClient.swift index 2b813ca1..d2b66d1a 100644 --- a/Packages/Network/Sources/Network/OpenAIClient.swift +++ b/Packages/Network/Sources/Network/OpenAIClient.swift @@ -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)