# ChatGPT Integration Setup

## Overview

The search feature now uses OpenAI's ChatGPT API to find worship songs based on your search query and filter selection.

## Setup Instructions

### 1. Get an OpenAI API Key

1. Go to [https://platform.openai.com/api-keys](https://platform.openai.com/api-keys)
2. Sign in or create an account
3. Click "Create new secret key"
4. Copy the API key (it starts with `sk-...`)

### 2. Option A: Use a `.env` File (Recommended)

Create a file named `.env` inside the `backend` folder (or copy `.env.example`):

```envenv
OPENAI_API_KEY=sk-your_openai_key_here
GENIUS_TOKEN=your_genius_token_here
```

The application now auto-loads `.env` because `load_dotenv()` is called at the top of `backend/app.py`.

Restart the backend after changes:

```powershell
python app.py
```

### 2. Option B: Set Environment Variable

#### Windows (PowerShell)

```powershell
# Set for current session only
$env:OPENAI_API_KEY="your-api-key-here"

# Or set permanently (requires admin)
[System.Environment]::SetEnvironmentVariable('OPENAI_API_KEY', 'your-api-key-here', 'User')
```

#### Windows (Command Prompt)

```cmd
set OPENAI_API_KEY=your-api-key-here
```

#### Alternative: Create .env file

Create a file named `.env` in the `backend` folder:

```env
OPENAI_API_KEY=your-api-key-here
```

Then update `backend/app.py` to load it:

```python
from dotenv import load_dotenv
load_dotenv()
```

And add `python-dotenv` to `backend/requirements.txt`.

### 3. Install Dependencies

```powershell
cd "E:\Documents\Website Projects\Church_SongLyric\backend"
pip install -r requirements.txt
```

### 4. Restart Backend Server

After setting the API key, restart the Flask server:

```powershell
python app.py
```

## Features

### Search Filters

- **All**: Searches for songs related to any field
- **Song Title**: Focuses search on song titles
- **Artist**: Searches by artist/singer name
- **Band**: Searches by band/group name

### Fallback Mode

If the OpenAI API key is not set or ChatGPT is unavailable, the system will return mock data with a note explaining the situation.

## Cost Considerations

- The ChatGPT integration uses the GPT-4 model
- Each search costs approximately $0.01-0.03 depending on response length
- Consider using GPT-3.5-turbo for lower costs by changing `model="gpt-4"` to `model="gpt-3.5-turbo"` in `backend/app.py`

## Troubleshooting

- **Error: "No API key"**: Make sure you've set the `OPENAI_API_KEY` environment variable
- **Mock data returned**: This means the API key isn't set or ChatGPT encountered an error
- **Slow responses**: ChatGPT API calls take 2-5 seconds; this is normal
