Added condition that early messages are shorter.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-10-06 13:13:40 -07:00
parent 047c1ac8ff
commit 56b20f6e69
4 changed files with 23 additions and 8 deletions

View File

@@ -3,14 +3,15 @@ from . import config, prompt, history
_client = OpenAI(api_key=config.OPENAI_API_KEY)
async def generate_reply() -> str:
async def generate_reply(short: bool = False) -> str:
msgs = history.build_messages(prompt.SYSTEM_PROMPT)
msgs.append({"role": "user", "content": prompt.USER_NUDGE})
# Use a tighter nudge for early small-talk
msgs.append({"role": "user", "content": prompt.EARLY_NUDGE if short else prompt.USER_NUDGE})
resp = _client.chat.completions.create(
model=config.OPENAI_MODEL,
messages=msgs,
temperature=0.8,
max_tokens=180,
max_tokens=80 if short else 180,
presence_penalty=0.3,
frequency_penalty=0.2,
)

View File

@@ -34,12 +34,15 @@ async def run():
async def debounced_reply(chat_id: int):
await asyncio.sleep(config.DEBOUNCE_SEC)
try:
reply = await ai.generate_reply()
# If it's the 1st or 2nd user message in the conversation, keep it extra short
user_count = history.count_role("user")
short_mode = user_count in (1, 2)
reply = await ai.generate_reply(short=short_mode)
except Exception as e:
print(f"OpenAI error: {e}")
reply = "sorry—mind saying that a bit slower? I get lost easy."
reply = "hey—sorry, got a bit mixed up. how are you doing?"
history.append("assistant", reply)
await tg_helpers.send_with_catchup(client, chat_id, reply, ai.generate_reply)
await tg_helpers.send_with_catchup(client, chat_id, reply, lambda: ai.generate_reply(short=short_mode))
@client.on(events.NewMessage(incoming=True))
async def on_msg(event):

View File

@@ -40,3 +40,9 @@ def build_messages(system_prompt: str):
if r["role"] in ("user", "assistant"):
msgs.append({"role": r["role"], "content": r["content"]})
return msgs
def count_role(role: str) -> int:
"""Return how many messages of a given role are in the (pruned) history."""
recs = prune(load())
return sum(1 for r in recs if r.get("role") == role)

View File

@@ -25,3 +25,8 @@ Output:
""".strip()
USER_NUDGE = "Please respond as Jon, following all the rules above. Keep it brief (1-4 short sentences)."
EARLY_NUDGE = (
"Please respond as Jon with 1 short line (max 12 short sentences). "
"Keep it casual, a tiny typo is okay. A simple greeting + one friendly followup is enough."
)