File size: 2,760 Bytes
c4f0624
07bd23e
c4f0624
 
07bd23e
c4f0624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from openai import OpenAI
from dotenv import load_dotenv
import os

load_dotenv()


class LLMHandler:
    def __init__(self, model_name="gpt-4o-mini"):
        """
        Initializes the LLMHandler with the specified OpenAI model.
        """
        self.openai_api_key = os.getenv("OPENAI_API_KEY")
        if not self.openai_api_key:
            raise ValueError("OPENAI_API_KEY environment variable not set.")

        # Initialize OpenAI client
        self.client = OpenAI(api_key=self.openai_api_key)
        self.model_name = model_name

    def generate_response(self, user_prompt, data):
        """
        Generate a concise response using the LLM based on user prompt and data.
        :param user_prompt: Prompt provided by the user.
        :param data: Dictionary containing the instance information.
        :return: Generated response text.
        """
        # Refined prompt to handle encoding and formatting
        prompt = (
            f"You are a professional AI model tasked with writing personalized invite texts "
            f"that are concise (less than 40 words), brochure-suitable, and tailored as per the category in the given sample.\n\n"
            f"Consider the user prompt: {user_prompt}\n\n"
            f"Details of the individual:\n"
            f"- Name: {data['Name']}\n"
            f"- Job Title: {data['Job Title']}\n"
            f"- Organisation: {data['Organisation']}\n"
            f"- Area of Interest: {data['Area of Interest']}\n"
            f"- Category: {data['Category']}\n\n"
            f"The response **MUST**:\n"
            f"- Start with 'Hello {data['Name']}'.\n"
            f"- Be concise, professional, and STRICTLY DO NOT generate invalid characters or encoding errors (e.g. 'SoraVR’s').\n"
            f"- Use standard English punctuation, such as single quotes (e.g., 'can't', 'it's').\n"
            f"- STRICTLY Give only one response for the Category the sample belongs to.\n"
            f"- Do NOT include preambles or unnecessary text.\n\n"
            f"Return the final response cleanly, without any extraneous symbols or characters."
        )

        # Query the OpenAI client and return the response
        completion = self.client.chat.completions.create(
            model=self.model_name,
            messages=[
                {"role": "system", "content": "You are a professional assistant."},
                {"role": "user", "content": prompt},
            ]
        )

        # Extract and clean the generated response
        response = completion.choices[0].message.content.strip()

        # Optional: Post-process to clean invalid characters
        #response_cleaned = response.encode('utf-8').decode('utf-8', errors='ignore')

        return response