Oddly enough I couldn’t find anything like a proper AI Tarot card reading game online so ofcource I had to build it. Tarot reading games seem simple to implement. Building it and making a post does not take much time while the end result is quite fun! Let’s first start with importing libraries and defining the API key.

import random
import openai
import os
os.environ["OPENAI_API_KEY"] = 'API_KEY_HERE'

Below is my go-to function to interact with the openAI API. There are a variety of engines that can be used, see the OpenAI page for a detailed description of the available options. N defines the number of responses generated and temperature defines the randomness of the response (1 is maximum).

def generate_response(prompt):
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=1,
    )
    return response.choices[0].text.strip()

Define a dictionary containing all the cards in a Tarot deck.

# Define the cards in the deck
cards = [
    "The Fool", "The Magician", "The High Priestess", "The Empress", "The Emperor",
    "The Hierophant", "The Lovers", "The Chariot", "Strength", "The Hermit",
    "Wheel of Fortune", "Justice", "The Hanged Man", "Death", "Temperance",
    "The Devil", "The Tower", "The Star", "The Moon", "The Sun",
    "Judgement", "The World",
    "Ace of Wands", "Two of Wands", "Three of Wands", "Four of Wands", "Five of Wands",
    "Six of Wands", "Seven of Wands", "Eight of Wands", "Nine of Wands", "Ten of Wands",
    "Page of Wands", "Knight of Wands", "Queen of Wands", "King of Wands",
    "Ace of Cups", "Two of Cups", "Three of Cups", "Four of Cups", "Five of Cups",
    "Six of Cups", "Seven of Cups", "Eight of Cups", "Nine of Cups", "Ten of Cups",
    "Page of Cups", "Knight of Cups", "Queen of Cups", "King of Cups",
    "Ace of Swords", "Two of Swords", "Three of Swords", "Four of Swords", "Five of Swords",
    "Six of Swords", "Seven of Swords", "Eight of Swords", "Nine of Swords", "Ten of Swords",
    "Page of Swords", "Knight of Swords", "Queen of Swords", "King of Swords",
    "Ace of Pentacles", "Two of Pentacles", "Three of Pentacles", "Four of Pentacles", "Five of Pentacles",
    "Six of Pentacles", "Seven of Pentacles", "Eight of Pentacles", "Nine of Pentacles", "Ten of Pentacles",
    "Page of Pentacles", "Knight of Pentacles", "Queen of Pentacles", "King of Pentacles"
]

This next code asks the user to enter their name and then offers them a choice of three tarot readings. The user can choose one of the readings by entering a number. The program then randomly selects three tarot cards and displays them to the user based on the type of reading they chose.

It also generates a response using OpenAI’s API, which provides a two-sentence description of each card and a three-sentence summary of all three cards combined. Finally, it displays the response to the user. The program will continue to run until the user enters the word “stop”.

while True:
    user_input = input("Tell me your self given name (or type 'stop'): ")
    if user_input == 'stop':
        break
    user_input_choice = input(f"Pick your tarot reading type {user_input}: \n 1 Understanding a Situation. \n 2 Make a Descision. \n 3 Standard past/present/future. \n ")

    past_card = random.choice(cards)
    cards.remove(past_card)

    present_card = random.choice(cards)
    cards.remove(present_card)

    future_card = random.choice(cards)
    
    openai.api_key = os.environ["OPENAI_API_KEY"]
    prompt = f"Give a mystical tarot reading with a two sentence description of each card and then a three sentence summary of all cards combined: {past_card}, {present_card}, {future_card}"
    
    # Print the drawn cards
    if user_input_choice == '1':   
        print(f"Dear {user_input}, here is your reading, keep your situation in mind.")
        print("Current situation: ", past_card)
        print("Obstacle: ", present_card)
        print("Advice: ", future_card)
        print('~~~~ considering the cards ~~~~')
        txt = generate_response(prompt)
        print(f'{txt}')
    elif user_input_choice == '2':
        print(f"Dear {user_input}, here is your reading, keep your descision to make in mind.")
        print("Opportunities: ", past_card)
        print("Challenges: ", present_card)
        print("Outcome: ", future_card)
        print('~~~~ considering the cards ~~~~')   
        txt = generate_response(prompt)
        print(f'{txt}')
    else:
        print(f"Dear {user_input}, here is your reading.")
        print("Past: ", past_card)
        print("Present: ", present_card)
        print("Future: ", future_card)
        print('~~~~ considering the cards ~~~~')
        txt = generate_response(prompt)
        print(f'{txt}')
Tell me your self given name (or type 'stop'): Alex de Vries
Pick your tarot reading type Alex de Vries: 
 1 Understanding a Situation. 
 2 Make a Descision. 
 3 Standard past/present/future. 
 1
Dear Alex de Vries, here is your reading, keep your situation in mind.
Current situation:  The Hanged Man
Obstacle:  Page of Pentacles
Advice:  Page of Cups
~~~~ considering the cards ~~~~
The Hanged Man: This card shows a person suspended in midair, looking toward the sky, seemingly contemplating their situation but very still and accepting. This card symbolizes a moment of pause, where one must surrender to the situation and find new meaning.

Page of Pentacles: This card speaks of patience, a young figure sits with a pentacle in hand, a symbol of both physical and spiritual wealth. This card encourages us to take our time, consider our options and make wise investments in our own lives.

Page of Cups: Here we find a youth intently looking into a cup, a symbol of the feelings and intuitions he is currently seeking. This card invites us to explore our own emotions and feel into the possibilities open to us.

The combined message of these three cards highlights the importance of taking a moment to pause and consider our options. We are advised to listen to our intuition and use our own wisdom to make decisions that will benefit us and bring us greater wealth both materially and spiritually. We must take the time to reflect on our innermost feelings and gain clarity on our current path.

May it give you some AI generated support!

Leave a Reply

Your email address will not be published. Required fields are marked *