Skip to main content

Overview

RAG Chat requires environment variables to authenticate with external services. All variables should be stored in a .env file in the project root directory.

Required Variables

OPENAI_API_KEY

OPENAI_API_KEY
string
required
Your OpenAI API key for accessing GPT models and embedding services.
Used For:
  • Generating text embeddings via OpenAIEmbeddings()
  • Powering chat responses through ChatOpenAI()
  • Both embedding and chat functionality require a valid API key
Format:
OPENAI_API_KEY='sk-proj-xxxxxxxxxxxxxxxxxxxx'

Setup Instructions

1. Obtain an OpenAI API Key

1

Create OpenAI Account

Visit platform.openai.com and sign up or log in to your account.
2

Navigate to API Keys

Go to SettingsAPI Keys or visit platform.openai.com/api-keys directly.
3

Create New Key

Click “Create new secret key” and give it a descriptive name (e.g., “RAG Chat Development”).
4

Copy and Store

Copy the key immediately. OpenAI will only show it once. Store it securely.

2. Configure Your Project

OPENAI_API_KEY='sk-proj-your-actual-key-here'
1

Create .env File

Copy the .env.example file to .env in your project root:
cp .env.example .env
2

Add Your Key

Edit .env and paste your OpenAI API key:
OPENAI_API_KEY='sk-proj-xxxxxxxxxxxxxxxxxxxx'
3

Verify Loading

The application automatically loads environment variables using python-dotenv:
from dotenv import load_dotenv
load_dotenv()  # Called at app.py:13

Security Best Practices

Never commit your .env file to version control. Always keep API keys private.

Protect Your API Key

Ensure .env is listed in your .gitignore file:
.gitignore
# Environment variables
.env
.env.local
The project includes .env.example as a template with empty values.
Create different API keys for:
  • Development and testing
  • Production deployments
  • Team members (if applicable)
This makes it easier to rotate keys and track usage.
Regularly check your OpenAI dashboard for:
  • Unexpected usage spikes
  • API calls from unknown sources
  • Cost tracking and budget alerts
Set up usage limits in the OpenAI dashboard to prevent overcharges.
For production applications:
  • Rotate API keys every 90 days
  • Immediately rotate if a key may have been exposed
  • Delete unused keys from the OpenAI dashboard

Deployment Configuration

When deploying RAG Chat, configure environment variables through your hosting platform:
  1. Go to your app settings in Streamlit Cloud
  2. Navigate to Secrets
  3. Add your environment variables in TOML format:
OPENAI_API_KEY = "sk-proj-xxxxxxxxxxxxxxxxxxxx"

Troubleshooting

Symptoms: Application crashes with OpenAI authentication errorSolutions:
  • Verify your API key is correctly copied (no extra spaces)
  • Check that .env is in the project root directory
  • Ensure the key starts with sk-proj- or sk-
  • Verify the key hasn’t been revoked in OpenAI dashboard
Symptoms: Application can’t find OPENAI_API_KEYSolutions:
  • Restart the application after creating .env
  • Check that python-dotenv is installed: pip install python-dotenv
  • Verify .env file is not named .env.txt or similar
  • Check file permissions (should be readable)
Symptoms: RateLimitError from OpenAI APISolutions:
  • Check your API usage limits in OpenAI dashboard
  • Upgrade to a paid OpenAI account if on free tier
  • Implement rate limiting in your application
  • Consider using a lower-tier model (e.g., gpt-3.5-turbo)

Build docs developers (and LLMs) love