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

Code before:

    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):
        """Creates the query for the API request."""
        q = self.get_prime_text() + self.input_prefix + prompt
        if self.append_input_suffix_and_output_prefix_to_query:
            q = q + self.input_suffix + self.output_prefix

        return q

    def submit_request(self, prompt):
        """Calls the OpenAI API with the specified parameters."""
        response = openai.Completion.create(engine=self.get_engine(),
                                            prompt=self.craft_query(prompt),
                                            max_tokens=self.get_max_tokens(),
                                            temperature=self.get_temperature(),
                                            top_p=1,
                                            n=1,
                                            stream=False,
                                            stop=self.stop)
        return response

    def get_top_reply(self, prompt):
        """Obtains the best result as returned by the API."""
        response = self.submit_request(prompt)
        return response['choices'][0]['text']


Review comment (suggestion):
Need to have `q = self.get_prime_text() + self.input_prefix + prompt + self.input_suffix`, otherwise the examples are not backwards-compatible

Code after:

    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()


    def format_query(self, prime_text, prompt):
        """Creates the query for the API request."""
        query = prime_text + self.input_format.get_wrapped_text(prompt)
        if self.append_output_prefix_to_query:
            query = query + self.output_format.get_prefix()
        return query

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,
                 query_format=QueryFormat()):
        self.examples = []
        self.engine = engine
        self.temperature = temperature
        self.max_tokens = max_tokens
        self.query_format = query_format

        assert isinstance(query_format, QueryFormat), \
        "Please create a QueryFormat object."

    def add_example(self, ex):
        """Adds an example to the object. Example must be an instance
        of the Example class."""