Last active
April 24, 2023 17:57
-
-
Save yuchenlin/8cfe6115f05dd5a72e856039f9db5800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Decompose and memorize by program, only asking LLMs to do low-level computations. | |
""" | |
import openai | |
import re | |
import math | |
from tenacity import ( | |
retry, | |
stop_after_attempt, | |
wait_random_exponential, | |
) # for exponential backoff | |
@retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(10)) | |
def completion_with_backoff(**kwargs): | |
return openai.ChatCompletion.create(**kwargs) | |
def extract_last_num(input_str): | |
pattern = r'\d+' | |
nums = re.findall(pattern, input_str) | |
if len(nums) > 0: | |
return nums[-1] | |
else: | |
return None | |
def call_gpt(input_text, model="gpt-3.5-turbo", history=[]): | |
assert openai.api_key is not None | |
messages = [] | |
for h in history: | |
messages.append({"role": "user", "content": str(h[0])}) | |
messages.append({"role": "assistant", "content": str(h[1])}) | |
messages.append( | |
{"role": "user", "content": input_text} | |
) | |
completion = completion_with_backoff( | |
model=model, | |
messages=messages, | |
n=1, temperature=0, top_p=1, | |
) | |
result = completion.choices[0].message["content"].strip().replace(",", "") | |
return extract_last_num(result) | |
def gpt_multiplication(a, b): | |
print("-"*100) | |
print(f"Computing: {a}*{b}") | |
correct = int(a)*int(b) | |
sub_problems = [] | |
for i, d1 in enumerate(a[::-1]): | |
n = int(d1) * int("1"+"".join(["0"]*i)) | |
for j, d2 in enumerate(b[::-1]): | |
m = int(d2) * int("1"+"".join(["0"]*j)) | |
p = f"{n}*{m}=?" | |
# print(p) | |
sub_problems.append(p) | |
assert sum([eval(sub_p.replace("=?", "")) for sub_p in sub_problems]) == int(correct) | |
results = [] | |
history = [("900*12345=?", "11110500")] | |
for sub_p in sub_problems[::-1]: | |
# history.append([sub_p, r]) | |
truth = eval(sub_p.replace("=?", "")) | |
r = call_gpt(sub_p, history=history) | |
# r = str(truth) | |
print(f"GPT: {sub_p} --> {r}", truth==int(r)) | |
results.append(r) | |
assert sum([int(r) for r in results]) == int(correct) | |
history = [("123456+789=?", "124245")] | |
s = results[0] | |
for r in results[1:]: | |
num_common_zeros = 0 | |
min_ = min(len(s), len(r)) | |
for i in range(1, min_+1): | |
if r[-i] =="0" and s[-i] == "0": | |
num_common_zeros += 1 | |
else: | |
break | |
# history.append([sub_p, new_s]) | |
if num_common_zeros > 0 and min_ > 1: | |
ss = s[:-num_common_zeros] | |
rr = r[:-num_common_zeros] | |
else: | |
ss = s | |
rr = r | |
p = f"{ss}+{rr}=?" | |
truth = int(ss) + int(rr) | |
# new_s = str(truth) | |
new_s = call_gpt(p, history=history) | |
print(f"GPT: {p} ({s} + {r}) --> {new_s} (+{'0'*num_common_zeros})", int(truth)==int(new_s)) | |
s = new_s + "0"*num_common_zeros | |
print("Finall result:", s, "Truth:", correct, "Same:", int(correct)==int(s)) | |
return correct==s | |
gpt_multiplication(a = "13122", b = "12344") | |
gpt_multiplication(a = "78954", b = "67934") | |
gpt_multiplication(a = "54968", b = "45648") | |
gpt_multiplication(a = "389683", b = "12344") | |
# gpt_multiplication(a = "919293", b = "12344") | |
# gpt_multiplication(a = "389683", b = "834234") | |
""" | |
---------------------------------------------------------------------------------------------------- | |
Computing: 13122*12344 | |
GPT: 10000*10000=? --> 100000000 True | |
GPT: 10000*2000=? --> 20000000 True | |
GPT: 10000*300=? --> 3000000 True | |
GPT: 10000*40=? --> 400000 True | |
GPT: 10000*4=? --> 40000 True | |
GPT: 3000*10000=? --> 30000000 True | |
GPT: 3000*2000=? --> 6000000 True | |
GPT: 3000*300=? --> 900000 True | |
GPT: 3000*40=? --> 120000 True | |
GPT: 3000*4=? --> 12000 True | |
GPT: 100*10000=? --> 1000000 True | |
GPT: 100*2000=? --> 200000 True | |
GPT: 100*300=? --> 30000 True | |
GPT: 100*40=? --> 4000 True | |
GPT: 100*4=? --> 400 True | |
GPT: 20*10000=? --> 200000 True | |
GPT: 20*2000=? --> 40000 True | |
GPT: 20*300=? --> 6000 True | |
GPT: 20*40=? --> 800 True | |
GPT: 20*4=? --> 80 True | |
GPT: 2*10000=? --> 20000 True | |
GPT: 2*2000=? --> 4000 True | |
GPT: 2*300=? --> 600 True | |
GPT: 2*40=? --> 80 True | |
GPT: 2*4=? --> 8 True | |
GPT: 10+2=? (100000000 + 20000000) --> 12 (+0000000) True | |
GPT: 120+3=? (120000000 + 3000000) --> 123 (+000000) True | |
GPT: 1230+4=? (123000000 + 400000) --> 1234 (+00000) True | |
GPT: 12340+4=? (123400000 + 40000) --> 12344 (+0000) True | |
GPT: 12344+3000=? (123440000 + 30000000) --> 15344 (+0000) True | |
GPT: 15344+600=? (153440000 + 6000000) --> 15944 (+0000) True | |
GPT: 15944+90=? (159440000 + 900000) --> 16034 (+0000) True | |
GPT: 16034+12=? (160340000 + 120000) --> 16046 (+0000) True | |
GPT: 160460+12=? (160460000 + 12000) --> 160472 (+000) True | |
GPT: 160472+1000=? (160472000 + 1000000) --> 161472 (+000) True | |
GPT: 161472+200=? (161472000 + 200000) --> 161672 (+000) True | |
GPT: 161672+30=? (161672000 + 30000) --> 161702 (+000) True | |
GPT: 161702+4=? (161702000 + 4000) --> 161706 (+000) True | |
GPT: 1617060+4=? (161706000 + 400) --> 1617064 (+00) True | |
GPT: 1617064+2000=? (161706400 + 200000) --> 1619064 (+00) True | |
GPT: 1619064+400=? (161906400 + 40000) --> 1619464 (+00) True | |
GPT: 1619464+60=? (161946400 + 6000) --> 1619524 (+00) True | |
GPT: 1619524+8=? (161952400 + 800) --> 1619532 (+00) True | |
GPT: 16195320+8=? (161953200 + 80) --> 16195328 (+0) True | |
GPT: 16195328+2000=? (161953280 + 20000) --> 16197328 (+0) True | |
GPT: 16197328+400=? (161973280 + 4000) --> 16197728 (+0) True | |
GPT: 16197728+60=? (161977280 + 600) --> 16197788 (+0) True | |
GPT: 16197788+8=? (161977880 + 80) --> 16197796 (+0) True | |
GPT: 161977960+8=? (161977960 + 8) --> 161977968 (+) True | |
Finall result: 161977968 Truth: 161977968 Same: True | |
---------------------------------------------------------------------------------------------------- | |
Computing: 78954*67934 | |
GPT: 70000*60000=? --> 4200000000 True | |
GPT: 70000*7000=? --> 490000000 True | |
GPT: 70000*900=? --> 63000000 True | |
GPT: 70000*30=? --> 2100000 True | |
GPT: 70000*4=? --> 280000 True | |
GPT: 8000*60000=? --> 480000000 True | |
GPT: 8000*7000=? --> 56000000 True | |
GPT: 8000*900=? --> 7200000 True | |
GPT: 8000*30=? --> 240000 True | |
GPT: 8000*4=? --> 32000 True | |
GPT: 900*60000=? --> 54000000 True | |
GPT: 900*7000=? --> 6300000 True | |
GPT: 900*900=? --> 810000 True | |
GPT: 900*30=? --> 27000 True | |
GPT: 900*4=? --> 3600 True | |
GPT: 50*60000=? --> 3000000 True | |
GPT: 50*7000=? --> 350000 True | |
GPT: 50*900=? --> 45000 True | |
GPT: 50*30=? --> 1500 True | |
GPT: 50*4=? --> 200 True | |
GPT: 4*60000=? --> 240000 True | |
GPT: 4*7000=? --> 28000 True | |
GPT: 4*900=? --> 3600 True | |
GPT: 4*30=? --> 120 True | |
GPT: 4*4=? --> 16 True | |
GPT: 420+49=? (4200000000 + 490000000) --> 469 (+0000000) True | |
GPT: 4690+63=? (4690000000 + 63000000) --> 4753 (+000000) True | |
GPT: 47530+21=? (4753000000 + 2100000) --> 47551 (+00000) True | |
GPT: 475510+28=? (4755100000 + 280000) --> 475538 (+0000) True | |
GPT: 475538+48000=? (4755380000 + 480000000) --> 523538 (+0000) True | |
GPT: 523538+5600=? (5235380000 + 56000000) --> 529138 (+0000) True | |
GPT: 529138+720=? (5291380000 + 7200000) --> 529858 (+0000) True | |
GPT: 529858+24=? (5298580000 + 240000) --> 529882 (+0000) True | |
GPT: 5298820+32=? (5298820000 + 32000) --> 5298852 (+000) True | |
GPT: 5298852+54000=? (5298852000 + 54000000) --> 5352852 (+000) True | |
GPT: 5352852+6300=? (5352852000 + 6300000) --> 5359152 (+000) True | |
GPT: 5359152+810=? (5359152000 + 810000) --> 5359962 (+000) True | |
GPT: 5359962+27=? (5359962000 + 27000) --> 5359989 (+000) True | |
GPT: 53599890+36=? (5359989000 + 3600) --> 53599926 (+00) True | |
GPT: 53599926+30000=? (5359992600 + 3000000) --> 53629926 (+00) True | |
GPT: 53629926+3500=? (5362992600 + 350000) --> 53633426 (+00) True | |
GPT: 53633426+450=? (5363342600 + 45000) --> 53633876 (+00) True | |
GPT: 53633876+15=? (5363387600 + 1500) --> 53633891 (+00) True | |
GPT: 53633891+2=? (5363389100 + 200) --> 53633893 (+00) True | |
GPT: 53633893+2400=? (5363389300 + 240000) --> 53636293 (+00) True | |
GPT: 53636293+280=? (5363629300 + 28000) --> 53636573 (+00) True | |
GPT: 53636573+36=? (5363657300 + 3600) --> 53636609 (+00) True | |
GPT: 536366090+12=? (5363660900 + 120) --> 536366102 (+0) True | |
GPT: 5363661020+16=? (5363661020 + 16) --> 5363661036 (+) True | |
Finall result: 5363661036 Truth: 5363661036 Same: True | |
---------------------------------------------------------------------------------------------------- | |
Computing: 54968*45648 | |
GPT: 50000*40000=? --> 2000000000 True | |
GPT: 50000*5000=? --> 250000000 True | |
GPT: 50000*600=? --> 30000000 True | |
GPT: 50000*40=? --> 2000000 True | |
GPT: 50000*8=? --> 400000 True | |
GPT: 4000*40000=? --> 160000000 True | |
GPT: 4000*5000=? --> 20000000 True | |
GPT: 4000*600=? --> 2400000 True | |
GPT: 4000*40=? --> 160000 True | |
GPT: 4000*8=? --> 32000 True | |
GPT: 900*40000=? --> 36000000 True | |
GPT: 900*5000=? --> 4500000 True | |
GPT: 900*600=? --> 540000 True | |
GPT: 900*40=? --> 36000 True | |
GPT: 900*8=? --> 7200 True | |
GPT: 60*40000=? --> 2400000 True | |
GPT: 60*5000=? --> 300000 True | |
GPT: 60*600=? --> 36000 True | |
GPT: 60*40=? --> 2400 True | |
GPT: 60*8=? --> 480 True | |
GPT: 8*40000=? --> 320000 True | |
GPT: 8*5000=? --> 40000 True | |
GPT: 8*600=? --> 4800 True | |
GPT: 8*40=? --> 320 True | |
GPT: 8*8=? --> 64 True | |
GPT: 200+25=? (2000000000 + 250000000) --> 225 (+0000000) True | |
GPT: 225+3=? (2250000000 + 30000000) --> 228 (+0000000) True | |
GPT: 2280+2=? (2280000000 + 2000000) --> 2282 (+000000) True | |
GPT: 22820+4=? (2282000000 + 400000) --> 22824 (+00000) True | |
GPT: 22824+1600=? (2282400000 + 160000000) --> 24424 (+00000) True | |
GPT: 24424+200=? (2442400000 + 20000000) --> 24624 (+00000) True | |
GPT: 24624+24=? (2462400000 + 2400000) --> 24648 (+00000) True | |
GPT: 246480+16=? (2464800000 + 160000) --> 246496 (+0000) True | |
GPT: 2464960+32=? (2464960000 + 32000) --> 2464992 (+000) True | |
GPT: 2464992+36000=? (2464992000 + 36000000) --> 2500992 (+000) True | |
GPT: 2500992+4500=? (2500992000 + 4500000) --> 2505492 (+000) True | |
GPT: 2505492+540=? (2505492000 + 540000) --> 2506032 (+000) True | |
GPT: 2506032+36=? (2506032000 + 36000) --> 2506068 (+000) True | |
GPT: 25060680+72=? (2506068000 + 7200) --> 25060752 (+00) True | |
GPT: 25060752+24000=? (2506075200 + 2400000) --> 25084752 (+00) True | |
GPT: 25084752+3000=? (2508475200 + 300000) --> 25087752 (+00) True | |
GPT: 25087752+360=? (2508775200 + 36000) --> 25088112 (+00) True | |
GPT: 25088112+24=? (2508811200 + 2400) --> 25088136 (+00) True | |
GPT: 250881360+48=? (2508813600 + 480) --> 250881408 (+0) True | |
GPT: 250881408+32000=? (2508814080 + 320000) --> 250913408 (+0) True | |
GPT: 250913408+4000=? (2509134080 + 40000) --> 250917408 (+0) True | |
GPT: 250917408+480=? (2509174080 + 4800) --> 250917888 (+0) True | |
GPT: 250917888+32=? (2509178880 + 320) --> 250917920 (+0) True | |
GPT: 2509179200+64=? (2509179200 + 64) --> 2509179264 (+) True | |
Finall result: 2509179264 Truth: 2509179264 Same: True | |
---------------------------------------------------------------------------------------------------- | |
Computing: 389683*12344 | |
GPT: 300000*10000=? --> 3000000000 True | |
GPT: 300000*2000=? --> 600000000 True | |
GPT: 300000*300=? --> 90000000 True | |
GPT: 300000*40=? --> 12000000 True | |
GPT: 300000*4=? --> 1200000 True | |
GPT: 80000*10000=? --> 800000000 True | |
GPT: 80000*2000=? --> 160000000 True | |
GPT: 80000*300=? --> 24000000 True | |
GPT: 80000*40=? --> 3200000 True | |
GPT: 80000*4=? --> 320000 True | |
GPT: 9000*10000=? --> 90000000 True | |
GPT: 9000*2000=? --> 18000000 True | |
GPT: 9000*300=? --> 2700000 True | |
GPT: 9000*40=? --> 360000 True | |
GPT: 9000*4=? --> 36000 True | |
GPT: 600*10000=? --> 6000000 True | |
GPT: 600*2000=? --> 1200000 True | |
GPT: 600*300=? --> 180000 True | |
GPT: 600*40=? --> 24000 True | |
GPT: 600*4=? --> 2400 True | |
GPT: 80*10000=? --> 800000 True | |
GPT: 80*2000=? --> 160000 True | |
GPT: 80*300=? --> 24000 True | |
GPT: 80*40=? --> 3200 True | |
GPT: 80*4=? --> 320 True | |
GPT: 3*10000=? --> 30000 True | |
GPT: 3*2000=? --> 6000 True | |
GPT: 3*300=? --> 900 True | |
GPT: 3*40=? --> 120 True | |
GPT: 3*4=? --> 12 True | |
GPT: 30+6=? (3000000000 + 600000000) --> 36 (+00000000) True | |
GPT: 360+9=? (3600000000 + 90000000) --> 369 (+0000000) True | |
GPT: 3690+12=? (3690000000 + 12000000) --> 3702 (+000000) True | |
GPT: 37020+12=? (3702000000 + 1200000) --> 37032 (+00000) True | |
GPT: 37032+8000=? (3703200000 + 800000000) --> 45032 (+00000) True | |
GPT: 45032+1600=? (4503200000 + 160000000) --> 46632 (+00000) True | |
GPT: 46632+240=? (4663200000 + 24000000) --> 46872 (+00000) True | |
GPT: 46872+32=? (4687200000 + 3200000) --> 46904 (+00000) True | |
GPT: 469040+32=? (4690400000 + 320000) --> 469072 (+0000) True | |
GPT: 469072+9000=? (4690720000 + 90000000) --> 478072 (+0000) True | |
GPT: 478072+1800=? (4780720000 + 18000000) --> 479872 (+0000) True | |
GPT: 479872+270=? (4798720000 + 2700000) --> 480142 (+0000) True | |
GPT: 480142+36=? (4801420000 + 360000) --> 480178 (+0000) True | |
GPT: 4801780+36=? (4801780000 + 36000) --> 4801816 (+000) True | |
GPT: 4801816+6000=? (4801816000 + 6000000) --> 4807816 (+000) True | |
GPT: 4807816+1200=? (4807816000 + 1200000) --> 4809016 (+000) True | |
GPT: 4809016+180=? (4809016000 + 180000) --> 4809196 (+000) True | |
GPT: 4809196+24=? (4809196000 + 24000) --> 4809220 (+000) True | |
GPT: 48092200+24=? (4809220000 + 2400) --> 48092224 (+00) True | |
GPT: 48092224+8000=? (4809222400 + 800000) --> 48000224 (+00) False | |
GPT: 48000224+1600=? (4800022400 + 160000) --> 48001824 (+00) True | |
GPT: 48001824+240=? (4800182400 + 24000) --> 48002064 (+00) True | |
GPT: 48002064+32=? (4800206400 + 3200) --> 48002096 (+00) True | |
GPT: 480020960+32=? (4800209600 + 320) --> 480020992 (+0) True | |
GPT: 480020992+3000=? (4800209920 + 30000) --> 480023992 (+0) True | |
GPT: 480023992+600=? (4800239920 + 6000) --> 480024592 (+0) True | |
GPT: 480024592+90=? (4800245920 + 900) --> 480024682 (+0) True | |
GPT: 480024682+12=? (4800246820 + 120) --> 480024694 (+0) True | |
GPT: 4800246940+12=? (4800246940 + 12) --> 4800246952 (+) True | |
Finall result: 4800246952 Truth: 4810246952 Same: False | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment