Forays Into AI

The only way to discover the limits of the possible is to go beyond them into the impossible. - Arthur C. Clarke

Building a Simple Multi-Agent Physics Teacher Application with AutoGen

AutoGen is a robust framework designed to develop Large Language Model (LLM) applications using multiple agents capable of conversing to solve tasks. It allows for customization and seamless human interaction, operating in various modes that integrate LLMs, human inputs, and tools. AutoGen simplifies the creation of next-generation LLM applications by facilitating multi-agent conversations with minimal effort.

In this simple tutorial, we will explore how to build a simple multi-agent application using the AutoGen framework. We will create a physics teacher application where a student agent interacts with a teacher agent to learn about Newton's laws of motion.

Full code can be found on Github.

Step 1: Setting up the Environment

First, make sure you have AutoGen installed. You can install it using pip:

pip install autogen python-dotenv

Next, create a new Python file named physics_teacher.py and import the necessary modules:

import os
from dotenv import load_dotenv
from autogen import ConversableAgent

We will use the dotenv module to load the API key from a .env file. Make sure you have a .env file in the same directory with your API key defined.

Step 2: Configuring the Language Model

In this example, we will use the GPT-3.5-turbo model. Define the llm_config dictionary with the model name:

llm_config = {"model": "gpt-3.5-turbo"}

Step 3: Creating the Teacher Agent

Now, let's create the teacher agent using the ConversableAgent class from AutoGen:

teacher = ConversableAgent(
    name="maestro",
    system_message= 
    "Your name is Isaac and you are a physics teacher."
    "The student will ask questions asking you to explain. You only answer physics questions.",
    llm_config=llm_config,
    human_input_mode="NEVER",
)

We set the name of the teacher agent to "maestro" and provide a system message that defines the agent's role and behavior. These are, of course, rather simple prompts but they serve the purpose. The human_input_mode is set to "NEVER" since we want the teacher agent to operate autonomously without human intervention.

Step 4: Creating the Student Agent

Next, we create the student agent:

student = ConversableAgent(
    name="student",
    system_message=
    "Your name is Joe and you are a student. "
    "You don't know Newton's laws of motion, and you want the teacher to help you with that."
    "You will the teacher questions so he can explain."
    "When you finally understand and want to end the conversation, say 'I understand. Thank you.'",
    llm_config=llm_config,
    human_input_mode="NEVER",
    is_termination_msg=lambda msg: "I understand. Thank you" in msg["content"] or "Goodbye" in msg["content"],
)

Similar to the teacher agent, we set the name and system message for the student agent. We also define a termination condition using the is_termination_msg parameter, which checks if the student's message contains "I understand. Thank you" or "Goodbye" to end the conversation.

Step 5: Initiating the Conversation

Finally, we initiate the conversation between the student and teacher agents:

if __name__ == "__main__":
    chat_result = student.initiate_chat(
        recipient=teacher,
        message="Good morning, can I please have some help with Newton's laws of motion?"
    )

The student agent initiates the chat by sending a message to the teacher agent asking for help with Newton's laws of motion.

Running the Application

To run the application, simply execute the physics_teacher.py file:

python physics_teacher.py

Here is a sample chat output: Teacher Student Chat

Conclusion

In this tutorial, we learned how to build a simple multi-agent physics teacher application using AutoGen. We created a teacher agent and a student agent, customized their behaviors using system messages, and initiated a conversation between them. AutoGen simplifies the process of creating conversational applications by providing a high-level abstraction for managing agents and their interactions.

AutoGen provides a flexible and powerful framework for building diverse LLM applications with ease.

TaggedAutoGenGenAI

Can You Be a Successful Programmer in 2027 Without AI Skills?

As AI transforms the tech landscape, will programmers need to adapt and learn AI to stay relevant and successful in 2027 and beyond? I would say the answer is a clear yes, but there is more to it.

Creating a Real-time Chat Application with Streamlit and Neo4j

Learn how to build a chat application with Streamlit and Neo4j in our latest tutorial. We'll guide you through setting up Docker, using an open source LLM, and managing chat histories with Neo4j. Perfect for both beginners, this post provides all the tools needed to create an AI-enhanced chat application. Dive into the code and start building today!

Building a simple chat application using Streamlit and Langchain

Learn how to create a user-friendly chat application with Streamlit and Langchain, integrating semantic search for enhanced interactions.