AI

Custom AI Bots using OpenAI’s Assistant API hosted on Streamlit

I have done a basic version of this Streamlit app earlier in April with the IP3s. As I am preparing for yet another professional sharing, this time with my school’s teachers, this app is revived with a bit of refinement.

During the testing, the dialogue went like this, with the user prompt / input emphasised:

Two balls of different masses but equal volume rest at the back of a truck with a smooth floor. The truck, initially moving at a constant speed, abruptly comes to a stop. Describe how both balls will react.

wHAT IS LIFE (trying to throw the AI off by making irrelevant comments)

Let’s start by identifying the relevant concepts for the problem at hand. The scenario involves two balls of different masses but equal volume in a truck that abruptly stops.

  1. Newton’s First Law of Motion: This law states that an object will remain at rest or in uniform motion in a straight line unless acted upon by an external force.
  2. Inertia: This is the property of an object to resist changes in its state of motion. It is directly related to the mass of the object.
  3. Friction: The problem specifies that the floor is smooth, implying negligible friction.

Let’s consider the implications of these concepts:

  • What happens to objects inside the truck when it abruptly stops, considering Newton’s First Law?
  • How does the smooth floor (negligible friction) affect the motion of the balls?
  • Since the balls have different masses but equal volumes, how does their mass affect their motion?

Can you identify how these concepts apply to the problem?

The metal ball will move faster

Let’s think about this step-by-step.

  1. Newton’s First Law: What does it say about the motion of the balls when the truck stops abruptly?
  2. Smooth Floor: If the floor is smooth and there’s no friction, how does this affect the balls’ motion?
  3. Initial Speed: If both balls were moving at the same speed as the truck, what happens to their speed immediately after the truck stops?

Could you reconsider your answer based on these points?

the metal ball will move faster as it is heavier

Let’s focus on Newton’s First Law of Motion again. It states that an object in motion will stay in motion unless acted upon by an external force.

  1. Initial Speed: Both balls were moving at the same speed as the truck. When the truck stops, what happens to the balls’ motion?
  2. Smooth Floor: With no friction, will the mass of the balls affect their speed immediately after the truck stops?

Think about these points and tell me how the balls will move when the truck stops.

both balls will move forward

Great! You’ve recognized that both balls will move forward due to their inertia.

Now, considering the smooth floor and the fact that both balls were initially moving at the same speed:

  1. Smooth Floor: With no friction, will the balls’ masses affect their speeds immediately after the truck stops?
  2. Equal Volume: Does the volume of the balls have any effect on their motion in this scenario?

What can you conclude about the speeds of the two balls immediately after the truck stops?

they will be the same!

Excellent! You’ve got it.

  1. Newton’s First Law: Both balls will continue moving forward due to their inertia.
  2. Smooth Floor: With no friction, the balls’ masses do not affect their speeds.
  3. Initial Speed: Both balls will continue at the same speed they had when the truck was moving.

Well done! If you have any more questions or need further clarification, feel free to ask.

This is the end of the conversation with the AI bot. I thought the AI managed to keep to the system prompts given.

To prepare for this scaffolding conversation, I gave the AI a knowledge base consisting of the syllabus learning outcomes so that it does not engage at a level beyond the requirements.

The prompts I gave the system were:

You will play the role of a Socratic tutor who will engage the user in a discussion of a specific physics problem:
"Two balls of different masses but equal volume rest at the back of a truck with a smooth floor. The truck, initially moving at a constant speed, abruptly comes to a stop. Describe will both balls will react."

Do not answer the question directly. Referring to the syllabus document, point out the relevant topic and concepts before asking the user if he or she needs further help.

The success criteria are three-fold:
1. The user must recognise that the floor is smooth so there is no friction.
2. The user must be able to tell that both balls will move forward after the truck stops because of their inertia, according to Newton's First Law.
3. Since the floor is smooth and the two balls were initially moving at the same speed, they will also continue with the same speed immediately after the truck stops.

Guide the user toward the success criteria with questions only and never answer for them.

