Skip to main content

[Beta] 服务账户

如果你想创建不属于特定用户的虚拟密钥,而是为生产项目创建的密钥,可以使用此功能。

使用方法

1. 设置服务账户的配置

如果你想为服务账户密钥创建仅适用的配置,请设置 service_account_settings

general_settings:
service_account_settings:
enforced_params: ["user"] # 这意味着所有通过服务账户密钥发出的请求都必须包含 "user" 参数

2. 在 LiteLLM 代理管理界面创建服务账户密钥

3. 测试服务账户密钥

curl --location 'http://localhost:4000/chat/completions' \
--header 'Authorization: Bearer <sk-your-service-account>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "hello"
}
]
}'

预期响应

{
"error": {
"message": "BadRequest 请在请求体中传递 param=user。这是服务账户的必填参数",
"type": "bad_request_error",
"param": "user",
"code": "400"
}
}
curl --location 'http://localhost:4000/chat/completions' \
--header 'Authorization: Bearer <sk-your-service-account>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "hello"
}
],
"user": "test-user"
}'

预期响应

{
"id": "chatcmpl-ad9595c7e3784a6783b469218d92d95c",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "\n\nHello there, how may I assist you today?",
"role": "assistant",
"tool_calls": null,
"function_call": null
}
}
],
"created": 1677652288,
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion",
"system_fingerprint": "fp_44709d6fcb",
"usage": {
"completion_tokens": 12,
"prompt_tokens": 9,
"total_tokens": 21,
"completion_tokens_details": null
},
"service_tier": null
}
优云智算