Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
anpl
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ziyuan Nan
anpl
Commits
dce34162
Commit
dce34162
authored
May 08, 2023
by
nzy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First release
parent
a75d7f80
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
54 deletions
+31
-54
anpl/synthesizer.py
+1
-3
readme.md
+12
-0
robotA.py
+6
-47
robotB.py
+1
-0
utils.py
+11
-4
No files found.
anpl/synthesizer.py
View file @
dce34162
...
@@ -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
...
...
readme.md
View file @
dce34162
# 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生成的字符,所以
...
...
robotA.py
View file @
dce34162
...
@@ -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
))
robotB.py
View file @
dce34162
...
@@ -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]"
)
...
...
utils.py
View file @
dce34162
...
@@ -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
):
text
=
Text
()
if
matrix
.
ndim
==
2
:
for
row
in
list
(
matrix
):
text
=
Text
()
for
pixel
in
row
:
for
row
in
list
(
matrix
):
for
pixel
in
row
:
text
.
append
(
"⬛"
,
style
=
colors
[
pixel
])
text
.
append
(
"
\n
"
)
elif
matrix
.
ndim
==
1
:
text
=
Text
()
for
pixel
in
list
(
matrix
):
text
.
append
(
"⬛"
,
style
=
colors
[
pixel
])
text
.
append
(
"⬛"
,
style
=
colors
[
pixel
])
text
.
append
(
"
\n
"
)
else
:
text
=
Text
(
str
(
matrix
))
return
text
return
text
def
rich_dumps
(
obj
):
def
rich_dumps
(
obj
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment