File size: 1,312 Bytes
d1c2c35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")