基于Cloudflare Workers AI的专业、可扩展语言模型推理API服务
除了文档页面和健康检查外,所有API端点都需要API密钥认证。请在请求头中包含:
或使用Bearer token:
管理员可以在此生成和管理API密钥。需要管理员主密钥认证。
检查API健康状态(公开访问)
{"status": "healthy", "version": "1.1.0", "features": {"authentication": true}}
列出所有可用的语言模型(公开访问)
{"models": [{"id": "@cf/meta/llama-3-8b-instruct", "name": "Llama 3 8B", "type": "text-generation"}], "total": 10}
生成新的API密钥(需要管理员主密钥)
curl -X POST https://lmapi.518004.com/api/admin/keys/generate \
-H "Content-Type: application/json" \
-H "X-API-Key: master-admin-key" \
-d '{"name": "客户端密钥", "description": "用于客户端应用的API密钥"}'
列出所有API密钥(需要管理员主密钥)
curl -H "X-API-Key: master-admin-key" "https://lmapi.518004.com/api/admin/keys/list?details=true"
撤销指定的API密钥(需要管理员主密钥)
curl -X POST https://lmapi.518004.com/api/admin/keys/revoke \
-H "Content-Type: application/json" \
-H "X-API-Key: master-admin-key" \
-d '{"apiKey": "lm-xxx...xxxxx"}'
生成文本完成(需要API密钥)
curl -X POST https://lmapi.518004.com/api/generate \
-H "Content-Type: application/json" \
-H "X-API-Key: your-api-key" \
-d '{"prompt": "写一首关于AI的俳句:", "max_tokens": 50, "temperature": 0.7}'
响应示例:
{"text": "硅晶思维转\n超越人类理解\n未来在此开", "model": "@cf/meta/llama-3-8b-instruct"}
对话完成,支持上下文(需要API密钥)
curl -X POST https://lmapi.518004.com/api/chat \
-H "Content-Type: application/json" \
-H "X-API-Key: your-api-key" \
-d '{"messages": [{"role": "user", "content": "你好!你好吗?"}], "max_tokens": 100}'
生成文本嵌入向量(需要API密钥)
curl -X POST https://lmapi.518004.com/api/embed \
-H "Content-Type: application/json" \
-H "X-API-Key: your-api-key" \
-d '{"text": "你好世界", "model": "@cf/baai/bge-base-en-v1.5"}'
const response = await fetch('https://lmapi.518004.com/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your-api-key'
},
body: JSON.stringify({
messages: [{ role: 'user', content: '你好!' }],
model: '@cf/meta/llama-3-8b-instruct'
})
});
const data = await response.json();
console.log(data.message.content);