File size: 14,914 Bytes
24926a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | # 客户端集成示例
本文档提供各种编程语言和客户端工具集成 AI Studio Proxy API 的示例代码。
---
## 📋 目录
- [cURL 命令行](#curl-命令行)
- [Python](#python)
- [JavaScript / Node.js](#javascript--nodejs)
- [客户端工具](#客户端工具)
---
## cURL 命令行
### 健康检查
```bash
curl http://127.0.0.1:2048/health
```
### 获取模型列表
```bash
curl http://127.0.0.1:2048/v1/models
```
### 非流式聊天请求
```bash
curl -X POST http://127.0.0.1:2048/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "gemini-2.5-pro",
"messages": [
{
"role": "user",
"content": "你好,请介绍一下自己"
}
],
"stream": false,
"temperature": 0.7,
"max_output_tokens": 2048
}'
```
### 流式聊天请求 (SSE)
```bash
curl -X POST http://127.0.0.1:2048/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "gemini-2.5-flash",
"messages": [
{
"role": "user",
"content": "请讲一个关于人工智能的故事"
}
],
"stream": true
}' --no-buffer
```
### 带参数的请求
```bash
curl -X POST http://127.0.0.1:2048/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-2.5-pro",
"messages": [
{
"role": "system",
"content": "你是一位专业的 Python 开发者"
},
{
"role": "user",
"content": "如何使用 asyncio 实现并发?"
}
],
"stream": false,
"temperature": 0.5,
"max_output_tokens": 4096,
"top_p": 0.9,
"stop": ["\n\nUser:", "\n\nAssistant:"]
}'
```
---
## Python
### 使用 OpenAI SDK
#### 安装
```bash
pip install openai
```
#### 基本用法
```python
from openai import OpenAI
# 初始化客户端
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-api-key" # 如果服务器不需要认证,可以是任意值
)
# 非流式请求
def basic_chat():
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[
{"role": "system", "content": "你是一个有用的助手"},
{"role": "user", "content": "什么是 FastAPI?"}
]
)
print(response.choices[0].message.content)
basic_chat()
```
#### 流式响应
```python
from openai import OpenAI
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-api-key"
)
def streaming_chat():
stream = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[
{"role": "user", "content": "请讲一个关于机器学习的故事"}
],
stream=True
)
print("AI: ", end="", flush=True)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print() # 换行
streaming_chat()
```
#### 带参数的请求
```python
from openai import OpenAI
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-api-key"
)
def advanced_chat():
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[
{"role": "system", "content": "你是一个 Python 专家"},
{"role": "user", "content": "解释装饰器的工作原理"}
],
temperature=0.7,
max_tokens=2048,
top_p=0.9,
stop=["\n\n用户:", "\n\n助手:"]
)
print(response.choices[0].message.content)
print(f"\n使用的 tokens: {response.usage.total_tokens}")
advanced_chat()
```
#### 错误处理
```python
from openai import OpenAI, APIError, APIConnectionError
import time
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-api-key",
timeout=60.0
)
def chat_with_retry(messages, max_retries=3):
"""带重试机制的聊天"""
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=messages
)
return response.choices[0].message.content
except APIConnectionError as e:
print(f"连接错误 (尝试 {attempt + 1}/{max_retries}): {e}")
if attempt < max_retries - 1:
time.sleep(2 ** attempt) # 指数退避
continue
raise
except APIError as e:
print(f"API 错误: {e}")
raise
# 使用示例
try:
result = chat_with_retry([
{"role": "user", "content": "你好"}
])
print(result)
except Exception as e:
print(f"请求失败: {e}")
```
### 使用 requests 库
#### 安装
```bash
pip install requests
```
#### 非流式请求
```python
import requests
import json
def chat_non_streaming():
url = "http://127.0.0.1:2048/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer your-api-key"
}
data = {
"model": "gemini-2.5-pro",
"messages": [
{"role": "user", "content": "什么是深度学习?"}
],
"stream": False
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
print(result['choices'][0]['message']['content'])
else:
print(f"错误 {response.status_code}: {response.text}")
chat_non_streaming()
```
#### 流式请求 (SSE)
```python
import requests
import json
def chat_streaming():
url = "http://127.0.0.1:2048/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer your-api-key"
}
data = {
"model": "gemini-2.5-pro",
"messages": [
{"role": "user", "content": "请讲一个故事"}
],
"stream": True
}
response = requests.post(url, headers=headers, json=data, stream=True)
print("AI: ", end="", flush=True)
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_str = line[6:] # 移除 'data: ' 前缀
if data_str.strip() == '[DONE]':
print("\n")
break
try:
chunk = json.loads(data_str)
if 'choices' in chunk:
delta = chunk['choices'][0].get('delta', {})
content = delta.get('content', '')
if content:
print(content, end="", flush=True)
except json.JSONDecodeError:
continue
chat_streaming()
```
---
## JavaScript / Node.js
### 使用 OpenAI SDK
#### 安装
```bash
npm install openai
```
#### 基本用法
```javascript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'http://127.0.0.1:2048/v1',
apiKey: 'your-api-key',
});
// 非流式请求
async function basicChat() {
const response = await client.chat.completions.create({
model: 'gemini-2.5-pro',
messages: [
{ role: 'system', content: '你是一个有用的助手' },
{ role: 'user', content: '什么是 Node.js?' }
],
});
console.log(response.choices[0].message.content);
}
basicChat();
```
#### 流式响应
```javascript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'http://127.0.0.1:2048/v1',
apiKey: 'your-api-key',
});
async function streamingChat() {
const stream = await client.chat.completions.create({
model: 'gemini-2.5-pro',
messages: [
{ role: 'user', content: '请讲一个关于编程的故事' }
],
stream: true,
});
process.stdout.write('AI: ');
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
console.log('\n');
}
streamingChat();
```
#### 错误处理
```javascript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'http://127.0.0.1:2048/v1',
apiKey: 'your-api-key',
timeout: 60 * 1000, // 60秒超时
});
async function chatWithRetry(messages, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await client.chat.completions.create({
model: 'gemini-2.5-pro',
messages: messages,
});
return response.choices[0].message.content;
} catch (error) {
console.error(`尝试 ${attempt + 1}/${maxRetries} 失败:`, error.message);
if (attempt < maxRetries - 1) {
// 指数退避
await new Promise(resolve => setTimeout(resolve, 2 ** attempt * 1000));
continue;
}
throw error;
}
}
}
// 使用示例
chatWithRetry([
{ role: 'user', content: '你好' }
]).then(result => {
console.log(result);
}).catch(error => {
console.error('请求失败:', error);
});
```
### 使用 Fetch API
```javascript
// 非流式请求
async function chatNonStreaming() {
const response = await fetch('http://127.0.0.1:2048/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer your-api-key',
},
body: JSON.stringify({
model: 'gemini-2.5-pro',
messages: [
{ role: 'user', content: '什么是 JavaScript?' }
],
stream: false,
}),
});
const data = await response.json();
console.log(data.choices[0].message.content);
}
// 流式请求
async function chatStreaming() {
const response = await fetch('http://127.0.0.1:2048/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer your-api-key',
},
body: JSON.stringify({
model: 'gemini-2.5-pro',
messages: [
{ role: 'user', content: '请讲一个故事' }
],
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
process.stdout.write('AI: ');
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data.trim() === '[DONE]') {
console.log('\n');
return;
}
try {
const parsed = JSON.parse(data);
const content = parsed.choices[0]?.delta?.content || '';
process.stdout.write(content);
} catch (e) {
// 忽略解析错误
}
}
}
}
}
chatNonStreaming();
chatStreaming();
```
---
## 客户端工具
### Open WebUI
**配置步骤**:
1. 打开 Open WebUI
2. 进入 "设置" → "连接"
3. 在 "模型" 部分,点击 "添加模型"
4. 配置如下:
- **模型名称**: `aistudio-gemini`
- **API 基础 URL**: `http://127.0.0.1:2048/v1`
- **API 密钥**: 输入有效密钥或留空(根据服务器配置)
5. 保存设置
### ChatBox
**配置步骤**:
1. 打开 ChatBox
2. 进入 "设置" → "AI 提供商"
3. 选择 "OpenAI API"
4. 配置如下:
- **API 域名**: `http://127.0.0.1:2048`
- **API 密钥**: 输入有效密钥
- **模型**: 从下拉列表选择模型
5. 保存设置
### LobeChat
**配置步骤**:
1. 打开 LobeChat
2. 点击右上角设置图标
3. 进入 "语言模型" 设置
4. 选择 "OpenAI"
5. 配置如下:
- **API 地址**: `http://127.0.0.1:2048/v1`
- **API Key**: 输入有效密钥
6. 保存设置
### Continue (VS Code 扩展)
**配置步骤**:
1. 在 VS Code 中安装 Continue 扩展
2. 打开 Continue 设置 (JSON)
3. 添加配置:
```json
{
"models": [
{
"title": "AI Studio Gemini",
"provider": "openai",
"model": "gemini-2.5-pro",
"apiBase": "http://127.0.0.1:2048/v1",
"apiKey": "your-api-key"
}
]
}
```
4. 保存并重载 VS Code
---
## 最佳实践
### 1. 错误处理
始终实现错误处理和重试机制:
```python
def robust_chat(client, messages, max_retries=3):
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=messages,
timeout=60
)
return response
except Exception as e:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)
```
### 2. 超时设置
为请求设置合理的超时时间:
```python
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-api-key",
timeout=60.0 # 60秒超时
)
```
### 3. 流式处理
对于长文本生成,优先使用流式响应:
```python
stream = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[{"role": "user", "content": "写一篇长文"}],
stream=True
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
```
### 4. 参数调优
根据场景调整参数:
```python
# 创意写作 - 高温度
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[{"role": "user", "content": "写一首诗"}],
temperature=0.9,
max_tokens=2048
)
# 技术问答 - 低温度
response = client.chat.completions.create(
model="gemini-2.5-pro",
messages=[{"role": "user", "content": "什么是REST API?"}],
temperature=0.3,
max_tokens=1024
)
```
---
## 故障排除
### 连接错误
**问题**: 无法连接到服务器
**解决方案**:
```bash
# 检查服务器是否运行
curl http://127.0.0.1:2048/health
# 检查端口是否正确
# 如果使用自定义端口,需要修改 base_url
```
### 认证错误
**问题**: 401 Unauthorized
**解决方案**:
```python
# 确保提供了有效的 API 密钥
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-valid-api-key" # 使用有效密钥
)
```
### 超时错误
**问题**: 请求超时
**解决方案**:
```python
# 增加超时时间
client = OpenAI(
base_url="http://127.0.0.1:2048/v1",
api_key="your-api-key",
timeout=120.0 # 增加到 120 秒
)
```
---
## 相关文档
- [API 使用指南](api-usage.md) - 详细的 API 端点说明
- [OpenAI 兼容性说明](openai-compatibility.md) - 兼容性和限制
- [故障排除指南](troubleshooting.md) - 常见问题解决
---
**最后更新**: 2024年11月
**当前版本**: v0.6.0
如有问题或需要更多示例,请提交 Issue。
|