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

Code before:
        return f"input: {self.input}\noutput: {self.output}\n"


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

    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)

    def find_example(self, id):
        """ Returns the index of the example with specific id"""
        for i, example in enumerate(self.examples):
            if example.get_id() == id:
                return i

    def clear_example(self, id):
        """Clears example with the specific id"""
        index = self.find_example(id)
        if index is not None:
            del self.examples[index]

    def get_example(self, id):
        """ Get a single example """
        index = self.find_example(id)
        if index is not None:
            return self.examples[index]

    def get_all_examples(self):
        """ Returns all examples as a list of dicts"""
        return [i.as_dict() for i in self.examples]

    def get_prime_text(self):
        """Formats all examples to prime the model."""
        return '\n'.join([i.format() for i in self.examples]) + '\n'

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


Review comment (refactor):
Instead of keeping the examples as a list, change it to a dictionary of `{ id: Example }`. This will simplify the logic for getting and deleting examples 

Code after:


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\n",
                 append_output_prefix_to_query=False):
        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_output_prefix_to_query = append_output_prefix_to_query
        self.stop = (output_suffix + input_prefix).strip()

    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[ex.get_id()] = ex

    def delete_example(self, id):
        """Delete example with the specific id."""
        if id in self.examples:
            del self.examples[id]

    def get_example(self, id):
        """Get a single example."""
        return self.examples.get(id, None)

    def get_all_examples(self):
        """Returns all examples as a list of dicts."""
        return {k: v.as_dict() for k, v in self.examples.items()}

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