Skip to content

Commit 5d7b472

Browse files
authored
Merge pull request #5 from xiaoti12/fix_base_url
fix: gemini base url未生效
2 parents 4d3ee24 + fbbf6a8 commit 5d7b472

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

services/llm/gemini/apiExecutor.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import { GoogleGenAI, GenerateContentResponse } from "@google/genai";
22
import { KeyManager } from '../../keyManager';
33

4-
/**
5-
* 将请求URL转换为代理URL(如果有配置)
6-
*/
7-
function getProxiedUrl(urlString: string, apiEndpoint?: string): string {
8-
if (!apiEndpoint?.trim()) return urlString;
9-
10-
try {
11-
const proxyUrl = new URL(apiEndpoint);
12-
if (urlString.includes('generativelanguage.googleapis.com')) {
13-
const originalUrl = new URL(urlString);
14-
const finalProxyUrl = new URL(proxyUrl);
15-
16-
const newPathname = (finalProxyUrl.pathname.replace(/\/$/, '') + originalUrl.pathname).replace(/\/\//g, '/');
17-
finalProxyUrl.pathname = newPathname;
18-
finalProxyUrl.search = originalUrl.search;
19-
20-
return finalProxyUrl.toString();
21-
}
22-
} catch (e) {
23-
console.error("提供的 API Base URL 无效:", apiEndpoint, e);
24-
}
25-
return urlString;
26-
}
27-
284
/**
295
* 打印400错误的详细分析
306
*/
@@ -81,7 +57,10 @@ export async function executeWithKeyRotation<T>(
8157
if (!key) continue;
8258

8359
try {
84-
const ai = new GoogleGenAI({ apiKey: key });
60+
const ai = new GoogleGenAI({
61+
apiKey: key,
62+
httpOptions: trimmedApiEndpoint ? { baseUrl: trimmedApiEndpoint } : undefined
63+
});
8564
const result = await operation(ai);
8665
keyManager.saveSuccessIndex();
8766
return result;
@@ -118,14 +97,19 @@ export async function* executeStreamWithKeyRotation<T extends GenerateContentRes
11897
return;
11998
}
12099

100+
const trimmedApiEndpoint = apiEndpoint?.trim();
101+
121102
let lastError: unknown = null;
122103
let success = false;
123104
for (let i = 0; i < keyManager.getTotalKeys(); i++) {
124105
const { key } = keyManager.getNextKey();
125106
if (!key) continue;
126107

127108
try {
128-
const ai = new GoogleGenAI({ apiKey: key });
109+
const ai = new GoogleGenAI({
110+
apiKey: key,
111+
httpOptions: trimmedApiEndpoint ? { baseUrl: trimmedApiEndpoint } : undefined
112+
});
129113
const stream = await operation(ai);
130114
keyManager.saveSuccessIndex();
131115
yield* stream;

0 commit comments

Comments
 (0)