File size: 2,569 Bytes
9902a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import openai
import gradio as gr
from langchain import OpenAI
from langchain.chains import PALChain
import datetime

gpt_only_prompt = "Calculate the following, giving only the final answer:\n"
prompt = ""

def openai_create(prompt):
    print("prompt: " + prompt)

    # We use temperature of 0.0 because it gives the most predictable, factual answer (i.e. avoids hallucination).
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=prompt,
        temperature=0.0,
        max_tokens=300,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )

    return response.choices[0].text


def calc_gpt_only(math_problem):
    answer = openai_create(gpt_only_prompt + math_problem + "\n")

    print("math problem: " + math_problem)
    print("calc_gpt_only answer: " + answer)

    html = "<pre>" + answer + "</pre>"
    return html


def calc_gpt_pal(math_problem):
    llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)
    pal_chain = PALChain.from_math_prompt(llm, verbose=True)
    answer = pal_chain.run(math_problem)

    print("math problem: " + math_problem)
    print("calc_gpt_pal answer: " + answer)

    html = "<pre>" + answer + "</pre>"
    return html


block = gr.Blocks(css=".gradio-container {background-color: lightgray}")

with block:
    title = gr.Markdown("""<h3><center>Comparing GPT math results</center></h3>""")
    answer_html = gr.Markdown()

    request = gr.Textbox(label="Math question:",
                         placeholder="Ex: What is the sum of the first 10 prime numbers?")
    with gr.Row():
        gpt_only = gr.Button(value="GPT Only", variant="secondary").style(full_width=False)
        gpt_pal = gr.Button(value="GPT w/PAL", variant="secondary").style(full_width=False)

    gr.Examples(
        examples=["42 times 81",
                  "Olivia has $23. She bought five bagels for $3 each. How much money does she have left?",
                  "What is the sum of the first 21 Fibonacci numbers?",
                  "Jane quit her job on Mar 20, 2020. 176 days have passed since then. What is the date tomorrow in MM/DD/YYYY?"],
        inputs=request
    )

    gr.HTML("See <a href='https://reasonwithpal.com/'>PAL: Program-aided Language Models</a>")

    gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")

    gpt_only.click(calc_gpt_only, inputs=[request], outputs=[answer_html])
    gpt_pal.click(calc_gpt_pal, inputs=[request], outputs=[answer_html])

block.launch()