dhanvanth183 commited on
Commit
c4f0624
·
verified ·
1 Parent(s): 988b788

Update openai_llms.py

Browse files
Files changed (1) hide show
  1. openai_llms.py +63 -63
openai_llms.py CHANGED
@@ -1,63 +1,63 @@
1
- from openai import OpenAI
2
- from dotenv import load_dotenv
3
- import os
4
-
5
- load_dotenv()
6
-
7
-
8
- class LLMHandler:
9
- def __init__(self, model_name="gpt-4o-mini"):
10
- """
11
- Initializes the LLMHandler with the specified OpenAI model.
12
- """
13
- self.openai_api_key = os.getenv("OPENAI_API_KEY")
14
- if not self.openai_api_key:
15
- raise ValueError("OPENAI_API_KEY environment variable not set.")
16
-
17
- # Initialize OpenAI client
18
- self.client = OpenAI(api_key=self.openai_api_key)
19
- self.model_name = model_name
20
-
21
- def generate_response(self, user_prompt, data):
22
- """
23
- Generate a concise response using the LLM based on user prompt and data.
24
- :param user_prompt: Prompt provided by the user.
25
- :param data: Dictionary containing the instance information.
26
- :return: Generated response text.
27
- """
28
- # Refined prompt to handle encoding and formatting
29
- prompt = (
30
- f"You are a professional AI model tasked with writing personalized invite texts "
31
- f"that are concise (less than 40 words), brochure-suitable, and tailored as per the category in the given sample.\n\n"
32
- f"Consider the user prompt: {user_prompt}\n\n"
33
- f"Details of the individual:\n"
34
- f"- Name: {data['Name']}\n"
35
- f"- Job Title: {data['Job Title']}\n"
36
- f"- Organisation: {data['Organisation']}\n"
37
- f"- Area of Interest: {data['Area of Interest']}\n"
38
- f"- Category: {data['Category']}\n\n"
39
- f"The response **MUST**:\n"
40
- f"- Start with 'Hello {data['Name']}'.\n"
41
- f"- Be concise, professional, and STRICTLY DO NOT generate invalid characters or encoding errors (e.g. 'SoraVR’s').\n"
42
- f"- Use standard English punctuation, such as single quotes (e.g., 'can't', 'it's').\n"
43
- f"- STRICTLY Give only one response for the Category the sample belongs to.\n"
44
- f"- Do NOT include preambles or unnecessary text.\n\n"
45
- f"Return the final response cleanly, without any extraneous symbols or characters."
46
- )
47
-
48
- # Query the OpenAI client and return the response
49
- completion = self.client.chat.completions.create(
50
- model=self.model_name,
51
- messages=[
52
- {"role": "system", "content": "You are a professional assistant."},
53
- {"role": "user", "content": prompt},
54
- ]
55
- )
56
-
57
- # Extract and clean the generated response
58
- response = completion.choices[0].message.content.strip()
59
-
60
- # Optional: Post-process to clean invalid characters
61
- #response_cleaned = response.encode('utf-8').decode('utf-8', errors='ignore')
62
-
63
- return response
 
1
+ from openai import OpenAI
2
+ #from dotenv import load_dotenv
3
+ import os
4
+
5
+ #load_dotenv()
6
+
7
+
8
+ class LLMHandler:
9
+ def __init__(self, model_name="gpt-4o-mini"):
10
+ """
11
+ Initializes the LLMHandler with the specified OpenAI model.
12
+ """
13
+ self.openai_api_key = os.getenv("OPENAI_API_KEY")
14
+ if not self.openai_api_key:
15
+ raise ValueError("OPENAI_API_KEY environment variable not set.")
16
+
17
+ # Initialize OpenAI client
18
+ self.client = OpenAI(api_key=self.openai_api_key)
19
+ self.model_name = model_name
20
+
21
+ def generate_response(self, user_prompt, data):
22
+ """
23
+ Generate a concise response using the LLM based on user prompt and data.
24
+ :param user_prompt: Prompt provided by the user.
25
+ :param data: Dictionary containing the instance information.
26
+ :return: Generated response text.
27
+ """
28
+ # Refined prompt to handle encoding and formatting
29
+ prompt = (
30
+ f"You are a professional AI model tasked with writing personalized invite texts "
31
+ f"that are concise (less than 40 words), brochure-suitable, and tailored as per the category in the given sample.\n\n"
32
+ f"Consider the user prompt: {user_prompt}\n\n"
33
+ f"Details of the individual:\n"
34
+ f"- Name: {data['Name']}\n"
35
+ f"- Job Title: {data['Job Title']}\n"
36
+ f"- Organisation: {data['Organisation']}\n"
37
+ f"- Area of Interest: {data['Area of Interest']}\n"
38
+ f"- Category: {data['Category']}\n\n"
39
+ f"The response **MUST**:\n"
40
+ f"- Start with 'Hello {data['Name']}'.\n"
41
+ f"- Be concise, professional, and STRICTLY DO NOT generate invalid characters or encoding errors (e.g. 'SoraVR’s').\n"
42
+ f"- Use standard English punctuation, such as single quotes (e.g., 'can't', 'it's').\n"
43
+ f"- STRICTLY Give only one response for the Category the sample belongs to.\n"
44
+ f"- Do NOT include preambles or unnecessary text.\n\n"
45
+ f"Return the final response cleanly, without any extraneous symbols or characters."
46
+ )
47
+
48
+ # Query the OpenAI client and return the response
49
+ completion = self.client.chat.completions.create(
50
+ model=self.model_name,
51
+ messages=[
52
+ {"role": "system", "content": "You are a professional assistant."},
53
+ {"role": "user", "content": prompt},
54
+ ]
55
+ )
56
+
57
+ # Extract and clean the generated response
58
+ response = completion.choices[0].message.content.strip()
59
+
60
+ # Optional: Post-process to clean invalid characters
61
+ #response_cleaned = response.encode('utf-8').decode('utf-8', errors='ignore')
62
+
63
+ return response