File size: 1,388 Bytes
596c225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# + tags=["hide_inp"]
desc = """
### Word Problem Solver

Chain that solves a math word problem by first generating and then running Python code. [[Code](https://github.com/srush/MiniChain/blob/main/examples/math.py)]

(Adapted from Dust [maths-generate-code](https://dust.tt/spolu/a/d12ac33169))
"""
# -

# $

from minichain import show, prompt, OpenAI, Python


@prompt(OpenAI(), template_file="math.pmpt.tpl")
def pal_prompt(model, question):
    return model(dict(question=question))


@prompt(Python())
def python(model, inp):
    return int(model(inp + "\nprint(solution())"))

def math_demo(question):
    return python(pal_prompt(question))

# $
    
# + tags=["hide_inp"]    
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?",
                        "Carla is downloading a 200 GB file. She can download 2 GB/minute, but 40% of the way through the download, the download fails. Then Carla has to restart the download from the beginning. How load did it take her to download the file in minutes?"],
              out_type="markdown",
              description=desc,
              code=open("math_demo.py", "r").read().split("$")[1].strip().strip("#").strip(),
              )
if __name__ == "__main__":
    gradio.launch()
# -