import openai
import os


# Define the Clauses class
class Clauses:
    def __init__(self):
        """
        Initialize the Extractor class.
        """

        # Set OpenAI API key
        # os.environ["OPENAI_API_KEY"] = ""

    def get_extracted_clauses(extracted_summary):
        """
        Gets extracted clauses using GPT-3 based on the provided PDF.

        Args:
            max_tokens (int, optional): Maximum number of tokens for GPT-3 response.

        Returns:
            str: Extracted clauses from GPT-3 response.
        """
        try:
            # Prepare a prompt for GPT-3 that includes the extracted PDF text and instructions
            prompt = f"""
            Extract clauses and sub-clauses from the provided contract PDF:

            {extracted_summary}

            Instructions: Organize the extracted clauses and sub clauses in a readable format.
            """
            # Use GPT-3 to process the prompt and generate clauses
            response = openai.Completion.create(
                engine="text-davinci-003",
                prompt=prompt,
                max_tokens=1000
            )

            # Extract the generated text from the GPT-3 response
            result = response['choices'][0]['text'].strip()
            return result

        except Exception as e:
            # If an error occurs during GPT-3 processing, log the error and raise an exception
            print(f"Error occurred while processing PDF with GPT-3. Error message: {str(e)}")
            raise