From 1f24706f3f07349b832b17877bf5390ad74494e1 Mon Sep 17 00:00:00 2001 From: hex2077 Date: Mon, 21 Jul 2025 12:00:10 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=AF=B4=E6=98=8E=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?API=E8=AF=B7=E6=B1=82=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在README中为无法直接访问Google服务的用户添加代理设置说明 优化gemini-core.js中的API请求代码结构以提高可读性 --- README-EN.md | 18 ++++++++++++++++++ README.md | 20 +++++++++++++++++++- gemini-core.js | 23 +++++++++++++++-------- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/README-EN.md b/README-EN.md index e017e09..33b542c 100644 --- a/README-EN.md +++ b/README-EN.md @@ -129,6 +129,23 @@ This project consists of three core files, each with its own specific function: ``` #### 💻 Call the API (Default API Key: `123456`) +> **Hint**: If you are in an environment where you cannot directly access Google services, please set up a global HTTP/HTTPS proxy for your terminal first. +> +> - **Windows (CMD):** +> ```cmd +> set http_proxy=http://127.0.0.1:7890 +> set https_proxy=http://127.0.0.1:7890 +> ``` +> - **Windows (PowerShell):** +> ```powershell +> $env:http_proxy="http://127.0.0.1:7890" +> $env:https_proxy="http://127.0.0.1:7890" +> ``` +> - **macOS/Linux (Bash/Zsh):** +> ```bash +> export http_proxy=http://127.0.0.1:7890 +> export https_proxy=http://127.0.0.1:7890 +> ``` * **List Models** ```bash curl "http://localhost:3000/v1beta/models?key=123456" @@ -161,6 +178,7 @@ This project consists of three core files, each with its own specific function: ``` #### 💻 Call the API (as an OpenAI client) + * **List Models** ```bash curl http://localhost:8000/v1/models \ diff --git a/README.md b/README.md index 301e47a..b5f60f5 100644 --- a/README.md +++ b/README.md @@ -123,12 +123,29 @@ ```bash node gemini-api-server.js --oauth-creds-file "/path/to/your/oauth_creds.json" ``` -* **通过指定项目ID启动** (例如,用于多项目环境) +* **通过指定项目ID启动** (例如,用于多项目环境或必须指定项目ID的用户) ```bash node gemini-api-server.js --project-id your-gcp-project-id ``` #### 💻 调用 API (默认 API Key: `123456`) +> **提示**: 如果您在无法直接访问 Google 服务的环境中使用,请先为您的终端设置全局 HTTP/HTTPS 代理。 +> +> - **Windows (CMD):** +> ```cmd +> set http_proxy=http://127.0.0.1:7890 +> set https_proxy=http://127.0.0.1:7890 +> ``` +> - **Windows (PowerShell):** +> ```powershell +> $env:http_proxy="http://127.0.0.1:7890" +> $env:https_proxy="http://127.0.0.1:7890" +> ``` +> - **macOS/Linux (Bash/Zsh):** +> ```bash +> export http_proxy=http://127.0.0.1:7890 +> export https_proxy=http://127.0.0.1:7890 +> ``` * **列出模型** ```bash curl "http://localhost:3000/v1beta/models?key=123456" @@ -161,6 +178,7 @@ ``` #### 💻 调用 API (以 OpenAI 客户端方式) + * **列出模型** ```bash curl http://localhost:8000/v1/models \ diff --git a/gemini-core.js b/gemini-core.js index 2a82ba7..bddc159 100644 --- a/gemini-core.js +++ b/gemini-core.js @@ -294,11 +294,14 @@ export class GeminiApiService { async callApi(method, body, isRetry = false) { try { - const res = await this.authClient.request({ + const requestOptions = { url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:${method}`, - method: "POST", headers: { "Content-Type": "application/json" }, - responseType: "json", body: JSON.stringify(body), - }); + method: "POST", + headers: { "Content-Type": "application/json" }, + responseType: "json", + body: JSON.stringify(body), + }; + const res = await this.authClient.request(requestOptions); return res.data; } catch (error) { if (error.response?.status === 401 && !isRetry) { @@ -312,11 +315,15 @@ export class GeminiApiService { async * streamApi(method, body, isRetry = false) { try { - const res = await this.authClient.request({ + const requestOptions = { url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:${method}`, - method: "POST", params: { alt: "sse" }, headers: { "Content-Type": "application/json" }, - responseType: "stream", body: JSON.stringify(body), - }); + method: "POST", + params: { alt: "sse" }, + headers: { "Content-Type": "application/json" }, + responseType: "stream", + body: JSON.stringify(body), + }; + const res = await this.authClient.request(requestOptions); if (res.status !== 200) { let errorBody = ''; for await (const chunk of res.data) errorBody += chunk.toString();