Can cheap, fast, Natural Language Processing be the brains inside a game? Could I find out without building just another chatbot?
Well, yeah -here's the code. And I even wrote:
- a blogpost on buiding a local-hosted version you can run on your own machine over in this other repo
- a blogpost about updating the local version to run on the web - you should probably read this as it's about this repo
- a deployed version of the repo you're looking at now which you can play for yourself online
- and even a completely no-code implementation
You (the player) are communicating remotely with a distant Cleanerbot, directing it on a search and rescue mission on a burning spaceship to excuse any latency between input and response. You speak rather than type your inputs. This is a 21st century text adventure after all.
The index.html file is the web interface which triggers the start of a new session on load, then records audio from the player and passes it to the Flask file, main.py.
This Flask application uses Google Cloud Speech-to-Text for voice commands and the Claude API for processing. Claude's response is compared to a large IF statement ('game logic') which then determines what information is written to a response_log.
That response_log is then parsed and presented to the player on a webpage. Most commonly, the responses use the function add_response() or add_response_special() which are just ways of informing what CSS the webage should use to present the response.
- Voice Commands: Users can send commands through voice recordings.
- Session Management: Each user session is uniquely identified and managed.
- Responsive Design: The application is optimized for both desktop and mobile devices.
- Game State Saving: Players can save and load their game state using unique phrases.
- Error Logging: Tracks and logs errors encountered during gameplay.
- Voice Command Handling: Processes voice commands to interact with the game.
-
Clone the repository:
git clone https://github.com/your-username/cleanerbot-rescue.git
-
Navigate to the project directory:
cd cleanerbot-rescue -
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Set up Google Cloud Storage: Ensure you have a GCS bucket set up and have the necessary credentials stored in an environment variable
GOOGLE_APPLICATION_CREDENTIALS. -
Populate your Google Cloud Storage bucket Unzip the files in GCS.zip and upload them to the bucket:
baseprompt.txt- separated from the flask file for easier admins1.txt,s2.txt,s3.txt- word lists used for saving a player's game stategamesaves.txt- csv containing saved games and their code phrasesactioncounts.txtanderrorcounts.txt- used in the player's report card when they complete the game.
-
Set up Google App Engine: You'll need to create an area in which you can host the game. That's where all the other files go.
Environment Variables:
- your_anthropic_api_key: Your Anthropic API key
- GOOGLE_APPLICATION_CREDENTIALS: Path to your Google Cloud credentials JSON file.
- GCS_BUCKET_NAME: The name of your GCS bucket.
*** Don't forget that as well as adding the path to your Google Cloud credentials you'll need to include the JSON file itself in your version ***
I've split this between index.html and main.py.
The main structure of the application is defined in the index.html file. It includes:
- Header: Contains metadata and links to external resources.
- Body:
- A header (
<h1>) for the game title. - A content div (
.content) to display responses. - A footer (
.footer) with buttons for user interaction.
- A header (
The styles are embedded within the <style> tag in the index.html file. Key styles include:
- Background and Text: Customized to create a gaming atmosphere.
- Buttons: Styled for various states (enabled, disabled, mouseover).
The JavaScript code handles:
- Voice Recording: Uses the
MediaRecorderAPI to capture audio. - Session Management: Generates and manages unique session IDs for users.
- Dynamic Content: Updates the content and footer based on user interactions and responses.
- Font Awesome: For icons used in the footer.
- Audio Recorder Polyfill: To ensure compatibility across browsers for audio recording.
- record_endpoint(): Processes the audio input to text, sends that text to Claude, and applies Claude's response to a great big IF statement of game logic.
- add_response(): Adds a new entry to the response_log in the player's session file, which
index.htmlparses and displays on the webpage.
Errors encountered during gameplay are logged, and appropriate messages are returned to the player.
Each player's session is controlled through a unique session file stored in Google Cloud Storage. Note that while we have mechanisms for closing a session (for example inactivity of >30 mins) there's no mechanism for preiodically clearing 'dead' session files. You can handle this with a cron job.
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
- Make your changes.
- Commit your changes:
git commit -m "Description of changes" - Push to your branch:
git push origin feature-branch
- Create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.