tawada / grass-grower

0 stars 0 forks source link

Incorrect instantiation method for the OpenAI API client in `services/llm/__init__.py`. #91

Closed tawada closed 3 months ago

tawada commented 4 months ago

Upon reviewing the code, one issue stands out: The OpenAI API client instantiation in services/llm/__init__.py uses a deprecated or non-existent method openai.OpenAI. The correct usage should be the instantiation of the openai client without an explicit class method like openai.OpenAI. Instead, the openai library should be properly utilized as per its documentation.

Here's the correct way to initialize the openai client based on OpenAI's API:

import openai

def get_openai_client(api_key: str = None) -> openai.OpenAI:
    """Factory function to create and configure an OpenAI client."""
    try:
        if api_key is None:
            api_key = os.environ["OPENAI_API_KEY"]
        openai.api_key = api_key
        return openai
    except KeyError as err:
        log(
            ("OPENAI_API_KEY is not set in environment variables. "
             "Please set it to use LLM functionalities."),
            level="error",
        )
        raise llm_exceptions.NotFoundAPIKeyException(
            "API key must be provided as an argument or in the environment"
        ) from err

This ensures the functionality will not break due to incorrect instantiation of the OpenAI client.