# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/11-mind.ipynb (unless otherwise specified).

__all__ = []

# Internal Cell
from flask import Flask, request
app = Flask(__name__)

# Internal Cell
import openai

# Internal Cell
prompt = ''
response = ''

def test_completion(mock_openai):
    res = openai.Completion.create(
        engine = 'text-davinci-003',
        prompt = f'{prompt}\n',
        max_tokens = 1024,
        temperature = 0,
        top_p = 0.9,
        frequency_penalty = 0,
        presence_penalty = 0,
        stop = None,
    )
    global response
    response = res.get('choices', [{}])[0].get('text', '').strip()
    assert True

# Internal Cell
@app.route('/openai')
def answer():
    global prompt
    prompt = request.args.get('q')
    test_completion(None)
    return response

# Internal Cell
@app.route('/')
def free_answer():
    global prompt
    prompt = request.args.get('q')
    import ipymock
    import ipymock.browser
    ipymock.do(
        mock_openai = ipymock.browser.mock_openai,
        test_completion = test_completion,
    )
    return response

# Internal Cell
if __name__ == '__main__':
    app.run()