|
|
|
desc = """ |
|
### Word Problem Solver |
|
|
|
Chain that solves a math word problem by first generating and then running Python code. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/srush/MiniChain/blob/master/examples/math_demo.ipynb) |
|
|
|
(Adapted from Dust [maths-generate-code](https://dust.tt/spolu/a/d12ac33169)) |
|
""" |
|
|
|
|
|
|
|
|
|
from minichain import show, prompt, OpenAI, Python, GradioConf |
|
import gradio as gr |
|
|
|
|
|
@prompt(OpenAI(), template_file="math.pmpt.tpl", |
|
gradio_conf=GradioConf(block_input=gr.Markdown)) |
|
def math_prompt(model, question |
|
): |
|
"Prompt to call GPT with a Jinja template" |
|
return model(dict(question=question)) |
|
|
|
@prompt(Python(), template="import math\n{{code}}") |
|
def python(model, code): |
|
"Prompt to call Python interpreter" |
|
code = "\n".join(code.strip().split("\n")[1:-1]) |
|
return model(dict(code=code)) |
|
|
|
def math_demo(question): |
|
"Chain them together" |
|
return python(math_prompt(question)) |
|
|
|
|
|
|
|
|
|
gradio = show(math_demo, |
|
examples=["What is the sum of the powers of 3 (3^i) that are smaller than 100?", |
|
"What is the sum of the 10 first positive integers?",], |
|
|
|
subprompts=[math_prompt, python], |
|
description=desc, |
|
code=open("math_demo.py", "r").read().split("$")[1].strip().strip("#").strip(), |
|
) |
|
if __name__ == "__main__": |
|
gradio.queue().launch() |
|
|
|
|
|
|