|
| 1 | +""" |
| 2 | +Transformation logic from OpenAI /v1/embeddings format to Isaacus's /v1/embeddings format. |
| 3 | +
|
| 4 | +Reference: https://docs.isaacus.com/api-reference/embeddings |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import List, Optional, Union, cast |
| 8 | + |
| 9 | +import httpx |
| 10 | + |
| 11 | +from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj |
| 12 | +from litellm.llms.base_llm.chat.transformation import BaseLLMException |
| 13 | +from litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfig |
| 14 | +from litellm.secret_managers.main import get_secret_str |
| 15 | +from litellm.types.llms.openai import AllEmbeddingInputValues, AllMessageValues |
| 16 | +from litellm.types.utils import EmbeddingResponse, Usage |
| 17 | + |
| 18 | + |
| 19 | +class IsaacusError(BaseLLMException): |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + status_code: int, |
| 23 | + message: str, |
| 24 | + headers: Union[dict, httpx.Headers] = {}, |
| 25 | + ): |
| 26 | + self.status_code = status_code |
| 27 | + self.message = message |
| 28 | + self.request = httpx.Request( |
| 29 | + method="POST", url="https://api.isaacus.com/v1/embeddings" |
| 30 | + ) |
| 31 | + self.response = httpx.Response(status_code=status_code, request=self.request) |
| 32 | + super().__init__( |
| 33 | + status_code=status_code, |
| 34 | + message=message, |
| 35 | + headers=headers, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +class IsaacusEmbeddingConfig(BaseEmbeddingConfig): |
| 40 | + """ |
| 41 | + Reference: https://docs.isaacus.com/api-reference/embeddings |
| 42 | +
|
| 43 | + The Isaacus embeddings API provides access to the Kanon 2 Embedder for law. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self) -> None: |
| 47 | + pass |
| 48 | + |
| 49 | + def get_complete_url( |
| 50 | + self, |
| 51 | + api_base: Optional[str], |
| 52 | + api_key: Optional[str], |
| 53 | + model: str, |
| 54 | + optional_params: dict, |
| 55 | + litellm_params: dict, |
| 56 | + stream: Optional[bool] = None, |
| 57 | + ) -> str: |
| 58 | + if api_base: |
| 59 | + if not api_base.endswith("/embeddings"): |
| 60 | + api_base = f"{api_base}/embeddings" |
| 61 | + return api_base |
| 62 | + return "https://api.isaacus.com/v1/embeddings" |
| 63 | + |
| 64 | + def get_supported_openai_params(self, model: str) -> list: |
| 65 | + return ["dimensions"] |
| 66 | + |
| 67 | + def map_openai_params( |
| 68 | + self, |
| 69 | + non_default_params: dict, |
| 70 | + optional_params: dict, |
| 71 | + model: str, |
| 72 | + drop_params: bool, |
| 73 | + ) -> dict: |
| 74 | + """ |
| 75 | + Map OpenAI params to Isaacus params |
| 76 | +
|
| 77 | + Reference: https://docs.isaacus.com/api-reference/embeddings |
| 78 | + """ |
| 79 | + if "dimensions" in non_default_params: |
| 80 | + optional_params["dimensions"] = non_default_params["dimensions"] |
| 81 | + return optional_params |
| 82 | + |
| 83 | + def validate_environment( |
| 84 | + self, |
| 85 | + headers: dict, |
| 86 | + model: str, |
| 87 | + messages: List[AllMessageValues], |
| 88 | + optional_params: dict, |
| 89 | + litellm_params: dict, |
| 90 | + api_key: Optional[str] = None, |
| 91 | + api_base: Optional[str] = None, |
| 92 | + ) -> dict: |
| 93 | + if api_key is None: |
| 94 | + api_key = get_secret_str("ISAACUS_API_KEY") |
| 95 | + return { |
| 96 | + "Authorization": f"Bearer {api_key}", |
| 97 | + "Content-Type": "application/json", |
| 98 | + } |
| 99 | + |
| 100 | + def transform_embedding_request( |
| 101 | + self, |
| 102 | + model: str, |
| 103 | + input: AllEmbeddingInputValues, |
| 104 | + optional_params: dict, |
| 105 | + headers: dict, |
| 106 | + ) -> dict: |
| 107 | + """ |
| 108 | + Transform OpenAI-style embedding request to Isaacus format. |
| 109 | +
|
| 110 | + OpenAI uses 'input' while Isaacus uses 'texts'. |
| 111 | + """ |
| 112 | + # Convert input to list of strings if needed |
| 113 | + if isinstance(input, str): |
| 114 | + texts = [input] |
| 115 | + elif isinstance(input, list): |
| 116 | + if len(input) > 0 and isinstance(input[0], (list, int)): |
| 117 | + raise ValueError( |
| 118 | + "Isaacus does not support token array inputs. Input must be a string or list of strings." |
| 119 | + ) |
| 120 | + texts = cast(List[str], input) |
| 121 | + else: |
| 122 | + texts = [input] |
| 123 | + |
| 124 | + request_data = { |
| 125 | + "model": model, |
| 126 | + "texts": texts, |
| 127 | + } |
| 128 | + |
| 129 | + # Add optional parameters |
| 130 | + # Isaacus-specific parameters: task, overflow_strategy, dimensions |
| 131 | + if "task" in optional_params: |
| 132 | + request_data["task"] = optional_params["task"] |
| 133 | + if "overflow_strategy" in optional_params: |
| 134 | + request_data["overflow_strategy"] = optional_params["overflow_strategy"] |
| 135 | + if "dimensions" in optional_params: |
| 136 | + request_data["dimensions"] = optional_params["dimensions"] |
| 137 | + |
| 138 | + return request_data |
| 139 | + |
| 140 | + def transform_embedding_response( |
| 141 | + self, |
| 142 | + model: str, |
| 143 | + raw_response: httpx.Response, |
| 144 | + model_response: EmbeddingResponse, |
| 145 | + logging_obj: LiteLLMLoggingObj, |
| 146 | + api_key: Optional[str] = None, |
| 147 | + request_data: dict = {}, |
| 148 | + optional_params: dict = {}, |
| 149 | + litellm_params: dict = {}, |
| 150 | + ) -> EmbeddingResponse: |
| 151 | + try: |
| 152 | + raw_response_json = raw_response.json() |
| 153 | + except Exception: |
| 154 | + raise IsaacusError( |
| 155 | + message=raw_response.text, status_code=raw_response.status_code |
| 156 | + ) |
| 157 | + |
| 158 | + # Transform Isaacus response format to OpenAI format |
| 159 | + # Isaacus format: {"embeddings": [{"embedding": [...], "index": 0}, ...], "usage": {"input_tokens": 10}} |
| 160 | + # OpenAI format: {"data": [{"embedding": [...], "index": 0, "object": "embedding"}], "model": "...", "usage": {...}} |
| 161 | + |
| 162 | + embeddings_data = raw_response_json.get("embeddings", []) |
| 163 | + output_data = [] |
| 164 | + |
| 165 | + for emb_obj in embeddings_data: |
| 166 | + output_data.append( |
| 167 | + { |
| 168 | + "object": "embedding", |
| 169 | + "index": emb_obj.get("index", 0), |
| 170 | + "embedding": emb_obj.get("embedding", []), |
| 171 | + } |
| 172 | + ) |
| 173 | + |
| 174 | + model_response.model = model |
| 175 | + model_response.data = output_data |
| 176 | + model_response.object = "list" |
| 177 | + |
| 178 | + # Set usage information |
| 179 | + # Isaacus returns usage with "input_tokens" |
| 180 | + usage_data = raw_response_json.get("usage", {}) |
| 181 | + input_tokens = usage_data.get("input_tokens", 0) |
| 182 | + |
| 183 | + usage = Usage( |
| 184 | + prompt_tokens=input_tokens, |
| 185 | + total_tokens=input_tokens, |
| 186 | + ) |
| 187 | + model_response.usage = usage |
| 188 | + |
| 189 | + return model_response |
| 190 | + |
| 191 | + def get_error_class( |
| 192 | + self, error_message: str, status_code: int, headers: Union[dict, httpx.Headers] |
| 193 | + ) -> BaseLLMException: |
| 194 | + return IsaacusError( |
| 195 | + message=error_message, status_code=status_code, headers=headers |
| 196 | + ) |
0 commit comments