JLW's picture
Add some descriptive text to the UI
4605466
raw
history blame
2.99 kB
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("""
This simple app demonstrates a couple of techniques for using GPT-3 to solve math problems.
The first technique is to simply ask GPT-3 to solve the problem. The second technique is to use
GPT-3 to interpret the problem and then create/run a Python program to solve it. The program is
generated using the PALChain from the <a href='https://github.com/hwchase17/langchain'>LangChain</a> library.
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()