A simple Python application that demonstrates how to interact with OpenAI's GPT-4 API to create a basic chatbot.
- Simple chat completion using OpenAI's GPT-4 model
- Environment variable configuration for secure API key management
- Configurable temperature settings for controlling response creativity
- Clean output extraction from API responses
- Python 3.7 or higher
- OpenAI API key
- Clone this repository:
git clone <repository-url>
cd <repository-directory>- Install required dependencies:
pip install openai python-dotenv- Create a
.envfile in the project root and add your OpenAI API key:
API_KEY=your_openai_api_key_here
Run the script:
python main.pyThe application will send a predefined question to GPT-4 and display the response.
The temperature parameter controls the randomness/creativity of the AI responses:
- 0.0: Deterministic responses (same input → same output)
- Best for: factual questions, math problems, answers requiring consistency
- 0.1-0.9: Balanced randomness
- Best for: marketing copy, creative writing, storytelling
- 1.0+: Highly creative/random responses
Currently configured to use gpt-4.1-2025-04-14. You can modify this in the code to use other available models.
# Load environment variables
load_dotenv()
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("API_KEY"))
# Create chat completion
response = client.chat.completions.create(
model="gpt-4.1-2025-04-14",
messages=[{"role": "user", "content": "Your question here"}],
temperature=0.0
)
# Extract and display response
print(response.choices[0].message.content)| Variable | Description | Required |
|---|---|---|
API_KEY |
Your OpenAI API key | Yes |
- Never commit your
.envfile to version control - Keep your API key secure and don't share it publicly
- Consider using API key rotation for production applications
openai: Official OpenAI Python client librarypython-dotenv: Load environment variables from .env file
The current example asks "왜 강남은 강남이라고 할까요?" (Why is Gangnam called Gangnam?) and displays the AI's response in Korean.
To modify the chatbot:
- Change the question in the
messagesarray - Adjust the
temperaturevalue for different response styles - Switch to a different OpenAI model if needed
- Add conversation history by expanding the
messagesarray
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
For issues related to:
- OpenAI API: Check the OpenAI documentation
- This code: Open an issue in this repository