forked from phoenix/litellm-mirror
* fix(ollama.py): fix get model info request Fixes https://github.com/BerriAI/litellm/issues/6703 * feat(anthropic/chat/transformation.py): support passing user id to anthropic via openai 'user' param * docs(anthropic.md): document all supported openai params for anthropic * test: fix tests * fix: fix tests * feat(jina_ai/): add rerank support Closes https://github.com/BerriAI/litellm/issues/6691 * test: handle service unavailable error * fix(handler.py): refactor together ai rerank call * test: update test to handle overloaded error * test: fix test * Litellm router trace (#6742) * feat(router.py): add trace_id to parent functions - allows tracking retry/fallbacks * feat(router.py): log trace id across retry/fallback logic allows grouping llm logs for the same request * test: fix tests * fix: fix test * fix(transformation.py): only set non-none stop_sequences * Litellm router disable fallbacks (#6743) * bump: version 1.52.6 → 1.52.7 * feat(router.py): enable dynamically disabling fallbacks Allows for enabling/disabling fallbacks per key * feat(litellm_pre_call_utils.py): support setting 'disable_fallbacks' on litellm key * test: fix test * fix(exception_mapping_utils.py): map 'model is overloaded' to internal server error * fix(lm_studio/embed): support translating lm studio optional params ' * feat(auth_checks.py): fix auth check inside route - `/team/list` Fixes regression where non-admin w/ user_id=None able to query all teams * docs proxy_budget_rescheduler_min_time * helm run DISABLE_SCHEMA_UPDATE * docs helm pre sync hook * fix migration job.yaml * fix DATABASE_URL * use existing spec for migrations job * fix yaml on migrations job * fix migration job * update doc on pre sync hook * fix migrations-job.yaml * fix migration job * fix prisma migration * test - handle eol model claude-2, use claude-2.1 instead * (docs) add instructions on how to contribute to docker image * Update code blocks huggingface.md (#6737) * Update prefix.md (#6734) * fix test_supports_response_schema * mark Helm PreSyn as BETA * (Feat) Add support for storing virtual keys in AWS SecretManager (#6728) * add SecretManager to httpxSpecialProvider * fix importing AWSSecretsManagerV2 * add unit testing for writing keys to AWS secret manager * use KeyManagementEventHooks for key/generated events * us event hooks for key management endpoints * working AWSSecretsManagerV2 * fix write secret to AWS secret manager on /key/generate * fix KeyManagementSettings * use tasks for key management hooks * add async_delete_secret * add test for async_delete_secret * use _delete_virtual_keys_from_secret_manager * fix test secret manager * test_key_generate_with_secret_manager_call * fix check for key_management_settings * sync_read_secret * test_aws_secret_manager * fix sync_read_secret * use helper to check when _should_read_secret_from_secret_manager * test_get_secret_with_access_mode * test - handle eol model claude-2, use claude-2.1 instead * docs AWS secret manager * fix test_read_nonexistent_secret * fix test_supports_response_schema * ci/cd run again * LiteLLM Minor Fixes & Improvement (11/14/2024) (#6730) * fix(ollama.py): fix get model info request Fixes https://github.com/BerriAI/litellm/issues/6703 * feat(anthropic/chat/transformation.py): support passing user id to anthropic via openai 'user' param * docs(anthropic.md): document all supported openai params for anthropic * test: fix tests * fix: fix tests * feat(jina_ai/): add rerank support Closes https://github.com/BerriAI/litellm/issues/6691 * test: handle service unavailable error * fix(handler.py): refactor together ai rerank call * test: update test to handle overloaded error * test: fix test * Litellm router trace (#6742) * feat(router.py): add trace_id to parent functions - allows tracking retry/fallbacks * feat(router.py): log trace id across retry/fallback logic allows grouping llm logs for the same request * test: fix tests * fix: fix test * fix(transformation.py): only set non-none stop_sequences * Litellm router disable fallbacks (#6743) * bump: version 1.52.6 → 1.52.7 * feat(router.py): enable dynamically disabling fallbacks Allows for enabling/disabling fallbacks per key * feat(litellm_pre_call_utils.py): support setting 'disable_fallbacks' on litellm key * test: fix test * fix(exception_mapping_utils.py): map 'model is overloaded' to internal server error * test: handle gemini error * test: fix test * fix: new run * bump: version 1.52.7 → 1.52.8 * docs: add docs on jina ai rerank support * docs(reliability.md): add tutorial on disabling fallbacks per key * docs(logging.md): add 'trace_id' param to standard logging payload * (feat) add bedrock/stability.stable-image-ultra-v1:0 (#6723) * add stability.stable-image-ultra-v1:0 * add pricing for stability.stable-image-ultra-v1:0 * fix test_supports_response_schema * ci/cd run again * [Feature]: Stop swallowing up AzureOpenAi exception responses in litellm's implementation for a BadRequestError (#6745) * fix azure exceptions * test_bad_request_error_contains_httpx_response * test_bad_request_error_contains_httpx_response * use safe access to get exception response * fix get attr * [Feature]: json_schema in response support for Anthropic (#6748) * _convert_tool_response_to_message * fix ModelResponseIterator * fix test_json_response_format * test_json_response_format_stream * fix _convert_tool_response_to_message * use helper _handle_json_mode_chunk * fix _process_response * unit testing for test_convert_tool_response_to_message_no_arguments * update doc for JSON mode * fix: import audio check (#6740) * fix imagegeneration output_cost_per_image on model cost map (#6752) * (feat) Vertex AI - add support for fine tuned embedding models (#6749) * fix use fine tuned vertex embedding models * test_vertex_embedding_url * add _transform_openai_request_to_fine_tuned_embedding_request * add _transform_openai_request_to_fine_tuned_embedding_request * add transform_openai_request_to_vertex_embedding_request * add _transform_vertex_response_to_openai_for_fine_tuned_models * test_vertexai_embedding for ft models * fix test_vertexai_embedding_finetuned * doc fine tuned / custom embedding models * fix test test_partner_models_httpx * bump: version 1.52.8 → 1.52.9 * LiteLLM Minor Fixes & Improvements (11/13/2024) (#6729) * fix(utils.py): add logprobs support for together ai Fixes https://github.com/BerriAI/litellm/issues/6724 * feat(pass_through_endpoints/): add anthropic/ pass-through endpoint adds new `anthropic/` pass-through endpoint + refactors docs * feat(spend_management_endpoints.py): allow /global/spend/report to query team + customer id enables seeing spend for a customer in a team * Add integration with MLflow Tracing (#6147) * Add MLflow logger Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * Streaming handling Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * lint Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * address comments and fix issues Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * address comments and fix issues Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * Move logger construction code Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * Add docs Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * async handlers Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * new picture Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> --------- Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> * fix(mlflow.py): fix ruff linting errors * ci(config.yml): add mlflow to ci testing * fix: fix test * test: fix test * Litellm key update fix (#6710) * fix(caching): convert arg to equivalent kwargs in llm caching handler prevent unexpected errors * fix(caching_handler.py): don't pass args to caching * fix(caching): remove all *args from caching.py * fix(caching): consistent function signatures + abc method * test(caching_unit_tests.py): add unit tests for llm caching ensures coverage for common caching scenarios across different implementations * refactor(litellm_logging.py): move to using cache key from hidden params instead of regenerating one * fix(router.py): drop redis password requirement * fix(proxy_server.py): fix faulty slack alerting check * fix(langfuse.py): avoid copying functions/thread lock objects in metadata fixes metadata copy error when parent otel span in metadata * test: update test * fix(key_management_endpoints.py): fix /key/update with metadata update * fix(key_management_endpoints.py): fix key_prepare_update helper * fix(key_management_endpoints.py): reset value to none if set in key update * fix: update test ' * Litellm dev 11 11 2024 (#6693) * fix(__init__.py): add 'watsonx_text' as mapped llm api route Fixes https://github.com/BerriAI/litellm/issues/6663 * fix(opentelemetry.py): fix passing parallel tool calls to otel Fixes https://github.com/BerriAI/litellm/issues/6677 * refactor(test_opentelemetry_unit_tests.py): create a base set of unit tests for all logging integrations - test for parallel tool call handling reduces bugs in repo * fix(__init__.py): update provider-model mapping to include all known provider-model mappings Fixes https://github.com/BerriAI/litellm/issues/6669 * feat(anthropic): support passing document in llm api call * docs(anthropic.md): add pdf anthropic call to docs + expose new 'supports_pdf_input' function * fix(factory.py): fix linting error * add clear doc string for GCS bucket logging * Add docs to export logs to Laminar (#6674) * Add docs to export logs to Laminar * minor fix: newline at end of file * place laminar after http and grpc * (Feat) Add langsmith key based logging (#6682) * add langsmith_api_key to StandardCallbackDynamicParams * create a file for langsmith types * langsmith add key / team based logging * add key based logging for langsmith * fix langsmith key based logging * fix linting langsmith * remove NOQA violation * add unit test coverage for all helpers in test langsmith * test_langsmith_key_based_logging * docs langsmith key based logging * run langsmith tests in logging callback tests * fix logging testing * test_langsmith_key_based_logging * test_add_callback_via_key_litellm_pre_call_utils_langsmith * add debug statement langsmith key based logging * test_langsmith_key_based_logging * (fix) OpenAI's optional messages[].name does not work with Mistral API (#6701) * use helper for _transform_messages mistral * add test_message_with_name to base LLMChat test * fix linting * add xAI on Admin UI (#6680) * (docs) add benchmarks on 1K RPS (#6704) * docs litellm proxy benchmarks * docs GCS bucket * doc fix - reduce clutter on logging doc title * (feat) add cost tracking stable diffusion 3 on Bedrock (#6676) * add cost tracking for sd3 * test_image_generation_bedrock * fix get model info for image cost * add cost_calculator for stability 1 models * add unit testing for bedrock image cost calc * test_cost_calculator_with_no_optional_params * add test_cost_calculator_basic * correctly allow size Optional * fix cost_calculator * sd3 unit tests cost calc * fix raise correct error 404 when /key/info is called on non-existent key (#6653) * fix raise correct error on /key/info * add not_found_error error * fix key not found in DB error * use 1 helper for checking token hash * fix error code on key info * fix test key gen prisma * test_generate_and_call_key_info * test fix test_call_with_valid_model_using_all_models * fix key info tests * bump: version 1.52.4 → 1.52.5 * add defaults used for GCS logging * LiteLLM Minor Fixes & Improvements (11/12/2024) (#6705) * fix(caching): convert arg to equivalent kwargs in llm caching handler prevent unexpected errors * fix(caching_handler.py): don't pass args to caching * fix(caching): remove all *args from caching.py * fix(caching): consistent function signatures + abc method * test(caching_unit_tests.py): add unit tests for llm caching ensures coverage for common caching scenarios across different implementations * refactor(litellm_logging.py): move to using cache key from hidden params instead of regenerating one * fix(router.py): drop redis password requirement * fix(proxy_server.py): fix faulty slack alerting check * fix(langfuse.py): avoid copying functions/thread lock objects in metadata fixes metadata copy error when parent otel span in metadata * test: update test * bump: version 1.52.5 → 1.52.6 * (feat) helm hook to sync db schema (#6715) * v0 migration job * fix job * fix migrations job.yml * handle standalone DB on helm hook * fix argo cd annotations * fix db migration helm hook * fix migration job * doc fix Using Http/2 with Hypercorn * (fix proxy redis) Add redis sentinel support (#6154) * add sentinel_password support * add doc for setting redis sentinel password * fix redis sentinel - use sentinel password * Fix: Update gpt-4o costs to that of gpt-4o-2024-08-06 (#6714) Fixes #6713 * (fix) using Anthropic `response_format={"type": "json_object"}` (#6721) * add support for response_format=json anthropic * add test_json_response_format to baseLLM ChatTest * fix test_litellm_anthropic_prompt_caching_tools * fix test_anthropic_function_call_with_no_schema * test test_create_json_tool_call_for_response_format * (feat) Add cost tracking for Azure Dall-e-3 Image Generation + use base class to ensure basic image generation tests pass (#6716) * add BaseImageGenTest * use 1 class for unit testing * add debugging to BaseImageGenTest * TestAzureOpenAIDalle3 * fix response_cost_calculator * test_basic_image_generation * fix img gen basic test * fix _select_model_name_for_cost_calc * fix test_aimage_generation_bedrock_with_optional_params * fix undo changes cost tracking * fix response_cost_calculator * fix test_cost_azure_gpt_35 * fix remove dup test (#6718) * (build) update db helm hook * (build) helm db pre sync hook * (build) helm db sync hook * test: run test_team_logging firdst --------- Co-authored-by: Ishaan Jaff <ishaanjaffer0324@gmail.com> Co-authored-by: Dinmukhamed Mailibay <47117969+dinmukhamedm@users.noreply.github.com> Co-authored-by: Kilian Lieret <kilian.lieret@posteo.de> * test: update test * test: skip anthropic overloaded error * test: cleanup test * test: update tests * test: fix test * test: handle gemini overloaded model error * test: handle internal server error * test: handle anthropic overloaded error * test: handle claude instability --------- Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> Co-authored-by: Yuki Watanabe <31463517+B-Step62@users.noreply.github.com> Co-authored-by: Ishaan Jaff <ishaanjaffer0324@gmail.com> Co-authored-by: Dinmukhamed Mailibay <47117969+dinmukhamedm@users.noreply.github.com> Co-authored-by: Kilian Lieret <kilian.lieret@posteo.de> --------- Signed-off-by: B-Step62 <yuki.watanabe@databricks.com> Co-authored-by: Ishaan Jaff <ishaanjaffer0324@gmail.com> Co-authored-by: Jongseob Jeon <aiden.jongseob@gmail.com> Co-authored-by: Camden Clark <camdenaws@gmail.com> Co-authored-by: Rasswanth <61219215+IamRash-7@users.noreply.github.com> Co-authored-by: Yuki Watanabe <31463517+B-Step62@users.noreply.github.com> Co-authored-by: Dinmukhamed Mailibay <47117969+dinmukhamedm@users.noreply.github.com> Co-authored-by: Kilian Lieret <kilian.lieret@posteo.de>
1354 lines
46 KiB
Python
1354 lines
46 KiB
Python
import asyncio
|
|
import copy
|
|
import json
|
|
import traceback
|
|
import uuid
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import List, Optional, Union
|
|
|
|
import fastapi
|
|
from fastapi import APIRouter, Depends, Header, HTTPException, Request, status
|
|
from pydantic import BaseModel
|
|
|
|
import litellm
|
|
from litellm._logging import verbose_proxy_logger
|
|
from litellm.proxy._types import (
|
|
BlockTeamRequest,
|
|
CommonProxyErrors,
|
|
DeleteTeamRequest,
|
|
LiteLLM_AuditLogs,
|
|
LiteLLM_ModelTable,
|
|
LiteLLM_TeamMembership,
|
|
LiteLLM_TeamTable,
|
|
LiteLLM_TeamTableCachedObj,
|
|
LiteLLM_UserTable,
|
|
LitellmTableNames,
|
|
LitellmUserRoles,
|
|
Member,
|
|
NewTeamRequest,
|
|
ProxyErrorTypes,
|
|
ProxyException,
|
|
TeamAddMemberResponse,
|
|
TeamBase,
|
|
TeamInfoResponseObject,
|
|
TeamListResponseObject,
|
|
TeamMemberAddRequest,
|
|
TeamMemberDeleteRequest,
|
|
TeamMemberUpdateRequest,
|
|
TeamMemberUpdateResponse,
|
|
UpdateTeamRequest,
|
|
UserAPIKeyAuth,
|
|
)
|
|
from litellm.proxy.auth.auth_checks import (
|
|
allowed_route_check_inside_route,
|
|
get_team_object,
|
|
)
|
|
from litellm.proxy.auth.user_api_key_auth import _is_user_proxy_admin, user_api_key_auth
|
|
from litellm.proxy.management_helpers.utils import (
|
|
add_new_member,
|
|
management_endpoint_wrapper,
|
|
)
|
|
from litellm.proxy.utils import PrismaClient
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _is_user_team_admin(
|
|
user_api_key_dict: UserAPIKeyAuth, team_obj: LiteLLM_TeamTable
|
|
) -> bool:
|
|
for member in team_obj.members_with_roles:
|
|
if member.user_id is not None and member.user_id == user_api_key_dict.user_id:
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
async def get_all_team_memberships(
|
|
prisma_client: PrismaClient, team_id: List[str], user_id: Optional[str] = None
|
|
) -> List[LiteLLM_TeamMembership]:
|
|
"""Get all team memberships for a given user"""
|
|
## GET ALL MEMBERSHIPS ##
|
|
if not isinstance(user_id, str):
|
|
user_id = str(user_id)
|
|
|
|
team_memberships = await prisma_client.db.litellm_teammembership.find_many(
|
|
where=(
|
|
{"user_id": user_id, "team_id": {"in": team_id}}
|
|
if user_id is not None
|
|
else {"team_id": {"in": team_id}}
|
|
),
|
|
include={"litellm_budget_table": True},
|
|
)
|
|
|
|
returned_tm: List[LiteLLM_TeamMembership] = []
|
|
for tm in team_memberships:
|
|
returned_tm.append(LiteLLM_TeamMembership(**tm.model_dump()))
|
|
|
|
return returned_tm
|
|
|
|
|
|
#### TEAM MANAGEMENT ####
|
|
@router.post(
|
|
"/team/new",
|
|
tags=["team management"],
|
|
dependencies=[Depends(user_api_key_auth)],
|
|
response_model=LiteLLM_TeamTable,
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def new_team( # noqa: PLR0915
|
|
data: NewTeamRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
litellm_changed_by: Optional[str] = Header(
|
|
None,
|
|
description="The litellm-changed-by header enables tracking of actions performed by authorized users on behalf of other users, providing an audit trail for accountability",
|
|
),
|
|
):
|
|
"""
|
|
Allow users to create a new team. Apply user permissions to their team.
|
|
|
|
👉 [Detailed Doc on setting team budgets](https://docs.litellm.ai/docs/proxy/team_budgets)
|
|
|
|
|
|
Parameters:
|
|
- team_alias: Optional[str] - User defined team alias
|
|
- team_id: Optional[str] - The team id of the user. If none passed, we'll generate it.
|
|
- members_with_roles: List[{"role": "admin" or "user", "user_id": "<user-id>"}] - A list of users and their roles in the team. Get user_id when making a new user via `/user/new`.
|
|
- metadata: Optional[dict] - Metadata for team, store information for team. Example metadata = {"extra_info": "some info"}
|
|
- tpm_limit: Optional[int] - The TPM (Tokens Per Minute) limit for this team - all keys with this team_id will have at max this TPM limit
|
|
- rpm_limit: Optional[int] - The RPM (Requests Per Minute) limit for this team - all keys associated with this team_id will have at max this RPM limit
|
|
- max_budget: Optional[float] - The maximum budget allocated to the team - all keys for this team_id will have at max this max_budget
|
|
- budget_duration: Optional[str] - The duration of the budget for the team. Doc [here](https://docs.litellm.ai/docs/proxy/team_budgets)
|
|
- models: Optional[list] - A list of models associated with the team - all keys for this team_id will have at most, these models. If empty, assumes all models are allowed.
|
|
- blocked: bool - Flag indicating if the team is blocked or not - will stop all calls from keys with this team_id.
|
|
|
|
Returns:
|
|
- team_id: (str) Unique team id - used for tracking spend across multiple keys for same team id.
|
|
|
|
_deprecated_params:
|
|
- admins: list - A list of user_id's for the admin role
|
|
- users: list - A list of user_id's for the user role
|
|
|
|
Example Request:
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/new' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data '{
|
|
"team_alias": "my-new-team_2",
|
|
"members_with_roles": [{"role": "admin", "user_id": "user-1234"},
|
|
{"role": "user", "user_id": "user-2434"}]
|
|
}'
|
|
|
|
```
|
|
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/new' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data '{
|
|
"team_alias": "QA Prod Bot",
|
|
"max_budget": 0.000000001,
|
|
"budget_duration": "1d"
|
|
}'
|
|
```
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
|
|
|
if data.team_id is None:
|
|
data.team_id = str(uuid.uuid4())
|
|
else:
|
|
# Check if team_id exists already
|
|
_existing_team_id = await prisma_client.get_data(
|
|
team_id=data.team_id, table_name="team", query_type="find_unique"
|
|
)
|
|
if _existing_team_id is not None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": f"Team id = {data.team_id} already exists. Please use a different team id."
|
|
},
|
|
)
|
|
|
|
if (
|
|
user_api_key_dict.user_role is None
|
|
or user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN
|
|
): # don't restrict proxy admin
|
|
if (
|
|
data.tpm_limit is not None
|
|
and user_api_key_dict.tpm_limit is not None
|
|
and data.tpm_limit > user_api_key_dict.tpm_limit
|
|
):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": f"tpm limit higher than user max. User tpm limit={user_api_key_dict.tpm_limit}. User role={user_api_key_dict.user_role}"
|
|
},
|
|
)
|
|
|
|
if (
|
|
data.rpm_limit is not None
|
|
and user_api_key_dict.rpm_limit is not None
|
|
and data.rpm_limit > user_api_key_dict.rpm_limit
|
|
):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": f"rpm limit higher than user max. User rpm limit={user_api_key_dict.rpm_limit}. User role={user_api_key_dict.user_role}"
|
|
},
|
|
)
|
|
|
|
if (
|
|
data.max_budget is not None
|
|
and user_api_key_dict.max_budget is not None
|
|
and data.max_budget > user_api_key_dict.max_budget
|
|
):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": f"max budget higher than user max. User max budget={user_api_key_dict.max_budget}. User role={user_api_key_dict.user_role}"
|
|
},
|
|
)
|
|
|
|
if data.models is not None and len(user_api_key_dict.models) > 0:
|
|
for m in data.models:
|
|
if m not in user_api_key_dict.models:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": f"Model not in allowed user models. User allowed models={user_api_key_dict.models}. User id={user_api_key_dict.user_id}"
|
|
},
|
|
)
|
|
|
|
if user_api_key_dict.user_id is not None:
|
|
creating_user_in_list = False
|
|
for member in data.members_with_roles:
|
|
if member.user_id == user_api_key_dict.user_id:
|
|
creating_user_in_list = True
|
|
|
|
if creating_user_in_list is False:
|
|
data.members_with_roles.append(
|
|
Member(role="admin", user_id=user_api_key_dict.user_id)
|
|
)
|
|
|
|
## ADD TO MODEL TABLE
|
|
_model_id = None
|
|
if data.model_aliases is not None and isinstance(data.model_aliases, dict):
|
|
litellm_modeltable = LiteLLM_ModelTable(
|
|
model_aliases=json.dumps(data.model_aliases),
|
|
created_by=user_api_key_dict.user_id or litellm_proxy_admin_name,
|
|
updated_by=user_api_key_dict.user_id or litellm_proxy_admin_name,
|
|
)
|
|
model_dict = await prisma_client.db.litellm_modeltable.create(
|
|
{**litellm_modeltable.json(exclude_none=True)} # type: ignore
|
|
) # type: ignore
|
|
|
|
_model_id = model_dict.id
|
|
|
|
## ADD TO TEAM TABLE
|
|
complete_team_data = LiteLLM_TeamTable(
|
|
**data.json(),
|
|
model_id=_model_id,
|
|
)
|
|
|
|
# Set tags on the new team
|
|
if data.tags is not None:
|
|
from litellm.proxy.proxy_server import premium_user
|
|
|
|
if premium_user is not True:
|
|
raise ValueError(
|
|
f"Only premium users can add tags to teams. {CommonProxyErrors.not_premium_user.value}"
|
|
)
|
|
if complete_team_data.metadata is None:
|
|
complete_team_data.metadata = {"tags": data.tags}
|
|
else:
|
|
complete_team_data.metadata["tags"] = data.tags
|
|
|
|
# If budget_duration is set, set `budget_reset_at`
|
|
if complete_team_data.budget_duration is not None:
|
|
duration_s = _duration_in_seconds(duration=complete_team_data.budget_duration)
|
|
reset_at = datetime.now(timezone.utc) + timedelta(seconds=duration_s)
|
|
complete_team_data.budget_reset_at = reset_at
|
|
|
|
team_row: LiteLLM_TeamTable = await prisma_client.insert_data( # type: ignore
|
|
data=complete_team_data.json(exclude_none=True), table_name="team"
|
|
)
|
|
|
|
## ADD TEAM ID TO USER TABLE ##
|
|
for user in complete_team_data.members_with_roles:
|
|
## add team id to user row ##
|
|
await prisma_client.update_data(
|
|
user_id=user.user_id,
|
|
data={"user_id": user.user_id, "teams": [team_row.team_id]},
|
|
update_key_values_custom_query={
|
|
"teams": {
|
|
"push ": [team_row.team_id],
|
|
}
|
|
},
|
|
)
|
|
|
|
# Enterprise Feature - Audit Logging. Enable with litellm.store_audit_logs = True
|
|
if litellm.store_audit_logs is True:
|
|
_updated_values = complete_team_data.json(exclude_none=True)
|
|
|
|
_updated_values = json.dumps(_updated_values, default=str)
|
|
|
|
asyncio.create_task(
|
|
create_audit_log_for_update(
|
|
request_data=LiteLLM_AuditLogs(
|
|
id=str(uuid.uuid4()),
|
|
updated_at=datetime.now(timezone.utc),
|
|
changed_by=litellm_changed_by
|
|
or user_api_key_dict.user_id
|
|
or litellm_proxy_admin_name,
|
|
changed_by_api_key=user_api_key_dict.api_key,
|
|
table_name=LitellmTableNames.TEAM_TABLE_NAME,
|
|
object_id=data.team_id,
|
|
action="created",
|
|
updated_values=_updated_values,
|
|
before_value=None,
|
|
)
|
|
)
|
|
)
|
|
|
|
try:
|
|
return team_row.model_dump()
|
|
except Exception:
|
|
return team_row.dict()
|
|
|
|
|
|
@router.post(
|
|
"/team/update", tags=["team management"], dependencies=[Depends(user_api_key_auth)]
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def update_team(
|
|
data: UpdateTeamRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
litellm_changed_by: Optional[str] = Header(
|
|
None,
|
|
description="The litellm-changed-by header enables tracking of actions performed by authorized users on behalf of other users, providing an audit trail for accountability",
|
|
),
|
|
):
|
|
"""
|
|
Use `/team/member_add` AND `/team/member/delete` to add/remove new team members
|
|
|
|
You can now update team budget / rate limits via /team/update
|
|
|
|
Parameters:
|
|
- team_id: str - The team id of the user. Required param.
|
|
- team_alias: Optional[str] - User defined team alias
|
|
- metadata: Optional[dict] - Metadata for team, store information for team. Example metadata = {"team": "core-infra", "app": "app2", "email": "ishaan@berri.ai" }
|
|
- tpm_limit: Optional[int] - The TPM (Tokens Per Minute) limit for this team - all keys with this team_id will have at max this TPM limit
|
|
- rpm_limit: Optional[int] - The RPM (Requests Per Minute) limit for this team - all keys associated with this team_id will have at max this RPM limit
|
|
- max_budget: Optional[float] - The maximum budget allocated to the team - all keys for this team_id will have at max this max_budget
|
|
- budget_duration: Optional[str] - The duration of the budget for the team. Doc [here](https://docs.litellm.ai/docs/proxy/team_budgets)
|
|
- models: Optional[list] - A list of models associated with the team - all keys for this team_id will have at most, these models. If empty, assumes all models are allowed.
|
|
- blocked: bool - Flag indicating if the team is blocked or not - will stop all calls from keys with this team_id.
|
|
|
|
Example - update team TPM Limit
|
|
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/update' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data-raw '{
|
|
"team_id": "8d916b1c-510d-4894-a334-1c16a93344f5",
|
|
"tpm_limit": 100
|
|
}'
|
|
```
|
|
|
|
Example - Update Team `max_budget` budget
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/update' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data-raw '{
|
|
"team_id": "8d916b1c-510d-4894-a334-1c16a93344f5",
|
|
"max_budget": 10
|
|
}'
|
|
```
|
|
"""
|
|
from litellm.proxy.auth.auth_checks import _cache_team_object
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
proxy_logging_obj,
|
|
user_api_key_cache,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
|
|
|
if data.team_id is None:
|
|
raise HTTPException(status_code=400, detail={"error": "No team id passed in"})
|
|
verbose_proxy_logger.debug("/team/update - %s", data)
|
|
|
|
existing_team_row = await prisma_client.db.litellm_teamtable.find_unique(
|
|
where={"team_id": data.team_id}
|
|
)
|
|
|
|
if existing_team_row is None:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail={"error": f"Team not found, passed team_id={data.team_id}"},
|
|
)
|
|
|
|
updated_kv = data.json(exclude_none=True)
|
|
|
|
# Check budget_duration and budget_reset_at
|
|
if data.budget_duration is not None:
|
|
duration_s = _duration_in_seconds(duration=data.budget_duration)
|
|
reset_at = datetime.now(timezone.utc) + timedelta(seconds=duration_s)
|
|
|
|
# set the budget_reset_at in DB
|
|
updated_kv["budget_reset_at"] = reset_at
|
|
|
|
# check if user is trying to update tags for team
|
|
if "tags" in updated_kv and updated_kv["tags"] is not None:
|
|
from litellm.proxy.proxy_server import premium_user
|
|
|
|
if premium_user is not True:
|
|
raise ValueError(
|
|
f"Only premium users can add tags to teams. {CommonProxyErrors.not_premium_user.value}"
|
|
)
|
|
# remove tags from updated_kv
|
|
_tags = updated_kv.pop("tags")
|
|
if "metadata" in updated_kv and updated_kv["metadata"] is not None:
|
|
updated_kv["metadata"]["tags"] = _tags
|
|
else:
|
|
updated_kv["metadata"] = {"tags": _tags}
|
|
|
|
updated_kv = prisma_client.jsonify_object(data=updated_kv)
|
|
team_row: Optional[
|
|
LiteLLM_TeamTable
|
|
] = await prisma_client.db.litellm_teamtable.update(
|
|
where={"team_id": data.team_id}, data=updated_kv # type: ignore
|
|
)
|
|
|
|
if team_row is None or team_row.team_id is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={"error": "Team doesn't exist. Got={}".format(team_row)},
|
|
)
|
|
|
|
await _cache_team_object(
|
|
team_id=team_row.team_id,
|
|
team_table=LiteLLM_TeamTableCachedObj(**team_row.model_dump()),
|
|
user_api_key_cache=user_api_key_cache,
|
|
proxy_logging_obj=proxy_logging_obj,
|
|
)
|
|
|
|
# Enterprise Feature - Audit Logging. Enable with litellm.store_audit_logs = True
|
|
if litellm.store_audit_logs is True:
|
|
_before_value = existing_team_row.json(exclude_none=True)
|
|
_before_value = json.dumps(_before_value, default=str)
|
|
_after_value: str = json.dumps(updated_kv, default=str)
|
|
|
|
asyncio.create_task(
|
|
create_audit_log_for_update(
|
|
request_data=LiteLLM_AuditLogs(
|
|
id=str(uuid.uuid4()),
|
|
updated_at=datetime.now(timezone.utc),
|
|
changed_by=litellm_changed_by
|
|
or user_api_key_dict.user_id
|
|
or litellm_proxy_admin_name,
|
|
changed_by_api_key=user_api_key_dict.api_key,
|
|
table_name=LitellmTableNames.TEAM_TABLE_NAME,
|
|
object_id=data.team_id,
|
|
action="updated",
|
|
updated_values=_after_value,
|
|
before_value=_before_value,
|
|
)
|
|
)
|
|
)
|
|
|
|
return {"team_id": team_row.team_id, "data": team_row}
|
|
|
|
|
|
@router.post(
|
|
"/team/member_add",
|
|
tags=["team management"],
|
|
dependencies=[Depends(user_api_key_auth)],
|
|
response_model=TeamAddMemberResponse,
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def team_member_add(
|
|
data: TeamMemberAddRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
[BETA]
|
|
|
|
Add new members (either via user_email or user_id) to a team
|
|
|
|
If user doesn't exist, new user row will also be added to User Table
|
|
|
|
Only proxy_admin or admin of team, allowed to access this endpoint.
|
|
```
|
|
|
|
curl -X POST 'http://0.0.0.0:4000/team/member_add' \
|
|
-H 'Authorization: Bearer sk-1234' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"team_id": "45e3e396-ee08-4a61-a88e-16b3ce7e0849", "member": {"role": "user", "user_id": "krrish247652@berri.ai"}}'
|
|
|
|
```
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
proxy_logging_obj,
|
|
user_api_key_cache,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
|
|
|
if data.team_id is None:
|
|
raise HTTPException(status_code=400, detail={"error": "No team id passed in"})
|
|
|
|
if data.member is None:
|
|
raise HTTPException(
|
|
status_code=400, detail={"error": "No member/members passed in"}
|
|
)
|
|
|
|
existing_team_row = await get_team_object(
|
|
team_id=data.team_id,
|
|
prisma_client=prisma_client,
|
|
user_api_key_cache=user_api_key_cache,
|
|
parent_otel_span=None,
|
|
proxy_logging_obj=proxy_logging_obj,
|
|
check_cache_only=False,
|
|
)
|
|
if existing_team_row is None:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail={
|
|
"error": f"Team not found for team_id={getattr(data, 'team_id', None)}"
|
|
},
|
|
)
|
|
|
|
complete_team_data = LiteLLM_TeamTable(**existing_team_row.model_dump())
|
|
|
|
## CHECK IF USER IS PROXY ADMIN OR TEAM ADMIN
|
|
|
|
if (
|
|
hasattr(user_api_key_dict, "user_role")
|
|
and user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN.value
|
|
and not _is_user_team_admin(
|
|
user_api_key_dict=user_api_key_dict, team_obj=complete_team_data
|
|
)
|
|
):
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail={
|
|
"error": "Call not allowed. User not proxy admin OR team admin. route={}, team_id={}".format(
|
|
"/team/member_add",
|
|
complete_team_data.team_id,
|
|
)
|
|
},
|
|
)
|
|
|
|
updated_users: List[LiteLLM_UserTable] = []
|
|
updated_team_memberships: List[LiteLLM_TeamMembership] = []
|
|
|
|
## VALIDATE IF NEW MEMBER ##
|
|
if isinstance(data.member, Member):
|
|
try:
|
|
updated_user, updated_tm = await add_new_member(
|
|
new_member=data.member,
|
|
max_budget_in_team=data.max_budget_in_team,
|
|
prisma_client=prisma_client,
|
|
user_api_key_dict=user_api_key_dict,
|
|
litellm_proxy_admin_name=litellm_proxy_admin_name,
|
|
team_id=data.team_id,
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail={
|
|
"error": "Unable to add user - {}, to team - {}, for reason - {}".format(
|
|
data.member, data.team_id, str(e)
|
|
)
|
|
},
|
|
)
|
|
|
|
updated_users.append(updated_user)
|
|
if updated_tm is not None:
|
|
updated_team_memberships.append(updated_tm)
|
|
elif isinstance(data.member, List):
|
|
tasks: List = []
|
|
for m in data.member:
|
|
try:
|
|
updated_user, updated_tm = await add_new_member(
|
|
new_member=m,
|
|
max_budget_in_team=data.max_budget_in_team,
|
|
prisma_client=prisma_client,
|
|
user_api_key_dict=user_api_key_dict,
|
|
litellm_proxy_admin_name=litellm_proxy_admin_name,
|
|
team_id=data.team_id,
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail={
|
|
"error": "Unable to add user - {}, to team - {}, for reason - {}".format(
|
|
data.member, data.team_id, str(e)
|
|
)
|
|
},
|
|
)
|
|
updated_users.append(updated_user)
|
|
if updated_tm is not None:
|
|
updated_team_memberships.append(updated_tm)
|
|
|
|
await asyncio.gather(*tasks)
|
|
|
|
## ADD TO TEAM ##
|
|
if isinstance(data.member, Member):
|
|
# add to team db
|
|
new_member = data.member
|
|
|
|
# get user id
|
|
if new_member.user_id is None and new_member.user_email is not None:
|
|
for user in updated_users:
|
|
if (
|
|
user.user_email is not None
|
|
and user.user_email == new_member.user_email
|
|
):
|
|
new_member.user_id = user.user_id
|
|
|
|
complete_team_data.members_with_roles.append(new_member)
|
|
|
|
elif isinstance(data.member, List):
|
|
# add to team db
|
|
new_members = data.member
|
|
|
|
for nm in new_members:
|
|
if nm.user_id is None and nm.user_email is not None:
|
|
for user in updated_users:
|
|
if user.user_email is not None and user.user_email == nm.user_email:
|
|
nm.user_id = user.user_id
|
|
|
|
complete_team_data.members_with_roles.extend(new_members)
|
|
|
|
# ADD MEMBER TO TEAM
|
|
_db_team_members = [m.model_dump() for m in complete_team_data.members_with_roles]
|
|
updated_team = await prisma_client.db.litellm_teamtable.update(
|
|
where={"team_id": data.team_id},
|
|
data={"members_with_roles": json.dumps(_db_team_members)}, # type: ignore
|
|
)
|
|
|
|
# Check if updated_team is None
|
|
if updated_team is None:
|
|
raise HTTPException(
|
|
status_code=404, detail={"error": f"Team with id {data.team_id} not found"}
|
|
)
|
|
return TeamAddMemberResponse(
|
|
**updated_team.model_dump(),
|
|
updated_users=updated_users,
|
|
updated_team_memberships=updated_team_memberships,
|
|
)
|
|
|
|
|
|
@router.post(
|
|
"/team/member_delete",
|
|
tags=["team management"],
|
|
dependencies=[Depends(user_api_key_auth)],
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def team_member_delete(
|
|
data: TeamMemberDeleteRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
[BETA]
|
|
|
|
delete members (either via user_email or user_id) from a team
|
|
|
|
If user doesn't exist, an exception will be raised
|
|
```
|
|
curl -X POST 'http://0.0.0.0:8000/team/update' \
|
|
|
|
-H 'Authorization: Bearer sk-1234' \
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
-D '{
|
|
"team_id": "45e3e396-ee08-4a61-a88e-16b3ce7e0849",
|
|
"user_id": "krrish247652@berri.ai"
|
|
}'
|
|
```
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
|
|
|
if data.team_id is None:
|
|
raise HTTPException(status_code=400, detail={"error": "No team id passed in"})
|
|
|
|
if data.user_id is None and data.user_email is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={"error": "Either user_id or user_email needs to be passed in"},
|
|
)
|
|
|
|
_existing_team_row = await prisma_client.db.litellm_teamtable.find_unique(
|
|
where={"team_id": data.team_id}
|
|
)
|
|
|
|
if _existing_team_row is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={"error": "Team id={} does not exist in db".format(data.team_id)},
|
|
)
|
|
existing_team_row = LiteLLM_TeamTable(**_existing_team_row.model_dump())
|
|
|
|
## CHECK IF USER IS PROXY ADMIN OR TEAM ADMIN
|
|
|
|
if (
|
|
user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN.value
|
|
and not _is_user_team_admin(
|
|
user_api_key_dict=user_api_key_dict, team_obj=existing_team_row
|
|
)
|
|
):
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail={
|
|
"error": "Call not allowed. User not proxy admin OR team admin. route={}, team_id={}".format(
|
|
"/team/member_delete", existing_team_row.team_id
|
|
)
|
|
},
|
|
)
|
|
|
|
## DELETE MEMBER FROM TEAM
|
|
new_team_members: List[Member] = []
|
|
for m in existing_team_row.members_with_roles:
|
|
if (
|
|
data.user_id is not None
|
|
and m.user_id is not None
|
|
and data.user_id == m.user_id
|
|
):
|
|
continue
|
|
elif (
|
|
data.user_email is not None
|
|
and m.user_email is not None
|
|
and data.user_email == m.user_email
|
|
):
|
|
continue
|
|
new_team_members.append(m)
|
|
existing_team_row.members_with_roles = new_team_members
|
|
|
|
_db_new_team_members: List[dict] = [m.model_dump() for m in new_team_members]
|
|
|
|
_ = await prisma_client.db.litellm_teamtable.update(
|
|
where={
|
|
"team_id": data.team_id,
|
|
},
|
|
data={"members_with_roles": json.dumps(_db_new_team_members)}, # type: ignore
|
|
)
|
|
|
|
## DELETE TEAM ID from USER ROW, IF EXISTS ##
|
|
# get user row
|
|
key_val = {}
|
|
if data.user_id is not None:
|
|
key_val["user_id"] = data.user_id
|
|
elif data.user_email is not None:
|
|
key_val["user_email"] = data.user_email
|
|
existing_user_rows = await prisma_client.db.litellm_usertable.find_many(
|
|
where=key_val # type: ignore
|
|
)
|
|
|
|
if existing_user_rows is not None and (
|
|
isinstance(existing_user_rows, list) and len(existing_user_rows) > 0
|
|
):
|
|
for existing_user in existing_user_rows:
|
|
team_list = []
|
|
if data.team_id in existing_user.teams:
|
|
team_list = existing_user.teams
|
|
team_list.remove(data.team_id)
|
|
await prisma_client.db.litellm_usertable.update(
|
|
where={
|
|
"user_id": existing_user.user_id,
|
|
},
|
|
data={"teams": {"set": team_list}},
|
|
)
|
|
|
|
return existing_team_row
|
|
|
|
|
|
@router.post(
|
|
"/team/member_update",
|
|
tags=["team management"],
|
|
dependencies=[Depends(user_api_key_auth)],
|
|
response_model=TeamMemberUpdateResponse,
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def team_member_update(
|
|
data: TeamMemberUpdateRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
[BETA]
|
|
|
|
Update team member budgets
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
|
|
|
if data.team_id is None:
|
|
raise HTTPException(status_code=400, detail={"error": "No team id passed in"})
|
|
|
|
if data.user_id is None and data.user_email is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={"error": "Either user_id or user_email needs to be passed in"},
|
|
)
|
|
|
|
_existing_team_row = await prisma_client.db.litellm_teamtable.find_unique(
|
|
where={"team_id": data.team_id}
|
|
)
|
|
|
|
if _existing_team_row is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={"error": "Team id={} does not exist in db".format(data.team_id)},
|
|
)
|
|
existing_team_row = LiteLLM_TeamTable(**_existing_team_row.model_dump())
|
|
|
|
## CHECK IF USER IS PROXY ADMIN OR TEAM ADMIN
|
|
|
|
if (
|
|
user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN.value
|
|
and not _is_user_team_admin(
|
|
user_api_key_dict=user_api_key_dict, team_obj=existing_team_row
|
|
)
|
|
):
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail={
|
|
"error": "Call not allowed. User not proxy admin OR team admin. route={}, team_id={}".format(
|
|
"/team/member_delete", existing_team_row.team_id
|
|
)
|
|
},
|
|
)
|
|
|
|
returned_team_info: TeamInfoResponseObject = await team_info(
|
|
http_request=http_request,
|
|
team_id=data.team_id,
|
|
user_api_key_dict=user_api_key_dict,
|
|
)
|
|
|
|
## get user id
|
|
received_user_id: Optional[str] = None
|
|
if data.user_id is not None:
|
|
received_user_id = data.user_id
|
|
elif data.user_email is not None:
|
|
for member in returned_team_info["team_info"].members_with_roles:
|
|
if member.user_email is not None and member.user_email == data.user_email:
|
|
received_user_id = member.user_id
|
|
break
|
|
|
|
if received_user_id is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": "User id doesn't exist in team table. Data={}".format(data)
|
|
},
|
|
)
|
|
## find the relevant team membership
|
|
identified_budget_id: Optional[str] = None
|
|
for tm in returned_team_info["team_memberships"]:
|
|
if tm.user_id == received_user_id:
|
|
identified_budget_id = tm.budget_id
|
|
break
|
|
|
|
### upsert new budget
|
|
if identified_budget_id is None:
|
|
new_budget = await prisma_client.db.litellm_budgettable.create(
|
|
data={
|
|
"max_budget": data.max_budget_in_team,
|
|
"created_by": user_api_key_dict.user_id or "",
|
|
"updated_by": user_api_key_dict.user_id or "",
|
|
}
|
|
)
|
|
|
|
await prisma_client.db.litellm_teammembership.create(
|
|
data={
|
|
"team_id": data.team_id,
|
|
"user_id": received_user_id,
|
|
"budget_id": new_budget.budget_id,
|
|
},
|
|
)
|
|
else:
|
|
await prisma_client.db.litellm_budgettable.update(
|
|
where={"budget_id": identified_budget_id},
|
|
data={"max_budget": data.max_budget_in_team},
|
|
)
|
|
|
|
return TeamMemberUpdateResponse(
|
|
team_id=data.team_id,
|
|
user_id=received_user_id,
|
|
user_email=data.user_email,
|
|
max_budget_in_team=data.max_budget_in_team,
|
|
)
|
|
|
|
|
|
@router.post(
|
|
"/team/delete", tags=["team management"], dependencies=[Depends(user_api_key_auth)]
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def delete_team(
|
|
data: DeleteTeamRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
litellm_changed_by: Optional[str] = Header(
|
|
None,
|
|
description="The litellm-changed-by header enables tracking of actions performed by authorized users on behalf of other users, providing an audit trail for accountability",
|
|
),
|
|
):
|
|
"""
|
|
delete team and associated team keys
|
|
|
|
Parameters:
|
|
- team_ids: List[str] - Required. List of team IDs to delete. Example: ["team-1234", "team-5678"]
|
|
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/delete' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data-raw '{
|
|
"team_ids": ["8d916b1c-510d-4894-a334-1c16a93344f5"]
|
|
}'
|
|
```
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
|
|
|
if data.team_ids is None:
|
|
raise HTTPException(status_code=400, detail={"error": "No team id passed in"})
|
|
|
|
# check that all teams passed exist
|
|
for team_id in data.team_ids:
|
|
team_row = await prisma_client.get_data( # type: ignore
|
|
team_id=team_id, table_name="team", query_type="find_unique"
|
|
)
|
|
if team_row is None:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail={"error": f"Team not found, passed team_id={team_id}"},
|
|
)
|
|
|
|
# Enterprise Feature - Audit Logging. Enable with litellm.store_audit_logs = True
|
|
# we do this after the first for loop, since first for loop is for validation. we only want this inserted after validation passes
|
|
if litellm.store_audit_logs is True:
|
|
# make an audit log for each team deleted
|
|
for team_id in data.team_ids:
|
|
team_row: Optional[LiteLLM_TeamTable] = await prisma_client.get_data( # type: ignore
|
|
team_id=team_id, table_name="team", query_type="find_unique"
|
|
)
|
|
|
|
if team_row is None:
|
|
continue
|
|
|
|
_team_row = team_row.json(exclude_none=True)
|
|
|
|
asyncio.create_task(
|
|
create_audit_log_for_update(
|
|
request_data=LiteLLM_AuditLogs(
|
|
id=str(uuid.uuid4()),
|
|
updated_at=datetime.now(timezone.utc),
|
|
changed_by=litellm_changed_by
|
|
or user_api_key_dict.user_id
|
|
or litellm_proxy_admin_name,
|
|
changed_by_api_key=user_api_key_dict.api_key,
|
|
table_name=LitellmTableNames.TEAM_TABLE_NAME,
|
|
object_id=team_id,
|
|
action="deleted",
|
|
updated_values="{}",
|
|
before_value=_team_row,
|
|
)
|
|
)
|
|
)
|
|
|
|
# End of Audit logging
|
|
|
|
## DELETE ASSOCIATED KEYS
|
|
await prisma_client.delete_data(team_id_list=data.team_ids, table_name="key")
|
|
## DELETE TEAMS
|
|
deleted_teams = await prisma_client.delete_data(
|
|
team_id_list=data.team_ids, table_name="team"
|
|
)
|
|
return deleted_teams
|
|
|
|
|
|
@router.get(
|
|
"/team/info", tags=["team management"], dependencies=[Depends(user_api_key_auth)]
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def team_info(
|
|
http_request: Request,
|
|
team_id: str = fastapi.Query(
|
|
default=None, description="Team ID in the request parameters"
|
|
),
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
get info on team + related keys
|
|
|
|
Parameters:
|
|
- team_id: str - Required. The unique identifier of the team to get info on.
|
|
|
|
```
|
|
curl --location 'http://localhost:4000/team/info?team_id=your_team_id_here' \
|
|
--header 'Authorization: Bearer your_api_key_here'
|
|
```
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
try:
|
|
if prisma_client is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail={
|
|
"error": "Database not connected. Connect a database to your proxy - https://docs.litellm.ai/docs/simple_proxy#managing-auth---virtual-keys"
|
|
},
|
|
)
|
|
if team_id is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
detail={"message": "Malformed request. No team id passed in."},
|
|
)
|
|
|
|
if (
|
|
user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN.value
|
|
or user_api_key_dict.user_role
|
|
== LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY.value
|
|
):
|
|
pass
|
|
elif user_api_key_dict.team_id is None or (
|
|
team_id != user_api_key_dict.team_id
|
|
):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="key not allowed to access this team's info. Key team_id={}, Requested team_id={}".format(
|
|
user_api_key_dict.team_id, team_id
|
|
),
|
|
)
|
|
|
|
team_info: Optional[Union[LiteLLM_TeamTable, dict]] = (
|
|
await prisma_client.get_data(
|
|
team_id=team_id, table_name="team", query_type="find_unique"
|
|
)
|
|
)
|
|
if team_info is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"message": f"Team not found, passed team id: {team_id}."},
|
|
)
|
|
|
|
## GET ALL KEYS ##
|
|
keys = await prisma_client.get_data(
|
|
team_id=team_id,
|
|
table_name="key",
|
|
query_type="find_all",
|
|
expires=datetime.now(),
|
|
)
|
|
|
|
if keys is None:
|
|
keys = []
|
|
|
|
if team_info is None:
|
|
## make sure we still return a total spend ##
|
|
spend = 0
|
|
for k in keys:
|
|
spend += getattr(k, "spend", 0)
|
|
team_info = {"spend": spend}
|
|
|
|
## REMOVE HASHED TOKEN INFO before returning ##
|
|
for key in keys:
|
|
try:
|
|
key = key.model_dump() # noqa
|
|
except Exception:
|
|
# if using pydantic v1
|
|
key = key.dict()
|
|
key.pop("token", None)
|
|
|
|
## GET ALL MEMBERSHIPS ##
|
|
returned_tm = await get_all_team_memberships(
|
|
prisma_client, [team_id], user_id=None
|
|
)
|
|
|
|
if isinstance(team_info, dict):
|
|
_team_info = LiteLLM_TeamTable(**team_info)
|
|
elif isinstance(team_info, BaseModel):
|
|
_team_info = LiteLLM_TeamTable(**team_info.model_dump())
|
|
else:
|
|
_team_info = LiteLLM_TeamTable()
|
|
|
|
response_object = TeamInfoResponseObject(
|
|
team_id=team_id,
|
|
team_info=_team_info,
|
|
keys=keys,
|
|
team_memberships=returned_tm,
|
|
)
|
|
return response_object
|
|
|
|
except Exception as e:
|
|
verbose_proxy_logger.error(
|
|
"litellm.proxy.management_endpoints.team_endpoints.py::team_info - Exception occurred - {}\n{}".format(
|
|
e, traceback.format_exc()
|
|
)
|
|
)
|
|
if isinstance(e, HTTPException):
|
|
raise ProxyException(
|
|
message=getattr(e, "detail", f"Authentication Error({str(e)})"),
|
|
type=ProxyErrorTypes.auth_error,
|
|
param=getattr(e, "param", "None"),
|
|
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
|
|
)
|
|
elif isinstance(e, ProxyException):
|
|
raise e
|
|
raise ProxyException(
|
|
message="Authentication Error, " + str(e),
|
|
type=ProxyErrorTypes.auth_error,
|
|
param=getattr(e, "param", "None"),
|
|
code=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
|
|
@router.post(
|
|
"/team/block", tags=["team management"], dependencies=[Depends(user_api_key_auth)]
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def block_team(
|
|
data: BlockTeamRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
Blocks all calls from keys with this team id.
|
|
|
|
Parameters:
|
|
- team_id: str - Required. The unique identifier of the team to block.
|
|
|
|
Example:
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/block' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data '{
|
|
"team_id": "team-1234"
|
|
}'
|
|
```
|
|
|
|
Returns:
|
|
- The updated team record with blocked=True
|
|
|
|
|
|
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise Exception("No DB Connected.")
|
|
|
|
record = await prisma_client.db.litellm_teamtable.update(
|
|
where={"team_id": data.team_id}, data={"blocked": True} # type: ignore
|
|
)
|
|
|
|
if record is None:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail={"error": f"Team not found, passed team_id={data.team_id}"},
|
|
)
|
|
|
|
return record
|
|
|
|
|
|
@router.post(
|
|
"/team/unblock", tags=["team management"], dependencies=[Depends(user_api_key_auth)]
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def unblock_team(
|
|
data: BlockTeamRequest,
|
|
http_request: Request,
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
Blocks all calls from keys with this team id.
|
|
|
|
Parameters:
|
|
- team_id: str - Required. The unique identifier of the team to unblock.
|
|
|
|
Example:
|
|
```
|
|
curl --location 'http://0.0.0.0:4000/team/unblock' \
|
|
--header 'Authorization: Bearer sk-1234' \
|
|
--header 'Content-Type: application/json' \
|
|
--data '{
|
|
"team_id": "team-1234"
|
|
}'
|
|
```
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise Exception("No DB Connected.")
|
|
|
|
record = await prisma_client.db.litellm_teamtable.update(
|
|
where={"team_id": data.team_id}, data={"blocked": False} # type: ignore
|
|
)
|
|
|
|
if record is None:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail={"error": f"Team not found, passed team_id={data.team_id}"},
|
|
)
|
|
|
|
return record
|
|
|
|
|
|
@router.get(
|
|
"/team/list", tags=["team management"], dependencies=[Depends(user_api_key_auth)]
|
|
)
|
|
@management_endpoint_wrapper
|
|
async def list_team(
|
|
http_request: Request,
|
|
user_id: Optional[str] = fastapi.Query(
|
|
default=None, description="Only return teams which this 'user_id' belongs to"
|
|
),
|
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
|
):
|
|
"""
|
|
```
|
|
curl --location --request GET 'http://0.0.0.0:4000/team/list' \
|
|
--header 'Authorization: Bearer sk-1234'
|
|
```
|
|
|
|
Parameters:
|
|
- user_id: str - Optional. If passed will only return teams that the user_id is a member of.
|
|
"""
|
|
from litellm.proxy.proxy_server import (
|
|
_duration_in_seconds,
|
|
create_audit_log_for_update,
|
|
litellm_proxy_admin_name,
|
|
prisma_client,
|
|
)
|
|
|
|
if not allowed_route_check_inside_route(
|
|
user_api_key_dict=user_api_key_dict, requested_user_id=user_id
|
|
):
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail={
|
|
"error": "Only admin users can query all teams/other teams. Your user role={}".format(
|
|
user_api_key_dict.user_role
|
|
)
|
|
},
|
|
)
|
|
|
|
if prisma_client is None:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={"error": CommonProxyErrors.db_not_connected_error.value},
|
|
)
|
|
|
|
response = await prisma_client.db.litellm_teamtable.find_many()
|
|
|
|
filtered_response = []
|
|
if user_id:
|
|
for team in response:
|
|
if team.members_with_roles:
|
|
for member in team.members_with_roles:
|
|
if (
|
|
"user_id" in member
|
|
and member["user_id"] is not None
|
|
and member["user_id"] == user_id
|
|
):
|
|
filtered_response.append(team)
|
|
|
|
else:
|
|
filtered_response = response
|
|
|
|
_team_ids = [team.team_id for team in filtered_response]
|
|
returned_tm = await get_all_team_memberships(
|
|
prisma_client, _team_ids, user_id=user_id
|
|
)
|
|
|
|
returned_responses: List[TeamListResponseObject] = []
|
|
for team in filtered_response:
|
|
_team_memberships: List[LiteLLM_TeamMembership] = []
|
|
for tm in returned_tm:
|
|
if tm.team_id == team.team_id:
|
|
_team_memberships.append(tm)
|
|
|
|
# add all keys that belong to the team
|
|
keys = await prisma_client.db.litellm_verificationtoken.find_many(
|
|
where={"team_id": team.team_id}
|
|
)
|
|
|
|
try:
|
|
returned_responses.append(
|
|
TeamListResponseObject(
|
|
**team.model_dump(),
|
|
team_memberships=_team_memberships,
|
|
keys=keys,
|
|
)
|
|
)
|
|
except Exception as e:
|
|
team_exception = """Invalid team object for team_id: {}. team_object={}.
|
|
Error: {}
|
|
""".format(
|
|
team.team_id, team.model_dump(), str(e)
|
|
)
|
|
raise HTTPException(status_code=400, detail={"error": team_exception})
|
|
|
|
return returned_responses
|