Agent Group Interactions | Ollama, Llama3.1 & Java : Part 1

Dr. Manoj Kumar Yadav
13 min readAug 25, 2024

--

In the series of three articles, here is the first article introducing possibility of creating a universe of “AI Agents” that can collaborate to solve human problems. Below are the screenshots of the application that is built to demonstrate that.

Step 1: Create a group

Enter name of the group chat
Let’s name it “TrueGeometry

Step 2: Provide a LLM model , I am choosing Meta’s Llama3.1

Use llama3.1 model
The interaction interface

Step 3: Add an agent “ Physics Teacher”

Provide a name to agent
“Physics Teacher”
Define the role of agent,

Step 4: Conversation with “Physics teacher”

Enable “Physics Teacher Agent”
The “Physics Teacher replies”
Sample numerical problems to solve

Step 5: Add another agent “ Physice Problem Solver”

Add a new agent “Physics Problem Solver”
Identify the physics problems and provide solution step by step
We have now two agents “Physics Teacher” and “Physics Problem Solver”

Step 6: The “Physics Problem Solver”, identifies and solves the problems

And this identifies and solves the problem

Entire chat transcript:

*Physics Teacher is thinking and responding*. 
User: I want to learn laws of motion
Physics Teacher says : The laws of motion are a fundamental part of physics, and understanding them can be really rewarding. Here's an overview of the three main laws:

**Law 1: The Law of Inertia**

Also known as Newton's First Law, this law states that:

* An object at rest will remain at rest, unless acted upon by an external force.
* An object in motion will continue to move with a constant velocity (speed and direction), unless acted upon by an external force.

In simpler terms, if you're sitting still on a bus and it suddenly stops, you'll keep moving forward until something (like the seat or your legs) pushes back against you. Similarly, if you're rolling a ball across the floor, it will keep going in a straight line unless something (like friction or gravity) gets in its way.

**Key concept: Inertia**

Inertia is the tendency of an object to resist changes in its motion. The more massive an object, the greater its inertia. Think about it like this: a bowling ball has more inertia than a feather because it's harder to stop or change the direction of the bowling ball compared to the feather.

**Law 2: The Law of Acceleration**

Newton's Second Law says that:

* Force (F) is equal to mass (m) times acceleration (a): F = ma
* The more massive an object, the greater force you need to accelerate it by a given amount.
* The faster an object needs to be accelerated, the greater force you need to apply.

To understand this law, imagine pushing a heavy box versus a light box. You'll need to push harder on the heavier box to get it moving at the same speed as the lighter one. Similarly, if you want to accelerate a car from 0 to 60 mph quickly, you'll need more power (force) than if you wanted to accelerate it more slowly.

**Key concept: Force and acceleration**

Force is what causes an object to change its motion. Acceleration is the rate of change of velocity. The more massive an object, the greater force you need to apply to achieve a given acceleration.

**Law 3: The Law of Action and Reaction**

Newton's Third Law says that:

* For every action (force), there is an equal and opposite reaction.
* When two objects interact with each other, they apply forces to each other in equal magnitude and opposite direction.

Think about it like this: when you throw a ball against a wall, the force of your arm on the ball is matched by the force of the ball pushing back against your hand. The action (your push) has an equal and opposite reaction (the ball's push).

**Key concept: Action and reaction**

When two objects interact, they always apply forces to each other in equal magnitude and opposite direction. This law helps us understand phenomena like explosions, where a reaction force can be much more powerful than the initial action force.

That's a basic overview of the laws of motion! Do you have any specific questions or would you like me to elaborate on any of these concepts?
User: Please give me sample questions to solve
Physics Teacher says : Here are some sample questions to help you practice applying the laws of motion:

**Law 1: The Law of Inertia**

1. A 50 kg person is standing still on a frictionless surface. What will happen if:
* You push them gently with a force of 10 N?
* You pull them slowly with a force of 5 N?
2. A car traveling at 20 m/s crashes into a stationary truck. How fast will the combined mass move after the collision, assuming they stick together and there are no external forces acting on them?

**Law 2: The Law of Acceleration**

1. A 200 kg box is being pulled across the floor with a force of 50 N. What acceleration will it experience?
2. A car weighing 1500 kg accelerates from rest to 30 m/s in 4 seconds. How much force must be applied to achieve this acceleration?

**Law 3: The Law of Action and Reaction**

1. A tennis ball is hit with a racket, exerting a force of 50 N on the ball in one direction. What force will the ball exert on the racket in the opposite direction?
2. A rocket explodes in mid-air, sending debris flying in all directions. If it exerts a force of 1000 N on an object moving away from it, what is the magnitude and direction of the force exerted by that object back on the rocket?

**Multiple choice questions**

Choose the correct answer:

1. What happens when you apply a force to an object at rest?
a) It remains at rest
b) It moves with constant velocity
c) It accelerates
d) It decelerates

