Skip to content

Teaching Assistant Example

This example demonstrates how to create a more complex workflow involving multiple agents that interact with each other. It shows how to implement an educational scenario with a teacher agent generating questions and a student agent solving them.

Overview

The Teaching Assistant example demonstrates: - Creating multiple specialized agents - Configuring agent interactions - Managing context flow between agents - Handling structured educational content

Implementation

Here's the complete implementation:

from yosrai import Agent, Action, ContextFunctions

# Create teacher agent
teacher_agent = Agent(
    agent_code='teacher_agent', 
    agent_name='Teacher Agent',
    instructions_template='You are teacher for {{ grade }} grade, your name is YosrAI, you are asking student to answer multiple choice questions, the question should be in {{ subject }} subject and written in English',
    prompt_template='Generate a multiple choice question for the student to answer in subject: {{ subject }} for grade: {{ grade }}, return the question and the options Only',
    outputs={'messages': ContextFunctions.APPEND_MESSAGES, 'question': ContextFunctions.DEFAULT}
)

# Create student agent
student_agent = Agent(
    agent_code='student_agent', 
    agent_name='Student Agent',
    instructions_template='You are student in grade {{ grade }}, your name is Yosr, you are answering the question',
    prompt_template='Answer the question: {{ question }}',
    outputs={'messages': ContextFunctions.APPEND_MESSAGES, 'answer': ContextFunctions.DEFAULT}
)

# Set up the workflow
action = Action(action_name='Teaching Session')
action.Context(subject='math', grade=5, question='', answer='')
action.add_agent(teacher_agent)
action.add_agent(student_agent)
action.add_link(teacher_agent, student_agent)
action.add_link(student_agent, "END")

Workflow Visualization

The workflow shows the interaction between the teacher and student agents:

---
title: Teacher and Student
---
graph LR
START((Start)) --> teacher_agent[Teacher Agent]
teacher_agent(Teacher Agent) --> student_agent(Student Agent)
student_agent(Student Agent) --> END((End))

Key Components

Action Setup

The action is created with a name that describes the educational session:

action = Action(action_name='Teaching Session')

Teacher Agent Configuration

The teacher agent is configured to: - Generate grade-appropriate questions - Work within a specific subject area - Format questions as multiple choice

teacher_agent = Agent(
    agent_code='teacher_agent', 
    agent_name='Teacher Agent',
    instructions_template='You are teacher for {{ grade }} grade...',
    prompt_template='Generate a multiple choice question...',
    outputs={'messages': ContextFunctions.APPEND_MESSAGES, 'question': ContextFunctions.DEFAULT}
)

Student Agent Configuration

The student agent is set up to: - Process and understand questions - Apply grade-appropriate knowledge - Provide structured answers

student_agent = Agent(
    agent_code='student_agent', 
    agent_name='Student Agent',
    instructions_template='You are student in grade {{ grade }}...',
    prompt_template='Answer the question: {{ question }}',
    outputs={'messages': ContextFunctions.APPEND_MESSAGES, 'answer': ContextFunctions.DEFAULT}
)

Context Management

The context is initialized with educational parameters:

action.Context(subject='math', grade=5, question='', answer='')

Workflow Definition

The educational workflow is defined by: 1. Adding both agents to the action 2. Creating a link from teacher to student 3. Creating a link from student to END

action.add_agent(teacher_agent)
action.add_agent(student_agent)
action.add_link(teacher_agent, student_agent)
action.add_link(student_agent, "END")

Example Interaction

Here's a sample interaction:

Teacher Agent:

A farmer has 24 rows of apple trees in his orchard. Each row has 35 apple trees. How many apple trees are there in the orchard in total?

Options:
A) 59
B) 840
C) 720
D) 590

Student Agent:

Okay, I need to figure out how many apple trees there are in total.

I know there are 24 rows, and each row has 35 trees. That means I need to multiply 24 and 35.

Let me see... 24 times 35... I can do that!

First, I'll multiply 35 by 4 (the ones place in 24). That's 140.
Next, I'll multiply 35 by 20 (the tens place in 24). That's 700.
Finally, I add 140 and 700 together. That's 840!

So the answer is B) 840! There are 840 apple trees in the orchard.

Best Practices

When implementing the Teaching Assistant pattern:

  1. Grade-Appropriate Content: Ensure questions match the grade level
  2. Clear Instructions: Provide detailed instructions for both agents
  3. Structured Output: Use appropriate output formats for questions and answers
  4. Context Flow: Manage the flow of information between agents properly

Use Cases

This pattern is ideal for: - Educational applications - Tutorial systems - Assessment tools - Interactive learning environments - Question generation and evaluation

Next Steps

After implementing the Teaching Assistant pattern, you can: 1. Add more sophisticated question generation 2. Implement scoring and feedback 3. Add more agents for different roles (e.g., tutor, evaluator) 4. Enhance the educational content with multimedia elements