|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT license. |
3 | 3 |
|
| 4 | +import asyncio |
4 | 5 | import base64 |
| 6 | +import concurrent.futures |
5 | 7 | import json |
6 | 8 | import logging |
7 | 9 | from typing import Any, Dict, MutableSequence, Optional |
@@ -62,18 +64,119 @@ class OpenAIChatTarget(OpenAITarget, PromptChatTarget): |
62 | 64 |
|
63 | 65 | """ |
64 | 66 |
|
| 67 | + def _detect_model_capabilities(self) -> bool: |
| 68 | + """ |
| 69 | + Detect model multimodal capabilities via runtime testing. |
| 70 | + |
| 71 | + Sends a minimal multimodal test request to determine if the model |
| 72 | + supports image inputs. This is the most robust approach that works |
| 73 | + regardless of model names or naming conventions. |
| 74 | + |
| 75 | + Returns: |
| 76 | + bool: True if model supports multimodal input, False if text-only |
| 77 | + """ |
| 78 | + # Cache the result to avoid repeated testing |
| 79 | + if not hasattr(self, '_capability_cache'): |
| 80 | + self._capability_cache = {} |
| 81 | + |
| 82 | + cache_key = f"{self.endpoint}:{self.model_name}" |
| 83 | + if cache_key in self._capability_cache: |
| 84 | + return self._capability_cache[cache_key] |
| 85 | + |
| 86 | + try: |
| 87 | + # Create minimal 1x1 pixel transparent PNG as base64 |
| 88 | + # This is the smallest possible valid PNG image (67 bytes) |
| 89 | + minimal_png_b64 = ( |
| 90 | + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==" |
| 91 | + ) |
| 92 | + |
| 93 | + # Construct minimal test message with image + text |
| 94 | + test_messages = [{ |
| 95 | + "role": "user", |
| 96 | + "content": [ |
| 97 | + { |
| 98 | + "type": "text", |
| 99 | + "text": "Can you see this test image?" |
| 100 | + }, |
| 101 | + { |
| 102 | + "type": "image_url", |
| 103 | + "image_url": { |
| 104 | + "url": f"data:image/png;base64,{minimal_png_b64}", |
| 105 | + "detail": "low" # Minimize processing cost |
| 106 | + } |
| 107 | + } |
| 108 | + ] |
| 109 | + }] |
| 110 | + |
| 111 | + # Test request body - minimal parameters to reduce cost/time |
| 112 | + test_body = { |
| 113 | + "model": self.model_name, |
| 114 | + "messages": test_messages, |
| 115 | + "max_tokens": 1, # Minimal response to reduce cost |
| 116 | + "temperature": 0.0 # Deterministic for consistency |
| 117 | + } |
| 118 | + |
| 119 | + # Try the multimodal request |
| 120 | + async def _test_capability(): |
| 121 | + try: |
| 122 | + response = await self._async_client.chat.completions.create(**test_body) |
| 123 | + # If we got a response, the model supports multimodal |
| 124 | + return True |
| 125 | + except Exception as e: |
| 126 | + error_msg = str(e).lower() |
| 127 | + |
| 128 | + # Check for specific errors that indicate no multimodal support |
| 129 | + no_vision_indicators = [ |
| 130 | + "does not support image inputs", |
| 131 | + "vision is not supported", |
| 132 | + "invalid content type", |
| 133 | + "images not supported", |
| 134 | + "multimodal not supported", |
| 135 | + "text-only model" |
| 136 | + ] |
| 137 | + |
| 138 | + if any(indicator in error_msg for indicator in no_vision_indicators): |
| 139 | + return False |
| 140 | + |
| 141 | + # For other errors (auth, rate limit, etc.), assume text-only as safe default |
| 142 | + logger.warning(f"Capability test failed with error: {e}. Defaulting to text-only.") |
| 143 | + return False |
| 144 | + |
| 145 | + # Run the test - handle both running and new event loops |
| 146 | + try: |
| 147 | + loop = asyncio.get_running_loop() |
| 148 | + # If we're in an async context, create a task |
| 149 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 150 | + future = executor.submit(asyncio.run, _test_capability()) |
| 151 | + result = future.result(timeout=30) # 30 second timeout |
| 152 | + except RuntimeError: |
| 153 | + # No running loop, safe to use asyncio.run |
| 154 | + result = asyncio.run(_test_capability()) |
| 155 | + |
| 156 | + # Cache the result |
| 157 | + self._capability_cache[cache_key] = result |
| 158 | + logger.info(f"Detected model {self.model_name} multimodal capability: {result}") |
| 159 | + |
| 160 | + return result |
| 161 | + |
| 162 | + except Exception as e: |
| 163 | + # If runtime testing fails entirely, default to text-only as safe fallback |
| 164 | + logger.warning(f"Runtime capability detection failed: {e}. Defaulting to text-only.") |
| 165 | + self._capability_cache[cache_key] = False |
| 166 | + return False |
| 167 | + |
65 | 168 | @property |
66 | 169 | def SUPPORTED_INPUT_MODALITIES(self) -> "set[frozenset[PromptDataType]]": |
67 | 170 | """ |
68 | 171 | Get supported input modalities based on the specific OpenAI model. |
69 | 172 | |
70 | | - gpt-4o and gpt-4o-mini support multimodal input (text + images), |
71 | | - while other models (gpt-3.5-turbo, gpt-4, o1-*) support text only. |
| 173 | + Uses runtime testing to detect multimodal capabilities: |
| 174 | + - Sends a minimal test image+text request to the model |
| 175 | + - Returns multimodal support if successful, text-only if not |
| 176 | + - Caches results to avoid repeated testing |
| 177 | + - Works with any model regardless of naming conventions |
72 | 178 | """ |
73 | | - multimodal_models = {"gpt-4o", "gpt-4o-mini", "gpt-4o-2024-08-06", "gpt-4o-2024-05-13"} |
74 | | - |
75 | | - # Check if current model supports multimodal input |
76 | | - if any(model in self.model_name.lower() for model in multimodal_models): |
| 179 | + if self._detect_model_capabilities(): |
77 | 180 | return { |
78 | 181 | frozenset({"text"}), # text-only |
79 | 182 | frozenset({"text", "image_path"}) # text+image |
|
0 commit comments