Encourage the user when he/she gives a good response. However, if the user repeatedly gives the wrong answer, give more direct assistance.

In your response, do not use more than 40 words for each point you are trying to make. Keep it succinct.

The advantage of such an AI bot is that I can guide it to provide scaffold without taking away the students’ need to think for themselves. If it were a generic bot, it might simply give the answer right away.

While testing out the prompts, I notice that if I specify the success criteria for the AI, it is able to know when to stop the conversation. This ensures that the student is able to fulfill with all the requirements of the question before putting an end to the conversation.

If anyone is keen to set up such a bot so that students need not sign in to access it, you can fork the codes from my python file: https://github.com/physicstjc/askme/blob/main/assistant.py and edit the display test and images from there. The system prompts are to be edited at https://platform.openai.com/assistants/. Do note that you will need to pay for the use of the API with an OpenAI developer account (different from the ChatGPT plus subscription account). If you are not sure what Assistants are, feel free to try them out at https://platform.openai.com/playground.

After that, you can create a Streamlit account and connecting it to your github account. Create an app there by deploying the python file. Copy the OpenAI API key and the Assistant ID into the app settings in Streamlit, under the “Secrets” section:

OPENAI_API_KEY= "{insert key here}"
assistant_id = "{insert assistant id here}"

If you want to consider no-code approaches to creating your own AI bots, try out Poe.com, SchoolAI.com, Mizou.com or even ChatGPT Plus. Here is a table comparing their costs and pros and cons.

Slides for ST-LT Networking Session 16 Jul 2024

I am grateful for the opportunity to share about my recent experience with AI from an education practitioner’s point of view as well as a breakout session about how to use GAI to create simple web applications. The above are the slides.

Photos were taken by Lawrence (see his site: Open Source Physics @ SG here) but I cropped them a little so as not to show others’ faces without their permission.

AI prompts to generate web-based quizzes

With a set of questions on the equations of rectilinear motion as an example, the following quiz was generated using javascript by ChatGPT. This type of quiz is not linked to a database unless such integration is done, so the scores are not stored. However, they make for good self-assessment for students at the end of each topic.

The quizzes are easily generated using a set of prompts that are shown below, and hosted on any static page such as github pages or uploaded as a zip file into the Student Learning Space.

The URL for this sample quiz is at https://physicstjc.github.io/sls/kinematics-quiz. In fact, this quiz can be used as a template for quantitative quizzes for other topics as well. All the user has to do is to edit the five fields in each question: the question, correct answer, unit, equation and explanation within the index.html file (right-click to save and edit with plain text editor).

The format of the questions is written in this way:

const problems = [
      {
        question: "A car starts from rest and accelerates uniformly at 2.0 m/s² for 10 seconds. What is the final velocity of the car?",
        correctAnswer: 20,
        unit: "m/s",
        equation: "v = u + at",
        explanation: "Using v = u + at, where u = 0 (starts from rest), a = 2.0 m/s², and t = 10 s, we get v = 0 + 2.0*10 = 20 m/s."
      },
      {
        question: "A car traveling at 15 m/s accelerates uniformly at 3.0 m/s² for 5.0 seconds. What is the final velocity of the car?",
        correctAnswer: 30,
        unit: "m/s",
        equation: "v = u + at",
        explanation: "Using v = u + at, where u = 15 m/s, a = 3.0 m/s², and t = 5.0 s, we get v = 15 + 3.0*5.0 = 30 m/s."
      }]

The initial prompts given to ChatGPT were:

  • Create a javascript app to explain problems involving equations of motion, v = u + at, v^2 = u^2 + 2as, s = (u + v)*t /2 and s = ut + 0.5 at^2.
  • Give a word problem based on any one of these equations.
  • Use an input box for the user to key in their answer.
  • Indicate the required unit next to the input box.
  • If the answer is wrong, explain to the user why it is wrong.
  • If the user is correct, add 1 to the score and proceed to another problem.
  • Indicate the scores over the total number of questions attempted.

