Commit dce34162 by nzy

First release

parent a75d7f80
...@@ -17,7 +17,7 @@ def is_valid_python_syntax(string): ...@@ -17,7 +17,7 @@ def is_valid_python_syntax(string):
pattern = re.compile(r"```python(.+?)```", flags=re.DOTALL) pattern = re.compile(r"```python(.+?)```", flags=re.DOTALL)
def extract_code(text: str): def extract_code(text: str):
print(text) # print(text)
codes =[match.strip() for match in re.findall(pattern, text)] codes =[match.strip() for match in re.findall(pattern, text)]
if len(codes) > 0: if len(codes) > 0:
code = "\n".join(codes) code = "\n".join(codes)
...@@ -29,8 +29,6 @@ def collect(res: dict): return [extract_code(choice["message"]["content"]) for c ...@@ -29,8 +29,6 @@ def collect(res: dict): return [extract_code(choice["message"]["content"]) for c
def raw_query(msgs: list[dict], retries=3, **config) -> list[Optional[str]]: def raw_query(msgs: list[dict], retries=3, **config) -> list[Optional[str]]:
for i in range(retries): for i in range(retries):
print("retries")
print(i)
try: try:
res = openai.ChatCompletion.create(messages=msgs, **config) res = openai.ChatCompletion.create(messages=msgs, **config)
return res return res
......
# ANPL - A Natural Programming Language # ANPL - A Natural Programming Language
LARC地址: https://github.com/samacqua/LARC
## requirements ## requirements
本项目需要`Python >= 3.9`(因为用到了ast.unparse, 辛苦大家装一下环境 TAT) 本项目需要`Python >= 3.9`(因为用到了ast.unparse, 辛苦大家装一下环境 TAT)
...@@ -69,6 +71,12 @@ robotA主要有4种命令(比开会说的新增了一种!) ...@@ -69,6 +71,12 @@ robotA主要有4种命令(比开会说的新增了一种!)
**第一次编程很重要**!请在第一次编程前思考清楚,因为第一次交互提供的信息最多,会极大的影响后续结果。 **第一次编程很重要**!请在第一次编程前思考清楚,因为第一次交互提供的信息最多,会极大的影响后续结果。
### 我想做...
- 如果想修改自然语言描述怎么办?请edit对应的函数。可以不写函数体,但是必须写docstring。
- 函数名就不对怎么办?没办法,GG。
- 我已经有许多正确的子函数了,突然发现最开始的main函数写错了怎么办?直接edit main函数,在edit结束之前,系统不会删除任何已经生成的函数。
## robotB ## robotB
只是一个聊天机器人🤖,为了公平比较才做的。包含了四个命令。 只是一个聊天机器人🤖,为了公平比较才做的。包含了四个命令。
...@@ -86,6 +94,10 @@ robotA主要有4种命令(比开会说的新增了一种!) ...@@ -86,6 +94,10 @@ robotA主要有4种命令(比开会说的新增了一种!)
**第一次对话很重要**!请在第一次对话前思考清楚,第一次对话应该包含能够解出这道题目的所有信息。 **第一次对话很重要**!请在第一次对话前思考清楚,第一次对话应该包含能够解出这道题目的所有信息。
## 错误处理
ratelimit error,是api key的问题。说明请求过多,请等等再用,或者联系管理员。
## 注意事项 ## 注意事项
众所周知,把程序当作字符串玩儿是一件很危险的事情。再加上是chatgpt生成的字符,所以 众所周知,把程序当作字符串玩儿是一件很危险的事情。再加上是chatgpt生成的字符,所以
......
...@@ -4,56 +4,16 @@ from utils import sys_str, system_info, code_input, value_input, print_anpl, pri ...@@ -4,56 +4,16 @@ from utils import sys_str, system_info, code_input, value_input, print_anpl, pri
from copy import deepcopy from copy import deepcopy
import time import time
from anpl.anpl import IOExample, ANPL from anpl.anpl import IOExample, ANPL
from anpl.parser import ANPLParser from anpl.parser import ANPLParser
from anpl.synthesizer import fun_synthesis, batch_fun_synthesis from anpl.synthesizer import fun_synthesis, batch_fun_synthesis
from anpl.tracer import anpl_check, anpl_trace from anpl.tracer import anpl_check, anpl_trace
hole0_str = """\
def find_smallest_repeating_unit(input_grid: np.ndarray) -> np.ndarray:
for row in range(1, input_grid.shape[0]):
if np.array_equal(input_grid[:row, :], input_grid[row:row*2, :]):
return input_grid[:row, :]
return input_grid
"""
hole1_str = """\
def copy_repeating_unit(unit):
return np.repeat(matrix, 2, axis=0)
"""
hole2_str = """\
def replace_pixels(output_grid):
output_grid[output_grid == blue] = yellow
return output_grid
"""
re_hole1_0_str = """\
def copy_repeating_unit(unit):
raise NotImplementedError
"""
re_hole1_1_str = """\
def copy_repeating_unit(unit):
output_grid = np.empty((9, 3), dtype=unit.dtype)
num_repeats = output_grid.shape[0] // unit.shape[0]
remainder = output_grid.shape[0] % unit.shape[0]
for i in range(num_repeats):
output_grid[i * unit.shape[0]:(i + 1) * unit.shape[0], :] = unit
if remainder:
output_grid[num_repeats * unit.shape[0]:, :] = unit[:remainder, :]
return output_grid
"""
fake_reses = [hole0_str, hole1_str, hole2_str, [re_hole1_0_str, re_hole1_1_str]]
idx = 0
set_openai_key() set_openai_key()
task_id, logger, input_grid, output_grid = select_task() task_id, logger, input_grid, output_grid = select_task()
logger.log("system", "intro", "a") logger.log("system", "intro", "a")
parser = ANPLParser() parser = ANPLParser()
anpl = code_input(parser, logger) anpl = code_input(parser, logger)
...@@ -61,11 +21,7 @@ def syn_anpl(anpl: ANPL): ...@@ -61,11 +21,7 @@ def syn_anpl(anpl: ANPL):
holes = anpl.get_holes() holes = anpl.get_holes()
for hole in track(holes, description="Synthesizing..."): for hole in track(holes, description="Synthesizing..."):
for i in range(5): for i in range(5):
print(i)
res = fun_synthesis(anpl, hole, temp=i*0.1) res = fun_synthesis(anpl, hole, temp=i*0.1)
# global idx
# res = fake_reses[idx]
# idx += 1
logger.log("gpt", "syn code", res) logger.log("gpt", "syn code", res)
if res: if res:
newanpl = parser.try_parse(res, from_user=False) newanpl = parser.try_parse(res, from_user=False)
...@@ -155,13 +111,12 @@ while not is_correct: ...@@ -155,13 +111,12 @@ while not is_correct:
find_correct_anpl = False find_correct_anpl = False
raw_test_anpl = deepcopy(anpl) raw_test_anpl = deepcopy(anpl)
raw_test_anpl.funs[fun_name].reset_to_hole() raw_test_anpl.funs[fun_name].reset_to_hole()
reses = batch_fun_synthesis(test_anpl, fun_name, 10, 0.8) # The same config as CodeT reses = batch_fun_synthesis(raw_test_anpl, fun_name, 10, 0.8) # The same config as CodeT
# reses = fake_reses[3]
for res in track(reses, description="Checking"): for res in track(reses, description="Checking"):
if res is None: if res is None:
logger.log("gpt", "resyn", "nothing") logger.log("gpt", "resyn", "nothing")
continue continue
logger.log("gpt", "resyn", "res") logger.log("gpt", "resyn", res)
newanpl = parser.try_parse(res, from_user=False) newanpl = parser.try_parse(res, from_user=False)
if newanpl is None: if newanpl is None:
logger.log("system", "resyn_err", "gpt return wrong python") logger.log("system", "resyn_err", "gpt return wrong python")
...@@ -203,6 +158,10 @@ while not is_correct: ...@@ -203,6 +158,10 @@ while not is_correct:
if is_correct: if is_correct:
system_info("[green]ANPL CORRECT[/green], and here is the code") system_info("[green]ANPL CORRECT[/green], and here is the code")
print_anpl(anpl, for_user=False) print_anpl(anpl, for_user=False)
import pickle
time_str = time.strftime("%Y%m%d_%H%M%S")
with open(f"./log/task{task_id}_{time_str}.pkl", "wb") as f:
pickle.dump(anpl, f)
else: else:
system_info("Good luck next time.") system_info("Good luck next time.")
logger.log("system", "exit", str(is_correct)) logger.log("system", "exit", str(is_correct))
...@@ -74,6 +74,7 @@ while not is_correct: ...@@ -74,6 +74,7 @@ while not is_correct:
logger.log("system", "check", f"crash: {e}") logger.log("system", "check", f"crash: {e}")
system_info("[red]Crash[/red]") system_info("[red]Crash[/red]")
print(e) print(e)
continue
if np.array_equal(out, real_out): if np.array_equal(out, real_out):
logger.log("system", "check", f"correct") logger.log("system", "check", f"correct")
system_info("[green] Code CORRECT [/green]") system_info("[green] Code CORRECT [/green]")
......
...@@ -14,11 +14,18 @@ colors = ["#000000", "#0000FF", "#FF0000", "#008000", "#FFFF00", ...@@ -14,11 +14,18 @@ colors = ["#000000", "#0000FF", "#FF0000", "#008000", "#FFFF00",
"#808080", "#FFC0CB", "#FFA500", "#008080", "#800000"] "#808080", "#FFC0CB", "#FFA500", "#008080", "#800000"]
def array_to_str(matrix): def array_to_str(matrix):
if matrix.ndim == 2:
text = Text() text = Text()
for row in list(matrix): for row in list(matrix):
for pixel in row: for pixel in row:
text.append("⬛", style=colors[pixel]) text.append("⬛", style=colors[pixel])
text.append("\n") text.append("\n")
elif matrix.ndim == 1:
text = Text()
for pixel in list(matrix):
text.append("⬛", style=colors[pixel])
else:
text = Text(str(matrix))
return text return text
def rich_dumps(obj): def rich_dumps(obj):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment