Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit model API key from UI #8405

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ INFISICAL_TOKEN = ""

# Development Configs
LITELLM_MASTER_KEY = "sk-1234"
LITELLM_SALT_KEY = "sk-1234"
DATABASE_URL = "postgresql://llmproxy:dbpassword9090@db:5432/litellm"
STORE_MODEL_IN_DB = "True"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ If you have suggestions on how to improve the code quality feel free to open an
### Services
1. Setup .env file in root
2. Run dependant services `docker-compose up db prometheus`
3. Install prisma `npm install -g prisma`
4. Run `prisma generate` to generate the prisma client
5. (If database needs to be reset run `prisma migrate reset` followed by `prisma db push`)

### Backend
1. (In root) create virtual environment `python -m venv .venv`
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ services:
environment:
DATABASE_URL: "postgresql://llmproxy:dbpassword9090@db:5432/litellm"
STORE_MODEL_IN_DB: "True" # allows adding models to proxy via UI
LITELLM_SALT_KEY: ${LITELLM_SALT_KEY}
LITELLM_MASTER_KEY: ${LITELLM_MASTER_KEY}
env_file:
- .env # Load local .env file

Expand Down
31 changes: 27 additions & 4 deletions litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6103,16 +6103,22 @@ async def update_model(

### ENCRYPT PARAMS ###
for k, v in _new_litellm_params_dict.items():
encrypted_value = encrypt_value_helper(value=v)
model_params.litellm_params[k] = encrypted_value

try:
encrypted_value = encrypt_value_helper(value=v)
model_params.litellm_params[k] = encrypted_value
except Exception as e:
print(f"Error encrypting {k}: {e}") # Debug print
raise

### MERGE WITH EXISTING DATA ###
merged_dictionary = {}
_mp = model_params.litellm_params.dict()

for key, value in _mp.items():
if value is not None:
merged_dictionary[key] = value
# Ensure we encrypt the value before adding it to the merged dictionary
encrypted_value = encrypt_value_helper(value=value) if isinstance(value, str) else value
merged_dictionary[key] = encrypted_value
elif (
key in _existing_litellm_params_dict
and _existing_litellm_params_dict[key] is not None
Expand Down Expand Up @@ -6215,6 +6221,23 @@ async def model_info_v2(
openai_client = str(_openai_client)
_model["openai_client"] = openai_client

# Decrypt sensitive values in litellm_params
if "litellm_params" in _model:
decrypted_params = {}
for key, value in _model["litellm_params"].items():
if key in ["api_base", "base_url", "model_api_key"]: # Add any other keys that need decryption
try:
decrypted_value = decrypt_value_helper(value=value)
decrypted_params[key] = decrypted_value
except Exception as e:
verbose_proxy_logger.debug(f"Error decrypting {key}: {e}")
decrypted_params[key] = value
else:
decrypted_params[key] = value

_model["litellm_params"] = decrypted_params


# read litellm model_prices_and_context_window.json to get the following:
# input_cost_per_token, output_cost_per_token, max_tokens
litellm_model_info = get_litellm_model_info(model=_model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ const EditModelModal: React.FC<EditModelModalProps> = ({
<Form.Item className="mt-8" label="api_base" name="api_base">
<TextInput />
</Form.Item>
<Form.Item className="mt-8" label="model_api_key" name="model_api_key">
<TextInput type="password" />
</Form.Item>
<Form.Item className="mt-8" label="api_key" name="api_key">
<TextInput />
</Form.Item>
Expand Down
113 changes: 76 additions & 37 deletions ui/litellm-dashboard/src/components/model_dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1150,16 +1150,28 @@ const ModelDashboard: React.FC<ModelDashboardProps> = ({
LiteLLM Model
</TableHeaderCell>
{userRole === "Admin" && (
<TableHeaderCell
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
fontSize: "11px",
}}
>
API Base
</TableHeaderCell>
<>
<TableHeaderCell
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
fontSize: "11px",
}}
>
API Base
</TableHeaderCell>
<TableHeaderCell
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
fontSize: "11px",
}}
>
Model API Key
</TableHeaderCell>
</>
)}
<TableHeaderCell
style={{
Expand Down Expand Up @@ -1326,33 +1338,60 @@ const ModelDashboard: React.FC<ModelDashboardProps> = ({

</TableCell>
{userRole === "Admin" && (
<TableCell
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
}}
>
<Tooltip title={model && model.api_base}>
<pre
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
}}
className="text-xs"
title={
model && model.api_base
? model.api_base
: ""
}
>
{model && model.api_base
? model.api_base.slice(0, 20)
: "-"}
</pre>
</Tooltip>
</TableCell>
<>
<TableCell
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
}}
>
<Tooltip title={model && model.api_base}>
<pre
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
}}
className="text-xs"
title={
model && model.api_base
? model.api_base
: ""
}
>
{model && model.api_base
? model.api_base.slice(0, 20)
: "-"}
</pre>
</Tooltip>
</TableCell>
<TableCell
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
}}
>
<Tooltip title="Model API key hidden for security">
<pre
style={{
maxWidth: "150px",
whiteSpace: "normal",
wordBreak: "break-word",
}}
className="text-xs"
title={model && model.litellm_params.model_api_key
? model.litellm_params.model_api_key
: ""}
>
{model && model.litellm_params.model_api_key
? "***"
: "-"}
</pre>
</Tooltip>
</TableCell>
</>
)}
<TableCell
style={{
Expand Down