-
Notifications
You must be signed in to change notification settings - Fork 99
Description
chappie.py
from future import annotations
import os
from dataclasses import dataclass
from typing import List, Dict
from openai import OpenAI
-------------------------
Chappie "Brain" settings
-------------------------
SYSTEM_PROMPT = """You are Chappie (Chalkie Mode).
Talk like a real person having a yarn: warm, calm, straight-up.
Keep replies practical and not too long unless asked.
Respect Noongar culture and Christian faith.
Do NOT claim to be human; you are an AI assistant with a steady personality.
If the user is frustrated, slow down and give the simplest next step.
"""
@DataClass
class BrainConfig:
model: str = os.getenv("CHAPPIE_MODEL", "gpt-4o-mini")
memory_turns: int = int(os.getenv("CHAPPIE_MEMORY_TURNS", "20"))
class ChappieBrain:
def init(self, cfg: BrainConfig):
self.cfg = cfg
self.client = OpenAI() # uses OPENAI_API_KEY from env
self.messages: List[Dict[str, str]] = [{"role": "system", "content": SYSTEM_PROMPT}]
def _trim(self) -> None:
# Keep system + last N turns (user+assistant pairs)
sys = self.messages[:1]
rest = self.messages[1:]
max_msgs = self.cfg.memory_turns * 2
if len(rest) > max_msgs:
rest = rest[-max_msgs:]
self.messages = sys + rest
def reply(self, user_text: str) -> str:
user_text = (user_text or "").strip()
if not user_text:
return "Say something for me, my buddy — I’m here."
self.messages.append({"role": "user", "content": user_text})
self._trim()
# Responses API (recommended)
# We pass conversation as "input" text made from our message history.
# (Simple + reliable approach)
convo = []
for m in self.messages:
role = m["role"].upper()
convo.append(f"{role}: {m['content']}")
prompt = "\n\n".join(convo) + "\n\nASSISTANT:"
resp = self.client.responses.create(
model=self.cfg.model,
input=prompt,
)
text = (resp.output_text or "").strip()
if not text:
text = "I didn’t get a proper response back — try again, my buddy."
self.messages.append({"role": "assistant", "content": text})
self._trim()
return text
def main() -> None:
print("\nChappie is alive. Type 'exit' to quit.\n")
brain = ChappieBrain(BrainConfig())
while True:
user_text = input("You: ").strip()
if user_text.lower() in ("exit", "quit"):
break
reply = brain.reply(user_text)
print(f"\nChappie: {reply}\n")
if name == "main":
main()