| 76 | return re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL) |
| 77 | |
| 78 | class LLM_Wrapper: |
| 79 | @staticmethod |
| 80 | def _download_image_to_base64(img_url: str, timeout: int = 10) -> str: |
| 81 | """ |
| 82 | Download an image from URL and convert it to base64. |
| 83 | |
| 84 | Args: |
| 85 | img_url (str): The URL of the image to download |
| 86 | timeout (int): Request timeout in seconds |
| 87 | |
| 88 | Returns: |
| 89 | str: Base64 encoded image data |
| 90 | |
| 91 | Raises: |
| 92 | Exception: If image download or conversion fails |
| 93 | """ |
| 94 | try: |
| 95 | headers = { |
| 96 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' |
| 97 | } |
| 98 | |
| 99 | response = requests.get(img_url, timeout=timeout, headers=headers) |
| 100 | response.raise_for_status() |
| 101 | |
| 102 | # Convert to base64 |
| 103 | img_base64 = base64.b64encode(response.content).decode('utf-8') |
| 104 | return img_base64 |
| 105 | |
| 106 | except Exception as e: |
| 107 | raise Exception(f"Failed to download and convert image from {img_url}: {str(e)}") |
| 108 | |
| 109 | @staticmethod |
| 110 | def _send_api_key_usage( |
| 111 | api_key: str, |
| 112 | model_name: str, |
| 113 | source_name: str, |
| 114 | prompt_tokens: int, |
| 115 | completion_tokens: int, |
| 116 | create_time: datetime, |
| 117 | finish_time: datetime, |
| 118 | execution_time: float, |
| 119 | status: bool, |
| 120 | request_id: str, |
| 121 | remark: str = "", |
| 122 | ) -> bool: |
| 123 | """Send API key usage information to the API key manager.""" |
| 124 | try: |
| 125 | usage_data = { |
| 126 | "request_id": request_id, |
| 127 | "api_key": api_key, |
| 128 | "model_name": model_name, |
| 129 | "source_name": source_name, |
| 130 | "prompt_tokens": prompt_tokens, |
| 131 | "completion_tokens": completion_tokens, |
| 132 | "create_time": create_time.isoformat(), |
| 133 | "finish_time": finish_time.isoformat(), |
| 134 | "execution_time": execution_time, |
| 135 | "status": status, |
nothing calls this directly
no outgoing calls
no test coverage detected