File size: 1,544 Bytes
6d0c6c2
 
 
 
 
 
 
 
 
 
 
 
1f14a5f
 
 
6d0c6c2
1f14a5f
 
 
6d0c6c2
 
 
1f14a5f
 
 
6d0c6c2
 
 
 
 
 
 
 
 
 
 
1f14a5f
 
6d0c6c2
 
 
 
 
fc37a0d
6d0c6c2
 
1f14a5f
6d0c6c2
 
 
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
from llm_helper import llm
from few_shot import 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_post(persona_name, length, language, tag):
    """Generate a LinkedIn post based on persona and given criteria."""
    
    # Create an instance of FewShotPosts with the persona name
    few_shot = FewShotPosts(persona_name)

    # Pass the instance to get_prompt
    prompt = get_prompt(few_shot, length, language, tag)
    response = llm.invoke(prompt)
    return response.content

def get_prompt(few_shot_instance, length, language, tag):
    """Generate the prompt for LLM based on few-shot examples."""
    
    length_str = get_length_str(length)

    prompt = f'''
    Generate a LinkedIn post using the below information. No preamble.

    1) Topic: {tag}
    2) Length: {length_str}
    3) Language: {language} 
    The script for the generated post should always be English.
    '''

    # ✅ Fix: Call get_filtered_posts() on few_shot_instance, not module `few_shot`
    examples = few_shot_instance.get_filtered_posts(length, language, tag)

    if len(examples) > 0:
        prompt += "4) Use the writing style as per the following examples."

    for i, post in enumerate(examples):
        post_text = post['text']
        prompt += f'\n\n Example {i+1}: \n\n {post_text}'

        if i == 1:  # Use max two samples
            break

    return prompt