Friends_forever / src /generate_jokes.py
ak0601's picture
Upload 6 files
e386167 verified
raw
history blame
4.93 kB
import json
import random
from openai import OpenAI
import os
from dotenv import load_dotenv
from extract_features import parse_chat, extract_inside_jokes, random_memory
# Load environment variables
load_dotenv()
def generate_inside_jokes(count=3):
# Configure Groq API
client = OpenAI(
api_key=os.environ.get("GROQ_API_KEY"),
base_url="https://api.groq.com/openai/v1",
)
messages = parse_chat("chat.txt")
data = extract_inside_jokes(messages)
# meaningful memory for Sarah
random_real_memory = select_meaningful_memory(messages, client)
prompt = f"""
You are a comedian whoose task is to generate funny inside jokes for two best friends: Aman and Sarah.
They have a very close, playful, supportive and affectionate friendship.
Here is the context from their WhatsApp chats:
Funny lines they've said:
{json.dumps(data['funny'][:30], indent=2)}
Cute moments between them:
{json.dumps(data['cute'][:20], indent=2)}
Shared vocabulary & patterns:
{json.dumps(data['top_words'][:20], indent=2)}
One real memory to set the mood:
"{random_real_memory}"
Your Goal:
Generate {count} ORIGINAL inside jokes that sound exactly like something they would say to each other.
The jokes should be a mix of funny, teasing, and heartfelt(more of funny).
Guidelines:
1. **Be Human**: Use casual language, slang (if fits their vibe), and natural phrasing. Avoid robotic or stiff sentence structures.
2. **Show, Don't Tell**: Instead of saying "You are funny," say "Remember that time you tried to tell a joke and choked on water? Classic."
3. **Mix Humor and Heart**: The best inside jokes often come from a place of love. "Fries before guys" is funny but also shows loyalty.
4. **Use Context**: Reference their shared vocabulary or specific funny/cute moments provided above.
5. **Be creative**: The best inside jokes often come from a place of love. "Fries before guys" is funny but also shows loyalty.
6. Try to make atleast one line jokes
Output Format:
INSIDE JOKES:
1. [Joke/Phrase]
2. [Joke/Phrase]
...
MEMORY FOR SARAH:
"{random_real_memory}"
"""
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
def select_meaningful_memory(messages, client):
"""Selects a meaningful memory using LLM."""
# Filter for potentially interesting messages to save tokens
candidates = [m["text"] for m in messages if len(m["text"]) > 15]
if not candidates:
return "Remember that time we couldn't stop laughing? Good times."
# Sample if too many
if len(candidates) > 50:
candidates = random.sample(candidates, 50)
candidates_str = "\n".join([f"- {c}" for c in candidates])
prompt = f"""
Here are some excerpts from a chat history between two best friends, Aman and Sarah:
{candidates_str}
Your Task:
Select ONE specific message from this list that represents a "meaningful memory".
Criteria for "meaningful":
- A plan to meet (e.g., "Let's go to that movie", "Dinner tonight?")
- A sweet/emotional moment
- An inside joke or funny observation
- NOT just "Hello" or "Okay"
Return ONLY the exact text of the selected message. Nothing else.
"""
try:
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
except Exception:
return random.choice(candidates)
def generate_motivation():
# Configure Groq API
client = OpenAI(
api_key=os.environ.get("GROQ_API_KEY"),
base_url="https://api.groq.com/openai/v1",
)
try:
with open("sarah_about.txt", "r", encoding="utf-8") as f:
about_sarah = f.read()
except FileNotFoundError:
return "You are stronger than you know. Keep shining! ✨"
prompt = f"""
You are a supportive, wise, and kind friend.
Read this context about Sarah:
"{about_sarah}"
Your Task:
Generate a short, powerful, and personalized motivational thought for Sarah.
It should directly address her struggles (past burdens, insecurity) but focus on her strengths (kindness, resilience, future in Air Force).
Make it sound like it's coming from a place of deep understanding and belief in her.
Keep it under 2 sentences.
Output ONLY the motivational message.
"""
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
if __name__ == "__main__":
print(generate_inside_jokes(5))