The follow-up or refinement prompts were:

  • Add 10 different problems to the list.
  • Use “Previous” and “Next” buttons to navigate.
  • When the user returns to a question that was attempted previously, he should not be able to attempt that question again.
  • Add some css styling to make it appealing.
  • Randomise the appearance of the questions and do not repeat questions.
  • Represent the values in 2 significant figures.

The workflow for converting the generated codes to a web app can be found here.

Relative Velocity Simulation

0 m/s

In yet another demonstration of ChatGPT 4o’s capabilities, I generated the above simulation app without writing a single line of code myself: https://physicstjc.github.io/sls/relative-velocity/index.html

The process took 4 prompts.

Prompt 1: Create a javascript simulation in a single html file that demonstrates the addition of two velocity vectors to give a resultant velocity. The velocity of a boat travelling in still water moving across a river is shown as vertically upward. A slider will change the speed and direction of the water flow. The velocity of the river water is also shown. Finally, show the addition of the two vectors to give a third vector: the resultant velocity. Create a button for animating the boat’s movement.

Response: Here’s an HTML file with embedded JavaScript to create a simulation of the addition of two velocity vectors. This simulation includes a slider to adjust the speed and direction of the river flow, and a button to animate the boat’s movement. (Code is not included to avoid making this post too long.)

Prompt 2: Use two horizontal lines to show the shores of the river, also the starting and ending point of the boat’s journey. The boat should be represented with a pencil-like shape, moving upward from one horizontal line to another. The boat should be moving in the direction of the resultant velocity. The vectors should be represented with arrows, not just lines.

Response: Here’s the updated HTML file with the specified features:

Two horizontal lines representing the shores of the river.
A pencil-like shape representing the boat, moving from one shore to the other in the direction of the resultant velocity.
Arrows to represent the vectors.

Prompt 3: Very good! Now invert the shape of the boat vertically. Also, draw some light blue wavy curves between the two horizontal lines that are stationary when the river speed is zero but move at the river velocity otherwise.

Response: Here’s the updated HTML file with the specified changes:

The boat shape is inverted vertically.
Light blue wavy curves are drawn between the two horizontal lines to represent the river water. These waves are stationary when the river speed is zero but move at the river velocity otherwise.

Prompt 4: Keep the canvas to 640 pixels wide and 480 pixels tall. Resize everything else accordingly.

And what you see in the beginning of this post was the result.

A little experiment with an AI chatbot

I built a customised AI chatbot using OpenAI’s API, which could run on GPT3.5 or GPT4 and used it to facilitate socratic questioning for students to arrive at the correct answer to a commonly misunderstood Physics concept. The process was not too complicated. One would need a OpenAI developer account to obtain the API key, and a Streamlit account to deploy the app. To input the API key, update the settings of the Streamlit app with this line: OPENAI_API_KEY=”{key}”. The codes can be forked from here.

This video shows a simulation of the interaction that I had with it.

I also stored the interactions that my students had with the bot in csv format, saved in an AWS S3 bucket.

Here are some samples, in which you can see the system prompt given: Speak like a teacher who assesses the response of the student based on clarity, precision, accuracy, logic, relevance and significance. Help the user get to the answer by asking guiding questions to scaffold the learning. The question is: Two balls are placed at the back of a truck that is moving at constant velocity. The blue ball is twice the mass of the red ball. The floor of the truck is perfectly smooth. Compare the movement of the two balls when the truck comes to an abrupt stop. The success criteria for the user is to be able to explain that both balls will move at the same speed once the truck comes to an abrupt stop, according to Newton’s first law, since there will be no net force acting on them since the floor of the truck is smooth and there is no friction.

It was rather entertaining for the students as some of them tried to trick the bot to giving them the answers. I did a pre-test for this question before the test and only 3-5% of the students in both classes got the answer correct but almost all of them (ascertained by an informal poll) knew the correct answer after going through the discussion with the bot.

I hope to continue experimenting with variations of the system prompts when I have time. However, it’s back to marking and preparing for staff PD now!