Skip to content

Commit 421e0a3

Browse files
Litellm add managed files db (#9930)
* fix(openai.py): ensure openai file object shows up on logs * fix(managed_files.py): return unified file id as b64 str allows retrieve file id to work as expected * fix(managed_files.py): apply decoded file id transformation * fix: add unit test for file id + decode logic * fix: initial commit for litellm_proxy support with CRUD Endpoints * fix(managed_files.py): support retrieve file operation * fix(managed_files.py): support for DELETE endpoint for files * fix(managed_files.py): retrieve file content support supports retrieve file content api from openai * fix: fix linting error * test: update tests * fix: fix linting error * feat(managed_files.py): support reading / writing files in DB * feat(managed_files.py): support deleting file from DB on delete * test: update testing * fix(spend_tracking_utils.py): ensure each file create request is logged correctly * fix(managed_files.py): fix storing / returning managed file object from cache * fix(files/main.py): pass litellm params to azure route * test: fix test * build: add new prisma migration * build: bump requirements * test: add more testing * refactor: cleanup post merge w/ main * fix: fix code qa errors
1 parent 93037ea commit 421e0a3

File tree

19 files changed

+286
-158
lines changed

19 files changed

+286
-158
lines changed
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- CreateTable
2+
CREATE TABLE "LiteLLM_ManagedFileTable" (
3+
"id" TEXT NOT NULL,
4+
"unified_file_id" TEXT NOT NULL,
5+
"file_object" JSONB NOT NULL,
6+
"model_mappings" JSONB NOT NULL,
7+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
8+
"updated_at" TIMESTAMP(3) NOT NULL,
9+
10+
CONSTRAINT "LiteLLM_ManagedFileTable_pkey" PRIMARY KEY ("id")
11+
);
12+
13+
-- CreateIndex
14+
CREATE UNIQUE INDEX "LiteLLM_ManagedFileTable_unified_file_id_key" ON "LiteLLM_ManagedFileTable"("unified_file_id");
15+
16+
-- CreateIndex
17+
CREATE INDEX "LiteLLM_ManagedFileTable_unified_file_id_idx" ON "LiteLLM_ManagedFileTable"("unified_file_id");
18+

litellm-proxy-extras/litellm_proxy_extras/schema.prisma

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,14 @@ enum JobStatus {
354354
INACTIVE
355355
}
356356

357+
model LiteLLM_ManagedFileTable {
358+
id String @id @default(uuid())
359+
unified_file_id String @unique // The base64 encoded unified file ID
360+
file_object Json // Stores the OpenAIFileObject
361+
model_mappings Json // Stores the mapping of model_id -> provider_file_id
362+
created_at DateTime @default(now())
363+
updated_at DateTime @updatedAt
364+
365+
@@index([unified_file_id])
366+
}
367+

litellm-proxy-extras/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "litellm-proxy-extras"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
description = "Additional files for the LiteLLM Proxy. Reduces the size of the main litellm package."
55
authors = ["BerriAI"]
66
readme = "README.md"
@@ -22,7 +22,7 @@ requires = ["poetry-core"]
2222
build-backend = "poetry.core.masonry.api"
2323

2424
[tool.commitizen]
25-
version = "0.1.3"
25+
version = "0.1.4"
2626
version_files = [
2727
"pyproject.toml:version",
2828
"../requirements.txt:litellm-proxy-extras==",

litellm/litellm_core_utils/prompt_templates/common_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,20 @@ def get_format_from_file_id(file_id: Optional[str]) -> Optional[str]:
313313
unified_file_id = litellm_proxy:{};unified_id,{}
314314
If not a unified file id, returns 'file' as default format
315315
"""
316+
from litellm.proxy.hooks.managed_files import _PROXY_LiteLLMManagedFiles
317+
316318
if not file_id:
317319
return None
318320
try:
319-
if file_id.startswith(SpecialEnums.LITELM_MANAGED_FILE_ID_PREFIX.value):
321+
transformed_file_id = (
322+
_PROXY_LiteLLMManagedFiles._convert_b64_uid_to_unified_uid(file_id)
323+
)
324+
if transformed_file_id.startswith(
325+
SpecialEnums.LITELM_MANAGED_FILE_ID_PREFIX.value
326+
):
320327
match = re.match(
321328
f"{SpecialEnums.LITELM_MANAGED_FILE_ID_PREFIX.value}:(.*?);unified_id",
322-
file_id,
329+
transformed_file_id,
323330
)
324331
if match:
325332
return match.group(1)
@@ -343,6 +350,7 @@ def update_messages_with_model_file_ids(
343350
}
344351
}
345352
"""
353+
346354
for message in messages:
347355
if message.get("role") == "user":
348356
content = message.get("content")

litellm/proxy/_types.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from typing_extensions import Required, TypedDict
1717

1818
from litellm.types.integrations.slack_alerting import AlertType
19-
from litellm.types.llms.openai import AllMessageValues
19+
from litellm.types.llms.openai import AllMessageValues, OpenAIFileObject
2020
from litellm.types.router import RouterErrors, UpdateRouterConfig
2121
from litellm.types.utils import (
2222
CallTypes,
@@ -144,6 +144,7 @@ class LitellmTableNames(str, enum.Enum):
144144
USER_TABLE_NAME = "LiteLLM_UserTable"
145145
KEY_TABLE_NAME = "LiteLLM_VerificationToken"
146146
PROXY_MODEL_TABLE_NAME = "LiteLLM_ProxyModelTable"
147+
MANAGED_FILE_TABLE_NAME = "LiteLLM_ManagedFileTable"
147148

148149

149150
class Litellm_EntityType(enum.Enum):
@@ -2795,3 +2796,9 @@ class SpendUpdateQueueItem(TypedDict, total=False):
27952796
entity_type: Litellm_EntityType
27962797
entity_id: str
27972798
response_cost: Optional[float]
2799+
2800+
2801+
class LiteLLM_ManagedFileTable(LiteLLMPydanticObjectBase):
2802+
unified_file_id: str
2803+
file_object: OpenAIFileObject
2804+
model_mappings: Dict[str, str]

0 commit comments

Comments
 (0)