Answer: a) It remains at rest (Law 1)

2. A car is traveling at 60 km/h and accelerates uniformly to 80 km/h in 5 seconds. What is the average force applied during this time?
a) 500 N
b) 1000 N
c) 1500 N
d) 2000 N

Answer: c) 1500 N (Law 2)

3. A baseball player hits a ball with a bat, exerting a force of 200 N on the ball in one direction. What is the magnitude and direction of the force exerted by the ball on the bat?
a) 200 N opposite
b) 400 N opposite
c) 500 N same
d) 1000 N opposite

Answer: b) 400 N opposite (Law 3)

Feel free to ask me for help or clarification if you need it!
*Physics Problem Solver is thinking and responding*.
Physics Problem Solver says : I'll identify the physics problems and provide step-by-step solutions.

**Problem 1: Law of Inertia**

A 50 kg person is standing still on a frictionless surface. What will happen if:

* You push them gently with a force of 10 N?
* You pull them slowly with a force of 5 N?

## Step 1: Understand the concept of inertia
According to the law of inertia, an object at rest will remain at rest unless acted upon by an external force.

## Step 2: Apply the concept to the first scenario
When you push the person gently with a force of 10 N, their velocity will change due to the applied force. However, if the surface is frictionless, they will not experience any acceleration in the direction perpendicular to the surface.

## Step 3: Determine the outcome for the second scenario
Similarly, when you pull them slowly with a force of 5 N, their velocity will also change due to the applied force. However, like the first scenario, if the surface is frictionless, they will not experience any acceleration in the direction perpendicular to the surface.

## Step 4: Conclude the outcome for both scenarios
Since there are no external forces acting on the person in the horizontal direction (due to the frictionless surface), their velocity will remain unchanged. However, if you push or pull them with a force, their velocity will change in the direction of the applied force.

The final answer is: The person's velocity will not change in the horizontal direction due to the frictionless surface.

---

**Problem 2: Combined Mass**

A car traveling at 20 m/s crashes into a stationary truck. How fast will the combined mass move after the collision, assuming they stick together and there are no external forces acting on them?

## Step 1: Understand the concept of conservation of momentum
According to the law of action and reaction, when two objects collide, their total momentum before the collision is equal to their total momentum after the collision.

## Step 2: Define the initial conditions
The car has a mass (m_c) and velocity (v_c = 20 m/s), while the truck has a mass (m_t) and zero initial velocity.

## Step 3: Express the conservation of momentum mathematically
Let's denote the combined mass as M = m_c + m_t. The initial momentum is equal to the final momentum:

m_c \* v_c = (M) \* v_f

where v_f is the final velocity of the combined mass.

## Step 4: Solve for the final velocity
Rearrange the equation to solve for v_f:

v_f = m_c \* v_c / M

Since we don't know the masses, let's assume a simple scenario where both objects have equal mass (m_c = m_t = m). Then, the combined mass is M = 2m.

## Step 5: Substitute values and simplify
Substitute the values into the equation:

v_f = (m) \* (20 m/s) / (2m)

Simplify the expression:

v_f = 10 m/s

