docs: 添加代理设置说明并优化API请求代码结构

在README中为无法直接访问Google服务的用户添加代理设置说明
优化gemini-core.js中的API请求代码结构以提高可读性
This commit is contained in:
hex2077 2025-07-21 12:00:10 +08:00
parent e25c914e52
commit 1f24706f3f
3 changed files with 52 additions and 9 deletions

View file

@ -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 \

View file

@ -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 \

View file

@ -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();