This project automates the Twitter Feed of QuantPy using the OpenAI and Twitter API's.
- utilises OpenAI Chat GPT to generate twitter (X) posts from a list of quantitative python ideas with a specific template file
- develop OpenAI prompt template using Jupyter Notebooks environment for testing response
- tweets are posted to twitter (using tweepy) every 12 hours with the apscheduler module in python
- scripts are run using the operating system service and therefore from the command line of our remote server we can: check the status, stop or start the twitter bot service
- we also have access to the logs generated from the twitter bot
- Project 1: QuantPy Main Twitter Feed Automation
- Generate Quantitative Finance Twitter Content using OpenAI ChatGPT
- Schedule & Send Tweets using Raspberry Pi Headless Server
- Project 2: Automate scraping & publishing of top quality news on Quantitative Finance Topics
- Project 3: Train & Publish specific Quantitative Finance ChatGPT Model
- Project 4: Allow users to interact with trained model by tagging @TheQuantPy twitter handle on quant finance twitter posts 🎉 🍾
These steps are to be followed to create a clone of the twitter bot and run it on a remote server or rapsberry pi (as I have done here).
- Create OpenAI Account for API
- Create API key for ChatGPT API
- Create Twitter Account for your bot
- Create Developer Account, this can be done for free, however there is a 250 word explanation of use case required.
- Add environment variables to a .env file, and have this where you host your app. List of environment variables required and naming convention below:
- OPENAI_API_KEY
- TWITTER_API_KEY
- TWITTER_API_SECRET
- TWITTER_ACCESS_TOKEN
- TWITTER_ACCESS_TOKEN_SECRET
- TWITTER_BEARER_TOKEN
Setup Raspberry Pi in Headless mode, so you can access it like a remote server.
Ensure python 3.11 is installed, and download git.
sudo apt update
sudo apt install git
git clone https://github.com/TheQuantPy/quantpy-twitter-bot.git
cd quantpy-twitter-bot
python -m venv env
source env/bin/activate
pip install -r requirements.txt
Note
Important that twitter_bot.service file is installed properly on host server
We will run out twitter bot script as a service in the operating system so it can hva the following functionality:
- start,
- stop,
- restart, and
- status.
We will setup our bot script as a service using linux operating system sysemd, which can be commanded using systemctl.
cd /lib/systemd/system/
sudo nano twitter_bot.service
[Unit]
Description=QuantPy Twitter Bot Service
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/quantpy/quantpy-twitter-bot/env/bin/python /home/quantpy/quantpy-twitter-bot/main.py
Restart=on-abort
[Install]
WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/twitter_bot.service
chmod +x /home/quantpy/quantpy-twitter-bot/main.py
sudo systemctl daemon-reload
sudo systemctl enable twitter_bot.service
To start service:
sudo systemctl start twitter_bot.service
To check status:
sudo systemctl status twitter_bot.service
To stop service:
sudo systemctl stop twitter_bot.service
To check service logs:
sudo journalctl -f -u twitter_bot.service
Copyright © 2023 QuantPy Pty Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.