Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix endpoint to create fine tuning jobs

Ishaan Jaff9d90f174a7c8
diff --git a/litellm/proxy/fine_tuning_endpoints/endpoints.py b/litellm/proxy/fine_tuning_endpoints/endpoints.py
new file mode 100644
index 00000000000..b15de075fa8
--- /dev/null
+++ b/litellm/proxy/fine_tuning_endpoints/endpoints.py
@@ -0,0 +1,160 @@
+#########################################################################
+
+# /v1/fine_tuning Endpoints
+
+# Equivalent of https://platform.openai.com/docs/api-reference/fine-tuning
+##########################################################################
+
+import asyncio
+import traceback
+from datetime import datetime, timedelta, timezone
+from typing import List, Optional
+
+import fastapi
+import httpx
+from fastapi import (
+ APIRouter,
+ Depends,
+ File,
+ Form,
+ Header,
+ HTTPException,
+ Request,
+ Response,
+ UploadFile,
+ status,
+)
+
+import litellm
+from litellm._logging import verbose_proxy_logger
+from litellm.batches.main import FileObject
+from litellm.proxy._types import *
+from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
+
+router = APIRouter()
+
+from litellm.llms.fine_tuning_apis.openai import (
+ FineTuningJob,
+ FineTuningJobCreate,
+ OpenAIFineTuningAPI,
+)
+
+
+ "/v1/fine_tuning/jobs",
+ dependencies=[Depends(user_api_key_auth)],
+ tags=["fine-tuning"],
+)
+ "/fine_tuning/jobs",
+ dependencies=[Depends(user_api_key_auth)],
+ tags=["fine-tuning"],
+)
+async def create_fine_tuning_job(
+ request: Request,
+ fastapi_response: Response,
+ fine_tuning_request: FineTuningJobCreate,
+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
+):
+ """
+ Creates a fine-tuning job which begins the process of creating a new model from a given dataset.
+ This is the equivalent of POST https://api.openai.com/v1/fine_tuning/jobs
+
+ Supports Identical Params as: https://platform.openai.com/docs/api-reference/fine-tuning/create
+
+ Example Curl:
+ ```
+ curl http://localhost:4000/v1/fine_tuning/jobs \
+ -H "Content-Type: application/json" \
+ -H "Authorization: Bearer sk-1234" \
+ -d '{
+ "model": "gpt-3.5-turbo",
+ "training_file": "file-abc123",
+ "hyperparameters": {
+ "n_epochs": 4
+ }
+ }'
+ ```
+ """
+ from litellm.proxy.proxy_server import (
+ add_litellm_data_to_request,
+ general_settings,
+ get_custom_headers,
+ proxy_config,
+ proxy_logging_obj,
+ version,
+ )
+
+ try:
+ # Convert Pydantic model to dict
+ data = fine_tuning_request.model_dump(exclude_none=True)
+
+ verbose_proxy_logger.debug(
+ "Request received by LiteLLM:\n{}".format(json.dumps(data, indent=4)),
+ )
+
+ # Include original request and headers in the data
+ data = await add_litellm_data_to_request(
+ data=data,
+ request=request,
+ general_settings=general_settings,
+ user_api_key_dict=user_api_key_dict,
+ version=version,
+ proxy_config=proxy_config,
+ )
+
+ # For now, use custom_llm_provider=="openai" -> this will change as LiteLLM adds more providers for fine-tuning
+ response = await litellm.acreate_fine_tuning_job(
+ custom_llm_provider="openai", **data
+ )
+
+ ### ALERTING ###
+ asyncio.create_task(
+ proxy_logging_obj.update_request_status(
+ litellm_call_id=data.get("litellm_call_id", ""), status="success"
+ )
+ )
+
+ ### RESPONSE HEADERS ###
+ hidden_params = getattr(response, "_hidden_params", {}) or {}
+ model_id = hidden_params.get("model_id", None) or ""
+ cache_key = hidden_params.get("cache_key", None) or ""
+ api_base = hidden_params.get("api_base", None) or ""
+
+ fastapi_response.headers.update(
+ get_custom_headers(
+ user_api_key_dict=user_api_key_dict,
+ model_id=model_id,
+ cache_key=cache_key,
+ api_base=api_base,
+ version=version,
+ model_region=getattr(user_api_key_dict, "allowed_model_region", ""),
+ )
+ )
+
+ return response
+ except Exception as e:
+ await proxy_logging_obj.post_call_failure_hook(
+ user_api_key_dict=user_api_key_dict, original_exception=e, request_data=data
+ )
+ verbose_proxy_logger.error(
+ "litellm.proxy.proxy_server.create_fine_tuning_job(): Exception occurred - {}".format(
+ str(e)
+ )
+ )
+ verbose_proxy_logger.debug(traceback.format_exc())
+ if isinstance(e, HTTPException):
+ raise ProxyException(
+ message=getattr(e, "message", str(e.detail)),
+ type=getattr(e, "type", "None"),
+ param=getattr(e, "param", "None"),
+ code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
+ )
+ else:
+ error_msg = f"{str(e)}"
+ raise ProxyException(
+ message=getattr(e, "message", error_msg),
+ type=getattr(e, "type", "None"),
+ param=getattr(e, "param", "None"),
+ code=getattr(e, "status_code", 500),
+ )
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 5a2970df51d..a995980f276 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -153,6 +153,7 @@ from litellm.proxy.common_utils.init_callbacks import initialize_callbacks_on_pr
from litellm.proxy.common_utils.openai_endpoint_utils import (
remove_sensitive_info_from_deployment,
)
+from litellm.proxy.fine_tuning_endpoints.endpoints import router as fine_tuning_router
from litellm.proxy.guardrails.init_guardrails import initialize_guardrails
from litellm.proxy.health_check import perform_health_check
from litellm.proxy.health_endpoints._health_endpoints import router as health_router
@@ -9608,3 +9609,4 @@ app.include_router(debugging_endpoints_router)
app.include_router(ui_crud_endpoints_router)
app.include_router(openai_files_router)
app.include_router(team_callback_router)
+app.include_router(fine_tuning_router)