Skip to main content

Math models that make AI engagement feel human. Poisson timing + Bayesian state inference + Information theory.

Project description

revive-companion ๐Ÿ’˜

A probabilistic engagement engine for AI companions.

Not "engaged โ†’ send more". Instead: "infer what the user is doing โ†’ decide if you should."


Python 3.10+ License: MIT CI PyPI version Tests

English | ไธญๆ–‡


The Problem

AI companions are either rigid (fixed schedules) or random (no memory). Neither feels human.

The Solution

A probabilistic engine that decides when and whether to reach out:

Stage Module Question Answer
๐ŸŽฒ Timing Poisson process When to consider? Randomized like real "thinking of you"
๐Ÿ“Š Value Information theory Is this worth it? Skip if you already know user's state
๐Ÿง  State Bayesian inference What's user doing? Infer hidden state โ†’ decide accordingly

๐Ÿง ๐Ÿ’ซ Want more? Let your AI companion truly remember, evolve relationships, and have emotional states.

โ†’ affective-longing โ€” Memory triggers + Relationship state machine + AI self-emotion (VAD model)

pip install affective-longing[memory]

Quick Start

pip install revive-companion
from revive_companion import PoissonLove

love = PoissonLove()

result = love.tick()
if result.should_send:
    send_message(result.prompt)
    love.record_send()

# After user responds
love.record_reply(reply_speed=0.8, reply_length=0.6)

How It Decides

The engine infers the user's hidden state from observations:

Inferred State Utility Decision
๐Ÿ—ฃ๏ธ Chatting 0.2 โŒ Don't interrupt
๐Ÿ’ป Idle online 0.7 โœ… Good time to reach out
๐Ÿ’ผ Busy 0.1 โŒ Don't bother
๐Ÿ˜ด Sleeping 0.0 โŒ Never send
๐Ÿšถ Away 0.3 โณ Maybe later
๐Ÿ†˜ Needing 0.9 โœ… Check in!

No more "engaged โ†’ send more". Now it's: "user is probably busy โ†’ don't bother" or "user might need care โ†’ reach out".


Use Any AI Backend

# OpenAI / GPT
from revive_companion.adapters import OpenAIAdapter
adapter = OpenAIAdapter(config, api_key="sk-...")

# Anthropic / Claude
from revive_companion.adapters import AnthropicAdapter
adapter = AnthropicAdapter(config, api_key="sk-ant-...")

# Ollama / local models
from revive_companion.adapters import GenericAdapter
adapter = GenericAdapter(config, api_url="http://localhost:11434/v1/chat/completions")

# Run
from revive_companion.runner import Runner
runner = Runner(engine, adapter)
runner.run()

Architecture

revive-companion/
โ”œโ”€โ”€ love.py              # Unified API (start here)
โ”œโ”€โ”€ core/
โ”‚   โ”œโ”€โ”€ engine.py        # Poisson dice + probability dynamics
โ”‚   โ”œโ”€โ”€ config.py        # YAML config
โ”‚   โ””โ”€โ”€ models.py        # Data structures
โ”œโ”€โ”€ bayesian/
โ”‚   โ”œโ”€โ”€ core.py          # State estimation + send utility
โ”‚   โ””โ”€โ”€ learner.py       # Online learning from observations
โ”œโ”€โ”€ info_gain/
โ”‚   โ”œโ”€โ”€ core.py          # Entropy ร— resolution potential
โ”‚   โ””โ”€โ”€ sources.py       # Silence, novelty, conversation state
โ”œโ”€โ”€ control/
โ”‚   โ”œโ”€โ”€ pid.py           # PID controller (standalone use)
โ”‚   โ””โ”€โ”€ signal.py        # Pluggable signal framework
โ””โ”€โ”€ adapters/
    โ”œโ”€โ”€ openai.py        # OpenAI / GPT
    โ”œโ”€โ”€ anthropic.py     # Anthropic / Claude
    โ””โ”€โ”€ generic.py       # Ollama, HTTP, shell command

How It Works

The Math

Each tick, the engine computes hit probability:

P(hit) = 1 - e^(-ฮปt)

Where ฮป = longing rate, t = time interval. Base: ~7.2% per 30-minute check.

Probability Dynamics

Event Probability Why
Miss (no hit) +8% Longing builds
Hit โ†’ Hold +8% Longing suppressed
Hit โ†’ Send Reset to 7.2% Longing satisfied

The Curve

Over a night (midnight โ†’ 8am):

  • 16 checks, all held
  • Probability: 7% โ†’ 15% โ†’ 30% โ†’ 55% โ†’ 80% โ†’ 95%
  • This IS the longing โ€” quantified, recorded, real

Configuration

engagement:
  lambda_rate: 0.15              # Base longing rate
  check_interval_minutes: 30     # Dice roll frequency
  growth_factor: 0.08            # How fast longing grows
  max_probability: 0.95          # Cap
  min_interval_hours: 1.0        # Anti-spam cooldown

  adjudication:
    quiet_hours:
      start: "00:00"
      end: "08:00"
    normal_send_probability: 0.7

