- 1 if "joke" in message.lower():
- 2 try:
- 3 logger.info("Initializing OpenAI client for NSFW joke")
- 4 client = OpenAI(api_key=config['xai_api_key'], base_url=config['api_base_url'])
- 5 # Get recent jokes from history (last 3 assistant responses with "joke" intent)
- 6 recent_jokes = [msg['content'] for msg in list(history)[-6:] if msg['role'] == 'assistant' and 'joke' in msg.get('content', '').lower()]
- 7 recent_jokes_str = "; ".join(recent_jokes) if recent_jokes else "none"
- 8 joke_prompt = (
- 9 f"Generate a crude, adult-themed joke in British English, using varied themes (e.g., food, animals, objects, pub life) and slang like 'shagging', 'knob', 'bollocking', or 'tits up', "
- 10 f"similar in tone to 'The cheeky carrots were proper knobbed up in the soup!' "
- 11 f"Keep it short, cheeky, suitable for an IRC audience, and distinct from these recent jokes: {recent_jokes_str}. "
- 12 f"Deliver the joke in one sentence."
- 13 )
- 14 response = client.chat.completions.create(
- 15 model="grok-3",
- 16 messages=[
- 17 {"role": "system", "content": joke_prompt},
- 18 {"role": "user", "content": "Tell me a crude NSFW joke."}
- 19 ],
- 20 temperature=0.9, # random af
- 21 max_tokens=50,
- 22 timeout=config['api_timeout']
- 23 )
- 24 reply = response.choices[0].message.content.strip()
- 25 reply = normalize_reply_text(reply)
- 26 logger.info(f"Generated NSFW joke: {reply}")
- 27 except (APIError, APIConnectionError, Timeout, BadRequestError) as e:
- 28 logger.error(f"Joke API call failed: {type(e).__name__}: {str(e)}")
- 29 reply = "The naughty spud got caught shagging in the stew!"
- 30 history.append({"role": "user", "content": message})
- 31 history.append({"role": "assistant", "content": reply})
- 32 save_history(session_key, history)
- 33 return jsonify({'reply': reply}), 200, {'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0', 'X-Session-ID': session_id, 'X-Timestamp': timestamp}
Raw Paste