Write parse_intent(text) that turns a free-text model response into a decision the agent loop can act on. If the text contains the marker 'FINAL ANSWER:', return {'kind': 'final', 'value': <everything after the marker, stripped>}. Otherwise, if it matches 'ACTION: name(args)', return {'kind': 'action', 'name': name, 'args_text': args}. If neither matches, return {'kind': 'unknown'}. Standard library only.
from unittest import TestCase
from exercise import parse_intent
class Evaluate(TestCase):
def test_detects_final_answer(self):
self.assertEqual(parse_intent('done. FINAL ANSWER: 42'),
{'kind': 'final', 'value': '42'})
def test_detects_action_name(self):
self.assertEqual(parse_intent('ACTION: get_weather("Paris")')['name'], 'get_weather')
def test_action_captures_args_text(self):
self.assertEqual(parse_intent('ACTION: add(2, 3)')['args_text'], '2, 3')
def test_unknown_when_neither(self):
self.assertEqual(parse_intent('just chatting'), {'kind': 'unknown'})
import re
def parse_intent(text):
if 'FINAL ANSWER:' in text:
return {'kind': 'final', 'value': text.split('FINAL ANSWER:', 1)[1].strip()}
m = re.search(r'ACTION:\s*([A-Za-z0-9_]+)\((.*)\)', text)
if m:
return {'kind': 'action', 'name': m.group(1), 'args_text': m.group(2).strip()}
return {'kind': 'unknown'}