DEV Community

kk mors
kk mors

Posted on

The 3,000-Year-Old Algorithm That Powers Modern AI Fortune Telling

BaZi is not mysticism — it's an algorithm.

When most Westerners hear "Chinese fortune telling," they picture incense, turtle shells, and a mysterious old man. But BaZi (八字, "Eight Characters") is different. It's a deterministic, rules-based system that's been refined over 3,000 years — and modern AI is proving to be its perfect interpreter.

Here's how it works, why AI excels at it, and how you can calculate someone's destiny in 50 lines of Python.


The Algorithm: Your Birth Date → 8 Characters

BaZi distills your birth year, month, day, and hour into four pillars. Each pillar has a Heavenly Stem (天干) and an Earthly Branch (地支). Together, that's 8 characters — hence the name.

The mapping isn't arbitrary. It uses:

  • The 10 Heavenly Stems (五行 elements × Yin/Yang)
  • The 12 Earthly Branches (zodiac animals)
  • The Sexagenary cycle: a 60-unit calendar system running since 2637 BCE

The "magic" is in the interactions: each element generates or controls others (Wood feeds Fire, Water controls Fire, etc.). Your chart's balance of elements reveals your personality, career tendencies, and life patterns.


Calculating It in Python

The hardest part? Converting a Gregorian date to the Chinese lunar calendar. The solar terms add complexity — a "month" begins at a specific solar longitude, not on the 1st.

lunar-python handles all of this:

from lunar_python import Solar

solar = Solar.fromYmdHms(1988, 12, 30, 12, 0, 0)
lunar = solar.getLunar()
bazi = lunar.getEightChar()

print(f"Year:  {bazi.getYear()}")      # 戊辰
print(f"Month: {bazi.getMonth()}")      # 甲子
print(f"Day:   {bazi.getDay()}")        # 己
print(f"Hour:  {bazi.getTime()}")       # 庚午
print(f"Day Master: {bazi.getDay()}")   # 己 (Earth)
Enter fullscreen mode Exit fullscreen mode

The Day Master (日主) is the chart's center — it represents "you." Everything else is interpreted relative to this element.

Hidden Data in Each Pillar

Each pillar contains more than just its stem and branch. The Hidden Stems (藏干) add layers of element influence:

print(f"Year hidden stems:  {bazi.getYearHideGan()}")   # 戊, 乙, 癸
print(f"Month hidden stems:  {bazi.getMonthHideGan()}")  # 甲
print(f"Day hidden stems:    {bazi.getDayHideGan()}")    # 己, 丁, 乙
Enter fullscreen mode Exit fullscreen mode

The Ten Gods (十神) mapping each element's relationship to your Day Master reveals career inclinations, wealth potential, and relationship patterns.


Why AI is Surprisingly Good at This

Here's the key insight: BaZi maps perfectly to what LLMs do best.

1. Pattern matching across large reference tables — The 60 pillars, element cycles, Ten Gods... combinatorial lookup. LLMs excel at it.

2. Context-aware text generation — A human astrologer synthesizes dozens of interacting factors. AI juggles element balance, seasonal strength, clash/compatibility, and luck cycles simultaneously.

3. Cultural knowledge baked in — Models already "understand" the metaphors (Water = wisdom, Fire = passion, Earth = stability).

4. No cold reading — AI generates readings purely from the chart's mathematical structure. No guessing, no leading questions.

The result? AI-generated BaZi readings can be surprisingly insightful — not because the AI has mystical powers, but because it's processing a rigorous symbolic system at scale.


What I Built

I turned this into SoulChart — a web app that:

  • Takes your birth date/time (no email or account required)
  • Calculates your full BaZi chart with Python
  • Generates a personalized reading via DeepSeek AI
  • Shows a free preview — unlock the full report for $3.99

The free preview shows your Day Master personality. The full reading covers career, relationships, current-year luck, and life advice across 6 sections.


The Bigger Picture

Every ancient knowledge system is a compressed dataset. BaZi encodes centuries of observed human patterns into a symbolic grammar. Tarot, I Ching, Western astrology — same principle, different encoding.

AI is the first technology that can truly "decompress" these systems. Not by proving them right or wrong — but by applying their logic consistently and exhaustively.

BaZi's element theory has interesting parallels with modern personality frameworks like the Big Five — both describe tendencies on spectrums rather than fixed types. The ancient Chinese weren't "superstitious" — they were building classification systems with the best tools they had.


Try It

  • soulchart.cc — Enter your birth date, get a free reading
  • The calculation engine is open-source (lunar-python library)
  • If you're building an AI interpretation layer: key is constraining the model to use proper BaZi terminology and framing everything as "tendencies"

Fortune telling is a 3,000-year-old language. AI is the first truly fluent translator.

Top comments (0)