大模型API集成开发指南:从零到一快速接入
本指南将帮助您快速集成大模型API到您的应用中,涵盖从环境配置到生产部署的完整流程。
快速开始
1. 获取API密钥
- 注册LLM API账号
- 在控制台创建应用
- 生成API密钥
- 配置环境变量
# .env文件 LLM_API_KEY=your-api-key-here LLM_API_BASE_URL=https://api.llmapi.com/v1
多语言SDK集成
Python SDK
# 安装SDK
pip install llm-api-sdk
# 使用示例
from llm_api import Client
client = Client(api_key="your-api-key")
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
],
temperature=0.7,
stream=True
)
for chunk in response:
print(chunk.choices[0].delta.content, end="")JavaScript/TypeScript SDK
// 安装SDK
npm install @llm-api/sdk
// 使用示例
import { LLMAPIClient } from '@llm-api/sdk';
const client = new LLMAPIClient({
apiKey: process.env.LLM_API_KEY,
});
async function generateText() {
const stream = await client.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'user', content: 'Write a haiku about coding' }
],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
}Java SDK
// Maven依赖
<dependency>
<groupId>com.llmapi</groupId>
<artifactId>llm-api-java</artifactId>
<version>1.0.0</version>
</dependency>
// 使用示例
import com.llmapi.LLMAPIClient;
import com.llmapi.models.*;
LLMAPIClient client = new LLMAPIClient("your-api-key");
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model("gpt-4")
.messages(List.of(
new Message("user", "Explain quantum computing")
))
.maxTokens(500)
.build();
ChatCompletionResponse response = client.createChatCompletion(request);
System.out.println(response.getChoices().get(0).getMessage().getContent());RESTful API直接调用
HTTP请求示例
curl -X POST https://api.llmapi.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of France?"
}
],
"temperature": 0.7,
"max_tokens": 150
}'响应格式
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-4",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "The capital of France is Paris."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 8,
"total_tokens": 28
}
}流式响应处理
Server-Sent Events (SSE)
const eventSource = new EventSource(
'https://api.llmapi.com/v1/stream'
);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.done) {
eventSource.close();
} else {
console.log(data.content);
}
};WebSocket连接
const ws = new WebSocket(
'wss://api.llmapi.com/v1/ws'
);
ws.on('open', () => {
ws.send(JSON.stringify({
type: 'chat',
messages: [...],
stream: true
}));
});
ws.on('message', (data) => {
const chunk = JSON.parse(data);
process.stdout.write(chunk.content);
});错误处理最佳实践
常见错误码处理
401 Unauthorized认证错误
API密钥无效或过期
if (error.status === 401) {
// 刷新token或提示用户重新登录
await refreshApiKey();
}429 Too Many Requests限流错误
请求频率超过限制
if (error.status === 429) {
const retryAfter = error.headers['retry-after'];
await sleep(retryAfter * 1000);
return retry(request);
}503 Service Unavailable服务错误
服务暂时不可用
if (error.status === 503) {
// 实施指数退避重试
return exponentialBackoff(request);
}高级集成技巧
连接池优化
const pool = new ConnectionPool({
maxConnections: 10,
maxIdleTime: 30000,
keepAlive: true
});
const client = new LLMAPIClient({
apiKey: API_KEY,
httpClient: pool.getClient()
});请求重试机制
async function callWithRetry(fn, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(Math.pow(2, i) * 1000);
}
}
}生产环境部署清单
- 环境变量管理
使用密钥管理服务存储API密钥
- 监控和日志
设置API调用监控和错误追踪
- 缓存策略
实施响应缓存减少API调用
- 故障转移
配置备用API端点和降级策略
- 成本控制
设置使用量告警和预算限制
常见集成场景
💬
聊天机器人
集成到客服系统提供智能对话
📝
内容生成
自动化创作营销文案和文章
🔍
智能搜索
语义搜索和问答系统