Repository: shreyashankar/gpt3-sandbox
File: api/gpt.py (Python)

Code before:
def set_openai_key(key):
    """Sets OpenAI key."""
    openai.api_key = key

class Example():
    """Stores an input, output pair and formats it to prime the model."""

    def __init__(self, inp, out, input_prefix = "input: ", input_suffix = "\n", output_prefix = "output: ", output_suffix = "\n"):
        self.input = inp
        self.output = out
        self.input_prefix = input_prefix
        self.input_suffix = input_suffix
        self.output_prefix = output_prefix
        self.output_suffix = output_suffix

    def get_input(self):
        """Returns the input of the example."""
        return self.input

    def get_output(self):
        """Returns the intended output of the example."""
        return self.output

    def format(self):
        """Formats the input, output pair."""
        return self.input_prefix + f"{self.input}" + self.input_suffix + self.output_prefix + f"{self.output}\n"
        + self.output_suffix


class GPT:
    """The main class for a user to interface with the OpenAI API.
    A user can add examples and set parameters of the API request."""

    def __init__(self, engine='davinci',
                 temperature=0.5,
                 max_tokens=100,
                 input_prefix = "input: ",
                 input_suffix = "\n",
                 output_prefix = "output: ",
                 output_suffix = "\n",
                 append_input_suffix_and_output_prefix_to_query = False,
                 stop = "\ninput:"):
        self.examples = []
        self.engine = engine
        self.temperature = temperature
        self.max_tokens = max_tokens
        self.input_prefix = input_prefix
        self.input_suffix = input_suffix
        self.output_prefix = output_prefix
        self.output_suffix = output_suffix
        self.append_input_suffix_and_output_prefix_to_query = append_input_suffix_and_output_prefix_to_query

Review comment (suggestion):
This would have needed to be `self.input_prefix + self.input + self.input_suffix + self.output_prefix + self.output + self.output_suffix` (instead of the trailing `\n`)

Code after:
def set_openai_key(key):
    """Sets OpenAI key."""
    openai.api_key = key

class Example():
    """Stores an input, output pair it to prime the model."""

    def __init__(self, inp, out):
        self.input = inp
        self.output = out

    def get_input(self):
        """Returns the input of the example."""
        return self.input

    def get_output(self):
        """Returns the intended output of the example."""
        return self.output

class TextFormat():
    """Formats text by prefixing and suffixing with the provided parameters."""

    def __init__(self, prefix, suffix):
        self.prefix = prefix
        self.suffix = suffix

    def get_prefix(self):
        """Returns the prefix of this format."""
        return self.prefix

    def get_suffix(self):
        """Returns the suffix of this format."""
        return self.suffix

    def get_wrapped_text(self, text):
        """Formats the input, output pair."""
        return self.prefix + text + self.suffix

class QueryFormat():
    """Formats a query."""

    def __init__(self, input_format=TextFormat("input: ", "\n"),
                 output_format=TextFormat("output: ", "\n\n"),
                 append_output_prefix_to_query=False):
        self.input_format = input_format
        self.output_format = output_format
        self.append_output_prefix_to_query = append_output_prefix_to_query

        assert isinstance(input_format, TextFormat), \
        "Please create a TextFormat object."
        assert isinstance(output_format, TextFormat), \