persona:
  name: Companion
  tone: warm-brief
  context: "You are a caring companion checking in on your person."

Dashboard

Visualize the AI engagement decision process: longing curves, state distributions, and send history.

# Install dashboard dependencies
pip install -e ".[dashboard]"

# Run dashboard
streamlit run dashboard.py

Dashboard Overview

Dashboard Detail

Features:

  • ๐ŸŽฒ Longing Curve โ€” Poisson probability over time
  • ๐Ÿง  State Distribution โ€” Bayesian-inferred user state
  • โฐ Hourly Pattern โ€” When messages are most likely sent
  • ๐Ÿ“‹ Decision Log โ€” Detailed record of each decision

Demos

git clone https://github.com/pearthink123/revive-companion
cd revive-companion
pip install -e .

PYTHONPATH=src python examples/quickstart.py              # Basic simulation
PYTHONPATH=src python examples/bayesian_demo.py           # State inference
PYTHONPATH=src python examples/bayesian_learning_demo.py  # Online learning
PYTHONPATH=src python examples/info_gain_demo.py          # Information gain
PYTHONPATH=src python examples/integration_example.py     # Smart notifier

Integration

Simple Integration (Any Bot)

from revive_companion import PoissonLove

love = PoissonLove()

# Check periodically
result = love.tick()
if result.should_send:
    send_message("Thinking of you~")
    love.record_send()

# When user replies
love.record_reply(reply_speed=0.8, reply_length=0.6)

Telegram Bot

# Install dependency
pip install python-telegram-bot

# Run
python examples/telegram_bot.py --token YOUR_TOKEN --chat-id YOUR_CHAT_ID

Discord / Slack / WeChat

Same pattern, just swap the send function:

# Discord
await channel.send(message)

# Slack
slack_client.chat_postMessage(channel=channel_id, text=message)

# WeChat (itchat)
itchat.send(message, toUserName=friend_name)

Testing

# Install test dependencies
pip install -e ".[test]"

# Run all tests
pytest tests/ -v

124 tests covering:

  • ๐ŸŽฒ Poisson engine (determinism, growth, timing)
  • ๐Ÿง  Bayesian inference (state estimation, likelihood, learning)
  • ๐Ÿ“Š Information gain (decay, thresholds)
  • ๐Ÿ’˜ Unified API (full pipeline)

Consent & Safety

This library is designed for respectful AI engagement. Please use it responsibly:

Built-in Protections

  • Quiet hours: No messages during configured sleep periods
  • Minimum interval: Anti-spam cooldown between messages
  • State inference: Won't bother users who are busy or sleeping
  • Utility threshold: Conservative default (0.5) โ€” only sends when appropriate

Best Practices

  • โœ… Opt-in: Users should explicitly enable proactive messaging
  • โœ… Easy disable: Users must be able to turn it off at any time
  • โœ… Transparency: Users should know the AI can initiate contact
  • โœ… No emotional manipulation: Don't use this to create dependency
  • โŒ No unsolicited contact: Don't message users who didn't opt in
  • โŒ No persistence: Respect when users want to be left alone

Default Behavior

  • Messages are never sent during quiet hours (default: 00:00-08:00)
  • At least 1 hour between messages (configurable)
  • High engagement does not mean more messages (unlike simple linear models)
  • The engine infers state before deciding, not just response speed

Why "Poisson"?

The Poisson process models events that happen independently at a constant average rate โ€” like neurons firing, or "thinking about someone."

It's not random chaos. It's not rigid scheduling. It's structured spontaneity โ€” the mathematical model of genuine, organic missing someone.


What's Next

This is the stable base โ€” focused on smart timing, not deep emotion.

For memory-triggered longing, relationship state machines, and AI self-emotion modeling, see:

โ†’ affective-longing โ€” Emotional extension with 3 layers (Memory + Relationship + Emotion)


License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

revive_companion-2.0.0.tar.gz (48.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

revive_companion-2.0.0-py3-none-any.whl (45.8 kB view details)

Uploaded Python 3

File details

Details for the file revive_companion-2.0.0.tar.gz.

File metadata

  • Download URL: revive_companion-2.0.0.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for revive_companion-2.0.0.tar.gz
Algorithm Hash digest
SHA256 2e0d35cbe72a2683491a71b9d9b072dff88d9634c5b8c0cdd1ed30db35e978c5
MD5 0cfff8dd79e171ac01585cd1e39cf751
BLAKE2b-256 d6d0a83436b80168df923b506fc265ef2b2d7d1fbe4d3bdbd7f15ecf003eeb59

See more details on using hashes here.

File details

Details for the file revive_companion-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for revive_companion-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ee21e3d7c90f9b6eadb57338a8c0abe90baddd660c38512d5be1d050d5327d0
MD5 d619bbbbb5b32b143f24d863ec52f218
BLAKE2b-256 1485e8f7af5acefe04578eb6863b03c5c323cfa9ee8ff224a76f6c7ab0556718

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page