|
import anthropic |
|
import base64 |
|
import httpx |
|
import os |
|
import time |
|
from mimetypes import guess_type |
|
|
|
import random |
|
|
|
def seed_everything(seed): |
|
random.seed(seed) |
|
|
|
os.environ['PYTHONHASHSEED'] = str(seed) |
|
|
|
|
|
|
|
|
|
seed_everything(1) |
|
|
|
def local_image_to_data_url(image_path): |
|
mime_type, _ = guess_type(image_path) |
|
if mime_type is None: |
|
mime_type = 'application/octet-stream' |
|
with open(image_path, "rb") as image_file: |
|
base64_encoded_data = base64.b64encode(image_file.read()).decode('utf-8') |
|
return base64_encoded_data |
|
|
|
client = anthropic.Anthropic( |
|
api_key="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", |
|
) |
|
|
|
|
|
|
|
levels = [3,4,5,6,7,8] |
|
in_context_example_num = 0 |
|
|
|
if in_context_example_num > 0: |
|
output_path = "output/output_img_%d/"%(in_context_example_num) |
|
input_backup_path = "input/input_backup_img_%d/"%(in_context_example_num) |
|
else: |
|
output_path = "output/output_img/" |
|
input_backup_path = "input/input_backup_img/" |
|
|
|
os.makedirs(output_path, exist_ok=True) |
|
os.makedirs(input_backup_path, exist_ok=True) |
|
|
|
EXAMPLE_DICT = { |
|
3: [], |
|
4: [], |
|
5: [], |
|
6: [], |
|
7: [], |
|
8: [], |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import ipdb; ipdb.set_trace() |
|
for level in levels: |
|
os.makedirs(output_path + "level%d"%(level), exist_ok=True) |
|
os.makedirs(input_backup_path + "level%d"%(level), exist_ok=True) |
|
start_idx = 0 |
|
end_idx = 100 |
|
runned_term = 0 |
|
map_path = "../maps/level%d/img/"%(level) |
|
while True: |
|
try: |
|
curr_id = start_idx + runned_term |
|
if curr_id >= end_idx: |
|
break |
|
|
|
prompt_input_1 = ''' |
|
In this task, you will analyze a maze to determine if there is a hole in a specific position. |
|
The following figure illustrates the appearances of the player, holes, lands, and the goal within the maze. You will need to focus on the appearance of the hole. |
|
''' |
|
prompt_input_2 = ''' |
|
Here is an example to illustrate how to analyze and answer the question: |
|
''' |
|
prompt_input_3 = ''' |
|
Example question: Is there a hole in row 3, column 3? |
|
|
|
In this example: |
|
- We check the position in row 3, column 3. |
|
- According to the image, it is a land square. It does not contain a hole. |
|
- Therefore, you will output "<Output> No". |
|
|
|
Your output should be: "<Output> No" or "<Output> Yes", depending on whether there is a hole at the specified position. |
|
''' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prompt_input_4 = "\n\nNow you will analyze the following maze and answer the question: " |
|
with open("../maps/level%d/question/%d.txt"%(level, curr_id), "r") as f: |
|
prompt_input_5 = f.read() |
|
|
|
|
|
content_input_seq = [] |
|
content_input_seq.append({ |
|
"type": "text", |
|
"text": prompt_input_1, |
|
}) |
|
content_input_seq.append({ |
|
"type": "image", |
|
"source": { |
|
"type": "base64", |
|
"media_type": "image/png", |
|
"data": local_image_to_data_url("../prompt-visual-images/system-figure-1.png"), |
|
} |
|
}) |
|
content_input_seq.append({ |
|
"type": "text", |
|
"text": prompt_input_2, |
|
}) |
|
content_input_seq.append({ |
|
"type": "image", |
|
"source": { |
|
"type": "base64", |
|
"media_type": "image/png", |
|
"data": local_image_to_data_url("../prompt-visual-images/system-figure-2.png"), |
|
} |
|
}) |
|
content_input_seq.append({ |
|
"type": "text", |
|
"text": prompt_input_3, |
|
}) |
|
content_input_seq.append({ |
|
"type": "text", |
|
"text": prompt_input_4, |
|
}) |
|
content_input_seq.append({ |
|
"type": "image", |
|
"source": { |
|
"type": "base64", |
|
"media_type": "image/png", |
|
"data": local_image_to_data_url(map_path + "%d.png"%(curr_id)), |
|
} |
|
}) |
|
content_input_seq.append({ |
|
"type": "text", |
|
"text": prompt_input_5, |
|
}) |
|
response = client.messages.create( |
|
model="claude-3-sonnet-20240229", |
|
max_tokens=1024, |
|
system="You are a maze-solving agent playing a pixelated maze videogame.\nMazes are presented on grid maps, where each tile can be empty land, or contain a player, hole, or goal.", |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": content_input_seq |
|
}, |
|
], |
|
) |
|
with open(output_path + "level%d/%d.txt"%(level, curr_id), "w") as f: |
|
f.write(response.content[0].text) |
|
time.sleep(2) |
|
runned_term += 1 |
|
except: |
|
time.sleep(2) |
|
pass |
|
|
|
|