This Python script demonstrates how to interact with the Perplexity AI API to ask questions and store responses in a Supabase database.
- Install the required dependencies:
pip install -r requirements.txt- Create a
.envfile in the root directory and add your API keys:
PERPLEXITY_API_KEY=your_perplexity_api_key_here
SUPABASE_URL=your_supabase_project_url
SUPABASE_KEY=your_supabase_anon_keyYou can get your:
- Perplexity API key by signing up at https://www.perplexity.ai/
- Supabase credentials from your Supabase project settings
The script expects a table named AI_VOTES in your Supabase database with the following columns:
match_id(integer)winner_selection(text)margin_selection(text)reasoning(text)
Run the script using Python:
python perplexity_query.pyThe script will:
- Ask Perplexity AI about match predictions
- Process the response into structured data
- Store the predictions in your Supabase database
You can modify the following in the script:
- Change the model by updating the
modelparameter in the payload - Modify the question in the
main()function - Add additional parameters to the API call as needed
The script includes basic error handling for:
- Missing API keys
- Network request errors
- Invalid responses
- Database insertion errors