Implement the Goal Setting and Monitoring pattern
mainThe Goal Setting and Monitoring pattern involves an iterative loop where an agent generates an output, a reviewer evaluates that output against specific goals, and the agent refines the output based on feedback until the goals are met or a maximum number of iterations is reached.
In this implementation:
- Generate:
generate_promptconstructs a prompt containing the use case, goals, previous code, and feedback. - Execute: The LLM generates code.
- Review:
get_code_feedbackuses an LLM to critique the code against the provided goals. - Monitor:
goals_metuses an LLM to determine if the feedback indicates the goals have been satisfied (returningTrueorFalse). - Iterate: If goals are not met, the code and feedback are fed back into the next iteration.
# Core loop logic for the pattern
for i in range(max_iterations):
# 1. Generate
prompt = generate_prompt(use_case, goals, previous_code, feedback)
code_response = llm.invoke(prompt)
code = clean_code_block(code_response.content)
# 2. Review
feedback = get_code_feedback(code, goals)
feedback_text = feedback.content.strip()
# 3. Monitor
if goals_met(feedback_text, goals):
break
# 4. Prepare for next iteration
previous_code = code