| 5 | |
| 6 | |
| 7 | class GPT4_1(BaseAPIModel): |
| 8 | def __init__( |
| 9 | self, |
| 10 | api_key: Optional[str] = None, |
| 11 | base_url: Optional[str] = None, |
| 12 | model_name: Optional[str] = None, |
| 13 | **kwargs, |
| 14 | ): |
| 15 | config = Config() |
| 16 | |
| 17 | # Use provided parameters or fall back to config |
| 18 | self.api_key = api_key or config.gpt4_1_api_key |
| 19 | self.base_url = base_url or config.gpt4_1_base_url |
| 20 | self.model_name = model_name or config.gpt4_1_model_name |
| 21 | |
| 22 | # Validate that we have required configuration |
| 23 | if not self.api_key: |
| 24 | raise ValueError( |
| 25 | "GPT-4.1 API key not found. Please set GPT4_1_API_KEY in your .env file " |
| 26 | "or provide api_key parameter." |
| 27 | ) |
| 28 | |
| 29 | self.client = OpenAI( |
| 30 | api_key=self.api_key, |
| 31 | base_url=self.base_url, |
| 32 | ) |
| 33 | |
| 34 | # Initialize parent class |
| 35 | super().__init__(model_name=self.model_name, **kwargs) |
| 36 | |
| 37 | def generate( |
| 38 | self, prompt: Union[str, Dict[str, Any]], max_tokens: int = 512 |
| 39 | ) -> str: |
| 40 | """ |
| 41 | Generate response supporting both text and multimodal input. |
| 42 | |
| 43 | Args: |
| 44 | prompt: Either text string or multimodal dict |
| 45 | max_tokens: Maximum tokens to generate |
| 46 | |
| 47 | Returns: |
| 48 | Generated response string |
| 49 | """ |
| 50 | messages = [] |
| 51 | |
| 52 | # Handle multimodal vs text-only prompts |
| 53 | if isinstance(prompt, dict) and "images" in prompt: |
| 54 | # Multimodal prompt |
| 55 | content = [] |
| 56 | |
| 57 | content.append({"type": "text", "text": prompt["text"]}) |
| 58 | |
| 59 | for image_data in prompt["images"]: |
| 60 | content.append(image_data) |
| 61 | |
| 62 | messages.append({"role": "user", "content": content}) |
| 63 | else: |
| 64 | # Text-only prompt |
no outgoing calls