import os
from openai import OpenAI
from groq import Groq

class techv_client:
    """
    A flexible client for interacting with different LLM providers such as Groq or DeepSeek.
    """

    def __init__(self, provider: str, api_key: str = None, base_url: str = None):
        """
        Initialize the client for the specified LLM provider.

        Args:
            provider (str): The name of the LLM provider (e.g., 'groq', 'deepseek').
            api_key (str): The API key for the provider. If not provided, it will be fetched from the appropriate environment variable.
            base_url (str): The base URL for the provider's API (if applicable).
        """
        self.provider = provider.lower()
        self.api_key = api_key
        self.base_url = base_url

        if self.provider == "groq":
            self.api_key = self.api_key or os.getenv("GROQ_API_KEY")
            if not self.api_key:
                raise ValueError("API key must be provided or set in the GROQ_API_KEY environment variable.")
            self.client = Groq(api_key=self.api_key)

        elif self.provider == "deepseek":
            self.api_key = self.api_key or os.getenv("DEEPSEEK_API_KEY")
            if not self.api_key:
                raise ValueError("API key must be provided or set in the DEEPSEEK_API_KEY environment variable.")
            self.base_url = base_url or "https://api.deepseek.com"
            self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)

        else:
            raise ValueError(f"Unsupported provider: {self.provider}")

    def get_client(self):
        """
        Get the provider-specific client instance.

        Returns:
            The initialized client for the specified provider.
        """
        return self.client

