This document explains how to set up and use OpenAI models in the Study Chat application.
The application now supports OpenAI models (GPT-4o and GPT-3.5 Turbo) alongside Google's Gemini models. To use the OpenAI models, you need an OpenAI API key.
- Create an account at OpenAI if you don't already have one
- Navigate to the API keys page
- Create a new API key and copy it
- Set up your environment variable:
Create a .env.local file in the root of your project if it doesn't exist already and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here
The application will automatically detect if an OpenAI API key is available when a user selects an OpenAI model (GPT-4o or GPT-3.5 Turbo).
If the API key is found:
- The application will use the selected OpenAI model
- The model will be initialized with the appropriate configuration
If the API key is not found:
- The application will automatically fall back to using Gemini 2.0 Flash
- A notification will appear in the server logs
When deploying to your hosting environment, make sure to add the OPENAI_API_KEY as an environment variable in your hosting platform.
- Go to your project settings in Vercel
- Navigate to the "Environment Variables" section
- Add a new variable with the name
OPENAI_API_KEYand your key as the value - Redeploy your application
To verify that your OpenAI integration is working:
- Start your development server
- Open the application and navigate to a chat
- Look for the model selector dropdown menu
- Select either "GPT-4o" or "GPT-3.5 Turbo"
- Send a message and confirm that the model responds correctly
The server logs will show which model is being used for each request, helping you verify that the OpenAI models are working as expected.
If you encounter issues with the OpenAI integration:
- Verify that your API key is correct and has not expired
- Check that the
.env.localfile is properly formatted - Restart your development server after making changes to environment variables
- Check the server logs for specific error messages
If the API key is valid but you're still experiencing issues, you may need to check your API usage limits in your OpenAI account.