import os from logging import getLogger from dotenv import load_dotenv from openai import AzureOpenAI from openai import OpenAI logger = getLogger(__name__) # def openai client def init_openai(api_type: str = "open_ai") -> AzureOpenAI: """ Initialize OpenAI settings using environment variables based on the specified API type. This function sets up OpenAI configurations by pulling relevant details from environment variables based on the API type specified (either "open_ai" or "azure"). It ensures that all the necessary environment variables are available for the chosen API type. Args: api_type (str): The type of API to initialize. Accepted values are "open_ai" or "azure". Raises: AssertionError: If any of the required environment variables for the chosen API type are not set. ValueError: If an invalid API type is provided. """ if api_type == "open_ai": # load_dotenv() # azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") # azure_key = os.getenv("AZURE_OPENAI_API_KEY") # azure_version = os.getenv("AZURE_OPENAI_API_VERSION") # client = AzureOpenAI( # azure_endpoint=azure_endpoint, # api_key=azure_key, # api_version=azure_version, # ) api_key = os.getenv("OPENAI_API_KEY") client = OpenAI(api_key=api_key) return client else: raise ValueError(f"Invalid API type: {api_type}")