File size: 3,064 Bytes
765a4ee 45fc613 f7a52fb 7692c8e 45fc613 7692c8e 765a4ee f7a52fb 45fc613 17bea6a 45fc613 f7a52fb 765a4ee 0c02355 765a4ee 0c02355 e1a012a 0c02355 765a4ee e1a012a 0c02355 765a4ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
from llm_helper import llm
from few_shot import FewShotPosts
few_shot = FewShotPosts()
def get_length_str(length):
if length == "Short":
return "1 to 5 lines"
if length == "Medium":
return "6 to 10 lines"
if length == "Long":
return "11 to 15 lines"
def generate_closing_line(language, tag, tone):
"""
Generate a closing line using the LLM based on language, tag, and tone.
"""
closing_prompt = f"""
You are writing a LinkedIn post. Create a concise and engaging closing line.
- The closing line should reflect the topic: "{tag}".
- Use the tone/style: "{tone}".
- The closing line must encourage engagement or provide a call to action, relevant to the topic.
- Use the language: "{language}" (Hinglish means Hindi phrases written in English script).
Examples:
- Topic: "Job Search", Tone: "Motivational", Language: "English"
Closing Line: "Your dream job is closer than you think. Stay determined! π"
- Topic: "Mental Health", Tone: "Professional", Language: "English"
Closing Line: "Your mental well-being is essential. Letβs discuss ways to manage stress. π‘"
- Topic: "Dating", Tone: "Informal", Language: "Hinglish"
Closing Line: "Apka perfect date idea kya hai? Neeche share karein! π"
Now, write a relevant closing line for the following inputs:
Topic: "{tag}"
Tone: "{tone}"
Language: "{language}"
"""
response = llm.invoke(closing_prompt)
return response.content.strip()
def generate_post(length, language, tag, selected_tone=None):
"""
Generate a LinkedIn post dynamically with LLM including a generated closing line.
"""
prompt = get_prompt(length, language, tag)
response = llm.invoke(prompt)
post_content = response.content
# Generate a dynamic closing line using LLM
if selected_tone and tag:
try:
closing_line = generate_closing_line(language, tag, selected_tone)
post_content += f"\n\n\n{closing_line}"
except Exception as e:
# Fallback in case of LLM failure
post_content += f"\n\nThank you for reading. Your feedback is valued! π"
return post_content
def get_prompt(length, language, tag):
length_str = get_length_str(length)
prompt = f'''
Write a professional, engaging LinkedIn post.
1. Topic: "{tag}"
2. Post Length: "{length_str}"
3. Language: "{language}" (Hinglish means Hindi phrases written in English script).
4. Incorporate creativity, enthusiasm, emotional appeal, and actionable advice.
'''
examples = few_shot.get_filtered_posts(length, language, tag)
if examples:
prompt += "\nExamples of great posts:\n"
for i, post in enumerate(examples[:2]): # Limit to 2 examples
post_text = post['text']
prompt += f"Example {i + 1}: {post_text}\n"
prompt += "\nNow write the post."
return prompt
if __name__ == "__main__":
print(generate_post("Medium", "English", "Mental Health")) |