VectorV17 commited on
Commit
cd1de85
·
verified ·
1 Parent(s): d13d161

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -12,6 +12,8 @@ from langchain_openai import ChatOpenAI
12
  from langchain_core.prompts import ChatPromptTemplate
13
  from langchain_core.output_parsers import StrOutputParser
14
  from dotenv import load_dotenv
 
 
15
 
16
  # Load Environment Variables
17
  load_dotenv()
@@ -37,33 +39,36 @@ retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k
37
  # -------------------------------------------------------------------------
38
  # 2. Setup Robust LLM Interface via OpenAI-Compatible Endpoint
39
  # -------------------------------------------------------------------------
 
40
  print("Configuring LLM Interface...")
41
 
42
- # Safety check to ensure the token is actually being loaded
43
  if not hf_token:
44
- print("WARNING: HF_TOKEN is missing. Please check your environment variables.")
45
 
46
- chatModel = ChatOpenAI(
47
- model="Qwen/Qwen2.5-1.5B-Instruct",
48
- base_url="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-1.5B-Instruct/v1/",
49
- api_key=hf_token,
50
- max_tokens=400,
51
  temperature=0.3,
52
- max_retries=4, # Increased from 2 to give the API more chances to connect
53
- timeout=120.0 # FIX: Extended timeout to 2 minutes to survive cold starts
 
54
  )
55
 
56
- system_prompt = (
57
- "You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane. "
58
- "Answer the student's inquiry accurately using ONLY the retrieved context below. "
59
- "If the answer cannot be found in the context, politely inform them that you only handle Simplilearn academic protocols and advise them to contact support@simplilearn.com.\n\n"
60
- "Context:\n{context}"
61
- )
 
 
 
 
 
62
 
63
- prompt = ChatPromptTemplate.from_messages([
64
- ("system", system_prompt),
65
- ("human", "{input}"),
66
- ])
67
 
68
  def format_docs(docs):
69
  return "\n\n".join(doc.page_content for doc in docs)
 
12
  from langchain_core.prompts import ChatPromptTemplate
13
  from langchain_core.output_parsers import StrOutputParser
14
  from dotenv import load_dotenv
15
+ from langchain_huggingface import HuggingFaceEndpoint
16
+ from langchain_core.prompts import PromptTemplate
17
 
18
  # Load Environment Variables
19
  load_dotenv()
 
39
  # -------------------------------------------------------------------------
40
  # 2. Setup Robust LLM Interface via OpenAI-Compatible Endpoint
41
  # -------------------------------------------------------------------------
42
+
43
  print("Configuring LLM Interface...")
44
 
45
+ # Safety check for the token
46
  if not hf_token:
47
+ print("⚠️ WARNING: HF_TOKEN is missing. The app will fail to generate responses.")
48
 
49
+ # Using the native HF endpoint which understands "model waking up" statuses
50
+ chatModel = HuggingFaceEndpoint(
51
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3", # More reliable free-tier model
52
+ huggingfacehub_api_token=hf_token,
 
53
  temperature=0.3,
54
+ max_new_tokens=400,
55
+ task="text-generation",
56
+ model_kwargs={"wait_for_model": True} # Explicitly tells the API to wait during cold starts
57
  )
58
 
59
+ # Adjusting prompt for a standard text generation model instead of a chat model
60
+ system_prompt = """You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
61
+ Answer the student's inquiry accurately using ONLY the retrieved context below.
62
+ If the answer cannot be found in the context, politely inform them that you only handle Simplilearn academic protocols and advise them to contact support@simplilearn.com.
63
+
64
+ Context:
65
+ {context}
66
+
67
+ User Inquiry: {input}
68
+
69
+ Lane's Response:"""
70
 
71
+ prompt = PromptTemplate.from_template(system_prompt)
 
 
 
72
 
73
  def format_docs(docs):
74
  return "\n\n".join(doc.page_content for doc in docs)