Skip to content

Commit 3e7c947

Browse files
committed
Merge branch 'feature/google-generative-ai-custom-baseurl' into develop
2 parents d891b79 + e02a1f5 commit 3e7c947

6 files changed

Lines changed: 37 additions & 10 deletions

File tree

Core/Sources/HostApp/AccountSettings/ChatModelManagement/ChatModelEditView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ struct ChatModelEditView: View {
328328

329329
@ViewBuilder
330330
var googleAI: some View {
331+
baseURLTextField(prompt: Text("https://generativelanguage.googleapis.com")) {
332+
Text("/v1")
333+
}
334+
331335
apiKeyNamePicker
332336

333337
WithViewStore(

Pro

Submodule Pro updated from 617fca9 to 173f79c

Tool/Package.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ let package = Package(
6565
.package(url: "https://github.com/apple/swift-syntax.git", exact: "509.0.2"),
6666
.package(url: "https://github.com/GottaGetSwifty/CodableWrappers", from: "2.0.7"),
6767
.package(url: "https://github.com/krzyzanowskim/STTextView", from: "0.8.21"),
68-
.package(url: "https://github.com/google/generative-ai-swift", from: "0.4.4"),
68+
// A fork of https://github.com/google/generative-ai-swift to support setting base url.
69+
.package(
70+
url: "https://github.com/intitni/generative-ai-swift",
71+
branch: "support-setting-base-url"
72+
),
6973
.package(url: "https://github.com/intitni/CopilotForXcodeKit", from: "0.4.0"),
7074

7175
// TreeSitter
@@ -90,7 +94,7 @@ let package = Package(
9094
.target(
9195
name: "CustomAsyncAlgorithms",
9296
dependencies: [
93-
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms")
97+
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
9498
]
9599
),
96100

Tool/Sources/AIModel/ChatModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public struct ChatModel: Codable, Equatable, Identifiable {
102102
return "\(baseURL)/openai/deployments/\(deployment)/chat/completions?api-version=\(version)"
103103
case .googleAI:
104104
let baseURL = info.baseURL
105-
if baseURL.isEmpty { return "https://generativelanguage.googleapis.com/v1" }
106-
return "\(baseURL)/v1/chat/completions"
105+
if baseURL.isEmpty { return "https://generativelanguage.googleapis.com" }
106+
return "\(baseURL)"
107107
case .ollama:
108108
let baseURL = info.baseURL
109109
if baseURL.isEmpty { return "http://localhost:11434/api/chat" }

Tool/Sources/OpenAIService/APIs/GoogleAIChatCompletionsService.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ actor GoogleAIChatCompletionsService: ChatCompletionsAPI, ChatCompletionsStreamA
88
let model: ChatModel
99
var requestBody: ChatCompletionsRequestBody
1010
let prompt: ChatGPTPrompt
11+
let baseURL: String
1112

1213
init(
1314
apiKey: String,
1415
model: ChatModel,
1516
requestBody: ChatCompletionsRequestBody,
16-
prompt: ChatGPTPrompt
17+
prompt: ChatGPTPrompt,
18+
baseURL: String
1719
) {
1820
self.apiKey = apiKey
1921
self.model = model
2022
self.requestBody = requestBody
2123
self.prompt = prompt
24+
self.baseURL = baseURL
2225
}
2326

2427
func callAsFunction() async throws -> ChatCompletionResponseBody {
@@ -27,7 +30,8 @@ actor GoogleAIChatCompletionsService: ChatCompletionsAPI, ChatCompletionsStreamA
2730
apiKey: apiKey,
2831
generationConfig: .init(GenerationConfig(
2932
temperature: requestBody.temperature.map(Float.init)
30-
))
33+
)),
34+
baseURL: baseURL
3135
)
3236
let history = prompt.googleAICompatible.history.map { message in
3337
ModelContent(message)
@@ -53,6 +57,12 @@ actor GoogleAIChatCompletionsService: ChatCompletionsAPI, ChatCompletionsStreamA
5357
throw error
5458
case .responseStoppedEarly:
5559
throw error
60+
case .promptImageContentError:
61+
throw error
62+
case let .invalidAPIKey(message: message):
63+
throw error
64+
case .unsupportedUserLocation:
65+
throw error
5666
}
5767
} catch {
5868
throw error
@@ -67,7 +77,8 @@ actor GoogleAIChatCompletionsService: ChatCompletionsAPI, ChatCompletionsStreamA
6777
apiKey: apiKey,
6878
generationConfig: .init(GenerationConfig(
6979
temperature: requestBody.temperature.map(Float.init)
70-
))
80+
)),
81+
baseURL: baseURL
7182
)
7283
let history = prompt.googleAICompatible.history.map { message in
7384
ModelContent(message)
@@ -100,6 +111,12 @@ actor GoogleAIChatCompletionsService: ChatCompletionsAPI, ChatCompletionsStreamA
100111
continuation.finish(throwing: error)
101112
case .responseStoppedEarly:
102113
continuation.finish(throwing: error)
114+
case let .promptImageContentError(underlying: underlying):
115+
continuation.finish(throwing: error)
116+
case let .invalidAPIKey(message: message):
117+
continuation.finish(throwing: error)
118+
case .unsupportedUserLocation:
119+
continuation.finish(throwing: error)
103120
}
104121
} catch {
105122
continuation.finish(throwing: error)

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public class ChatGPTService: ChatGPTServiceType {
9595
apiKey: apiKey,
9696
model: model,
9797
requestBody: requestBody,
98-
prompt: prompt
98+
prompt: prompt,
99+
baseURL: endpoint.absoluteString
99100
)
100101
case .openAI, .openAICompatible, .azureOpenAI:
101102
return OpenAIChatCompletionsService(
@@ -129,7 +130,8 @@ public class ChatGPTService: ChatGPTServiceType {
129130
apiKey: apiKey,
130131
model: model,
131132
requestBody: requestBody,
132-
prompt: prompt
133+
prompt: prompt,
134+
baseURL: endpoint.absoluteString
133135
)
134136
case .openAI, .openAICompatible, .azureOpenAI:
135137
return OpenAIChatCompletionsService(

0 commit comments

Comments
 (0)