docs: 添加代理设置说明并优化API请求代码结构
在README中为无法直接访问Google服务的用户添加代理设置说明 优化gemini-core.js中的API请求代码结构以提高可读性
This commit is contained in:
parent
e25c914e52
commit
1f24706f3f
3 changed files with 52 additions and 9 deletions
18
README-EN.md
18
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`)
|
#### 💻 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**
|
* **List Models**
|
||||||
```bash
|
```bash
|
||||||
curl "http://localhost:3000/v1beta/models?key=123456"
|
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)
|
#### 💻 Call the API (as an OpenAI client)
|
||||||
|
|
||||||
* **List Models**
|
* **List Models**
|
||||||
```bash
|
```bash
|
||||||
curl http://localhost:8000/v1/models \
|
curl http://localhost:8000/v1/models \
|
||||||
|
|
|
||||||
20
README.md
20
README.md
|
|
@ -123,12 +123,29 @@
|
||||||
```bash
|
```bash
|
||||||
node gemini-api-server.js --oauth-creds-file "/path/to/your/oauth_creds.json"
|
node gemini-api-server.js --oauth-creds-file "/path/to/your/oauth_creds.json"
|
||||||
```
|
```
|
||||||
* **通过指定项目ID启动** (例如,用于多项目环境)
|
* **通过指定项目ID启动** (例如,用于多项目环境或必须指定项目ID的用户)
|
||||||
```bash
|
```bash
|
||||||
node gemini-api-server.js --project-id your-gcp-project-id
|
node gemini-api-server.js --project-id your-gcp-project-id
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 💻 调用 API (默认 API Key: `123456`)
|
#### 💻 调用 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
|
```bash
|
||||||
curl "http://localhost:3000/v1beta/models?key=123456"
|
curl "http://localhost:3000/v1beta/models?key=123456"
|
||||||
|
|
@ -161,6 +178,7 @@
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 💻 调用 API (以 OpenAI 客户端方式)
|
#### 💻 调用 API (以 OpenAI 客户端方式)
|
||||||
|
|
||||||
* **列出模型**
|
* **列出模型**
|
||||||
```bash
|
```bash
|
||||||
curl http://localhost:8000/v1/models \
|
curl http://localhost:8000/v1/models \
|
||||||
|
|
|
||||||
|
|
@ -294,11 +294,14 @@ export class GeminiApiService {
|
||||||
|
|
||||||
async callApi(method, body, isRetry = false) {
|
async callApi(method, body, isRetry = false) {
|
||||||
try {
|
try {
|
||||||
const res = await this.authClient.request({
|
const requestOptions = {
|
||||||
url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:${method}`,
|
url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:${method}`,
|
||||||
method: "POST", headers: { "Content-Type": "application/json" },
|
method: "POST",
|
||||||
responseType: "json", body: JSON.stringify(body),
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
responseType: "json",
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
};
|
||||||
|
const res = await this.authClient.request(requestOptions);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response?.status === 401 && !isRetry) {
|
if (error.response?.status === 401 && !isRetry) {
|
||||||
|
|
@ -312,11 +315,15 @@ export class GeminiApiService {
|
||||||
|
|
||||||
async * streamApi(method, body, isRetry = false) {
|
async * streamApi(method, body, isRetry = false) {
|
||||||
try {
|
try {
|
||||||
const res = await this.authClient.request({
|
const requestOptions = {
|
||||||
url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:${method}`,
|
url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:${method}`,
|
||||||
method: "POST", params: { alt: "sse" }, headers: { "Content-Type": "application/json" },
|
method: "POST",
|
||||||
responseType: "stream", body: JSON.stringify(body),
|
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) {
|
if (res.status !== 200) {
|
||||||
let errorBody = '';
|
let errorBody = '';
|
||||||
for await (const chunk of res.data) errorBody += chunk.toString();
|
for await (const chunk of res.data) errorBody += chunk.toString();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue