Repository: FujiwaraChoki/MoneyPrinter
File: Backend/gpt.py (Python)

Code before:
# Load environment variables
load_dotenv("../.env")

# Set environment variables
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
openai.api_key = OPENAI_API_KEY

def generate_response(prompt: str, ai_model: str) -> str:
    """
    Generate a response from the AI model based on the prompt.

    Args:
        prompt (str): The prompt to send to the AI model.
        ai_model (str): The AI model to use for generation.

    Returns:
        str: The response from the AI model.
    """
    if ai_model == 'g4f':
        response = g4f.ChatCompletion.create(
            model=g4f.models.gpt_35_turbo_16k_0613,
            messages=[{"role": "user", "content": prompt}],
        )
    elif ai_model in ['gpt3.5-turbo', 'gpt4']:
        model_name = "gpt-3.5-turbo" if ai_model == 'gpt3.5-turbo' else "gpt-4-1106-preview"
        response = openai.ChatCompletion.create(
            model=model_name,
            messages=[{"role": "user", "content": prompt}],
        ).choices[0].message.content
    else:
        raise ValueError("Invalid AI model selected.")
    return response

def generate_script(video_subject: str, paragraph_number: int, ai_model: str) -> str:
    """
    Generate a script for a video, depending on the subject of the video, the number of paragraphs, and the AI model.

    Args:
        video_subject (str): The subject of the video.
        paragraph_number (int): The number of paragraphs to generate.
        ai_model (str): The AI model to use for generation.

    Returns:
        str: The script for the video.
    """

    # Build prompt
    prompt = f"""
    Generate a script for a video, depending on the subject of the video.
    Subject: {video_subject}
    Number of paragraphs: {paragraph_number}

Review comment (suggestion):
ChatCompletion is deprecated in newer versions of _openai_. 
Change line to 
`response = openai.chat.completions.create(`
as described in the official docs: https://platform.openai.com/docs/quickstart?context=python

Code after:
# Load environment variables
load_dotenv("../.env")

# Set environment variables
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
openai.api_key = OPENAI_API_KEY


def generate_response(prompt: str, ai_model: str) -> str:
    """
    Generate a script for a video, depending on the subject of the video.

    Args:
        video_subject (str): The subject of the video.
        ai_model (str): The AI model to use for generation.


    Returns:

        str: The response from the AI model.

    """

    if ai_model == 'g4f':

        response = g4f.ChatCompletion.create(

            model=g4f.models.gpt_35_turbo_16k_0613,

            messages=[{"role": "user", "content": prompt}],

        )

    elif ai_model in ['gpt3.5-turbo', 'gpt4']:

        model_name = "gpt-3.5-turbo" if ai_model == 'gpt3.5-turbo' else "gpt-4-1106-preview"

        response = openai.ChatCompletion.create(

            model=model_name,

            messages=[{"role": "user", "content": prompt}],

        ).choices[0].message.content

    else:

        raise ValueError("Invalid AI model selected.")

    return response