File size: 3,621 Bytes
765a4ee
 
 
 
 
 
 
 
 
 
 
 
 
 
45fc613
 
 
 
 
 
 
 
77e9507
45fc613
 
 
 
fbe3161
45fc613
fbe3161
45fc613
fbe3161
45fc613
 
 
 
 
 
 
 
 
f7a52fb
7692c8e
0763bc2
7692c8e
765a4ee
 
dffd28c
6f28dc4
0763bc2
45fc613
 
 
dffd28c
2981bef
45fc613
0763bc2
 
f7a52fb
2981bef
f7a52fb
765a4ee
0c02355
765a4ee
 
0c02355
e1a012a
0c02355
 
 
 
dffd28c
0c02355
765a4ee
 
e1a012a
 
 
 
dffd28c
 
 
0c02355
 
765a4ee
 
 
 
fbe3161
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
89
90
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}".
    - Add hashtags only at the end, i.e., after the closing line and not before it. Ensure that hashtags are placed one line below the closing line, leaving a decent space for better readability and structure.
    - 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! πŸš€ #DreamJob #StayMotivated"
    - Topic: "Mental Health", Tone: "Professional", Language: "English"
      Closing Line: "Your mental well-being is essential. Let’s discuss ways to manage stress. πŸ’‘ #MentalHealth #StressManagement"
    - Topic: "Dating", Tone: "Informal", Language: "Hinglish"
      Closing Line: "Apka perfect date idea kya hai? Neeche share karein! πŸ˜„ #DatingTips #FunDates"
    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.strip()

    # Generate a dynamic closing line using LLM
    if selected_tone and tag:
        try:
            closing_line = generate_closing_line(language, tag, selected_tone)
            # Append the closing line with hashtags
            post_content += f"\n\n{closing_line}\n\n"
        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.
    5. Do not include hashtags in the main content.
    '''

    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']
            # Remove hashtags from examples
            cleaned_post_text = " ".join(word for word in post_text.split() if not word.startswith("#"))
            prompt += f"Example {i + 1}: {cleaned_post_text}\n"
    
    prompt += "\nNow write the post."
    return prompt


if __name__ == "__main__":
    print(generate_post("Medium", "English", "Mental Health"))