@@ -10,15 +10,18 @@ import Foundation
10
10
open class OpenAIDirectService : OpenAIService , DirectService {
11
11
private let unprotectedAPIKey : String
12
12
private let requestFormat : OpenAIRequestFormat
13
+ private let baseURL : String
13
14
14
15
/// This initializer is not public on purpose.
15
16
/// Customers are expected to use the factory `AIProxy.directOpenAIService` defined in AIProxy.swift
16
17
internal init (
17
18
unprotectedAPIKey: String ,
18
- requestFormat: OpenAIRequestFormat = . standard
19
+ requestFormat: OpenAIRequestFormat = . standard,
20
+ baseURL: String ? = nil
19
21
) {
20
22
self . unprotectedAPIKey = unprotectedAPIKey
21
23
self . requestFormat = requestFormat
24
+ self . baseURL = baseURL ?? " https://api.openai.com "
22
25
}
23
26
24
27
/// Initiates a non-streaming chat completion request to /v1/chat/completions.
@@ -37,7 +40,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
37
40
body. stream = false
38
41
body. streamOptions = nil
39
42
var request = try AIProxyURLRequest . createDirect (
40
- baseURL: " https://api.openai.com " ,
43
+ baseURL: self . baseURL ,
41
44
path: self . resolvedPath ( " chat/completions " ) ,
42
45
body: try body. serialize ( ) ,
43
46
verb: . post,
@@ -66,7 +69,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
66
69
body. stream = true
67
70
body. streamOptions = . init( includeUsage: true )
68
71
var request = try AIProxyURLRequest . createDirect (
69
- baseURL: " https://api.openai.com " ,
72
+ baseURL: self . baseURL ,
70
73
path: self . resolvedPath ( " chat/completions " ) ,
71
74
body: try body. serialize ( ) ,
72
75
verb: . post,
@@ -90,7 +93,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
90
93
body: OpenAICreateImageRequestBody
91
94
) async throws -> OpenAICreateImageResponseBody {
92
95
let request = try AIProxyURLRequest . createDirect (
93
- baseURL: " https://api.openai.com " ,
96
+ baseURL: self . baseURL ,
94
97
path: self . resolvedPath ( " images/generations " ) ,
95
98
body: try body. serialize ( ) ,
96
99
verb: . post,
@@ -114,7 +117,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
114
117
) async throws -> OpenAICreateTranscriptionResponseBody {
115
118
let boundary = UUID ( ) . uuidString
116
119
let request = try AIProxyURLRequest . createDirect (
117
- baseURL: " https://api.openai.com " ,
120
+ baseURL: self . baseURL ,
118
121
path: self . resolvedPath ( " audio/transcriptions " ) ,
119
122
body: formEncode ( body, boundary) ,
120
123
verb: . post,
@@ -148,7 +151,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
148
151
body: OpenAITextToSpeechRequestBody
149
152
) async throws -> Data {
150
153
let request = try AIProxyURLRequest . createDirect (
151
- baseURL: " https://api.openai.com " ,
154
+ baseURL: self . baseURL ,
152
155
path: self . resolvedPath ( " audio/speech " ) ,
153
156
body: try body. serialize ( ) ,
154
157
verb: . post,
@@ -175,7 +178,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
175
178
body: OpenAIModerationRequestBody
176
179
) async throws -> OpenAIModerationResponseBody {
177
180
let request = try AIProxyURLRequest . createDirect (
178
- baseURL: " https://api.openai.com " ,
181
+ baseURL: self . baseURL ,
179
182
path: self . resolvedPath ( " moderations " ) ,
180
183
body: try body. serialize ( ) ,
181
184
verb: . post,
@@ -198,7 +201,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
198
201
body: OpenAIEmbeddingRequestBody
199
202
) async throws -> OpenAIEmbeddingResponseBody {
200
203
let request = try AIProxyURLRequest . createDirect (
201
- baseURL: " https://api.openai.com " ,
204
+ baseURL: self . baseURL ,
202
205
path: self . resolvedPath ( " embeddings " ) ,
203
206
body: try body. serialize ( ) ,
204
207
verb: . post,
@@ -228,7 +231,7 @@ open class OpenAIDirectService: OpenAIService, DirectService {
228
231
) async throws -> OpenAIRealtimeSession {
229
232
aiproxyCallerDesiredLogLevel = logLevel
230
233
let request = try AIProxyURLRequest . createDirect (
231
- baseURL: " https://api.openai.com " ,
234
+ baseURL: self . baseURL ,
232
235
path: " /v1/realtime?model= \( model) " ,
233
236
body: nil ,
234
237
verb: . get,
0 commit comments