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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
[Feat ] /ocr - Add mode + Health check support for OCR models (#15767)
Ishaan Jaff9135e748a049
diff --git a/docs/my-website/docs/proxy/health.md b/docs/my-website/docs/proxy/health.mdindex c96753648b8..7df7685f335 100644--- a/docs/my-website/docs/proxy/health.md+++ b/docs/my-website/docs/proxy/health.md@@ -213,6 +213,20 @@ model_list:mode: realtime```+### OCR Models++To run OCR health checks, specify the mode as "ocr" in your config for the relevant model.++```yaml+model_list:+ - model_name: mistral/mistral-ocr-latest+ litellm_params:+ model: mistral/mistral-ocr-latest+ api_key: os.environ/MISTRAL_API_KEY+ model_info:+ mode: ocr+```+### Wildcard RoutesFor wildcard routes, you can specify a `health_check_model` in your config.yaml. This model will be used for health checks for that wildcard route.diff --git a/litellm/litellm_core_utils/health_check_helpers.py b/litellm/litellm_core_utils/health_check_helpers.pyindex 2f412479937..9cbee7fc70d 100644--- a/litellm/litellm_core_utils/health_check_helpers.py+++ b/litellm/litellm_core_utils/health_check_helpers.py@@ -2,11 +2,14 @@Helper functions for health check calls."""-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, Callable, Dict, Literal, Optionalif TYPE_CHECKING:from litellm.litellm_core_utils.litellm_logging import Logging+# Minimal PDF for health checks - base64 encoded 1-page PDF with just "test"+TEST_PDF_URL = "data:application/pdf;base64,JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL01lZGlhQm94IFswIDAgNjEyIDc5Ml0KL0NvbnRlbnRzIDQgMCBSCi9SZXNvdXJjZXMgPDwvRm9udCA8PC9GMSAyIDAgUj4+Pj4+PgplbmRvYmoKNCAwIG9iago8PC9MZW5ndGggNDQ+PgpzdHJlYW0KQlQKL0YxIDI0IFRmCjEwMCA3MDAgVGQKKHRlc3QpIFRqCkVUCmVuZHN0cmVhbQplbmRvYmoKMiAwIG9iago8PC9UeXBlIC9Gb250Ci9TdWJ0eXBlIC9UeXBlMQovQmFzZUZvbnQgL0hlbHZldGljYT4+CmVuZG9iagoxIDAgb2JqCjw8L1R5cGUgL1BhZ2VzCi9LaWRzIFszIDAgUl0KL0NvdW50IDE+PgplbmRvYmoKNSAwIG9iago8PC9UeXBlIC9DYXRhbG9nCi9QYWdlcyAxIDAgUj4+CmVuZG9iagp0cmFpbGVyCjw8L1NpemUgNgovUm9vdCA1IDAgUj4+CnN0YXJ0eHJlZgozMjQKJSVFT0Y="+class HealthCheckHelpers:@@ -78,3 +81,108 @@ class HealthCheckHelpers:return {"tags": [LITTELM_INTERNAL_HEALTH_SERVICE_ACCOUNT_NAME],}++ @staticmethod+ def get_mode_handlers(+ model: str,+ custom_llm_provider: str,+ model_params: dict,+ prompt: Optional[str] = None,+ input: Optional[list] = None,+ ) -> Dict[+ Literal[+ "chat",+ "completion",+ "embedding",+ "audio_speech",+ "audio_transcription",+ "image_generation",+ "rerank",+ "realtime",+ "batch",+ "responses",+ "ocr",+ ],+ Callable,+ ]:+ """+ Returns a dictionary of mode handlers for health check calls.++ Mode Handlers are Callables that need to be run for execution of the health check call.++ Args:+ model: The model name+ custom_llm_provider: The LLM provider+ model_params: The model parameters+ prompt: Optional prompt for health check+ input: Optional input for health check++ Returns:+ Dictionary mapping mode names to their handler functions+ """+ import litellm+ from litellm.litellm_core_utils.audio_utils.utils import (+ get_audio_file_for_health_check,+ )+ from litellm.litellm_core_utils.health_check_utils import _filter_model_params+ from litellm.realtime_api.main import _realtime_health_check++ return {+ "chat": lambda: litellm.acompletion(+ **model_params,+ ),+ "completion": lambda: litellm.atext_completion(+ **_filter_model_params(model_params=model_params),+ prompt=prompt or "test",+ ),+ "embedding": lambda: litellm.aembedding(+ **_filter_model_params(model_params=model_params),+ input=input or ["test"],+ ),+ "audio_speech": lambda: litellm.aspeech(+ **{+ **_filter_model_params(model_params=model_params),+ **(+ {"voice": "alloy"}+ if "voice"+ not in _filter_model_params(model_params=model_params)+ else {}+ ),+ },+ input=prompt or "test",+ ),+ "audio_transcription": lambda: litellm.atranscription(+ **_filter_model_params(model_params=model_params),+ file=get_audio_file_for_health_check(),+ ),+ "image_generation": lambda: litellm.aimage_generation(+ **_filter_model_params(model_params=model_params),+ prompt=prompt,+ ),+ "rerank": lambda: litellm.arerank(+ **_filter_model_params(model_params=model_params),+ query=prompt or "",+ documents=["my sample text"],+ ),+ "realtime": lambda: _realtime_health_check(+ model=model,+ custom_llm_provider=custom_llm_provider,+ api_base=model_params.get("api_base", None),+ api_key=model_params.get("api_key", None),+ api_version=model_params.get("api_version", None),+ ),+ "batch": lambda: litellm.alist_batches(+ **_filter_model_params(model_params=model_params),+ ),+ "responses": lambda: litellm.aresponses(+ **_filter_model_params(model_params=model_params),+ input=prompt or "test",+ ),+ "ocr": lambda: litellm.aocr(+ **_filter_model_params(model_params=model_params),+ document={+ "type": "document_url",+ "document_url": TEST_PDF_URL,+ },+ ),+ }\ No newline at end of filediff --git a/litellm/main.py b/litellm/main.pyindex f93387df4b8..36e769781b6 100644--- a/litellm/main.py+++ b/litellm/main.py@@ -5960,6 +5960,7 @@ async def ahealth_check("rerank","realtime","responses",+ "ocr",]] = "chat",prompt: Optional[str] = None,@@ -6022,57 +6023,13 @@ async def ahealth_check(litellm_logging_obj=litellm_logging_obj,)- mode_handlers = {- "chat": lambda: litellm.acompletion(- **model_params,- ),- "completion": lambda: litellm.atext_completion(- **_filter_model_params(model_params),- prompt=prompt or "test",- ),- "embedding": lambda: litellm.aembedding(- **_filter_model_params(model_params),- input=input or ["test"],- ),- "audio_speech": lambda: litellm.aspeech(- **{- **_filter_model_params(model_params),- **(- {"voice": "alloy"}- if "voice" not in _filter_model_params(model_params)- else {}- ),- },- input=prompt or "test",- ),- "audio_transcription": lambda: litellm.atranscription(- **_filter_model_params(model_params),- file=get_audio_file_for_health_check(),- ),- "image_generation": lambda: litellm.aimage_generation(- **_filter_model_params(model_params),- prompt=prompt,- ),- "rerank": lambda: litellm.arerank(- **_filter_model_params(model_params),- query=prompt or "",- documents=["my sample text"],- ),- "realtime": lambda: _realtime_health_check(- model=model,- custom_llm_provider=custom_llm_provider,- api_base=model_params.get("api_base", None),- api_key=model_params.get("api_key", None),- api_version=model_params.get("api_version", None),- ),- "batch": lambda: litellm.alist_batches(- **_filter_model_params(model_params),- ),- "responses": lambda: litellm.aresponses(- **_filter_model_params(model_params),- input=prompt or "test",- ),- }+ mode_handlers = HealthCheckHelpers.get_mode_handlers(+ model=model,+ custom_llm_provider=custom_llm_provider,+ model_params=model_params,+ prompt=prompt,+ input=input,+ )if mode in mode_handlers:_response = await mode_handlers[mode]()diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.pyindex 9964b6c94cc..d8ce39aaf8a 100644--- a/litellm/proxy/health_endpoints/_health_endpoints.py+++ b/litellm/proxy/health_endpoints/_health_endpoints.py@@ -620,7 +620,7 @@ async def shared_health_check_status_endpoint(Returns information about Redis connectivity, lock status, and cache status."""- from litellm.proxy.proxy_server import use_shared_health_check, redis_usage_cache+ from litellm.proxy.proxy_server import redis_usage_cache, use_shared_health_checkif not use_shared_health_check:return {@@ -636,7 +636,9 @@ async def shared_health_check_status_endpoint(}try:- from litellm.proxy.health_check_utils.shared_health_check_manager import SharedHealthCheckManager+ from litellm.proxy.health_check_utils.shared_health_check_manager import (+ SharedHealthCheckManager,+ )shared_health_manager = SharedHealthCheckManager(redis_cache=redis_usage_cache,@@ -910,6 +912,7 @@ async def test_model_connection("batch","rerank","realtime",+ "ocr",]] = fastapi.Body("chat", description="The mode to test the model with"),litellm_params: Dict = fastapi.Body(diff --git a/litellm/proxy/proxy_config.yaml b/litellm/proxy/proxy_config.yamlindex 2e8ebd15f58..995f3159bf3 100644--- a/litellm/proxy/proxy_config.yaml+++ b/litellm/proxy/proxy_config.yaml@@ -1,20 +1,7 @@model_list:- - model_name: aws/anthropic/bedrock-claude-3-5-sonnet-v1+ - model_name: openai-gpt-4o-realtime-audiolitellm_params:- model: bedrock/us.anthropic.claude-3-5-sonnet-20240620-v1:0- aws_region_name: us-east-1- custom_llm_provider: bedrock- - model_name: aws/anthropic/bedrock-claude-3-5-sonnet-v1- litellm_params:- model: bedrock/us.anthropic.claude-3-5-sonnet-20240620-v1:0- aws_region_name: us-west-2- custom_llm_provider: bedrock---guardrails:- - guardrail_name: "bedrock-pre-guard"- litellm_params:- guardrail: bedrock # supported values: "aporia", "bedrock", "lakera"- mode: "pre_call"- guardrailIdentifier: ff6ujrregl1q- guardrailVersion: "DRAFT"+ model: openai/gpt-4o-realtime-preview-2024-10-01+ api_key: os.environ/OPENAI_API_KEY+ model_info:+ mode: realtime\ No newline at end of filediff --git a/tests/litellm_utils_tests/test_health_check.py b/tests/litellm_utils_tests/test_health_check.pyindex 6cff1f08d9b..0a47dfc0ca3 100644--- a/tests/litellm_utils_tests/test_health_check.py+++ b/tests/litellm_utils_tests/test_health_check.py@@ -393,3 +393,16 @@ async def test_health_check_bad_model():assert (end_time - start_time < 2), "Health check took longer than health_check_timeout"++async def test_ahealth_check_ocr():+ litellm._turn_on_debug()+ response = await litellm.ahealth_check(+ model_params={+ "model": "mistral/mistral-ocr-latest",+ "api_key": os.getenv("MISTRAL_API_KEY"),+ },+ mode="ocr",+ )+ print(response)+ return response\ No newline at end of filediff --git a/ui/litellm-dashboard/src/components/add_model/add_model_modes.tsx b/ui/litellm-dashboard/src/components/add_model/add_model_modes.tsxindex 2440c4e8e4a..b6c2410a027 100644--- a/ui/litellm-dashboard/src/components/add_model/add_model_modes.tsx+++ b/ui/litellm-dashboard/src/components/add_model/add_model_modes.tsx@@ -9,6 +9,7 @@ export const TEST_MODES = [{ value: "rerank", label: "Rerank - /rerank" },{ value: "realtime", label: "Realtime - /realtime" },{ value: "batch", label: "Batch - /batch" },+ { value: "ocr", label: "OCR - /ocr" },];// Define the available auto router routing strategies