Spaces:
Sleeping
Sleeping
| 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 | |