Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved api logic into GPT class #16

Merged
merged 1 commit into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions api/GPT.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import openai

class Example():
def __init__(self, inp, out):
self.input = inp
Expand All @@ -16,6 +18,9 @@ def __init__(self, engine='davinci',
self.temperature=temperature
self.max_tokens=max_tokens

def set_openai_key(self, key):
openai.api_key=key

def add_example(self, ex):
assert isinstance(ex, Example), "Please create an Example object."
self.examples.append(ex.format())
Expand All @@ -34,3 +39,14 @@ def get_max_tokens(self):

def craft_query(self, prompt):
return self.get_prime_text() + "input: " + prompt + "\n"

def submit_request(self, prompt):
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="\ninput:")
return response
16 changes: 2 additions & 14 deletions api/demo_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@

from .UIConfig import UIConfig

def submit_openai_request(gpt, prompt):
response = openai.Completion.create(engine=gpt.get_engine(),
prompt=gpt.craft_query(prompt),
max_tokens=gpt.get_max_tokens(),
temperature=gpt.get_temperature(),
top_p=1,
n=1,
stream=False,
stop="\ninput:")
return response


def demo_web_app(gpt, config=UIConfig()):
app = Flask(__name__)
app.config.from_envvar('LATEX_TRANSLATOR_CONFIG')
CORS(app)
openai.api_key = app.config['OPENAI_KEY']
gpt.set_openai_key(app.config['OPENAI_KEY'])

@app.route("/params", methods=['GET'])
def get_params():
Expand All @@ -30,7 +18,7 @@ def get_params():
@app.route("/translate", methods=['GET', 'POST'])
def translate():
prompt = request.json['prompt']
response = submit_openai_request(gpt, prompt)
response = gpt.submit_request(prompt)
return {'text': response['choices'][0]['text'][7:]}

app.run()