A simple Node.js application demonstrating the OpenAI API with a basic chat completion example.
- Node.js (version 14.x or higher recommended)
- An OpenAI API key
- Clone or download this repository
- Install dependencies:
npm install
- Configure your OpenAI API key:
- Open the
.env
file - Replace
your_openai_api_key_here
with your actual OpenAI API key
OPENAI_API_KEY=your_actual_api_key
- Open the
Run the example script:
npm start
This will execute a simple prompt asking for a one-sentence bedtime story about a unicorn using GPT-4o.
The script demonstrates:
- Loading environment variables with dotenv
- Initializing the OpenAI client
- Using system prompts to guide the AI's behavior
- Setting temperature for controlling randomness (lower = more deterministic)
- Error handling for API requests
You can modify the openai.js
file to:
- Change the prompt
- Adjust the temperature parameter (0-1)
- Try different models
- Customize the system prompt
- Add more messages to the conversation