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

Code before:
        """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
        self.stop = stop

    def add_example(self, ex):
        """Adds an example to the object. Example must be an instance
        of the Example class."""
        assert isinstance(ex, Example), "Please create an Example object."
        self.examples.append(ex.format())

    def get_prime_text(self):
        """Formats all examples to prime the model."""
        return '\n'.join(self.examples) + '\n'

    def get_engine(self):
        """Returns the engine specified for the API."""
        return self.engine


Review comment (suggestion):
You can probably take out the `stop` parameter and set `self.stop = (output_suffix + input_prefix).strip()`

Code after:
        """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), \
        "Please create a TextFormat object."

    def get_input_format(self):
        """Returns the example input format."""
        return self.input_format

    def get_output_format(self):
        """Returns the example output format."""
        return self.output_format

    def get_append_output_prefix_to_query(self):
        """Indicates whether the output prefix should be appended to the query."""
        return self.append_output_prefix_to_query

    def format_example(self, ex):
        """Formats the input, output pair."""