|
| 1 | +""" |
| 2 | +Copyright (c) 2024, 2025, Oracle and/or its affiliates. |
| 3 | +Licensed under the Universal Permissive License v1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 4 | +""" |
| 5 | +# spell-checker:ignore litellm giskard ollama llms |
| 6 | +# pylint: disable=unused-argument,protected-access |
| 7 | + |
| 8 | +from typing import TYPE_CHECKING, List, Optional, Any |
| 9 | +import time |
| 10 | +import litellm |
| 11 | +from litellm.llms.ollama.completion.transformation import OllamaConfig |
| 12 | +from litellm.types.llms.openai import AllMessageValues |
| 13 | +from litellm.types.utils import ModelResponse |
| 14 | +from httpx._models import Response |
| 15 | + |
| 16 | +import common.logging_config as logging_config |
| 17 | + |
| 18 | +logger = logging_config.logging.getLogger("patches.litellm_patch") |
| 19 | + |
| 20 | +# Only patch if not already patched |
| 21 | +if not getattr(OllamaConfig.transform_response, "_is_custom_patch", False): |
| 22 | + if TYPE_CHECKING: |
| 23 | + from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj |
| 24 | + |
| 25 | + LiteLLMLoggingObj = _LiteLLMLoggingObj |
| 26 | + else: |
| 27 | + LiteLLMLoggingObj = Any |
| 28 | + |
| 29 | + def custom_transform_response( |
| 30 | + self, |
| 31 | + model: str, |
| 32 | + raw_response: Response, |
| 33 | + model_response: ModelResponse, |
| 34 | + logging_obj: LiteLLMLoggingObj, |
| 35 | + request_data: dict, |
| 36 | + messages: List[AllMessageValues], |
| 37 | + optional_params: dict, |
| 38 | + litellm_params: dict, |
| 39 | + encoding: str, |
| 40 | + api_key: Optional[str] = None, |
| 41 | + json_mode: Optional[bool] = None, |
| 42 | + ): |
| 43 | + """Custom transform response from .venv/lib/python3.11/site-packages/litellm/llms/ollama/completion/transformation.py""" |
| 44 | + logger.info("Custom transform_response is running") |
| 45 | + response_json = raw_response.json() |
| 46 | + |
| 47 | + model_response.choices[0].finish_reason = "stop" |
| 48 | + model_response.choices[0].message.content = response_json["response"] |
| 49 | + |
| 50 | + _prompt = request_data.get("prompt", "") |
| 51 | + prompt_tokens = response_json.get( |
| 52 | + "prompt_eval_count", |
| 53 | + len(encoding.encode(_prompt, disallowed_special=())), |
| 54 | + ) |
| 55 | + completion_tokens = response_json.get("eval_count", len(response_json.get("message", {}).get("content", ""))) |
| 56 | + |
| 57 | + setattr( |
| 58 | + model_response, |
| 59 | + "usage", |
| 60 | + litellm.Usage( |
| 61 | + prompt_tokens=prompt_tokens, |
| 62 | + completion_tokens=completion_tokens, |
| 63 | + total_tokens=prompt_tokens + completion_tokens, |
| 64 | + ), |
| 65 | + ) |
| 66 | + model_response.created = int(time.time()) |
| 67 | + model_response.model = "ollama/" + model |
| 68 | + return model_response |
| 69 | + |
| 70 | + # Mark it to avoid double patching |
| 71 | + custom_transform_response._is_custom_patch = True |
| 72 | + |
| 73 | + # Patch it |
| 74 | + OllamaConfig.transform_response = custom_transform_response |
0 commit comments