from typing import List, Dict
from .base import BaseProvider


class OpenAIProvider(BaseProvider):
    """OpenAI provider — GPT-4o, GPT-3.5 etc."""

    def __init__(self, api_key: str, model: str = "gpt-4o"):
        super().__init__(api_key, model)

    def complete(self, messages: List[Dict], system_prompt: str = "") -> str:
        try:
            from openai import OpenAI
        except ImportError:
            raise ImportError("Run: pip install openai")

        client = OpenAI(api_key=self.api_key)
        full_messages = []
        if system_prompt:
            full_messages.append({"role": "system", "content": system_prompt})
        full_messages += messages

        response = client.chat.completions.create(
            model=self.model,
            max_tokens=2048,
            messages=full_messages,
        )
        return response.choices[0].message.content
