import openai
import os


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

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

    def extract_key_value_pair(extracted_summary):
        """
        Extract key-value pairs from the refined summary.

        Prints the extracted key-value pairs.
        """
        try:


            # Use OpenAI's Completion API to analyze the text and extract key-value pairs
            response = openai.Completion.create(
                engine="text-davinci-003",
                temperature=0,
                prompt=f"analyze the given contract and get meaningful key value pairs in given content.contract in backticks.```{extracted_summary}```.",
                max_tokens=1000
            )

            # Extract and return the chatbot's reply
            result = response['choices'][0]['text'].strip()
            return result
        except Exception as e:
            # If an error occurs during the key-value extraction process, log the error
            print(f"Error occurred while extracting key-value pairs: {str(e)}")