Spaces:
Sleeping
Sleeping
Update Groq_llms.py
Browse filesUpdated the prompt in groq llms
- Groq_llms.py +52 -50
Groq_llms.py
CHANGED
|
@@ -1,50 +1,52 @@
|
|
| 1 |
-
import os
|
| 2 |
-
#from langchain_community.chat_models import ChatOpenAI
|
| 3 |
-
from langchain_groq import ChatGroq
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
load_dotenv()
|
| 7 |
-
|
| 8 |
-
class LLMHandler:
|
| 9 |
-
def __init__(self, model_name="llama-3.3-70b-versatile"):
|
| 10 |
-
"""
|
| 11 |
-
Initializes the LLMHandler with the specified Groq model.
|
| 12 |
-
"""
|
| 13 |
-
self.groq_api_key = os.getenv("GROQ_API_KEY")
|
| 14 |
-
if not self.groq_api_key:
|
| 15 |
-
raise ValueError("GROQ_API_KEY environment variable not set.")
|
| 16 |
-
|
| 17 |
-
# Initialize Groq LLM client
|
| 18 |
-
self.llm = ChatGroq(groq_api_key=self.groq_api_key, model_name=model_name)
|
| 19 |
-
|
| 20 |
-
def generate_response(self, user_prompt, data):
|
| 21 |
-
"""
|
| 22 |
-
Generate a concise response using the LLM based on user prompt and data.
|
| 23 |
-
:param user_prompt: Prompt provided by the user.
|
| 24 |
-
:param data: Dictionary containing the instance information (e.g., UID, Name, etc.).
|
| 25 |
-
:return: Generated response text.
|
| 26 |
-
"""
|
| 27 |
-
# Create the full prompt using user input and instance data
|
| 28 |
-
prompt = (
|
| 29 |
-
f"You are a professional AI model tasked with writing personalized invite texts "
|
| 30 |
-
f"that are concise (
|
| 31 |
-
f"\n\n"
|
| 32 |
-
f"
|
| 33 |
-
f"
|
| 34 |
-
f"-
|
| 35 |
-
f"-
|
| 36 |
-
f"-
|
| 37 |
-
f"-
|
| 38 |
-
f"-
|
| 39 |
-
f"
|
| 40 |
-
f"
|
| 41 |
-
f"
|
| 42 |
-
f"
|
| 43 |
-
|
| 44 |
-
f"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return response
|
| 50 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
#from langchain_community.chat_models import ChatOpenAI
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
class LLMHandler:
|
| 9 |
+
def __init__(self, model_name="llama-3.3-70b-versatile"):
|
| 10 |
+
"""
|
| 11 |
+
Initializes the LLMHandler with the specified Groq model.
|
| 12 |
+
"""
|
| 13 |
+
self.groq_api_key = os.getenv("GROQ_API_KEY")
|
| 14 |
+
if not self.groq_api_key:
|
| 15 |
+
raise ValueError("GROQ_API_KEY environment variable not set.")
|
| 16 |
+
|
| 17 |
+
# Initialize Groq LLM client
|
| 18 |
+
self.llm = ChatGroq(groq_api_key=self.groq_api_key, model_name=model_name)
|
| 19 |
+
|
| 20 |
+
def generate_response(self, user_prompt, data):
|
| 21 |
+
"""
|
| 22 |
+
Generate a concise response using the LLM based on user prompt and data.
|
| 23 |
+
:param user_prompt: Prompt provided by the user.
|
| 24 |
+
:param data: Dictionary containing the instance information (e.g., UID, Name, etc.).
|
| 25 |
+
:return: Generated response text.
|
| 26 |
+
"""
|
| 27 |
+
# Create the full prompt using user input and instance data
|
| 28 |
+
prompt = (
|
| 29 |
+
f"You are a professional AI model tasked with writing personalized invite texts "
|
| 30 |
+
f"that are concise (40 to 50 words), brochure-suitable, and tailored as per the user prompt.\n\n"
|
| 31 |
+
f"Consider the user prompt: {user_prompt}\n\n"
|
| 32 |
+
f"Details of the individual:\n"
|
| 33 |
+
f"- Name: {data['Name']}\n"
|
| 34 |
+
f"- Job Title: {data['Job Title']}\n"
|
| 35 |
+
f"- Organisation: {data['Organisation']}\n"
|
| 36 |
+
f"- Area of Interest: {data['Area of Interest']}\n"
|
| 37 |
+
f"- Category: {data['Category']}\n\n"
|
| 38 |
+
f"- Instance info: {data}\n"
|
| 39 |
+
f"- Go through the Instance info If the {user_prompt} mentions you to use variables that are not mentioned above. "
|
| 40 |
+
f"- The response **MUST**:\n"
|
| 41 |
+
f"- Start with 'Hello {data['Name']}'.\n"
|
| 42 |
+
f"- Be concise, professional, and STRICTLY DO NOT generate invalid characters or encoding errors (e.g. 'SoraVR’s').\n"
|
| 43 |
+
f"- Use standard English punctuation, such as single quotes (e.g., 'can't', 'it's').\n"
|
| 44 |
+
f"- STRICTLY Give only one response for the Category the sample belongs to.\n"
|
| 45 |
+
f"- Do NOT include preambles or unnecessary text.\n\n"
|
| 46 |
+
f"Return the final response cleanly, without any extraneous symbols or characters."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Query the LLM and return the response
|
| 50 |
+
response = self.llm.invoke(prompt)
|
| 51 |
+
return response.content.strip()
|
| 52 |
+
|