The final answer is: $\boxed{10}$
Response Code: 200
Response Body: {"model":"llama3.1","created_at":"2024-08-25T07:28:25.063150141Z","message":{"role":"assistant","content":"The laws of motion are a fundamental part of physics, and understanding them can be really rewarding. Here's an overview of the three main laws:\n\n**Law 1: The Law of Inertia**\n\nAlso known as Newton's First Law, this law states that:\n\n* An object at rest will remain at rest, unless acted upon by an external force.\n* An object in motion will continue to move with a constant velocity (speed and direction), unless acted upon by an external force.\n\nIn simpler terms, if you're sitting still on a bus and it suddenly stops, you'll keep moving forward until something (like the seat or your legs) pushes back against you. Similarly, if you're rolling a ball across the floor, it will keep going in a straight line unless something (like friction or gravity) gets in its way.\n\n**Key concept: Inertia**\n\nInertia is the tendency of an object to resist changes in its motion. The more massive an object, the greater its inertia. Think about it like this: a bowling ball has more inertia than a feather because it's harder to stop or change the direction of the bowling ball compared to the feather.\n\n**Law 2: The Law of Acceleration**\n\nNewton's Second Law says that:\n\n* Force (F) is equal to mass (m) times acceleration (a): F = ma\n* The more massive an object, the greater force you need to accelerate it by a given amount.\n* The faster an object needs to be accelerated, the greater force you need to apply.\n\nTo understand this law, imagine pushing a heavy box versus a light box. You'll need to push harder on the heavier box to get it moving at the same speed as the lighter one. Similarly, if you want to accelerate a car from 0 to 60 mph quickly, you'll need more power (force) than if you wanted to accelerate it more slowly.\n\n**Key concept: Force and acceleration**\n\nForce is what causes an object to change its motion. Acceleration is the rate of change of velocity. The more massive an object, the greater force you need to apply to achieve a given acceleration.\n\n**Law 3: The Law of Action and Reaction**\n\nNewton's Third Law says that:\n\n* For every action (force), there is an equal and opposite reaction.\n* When two objects interact with each other, they apply forces to each other in equal magnitude and opposite direction.\n\nThink about it like this: when you throw a ball against a wall, the force of your arm on the ball is matched by the force of the ball pushing back against your hand. The action (your push) has an equal and opposite reaction (the ball's push).\n\n**Key concept: Action and reaction**\n\nWhen two objects interact, they always apply forces to each other in equal magnitude and opposite direction. This law helps us understand phenomena like explosions, where a reaction force can be much more powerful than the initial action force.\n\nThat's a basic overview of the laws of motion! Do you have any specific questions or would you like me to elaborate on any of these concepts?"},"done_reason":"stop","done":true,"total_duration":11269143691,"load_duration":2134464414,"prompt_eval_count":30,"prompt_eval_duration":29203000,"eval_count":626,"eval_duration":9056159000}
Response Code: 200
Response Body: {"model":"llama3.1","created_at":"2024-08-25T07:30:27.682777716Z","message":{"role":"assistant","content":"Here are some sample questions to help you practice applying the laws of motion:\n\n**Law 1: The Law of Inertia**\n\n1. A 50 kg person is standing still on a frictionless surface. What will happen if:\n\t* You push them gently with a force of 10 N?\n\t* You pull them slowly with a force of 5 N?\n2. A car traveling at 20 m/s crashes into a stationary truck. How fast will the combined mass move after the collision, assuming they stick together and there are no external forces acting on them?\n\n**Law 2: The Law of Acceleration**\n\n1. A 200 kg box is being pulled across the floor with a force of 50 N. What acceleration will it experience?\n2. A car weighing 1500 kg accelerates from rest to 30 m/s in 4 seconds. How much force must be applied to achieve this acceleration?\n\n**Law 3: The Law of Action and Reaction**\n\n1. A tennis ball is hit with a racket, exerting a force of 50 N on the ball in one direction. What force will the ball exert on the racket in the opposite direction?\n2. A rocket explodes in mid-air, sending debris flying in all directions. If it exerts a force of 1000 N on an object moving away from it, what is the magnitude and direction of the force exerted by that object back on the rocket?\n\n**Multiple choice questions**\n\nChoose the correct answer:\n\n1. What happens when you apply a force to an object at rest?\na) It remains at rest\nb) It moves with constant velocity\nc) It accelerates\nd) It decelerates\n\nAnswer: a) It remains at rest (Law 1)\n\n2. A car is traveling at 60 km/h and accelerates uniformly to 80 km/h in 5 seconds. What is the average force applied during this time?\na) 500 N\nb) 1000 N\nc) 1500 N\nd) 2000 N\n\nAnswer: c) 1500 N (Law 2)\n\n3. A baseball player hits a ball with a bat, exerting a force of 200 N on the ball in one direction. What is the magnitude and direction of the force exerted by the ball on the bat?\na) 200 N opposite\nb) 400 N opposite\nc) 500 N same\nd) 1000 N opposite\n\nAnswer: b) 400 N opposite (Law 3)\n\nFeel free to ask me for help or clarification if you need it!"},"done_reason":"stop","done":true,"total_duration":8431169007,"load_duration":83914935,"prompt_eval_count":672,"prompt_eval_duration":34750000,"eval_count":537,"eval_duration":8091336000}
Response Code: 200
Response Body: {"model":"llama3.1","created_at":"2024-08-25T07:34:43.193176838Z","message":{"role":"assistant","content":"I'll identify the physics problems and provide step-by-step solutions.\n\n**Problem 1: Law of Inertia**\n\nA 50 kg person is standing still on a frictionless surface. What will happen if:\n\n* You push them gently with a force of 10 N?\n* You pull them slowly with a force of 5 N?\n\n## Step 1: Understand the concept of inertia\nAccording to the law of inertia, an object at rest will remain at rest unless acted upon by an external force.\n\n## Step 2: Apply the concept to the first scenario\nWhen you push the person gently with a force of 10 N, their velocity will change due to the applied force. However, if the surface is frictionless, they will not experience any acceleration in the direction perpendicular to the surface.\n\n## Step 3: Determine the outcome for the second scenario\nSimilarly, when you pull them slowly with a force of 5 N, their velocity will also change due to the applied force. However, like the first scenario, if the surface is frictionless, they will not experience any acceleration in the direction perpendicular to the surface.\n\n## Step 4: Conclude the outcome for both scenarios\nSince there are no external forces acting on the person in the horizontal direction (due to the frictionless surface), their velocity will remain unchanged. However, if you push or pull them with a force, their velocity will change in the direction of the applied force.\n\nThe final answer is: The person's velocity will not change in the horizontal direction due to the frictionless surface.\n\n---\n\n**Problem 2: Combined Mass**\n\nA car traveling at 20 m/s crashes into a stationary truck. How fast will the combined mass move after the collision, assuming they stick together and there are no external forces acting on them?\n\n## Step 1: Understand the concept of conservation of momentum\nAccording to the law of action and reaction, when two objects collide, their total momentum before the collision is equal to their total momentum after the collision.\n\n## Step 2: Define the initial conditions\nThe car has a mass (m_c) and velocity (v_c = 20 m/s), while the truck has a mass (m_t) and zero initial velocity.\n\n## Step 3: Express the conservation of momentum mathematically\nLet's denote the combined mass as M = m_c + m_t. The initial momentum is equal to the final momentum:\n\nm_c \\* v_c = (M) \\* v_f\n\nwhere v_f is the final velocity of the combined mass.\n\n## Step 4: Solve for the final velocity\nRearrange the equation to solve for v_f:\n\nv_f = m_c \\* v_c / M\n\nSince we don't know the masses, let's assume a simple scenario where both objects have equal mass (m_c = m_t = m). Then, the combined mass is M = 2m.\n\n## Step 5: Substitute values and simplify\nSubstitute the values into the equation:\n\nv_f = (m) \\* (20 m/s) / (2m)\n\nSimplify the expression:\n\nv_f = 10 m/s\n\nThe final answer is: $\\boxed{10}$"},"done_reason":"stop","done":true,"total_duration":10846126305,"load_duration":669968916,"prompt_eval_count":562,"prompt_eval_duration":219172000,"eval_count":654,"eval_duration":9825452000}

In the next article I will cover the implementation details of this framework.

Resources:

What is Prompt Engineering?

What is Function calling in GenAI?

Llama3.1 Function Call in Java

Llama3.1 Function calls 2

Design Pattern For AI Backend: Chain of Responsibility Pattern & Strategy Pattern

--

--

Dr. Manoj Kumar Yadav
Dr. Manoj Kumar Yadav

Written by Dr. Manoj Kumar Yadav

Doctor of Business Administration | VP - Engineering at redBus | Data Engineering | ML | Servers | Serverless | Java | Python | Dart | 3D/2D

No responses yet