Creating a simple word game with OpenAI

OpenAI’s ChatGPT can be pretty remarkable to interact with. Their API lets you build this functionality into your own applications! Neptyne is a fantastic sandbox for getting your applications off the ground and into the hands of users.

In this example, we’ll be building a simple game in which your objective is to say the same word as the AI! You can test out the application here: https://app.neptyne.com/-/h7pxwnsp7p.

To get started, you’ll need to create an OpenAI account and add your secret key from this page https://platform.openai.com/account/api-keys . Check out our previous entry TODOLINK for more information about secrets in Neptyne.

When working with the OpenAI api, in each request you send the context of the entire conversation. You can run the build_messages function in the REPL to see the current prompt.

At the start it reads:

[{'role': 'system',  'content': 'You and the user are playing a game trying to guess the same word. Each round both you and the user will guess a new word. You and the user should use the context of the conversation to try to converge on the same word. Only ever respond with 1 word. Do not repeat words. Guess a random word to start.'}]

The role of ‘system’ lets the AI know that this content is instructions of how it should act.

I guess the word zipper and from passing that prompt to the OpenAI model the model randomly guesses ‘banana’. The current prompt now reads:

 [{'role': 'system',  'content': 'You and the user are playing a game trying to guess the same word. Each round both you and the user will guess a new word. You and the user should use the context of the conversation to try to converge on the same word. Only ever respond with 1 word. Do not repeat words. Guess a random word to start.'}, {'role': 'assistant', 'content': banana}, {'role': 'user', 'content': 'zipper'}, {'role': 'system',  'content': "Guess another word based on the previous guesses to try to match the user's next word. Weigh the last guess of yourself and the user heavily, but consider all words both you and the user have guessed."}]

There are two new roles in the history now. ‘User’ refers to what the user has sent, and ‘assistant’ are the AI’s previous responses.

I now guess the word ‘peel’, my thought process being that pulling a zipper is kind of like peeling a banana. And ask OpenAI for a word as well. We both guessed ‘peel’! Success!

You did it in 2tries! Press reset to play again!
You Said
You Said
zipper
banana
peel
peel