A NestJS-based Tic Tac Toe game that uses OpenAI's GPT models for intelligent gameplay. The AI opponent is trained using fine-tuning to make strategic moves based on the current board state.
The purpose of this repository is to experiment with delegating game mechanics logic into AI, creating lightweight code, and providing a proof of concept for managing complex mechanics on the AI side.
- OpenAI GPT integration for AI moves
- Game state evaluation is performed by the GPT model
- Custom fine-tuned model training
- Terminal-based UI using blessed
- Responsive terminal interface
- Error handling and retry mechanisms
- Node.js
- npm
- OpenAI API key
- Clone the repository.
- Install dependencies:
npm install
- Create a
.env
file in the project root:
OPENAI_API_KEY=your_api_key_here
OPENAI_MODEL=gpt-4o-mini-2024-07-18
FINE_TUNED_MODEL=ft:gpt-4o-mini-2024-07-18:your-org::your-model-id
OPENAI_TRAINING_FILE_ID=your-training-file-id
The training data is stored in training_data.jsonl
. Each line contains a JSON object with the following structure:
{
"messages": [
{
"role": "user",
"content": "Game state: [['', '', ''], ['', '', ''], ['', '', '']]. You're 'O'. What's your next move?"
},
{
"role": "assistant",
"content": "{\"row\": 0, \"col\": 0}"
}
]
}
You can add more training examples to improve the AI's performance.
Run the upload script to send your training data to OpenAI:
npm run train:upload
This will output a File ID. Copy this ID and update your .env
file's OPENAI_TRAINING_FILE_ID
.
Start the fine-tuning process:
npm run train:create
This will output a Fine-tune Job ID. The process may take several minutes to complete.
Check status of all fine-tuning jobs:
npm run train:status
Check status of a specific job:
npm run train:status <job-id>
Once fine-tuning is complete, update your .env
file with the new model ID:
FINE_TUNED_MODEL=ft:gpt-4o-mini-2024-07-18:your-org::your-new-model-id
Now your game will use the newly trained model for the AI opponent.
npm run start:dev
npm run build
npm run start:prod
- Number keys (1-9) to make moves:
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
- 'r' to reset the game
- 'q' or 'Esc' to quit
- Tab/Shift+Tab to cycle focus
- Update the training data in
training_data.jsonl
with new examples - Re-run the training process
- Update the model ID in your
.env
file
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
- OpenAI for the GPT API
- Blessed library for terminal UI
- NestJS framework