"""Model-independent semantic prompts and state contracts. No labels are accepted."""
import hashlib,json,re
from pathlib import Path
PROTOCOL=json.loads(Path(__file__).with_name('protocol.json').read_text())
SYSTEM='''You control a camera to search for the object specified by the instruction. Center the requested object in the current view, then submit the current camera pose.
Actions:
rotate(dyaw,dpitch): rotate relative to the CURRENT pose, using integer degrees. Positive yaw turns right; positive pitch looks up. dyaw must be in [-180,180], dpitch in [-90,90].
submit(yaw,pitch): finish the search. These integers must equal the CURRENT camera pose, not a guessed target location. Submit only when the requested object is centered.
Example: at pose (0,0), rotate(-45,0) changes the pose to (315,0). If the target is then centered, the next action is submit(315,0).
You may explain your decision before the answer. Return exactly ONE action in exactly ONE ... block. A maximum of 10 decisions, including submit, is allowed. Use only the supplied observations. Do not invent observations or future events.'''
TRACK_TEXT={
'vision_only':'Available observations: camera images only. No audio is supplied.',
'vision_mono':'Available observations: camera images and the complete 10-second mono recording. The recording is fixed in time and microphone orientation. Mono audio has no left/right channel cues.',
'vision_stereo':'Available observations: camera images and the complete 10-second stereo recording. Left and right channels preserve their ordering. The recording is fixed in time and microphone orientation; camera rotation does not rotate the recording.'}
def digest(value):return hashlib.sha256(json.dumps(value,sort_keys=True,ensure_ascii=False,separators=(',',':')).encode()).hexdigest()
def messages(instruction,pose,history_actions,decision,track):
assert track in TRACK_TEXT and 1<=decision<=10 and len(history_actions)<=2
assert len(pose)==2 and all(isinstance(x,int) for x in pose)
content=[]
for action in history_actions:
content += [dict(type='text',text='Historical observation:'),dict(type='image'),dict(type='text',text='Executed action: '+action)]
content += [dict(type='text',text='Current observation:'),dict(type='image')]
if track!='vision_only':content.append(dict(type='audio',source='canonical_10s_recording'))
content.append(dict(type='text',text=f'Instruction: {instruction}\nCurrent camera pose: ({pose[0]},{pose[1]}).\nDecision: {decision}/10.\n'+TRACK_TEXT[track]))
return [dict(role='system',content=SYSTEM),dict(role='user',content=content)]
def parse_action(raw,pose):
blocks=re.findall(r'(.*?)',raw,re.S)
if len(blocks)!=1:return None
m=re.fullmatch(r'\s*(rotate|submit)\((-?\d+),\s*(-?\d+)\)\s*',blocks[0])
if not m:return None
op,y,p=m[1],int(m[2]),int(m[3])
if op=='rotate' and not(-180<=y<=180 and -90<=p<=90):return None
if op=='submit' and [y,p]!=pose:return None
return op,y,p
def verify_same_state(records):
"""Compare records for the SAME episode/state/history and same modality track."""
assert records
for k in ['episode_id','state_hash','track','protocol_sha256','semantic_prompt_sha256','rgb_sha256','audio_source_sha256','audio_effective_sha256']:
assert len({json.dumps(r[k],sort_keys=True) for r in records})==1, 'input/prompt mismatch: '+k
return True
if __name__=='__main__':
a=messages('Find the bell.',[0,0],[],1,'vision_stereo')
assert a==messages('Find the bell.',[0,0],[],1,'vision_stereo')
assert parse_action('donesubmit(0,0)',[0,0])==('submit',0,0)
assert parse_action('submit(1,0)',[0,0]) is None
assert parse_action('rotate(1,0)submit(0,0)',[0,0]) is None
print('Common semantic prompt and action-contract checks passed')