Skip to content

Commit

Permalink
PR review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellavelle committed Jul 25, 2020
1 parent 0803f46 commit 94793eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
22 changes: 8 additions & 14 deletions api/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ def set_openai_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"):
def __init__(self, inp, out):
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."""
Expand All @@ -26,11 +22,6 @@ 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.
Expand All @@ -43,8 +34,7 @@ def __init__(self, engine='davinci',
input_suffix = "\n",
output_prefix = "output: ",
output_suffix = "\n",
append_input_suffix_and_output_prefix_to_query = False,
stop = "\ninput:"):
append_input_suffix_and_output_prefix_to_query = False):
self.examples = []
self.engine = engine
self.temperature = temperature
Expand All @@ -54,13 +44,13 @@ def __init__(self, engine='davinci',
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
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.append(ex.format())
self.examples.append(self.format_example(ex))

def get_prime_text(self):
"""Formats all examples to prime the model."""
Expand Down Expand Up @@ -102,3 +92,7 @@ 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']

def format_example(self, ex):
"""Formats the input, output pair."""
return self.input_prefix + ex.get_input() + self.input_suffix + self.output_prefix + ex.get_output() + self.output_suffix
21 changes: 10 additions & 11 deletions examples/run_latex_q_and_a_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
input_suffix = question_suffix,
output_prefix = answer_prefix,
output_suffix = answer_suffix,
append_input_suffix_and_output_prefix_to_query = True,
stop = "\nQ:")
append_input_suffix_and_output_prefix_to_query = True)

gpt.add_example(Example('Two plus two equals four', '2 + 2 = 4', question_prefix, question_suffix, answer_prefix, answer_suffix))
gpt.add_example(Example('Two plus two equals four', '2 + 2 = 4'))
gpt.add_example(
Example('The integral from zero to infinity', '\\int_0^{\\infty}',question_prefix, question_suffix, answer_prefix, answer_suffix))
Example('The integral from zero to infinity', '\\int_0^{\\infty}'))
gpt.add_example(Example(
'The gradient of x squared plus two times x with respect to x', '\\nabla_x x^2 + 2x',question_prefix, question_suffix, answer_prefix, answer_suffix))
gpt.add_example(Example('The log of two times x', '\\log{2x}',question_prefix, question_suffix, answer_prefix, answer_suffix))
'The gradient of x squared plus two times x with respect to x', '\\nabla_x x^2 + 2x'))
gpt.add_example(Example('The log of two times x', '\\log{2x}'))
gpt.add_example(
Example('x squared plus y squared plus equals z squared', 'x^2 + y^2 = z^2',question_prefix, question_suffix, answer_prefix, answer_suffix))
Example('x squared plus y squared plus equals z squared', 'x^2 + y^2 = z^2'))
gpt.add_example(
Example('The sum from zero to twelve of i squared', '\\sum_{i=0}^{12} i^2',question_prefix, question_suffix, answer_prefix, answer_suffix))
gpt.add_example(Example('E equals m times c squared', 'E = mc^2',question_prefix, question_suffix, answer_prefix, answer_suffix))
gpt.add_example(Example('H naught of t', 'H_0(t)',question_prefix, question_suffix, answer_prefix, answer_suffix))
Example('The sum from zero to twelve of i squared', '\\sum_{i=0}^{12} i^2'))
gpt.add_example(Example('E equals m times c squared', 'E = mc^2'))
gpt.add_example(Example('H naught of t', 'H_0(t)'))
gpt.add_example(Example('f of n equals 1 over (b-a) if n is 0 otherwise 5',
'f(n) = \\begin{cases} 1/(b-a) &\\mbox{if } n \\equiv 0 \\\ # 5 \\end{cases}',question_prefix, question_suffix, answer_prefix, answer_suffix))
'f(n) = \\begin{cases} 1/(b-a) &\\mbox{if } n \\equiv 0 \\\ # 5 \\end{cases}'))

# Define UI configuration
config = UIConfig(description="Text to equation",
Expand Down

0 comments on commit 94793eb

Please sign in to comment.