Skip to content

Commit 85b54b3

Browse files
author
dongqing
committed
support custom base url for gemini in google AI studio
1 parent 30c1c25 commit 85b54b3

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/api/providers/gemini.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ export class GeminiHandler implements ApiHandler, SingleCompletionHandler {
1717
}
1818

1919
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
20-
const model = this.client.getGenerativeModel({
21-
model: this.getModel().id,
22-
systemInstruction: systemPrompt,
23-
})
20+
const model = this.client.getGenerativeModel(
21+
{
22+
model: this.getModel().id,
23+
systemInstruction: systemPrompt,
24+
},
25+
{
26+
baseUrl: this.options.googleGeminiBaseUrl || undefined,
27+
},
28+
)
2429
const result = await model.generateContentStream({
2530
contents: messages.map(convertAnthropicMessageToGemini),
2631
generationConfig: {
@@ -55,9 +60,14 @@ export class GeminiHandler implements ApiHandler, SingleCompletionHandler {
5560

5661
async completePrompt(prompt: string): Promise<string> {
5762
try {
58-
const model = this.client.getGenerativeModel({
59-
model: this.getModel().id,
60-
})
63+
const model = this.client.getGenerativeModel(
64+
{
65+
model: this.getModel().id,
66+
},
67+
{
68+
baseUrl: this.options.googleGeminiBaseUrl || undefined,
69+
},
70+
)
6171

6272
const result = await model.generateContent({
6373
contents: [{ role: "user", parts: [{ text: prompt }] }],

src/core/webview/ClineProvider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type GlobalStateKey =
9696
| "openRouterModelInfo"
9797
| "openRouterBaseUrl"
9898
| "openRouterUseMiddleOutTransform"
99+
| "googleGeminiBaseUrl"
99100
| "allowedCommands"
100101
| "soundEnabled"
101102
| "soundVolume"
@@ -1657,6 +1658,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
16571658
anthropicBaseUrl,
16581659
anthropicThinking,
16591660
geminiApiKey,
1661+
googleGeminiBaseUrl,
16601662
openAiNativeApiKey,
16611663
deepSeekApiKey,
16621664
azureApiVersion,
@@ -1704,6 +1706,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
17041706
this.updateGlobalState("lmStudioBaseUrl", lmStudioBaseUrl),
17051707
this.updateGlobalState("anthropicBaseUrl", anthropicBaseUrl),
17061708
this.updateGlobalState("anthropicThinking", anthropicThinking),
1709+
this.updateGlobalState("googleGeminiBaseUrl", googleGeminiBaseUrl),
17071710
this.storeSecret("geminiApiKey", geminiApiKey),
17081711
this.storeSecret("openAiNativeApiKey", openAiNativeApiKey),
17091712
this.storeSecret("deepSeekApiKey", deepSeekApiKey),
@@ -2516,6 +2519,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
25162519
anthropicBaseUrl,
25172520
anthropicThinking,
25182521
geminiApiKey,
2522+
googleGeminiBaseUrl,
25192523
openAiNativeApiKey,
25202524
deepSeekApiKey,
25212525
mistralApiKey,
@@ -2598,6 +2602,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
25982602
this.getGlobalState("lmStudioBaseUrl") as Promise<string | undefined>,
25992603
this.getGlobalState("anthropicBaseUrl") as Promise<string | undefined>,
26002604
this.getGlobalState("anthropicThinking") as Promise<number | undefined>,
2605+
this.getGlobalState("googleGeminiBaseUrl") as Promise<string | undefined>,
26012606
this.getSecret("geminiApiKey") as Promise<string | undefined>,
26022607
this.getSecret("openAiNativeApiKey") as Promise<string | undefined>,
26032608
this.getSecret("deepSeekApiKey") as Promise<string | undefined>,
@@ -2699,6 +2704,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
26992704
anthropicBaseUrl,
27002705
anthropicThinking,
27012706
geminiApiKey,
2707+
googleGeminiBaseUrl,
27022708
openAiNativeApiKey,
27032709
deepSeekApiKey,
27042710
mistralApiKey,

src/shared/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface ApiHandlerOptions {
5151
lmStudioModelId?: string
5252
lmStudioBaseUrl?: string
5353
geminiApiKey?: string
54+
googleGeminiBaseUrl?: string
5455
openAiNativeApiKey?: string
5556
mistralApiKey?: string
5657
mistralCodestralUrl?: string // New option for Codestral URL

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const ApiOptions = ({
7171
const [anthropicThinkingBudget, setAnthropicThinkingBudget] = useState(apiConfiguration?.anthropicThinking)
7272
const [azureApiVersionSelected, setAzureApiVersionSelected] = useState(!!apiConfiguration?.azureApiVersion)
7373
const [openRouterBaseUrlSelected, setOpenRouterBaseUrlSelected] = useState(!!apiConfiguration?.openRouterBaseUrl)
74+
const [googleGeminiBaseUrlSelected, setGoogleGeminiBaseUrlSelected] = useState(
75+
!!apiConfiguration?.googleGeminiBaseUrl,
76+
)
7477
const [isDescriptionExpanded, setIsDescriptionExpanded] = useState(false)
7578

7679
const inputEventTransform = <E,>(event: E) => (event as { target: HTMLInputElement })?.target?.value as any
@@ -574,6 +577,30 @@ const ApiOptions = ({
574577
placeholder="Enter API Key...">
575578
<span style={{ fontWeight: 500 }}>Gemini API Key</span>
576579
</VSCodeTextField>
580+
<Checkbox
581+
checked={googleGeminiBaseUrlSelected}
582+
onChange={(checked: boolean) => {
583+
setGoogleGeminiBaseUrlSelected(checked)
584+
if (!checked) {
585+
handleInputChange("googleGeminiBaseUrl")({
586+
target: {
587+
value: "",
588+
},
589+
})
590+
}
591+
}}>
592+
Use custom base URL
593+
</Checkbox>
594+
595+
{googleGeminiBaseUrlSelected && (
596+
<VSCodeTextField
597+
value={apiConfiguration?.googleGeminiBaseUrl || ""}
598+
style={{ width: "100%", marginTop: 3 }}
599+
type="url"
600+
onInput={handleInputChange("googleGeminiBaseUrl")}
601+
placeholder="Default: https://generativelanguage.googleapis.com"
602+
/>
603+
)}
577604
<p
578605
style={{
579606
fontSize: "12px",

0 commit comments

Comments
 (0)