|
| 1 | +""" |
| 2 | +Anthropic Batches API Handler |
| 3 | +""" |
| 4 | + |
| 5 | +import asyncio |
| 6 | +from typing import TYPE_CHECKING, Any, Coroutine, Optional, Union |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | +from litellm.llms.custom_httpx.http_handler import ( |
| 11 | + get_async_httpx_client, |
| 12 | +) |
| 13 | +from litellm.types.utils import LiteLLMBatch, LlmProviders |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj |
| 17 | +else: |
| 18 | + LiteLLMLoggingObj = Any |
| 19 | + |
| 20 | +from ..common_utils import AnthropicModelInfo |
| 21 | +from .transformation import AnthropicBatchesConfig |
| 22 | + |
| 23 | + |
| 24 | +class AnthropicBatchesHandler: |
| 25 | + """ |
| 26 | + Handler for Anthropic Message Batches API. |
| 27 | + |
| 28 | + Supports: |
| 29 | + - retrieve_batch() - Retrieve batch status and information |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + self.anthropic_model_info = AnthropicModelInfo() |
| 34 | + self.provider_config = AnthropicBatchesConfig() |
| 35 | + |
| 36 | + async def aretrieve_batch( |
| 37 | + self, |
| 38 | + batch_id: str, |
| 39 | + api_base: Optional[str], |
| 40 | + api_key: Optional[str], |
| 41 | + timeout: Union[float, httpx.Timeout], |
| 42 | + max_retries: Optional[int], |
| 43 | + logging_obj: Optional[LiteLLMLoggingObj] = None, |
| 44 | + ) -> LiteLLMBatch: |
| 45 | + """ |
| 46 | + Async: Retrieve a batch from Anthropic. |
| 47 | + |
| 48 | + Args: |
| 49 | + batch_id: The batch ID to retrieve |
| 50 | + api_base: Anthropic API base URL |
| 51 | + api_key: Anthropic API key |
| 52 | + timeout: Request timeout |
| 53 | + max_retries: Max retry attempts (unused for now) |
| 54 | + logging_obj: Optional logging object |
| 55 | + |
| 56 | + Returns: |
| 57 | + LiteLLMBatch: Batch information in OpenAI format |
| 58 | + """ |
| 59 | + # Resolve API credentials |
| 60 | + api_base = api_base or self.anthropic_model_info.get_api_base(api_base) |
| 61 | + api_key = api_key or self.anthropic_model_info.get_api_key() |
| 62 | + |
| 63 | + if not api_key: |
| 64 | + raise ValueError("Missing Anthropic API Key") |
| 65 | + |
| 66 | + # Create a minimal logging object if not provided |
| 67 | + if logging_obj is None: |
| 68 | + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObjClass |
| 69 | + logging_obj = LiteLLMLoggingObjClass( |
| 70 | + model="anthropic/unknown", |
| 71 | + messages=[], |
| 72 | + stream=False, |
| 73 | + call_type="batch_retrieve", |
| 74 | + start_time=None, |
| 75 | + litellm_call_id=f"batch_retrieve_{batch_id}", |
| 76 | + function_id="batch_retrieve", |
| 77 | + ) |
| 78 | + |
| 79 | + # Get the complete URL for batch retrieval |
| 80 | + retrieve_url = self.provider_config.get_retrieve_batch_url( |
| 81 | + api_base=api_base, |
| 82 | + batch_id=batch_id, |
| 83 | + optional_params={}, |
| 84 | + litellm_params={}, |
| 85 | + ) |
| 86 | + |
| 87 | + # Validate environment and get headers |
| 88 | + headers = self.provider_config.validate_environment( |
| 89 | + headers={}, |
| 90 | + model="", |
| 91 | + messages=[], |
| 92 | + optional_params={}, |
| 93 | + litellm_params={}, |
| 94 | + api_key=api_key, |
| 95 | + api_base=api_base, |
| 96 | + ) |
| 97 | + |
| 98 | + logging_obj.pre_call( |
| 99 | + input=batch_id, |
| 100 | + api_key=api_key, |
| 101 | + additional_args={ |
| 102 | + "api_base": retrieve_url, |
| 103 | + "headers": headers, |
| 104 | + "complete_input_dict": {}, |
| 105 | + }, |
| 106 | + ) |
| 107 | + # Make the request |
| 108 | + async_client = get_async_httpx_client(llm_provider=LlmProviders.ANTHROPIC) |
| 109 | + response = await async_client.get( |
| 110 | + url=retrieve_url, |
| 111 | + headers=headers |
| 112 | + ) |
| 113 | + response.raise_for_status() |
| 114 | + |
| 115 | + # Transform response to LiteLLM format |
| 116 | + return self.provider_config.transform_retrieve_batch_response( |
| 117 | + model=None, |
| 118 | + raw_response=response, |
| 119 | + logging_obj=logging_obj, |
| 120 | + litellm_params={}, |
| 121 | + ) |
| 122 | + |
| 123 | + def retrieve_batch( |
| 124 | + self, |
| 125 | + _is_async: bool, |
| 126 | + batch_id: str, |
| 127 | + api_base: Optional[str], |
| 128 | + api_key: Optional[str], |
| 129 | + timeout: Union[float, httpx.Timeout], |
| 130 | + max_retries: Optional[int], |
| 131 | + logging_obj: Optional[LiteLLMLoggingObj] = None, |
| 132 | + ) -> Union[LiteLLMBatch, Coroutine[Any, Any, LiteLLMBatch]]: |
| 133 | + """ |
| 134 | + Retrieve a batch from Anthropic. |
| 135 | + |
| 136 | + Args: |
| 137 | + _is_async: Whether to run asynchronously |
| 138 | + batch_id: The batch ID to retrieve |
| 139 | + api_base: Anthropic API base URL |
| 140 | + api_key: Anthropic API key |
| 141 | + timeout: Request timeout |
| 142 | + max_retries: Max retry attempts (unused for now) |
| 143 | + logging_obj: Optional logging object |
| 144 | + |
| 145 | + Returns: |
| 146 | + LiteLLMBatch or Coroutine: Batch information in OpenAI format |
| 147 | + """ |
| 148 | + if _is_async: |
| 149 | + return self.aretrieve_batch( |
| 150 | + batch_id=batch_id, |
| 151 | + api_base=api_base, |
| 152 | + api_key=api_key, |
| 153 | + timeout=timeout, |
| 154 | + max_retries=max_retries, |
| 155 | + logging_obj=logging_obj, |
| 156 | + ) |
| 157 | + else: |
| 158 | + return asyncio.run( |
| 159 | + self.aretrieve_batch( |
| 160 | + batch_id=batch_id, |
| 161 | + api_base=api_base, |
| 162 | + api_key=api_key, |
| 163 | + timeout=timeout, |
| 164 | + max_retries=max_retries, |
| 165 | + logging_obj=logging_obj, |
| 166 | + ) |
| 167 | + ) |
| 168 | + |
0 commit comments