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

Code before:
        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

    def get_temperature(self):
        """Returns the temperature specified for the API."""
        return self.temperature

    def get_max_tokens(self):
        """Returns the max tokens specified for the API."""
        return self.max_tokens

    def craft_query(self, prompt):

Review comment (bug):
Can you document or explain what this parameter, `append_input_suffix_and_output_prefix_to_query`,  could be used for? I understand why you may want the `output_prefix` to be optional in the priming text, but to format the priming properly, I think you may always need the `input_suffix` in the priming. But I could be wrong -- would love to hear your thoughts here!

Code after:

    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."""
        return self.input_format.get_wrapped_text(ex.get_input()) \
        + self.output_format.get_wrapped_text(ex.get_output())

    def get_stop_token(self):
        """Returns the stop token of this query format."""
        return (self.output_format.get_suffix() \
        + self.input_format.get_prefix()).strip()