Spaces:
Sleeping
Sleeping
from langchain_groq import ChatGroq | |
from langchain_core.prompts import PromptTemplate | |
from langchain_core.output_parsers import StrOutputParser | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() | |
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY") | |
model = ChatGroq(model="qwen/qwen3-32b") | |
prompt_template = PromptTemplate(template = '''You are a Mobile Plan Analyzer tool that evaluates a user's current mobile plan against their usage patterns and a recommended plan, and generates a clear, one-sentence justification highlighting improved value, efficiency, or suitability—tailored for quick stakeholder insights. | |
The user info is : {user_data}''', input_variables=["user_data"]) | |
parser = StrOutputParser() | |
def explain_recommendation(user_id, df): | |
if user_id and user_id in df['user_id'].values: | |
user_data = df[df['user_id'] == user_id].iloc[0] | |
chain = prompt_template | model | parser | |
response = chain.invoke({"user_data": user_data}) | |
if isinstance(response, str) and "</think>" in response: | |
return response.split("</think>")[1].replace("\n","").strip() | |
else: | |
return response | |
else: | |
raise ValueError("User ID not found in the dataset.") | |