Skip to main content
In this tutorial, you’ll create a command-line personal trainer application leveraging the OpenAI API and Python. You will:
  • Load real-world fitness data
  • Collect user health goals
  • Generate custom, actionable recommendations

Prerequisites

Storing your API key in plaintext is insecure. Use environment variables or a secrets manager instead of hardcoding it in your script.

1. Setup and Imports

Create a file named personal_trainer.py and add:

2. Load Fitness Data

Load your CSV dataset containing sleep, health, and lifestyle metrics. Update the file path as needed:
Ensure the CSV file has columns like sleep_duration, diet_quality, and stress_level for best results.

3. Collect User Health Goals

Prompt the user to enter one or more goals. Type done to finish:

4. Define the Trainer Function

This function builds a chat prompt from user goals and context, then calls the OpenAI chat completion API:
API calls may incur costs. Monitor your usage on the OpenAI dashboard.

5. Main Execution

Invoke the trainer function and display recommendations:

6. Example Run

Watch Video