How to Install and Run Ollama on macOS and Create a Stock Report with Python
This tutorial guides you through installing Ollama on macOS to run AI large language models locally, interacting with the Mistral model in the terminal, and creating a Python program to generate a daily stock report using live stock data. The program will save the report to a file, send it via Telegram, and be scheduled to run automatically using cron. This is for educational purposes only; I am not responsible for your stock trading decisions.
Prerequisites
- A macOS system with at least 16GB RAM (recommended for Mistral model).
- Visual Studio Code installed.
- Python 3 installed.
- A Telegram bot set up with a bot token and chat ID for sending messages (not covered in this tutorial; ensure you have a working
telegram_message
function).
Step 1: Install and Set Up Ollama
-
Download Ollama:
- Visit the Ollama website and click "Download for macOS".
- Open the downloaded file and drag the Ollama app to your Applications folder.
-
Start the Ollama Server:
- Open Terminal and run:
ollama serve
- This starts the Ollama server in the background.
- Open Terminal and run:
-
Download the Mistral Model:
- Open a new Terminal window (Command + T).
- Pull the Mistral model, which is lightweight and suitable for 16GB RAM systems:
ollama pull mistral
- Wait a few minutes for the download to complete, depending on your internet speed.
-
Run and Test the Mistral Model:
- In the new Terminal window, start the Mistral model:
ollama run mistral
- Wait a few seconds for it to boot up.
- Test it by typing questions, such as:
orWhere is Badlands National Park?
Why is the sky blue?
- The model runs locally on your Mac’s CPU without an internet connection.
- In the new Terminal window, start the Mistral model:
Step 2: Set Up the Python Environment
-
Open Visual Studio Code:
- Launch Visual Studio Code.
-
Create a Virtual Environment:
- Open the integrated terminal in Visual Studio Code (View > Terminal or Ctrl + ~).
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment:
source venv/bin/activate
- You should see
(venv)
in the terminal prompt.
-
Install Required Libraries:
- Install the
yfinance
andrequests
libraries:pip install yfinance requests
- Install the
Step 3: Create the Stock Report Program
-
Create a New File:
- In Visual Studio Code, go to File > New File or press Ctrl + N.
- Save the file as
stock_report.py
(File > Save As or Ctrl + S).
-
Add the Stock Report Code:
- Copy and paste the following code into
stock_report.py
:import yfinance as yf import requests import json from telegram import telegram_message # List of stocks stocks = ["AAPL", "MSFT", "NVDA", "AMZN", "TSLA"] # Get stock data for the past day data = yf.download(stocks, period="1d") # Create a prompt for Ollama with the stock data prompt = f"Generate a short report on these stocks for the past 24 hours: {', '.join(stocks)}. Data: {data.to_dict()}" print(f"Prompt: {prompt}") # Send the prompt to Ollama response = requests.post("http://localhost:11434/api/generate", json={"model": "mistral", "prompt": prompt, "stream": False}) result = response.json() report = result.get("response", "") print(' ') print('--------------------Report--------------------') # Display the report print(report) # Save the report to a text file with open('stock_report.txt', 'w') as f: f.write(report) # Send the report via Telegram telegram_message(report)
- Save the file (Ctrl + S).
- Note: Ensure you have a working
telegram_message
function defined elsewhere to send the report via Telegram.
- Copy and paste the following code into
-
Run the Program:
- Ensure the virtual environment is activated (
source venv/bin/activate
). - In the Visual Studio Code terminal, run:
python stock_report.py
- Alternatively, use Visual Studio Code’s Run feature (Run > Run Without Debugging or Ctrl + F5).
- The program will:
- Fetch stock data for AAPL, MSFT, NVDA, AMZN, and TSLA.
- Generate a report using the Mistral model.
- Print the report in the terminal.
- Save it to
stock_report.txt
in your project folder. - Send it via Telegram.
- Ensure the virtual environment is activated (
Step 4: Schedule the Script with Cron
-
Open the Crontab Editor:
- In the Visual Studio Code terminal, open the crontab editor:
crontab -e
- In the Visual Studio Code terminal, open the crontab editor:
-
Add a Cron Job:
- Add the following line to schedule the script to run daily at 6:00 PM (replace
/path/to/your/project
with your actual project folder path):0 18 * * * /bin/bash -c 'cd /path/to/your/project && source venv/bin/activate && python stock_report.py'
- To find your project path, run
pwd
in the terminal while in your project folder and use that path. - For example, if your project is in
/Users/jakegrim/YouTube_Demo
, use:0 18 * * * /bin/bash -c 'cd /Users/jakegrim/YouTube_Demo && source venv/bin/activate && python stock_report.py'
- Add the following line to schedule the script to run daily at 6:00 PM (replace
-
Save and Exit:
- In the vi editor, press
Esc
, type:wq
, and press Enter to save and exit.
- In the vi editor, press
-
Verify the Cron Job:
- Check that the cron job was added:
crontab -l
- The script will now run daily at 6:00 PM, generating and sending the stock report via Telegram.
- Check that the cron job was added:
Conclusion
You’ve successfully:
- Installed Ollama and run the Mistral model locally on macOS.
- Created a Python program to generate a daily stock report using live data and the Mistral model.
- Saved the report to a file and sent it via Telegram.
- Scheduled the program to run automatically using cron.
Now, you’ll receive a stock report every day at 6:00 PM via Telegram. Have fun exploring local AI and building awesome projects!