Using OpenAI API to Track Calories and Build a Health Coach

Jul 25, 2024 by Stein Ove Helset
Using OpenAI API to Track Calories and Build a Health Coach

Let's create a script that can do more than just keep track of the amount of calories you have eaten in a day. I want to make it possible to just say that I have eaten cereal for breakfast, and then use AI to figure out the number of calories. The AI has to be smart enough to ask you questions like what type of milk did you use, the amount, which cereal and similar.

You should also be able to say that you drank a bottle of Coca-Cola, and the AI will then automatically know how many calories this has in a bottle or can.

We are going to be using the new UI features from GPTScript, store data in a database and also learn about sub tools. If you haven't set up GPTScript already, the next section will take you through it.

Setting up GPTScript

We can begin with setting up GPTScript on your computer. The version I used for this tutorial is version 0.8.

A short guide:

  1. First you install the CLI (Command line interface) by running this command:
MacOS / Linux: brew install gptscript-ai/tap/gptscript Windows: winget install gptscript-ai.gptscript
  1. Get your OpenAI API key by going here: https://platform.openai.com/api-keys

  2. Add the API key to your environment by running this command:

MacOS / Linux: export OPENAI_API_KEY="your-api-key” Windows: $env:OPENAI_API_KEY = 'your-api-key'

And now you should be ready to jump to the next step. If you need more details for the installation, you can go to the official getting started guide for GPTScript by clicking here.

Prices

Before you start running the commands for running GPTScript, it might be smart to take a look at the price lists here: https://openai.com/pricing

Using OpenAI’s API is not free, but you do not pay for more than you actually use.

The script we are building in this tutorial will use a language model called gpt-4-o. This price can vary a little bit based on what you actually are sending in to the GPTscript, how long the texts are, etc.

Take a good look at the price lists to familiarize your self with them.

Starting the Chat UI

When you have everything installed and set up, we can go ahead and start the server. The only command you need to run is:

gptscript --ui

When you've done this, you can open up the url that appeared in your browser and you will see something like this:

health1.png

Click "Create a new script" to get started.

In the next screen, we are going to create a new "script" and instruct the chat bot on what to do. This is the information I filled in:

Filename: healthcoach.gpt Chat bot name: healthcoach Instructions: You are a health and fitness coach. I want you to help me keep track of what I eat, how many calories and similar. You can begin by asking the age and weight of the user, and calculate the amount this person should eat in a day. When the user asks you to track something, be 100% sure that you now the amount to track (For example: if the user asks you to track a bowl of cereal, ask which milk and which type of cereal).

Feel free to play around with the instructions as you want, but to get the results I'm getting, you should stay as close to this as possible. AI isn't very deterministic, so the results can still vary from mine if your instructions is exactly as mine.

When you click "Create my new chat bot", you will be presented with a screen like this:

health2.png

On the left side of the screen, you'll have the instructions and name you gave the chat bot. You can edit the scripts here, add sub tools, and similar. If you do any changes, you need to click the "restart the chat" button in the center of the screen in order for it to take effect.

On the right side of the screen, you'll have the chat interface. You can type anything you want here now and talk to your health coach. You're probably asked a few question like I was. I filled in this information:

My age is 36, I'm 66 kg, I'm 178 cm tall, I'm male and my activity level is moderately active.

When this is answered. You can start tracking your caloric intake. Say that you eat a snickers bar, drank a bottle of Coca-Cola or similar. You can also track your exercises if you want it to do that.

Storing the information in a database

If you restart the chat now, it will not remember the information about you or the information you have stored. Let's make a sub tool that can utilize sqlite 3 to store the information between sessions.

On the left side of the screen, click "Add new custom tool". You will then see a new screen like this:

health3.png

As you see, this is very similar to the script it self. But we want to keep the script as clean as possible, and it will also make the instructions for the AI more clear.

For the name, I used "database-manager" and the description is "Basic script for reading and writing to a sqlite database.".

Since we're going to let the tool talk to a database, it will need a few permission (we'll add these as "Basic tools").

Type "sys.read" and click the "+" button on the right side. Do the same with "sys.write" and "sys.exec" (remember to click "+" for each of these two as well).

The "read" will make it possible to read from the database, the "write" makes is possible to write", and the "exec" allows the script to run commands to execute the read and write commands.

Next, we need to instruct the script to store the information in a database. This will now create a database if there isn't one.

When this script is started, I want you to create a new sqlite3 database if there isn't already one. You are going to keep track of information about a users eating habits, caloric intake and information about the user (age, weight, height, gender, activity level and daily caloric needs).

Last but not least, we need to do some changes to our main script to use this database-manager. Scroll up and add "database-manager" as a "Basic tool" if this hasn't already been done automatically.

Next, we need to add one more line in the instructions field (append at the bottom):

You can use database-manager to store information about the user, and also everything the user has eaten or drank.

And that was it. Click the "Restart the chat bot" button and we're good to go.

The first time you will have to type in the information about your self again, but this time, it will be stored in a sqlite database. You will be prompted a question if you will allow the bot to execute commands on your behalf. I just click "Always" and you don't have to think about it again.

Summary

If you want, you can now start playing around with the instructions for the database tool or the main script. Maybe you need to do some changes in order to store information about your exercises, or is there something else you want this to do?

You can instruct the coach to be more proactive and tell you when you have eaten enough, ask it how many calories you have left this day, how much sugar you have eaten, etc. The possibilities are endless.

Stein Helset is a developer who creates tutorial videos to teach coding, with a focus on Django, Python, Vue, Htmx and much much more!