VectorV17 commited on
Commit
f3c336d
·
verified ·
1 Parent(s): 9b75448

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -33,8 +33,9 @@ retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k
33
 
34
  # 3. Setup Low Parameter Open-Source LLM Generator via Serverless API
35
  print("Configuring LLM Interface...")
 
36
  chatModel = HuggingFaceEndpoint(
37
- repo_id="Qwen/Qwen2.5-1.5B-Instruct",
38
  task="text-generation",
39
  max_new_tokens=500,
40
  temperature=0.3,
@@ -42,24 +43,22 @@ chatModel = HuggingFaceEndpoint(
42
  huggingfacehub_api_token=hf_token
43
  )
44
 
45
- # 4. Constructing the Chain using standard PromptTemplate (Fixes Data Type Error for Qwen)
46
- # We use Qwen's native ChatML tags so the model understands System/User/Assistant roles
47
- template = """<|im_start|>system
48
  You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
49
  Answer the student's inquiry accurately using ONLY the retrieved context below.
50
  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.
51
 
52
  Context:
53
- {context}<|im_end|>
54
- <|im_start|>user
55
- {input}<|im_end|>
56
- <|im_start|>assistant
57
  """
58
 
59
- # Switched from ChatPromptTemplate to standard PromptTemplate
60
  prompt = PromptTemplate.from_template(template)
61
 
62
- # Helper function to combine document chunks
63
  def format_docs(docs):
64
  return "\n\n".join(doc.page_content for doc in docs)
65
 
@@ -86,16 +85,15 @@ def process_chat(message, history):
86
  # Executes the RAG Chain
87
  bot_response = rag_chain.invoke({"input": message})
88
 
89
- # Clean up the response if Qwen accidentally prints the stop tokens
90
- bot_response = bot_response.replace("<|im_end|>", "").strip()
91
 
92
  except Exception as e:
93
- # NOW WE SHOW THE ACTUAL ERROR ON SCREEN!
94
  bot_response = (
95
  f"⚠️ **Server/API Error Encountered:**\n\n"
96
  f"`{str(e)}`\n\n"
97
- f"**Fix required:** If it says '401 Unauthorized', you must go to your Space **Settings -> Variables and secrets** "
98
- f"and add a New Secret named `HF_TOKEN` containing your Hugging Face Access Token."
99
  )
100
 
101
  # Appending messages via Gradio 6.x specification
 
33
 
34
  # 3. Setup Low Parameter Open-Source LLM Generator via Serverless API
35
  print("Configuring LLM Interface...")
36
+ # Switched to TinyLlama: A 1.1B model that natively supports text-generation and runs perfectly on HF Serverless
37
  chatModel = HuggingFaceEndpoint(
38
+ repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
39
  task="text-generation",
40
  max_new_tokens=500,
41
  temperature=0.3,
 
43
  huggingfacehub_api_token=hf_token
44
  )
45
 
46
+ # 4. Constructing the Chain using standard PromptTemplate
47
+ # Formatted explicitly for TinyLlama's token requirements to prevent hallucinations
48
+ template = """<|system|>
49
  You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
50
  Answer the student's inquiry accurately using ONLY the retrieved context below.
51
  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.
52
 
53
  Context:
54
+ {context}</s>
55
+ <|user|>
56
+ {input}</s>
57
+ <|assistant|>
58
  """
59
 
 
60
  prompt = PromptTemplate.from_template(template)
61
 
 
62
  def format_docs(docs):
63
  return "\n\n".join(doc.page_content for doc in docs)
64
 
 
85
  # Executes the RAG Chain
86
  bot_response = rag_chain.invoke({"input": message})
87
 
88
+ # Clean up any trailing end-of-sequence tokens if TinyLlama generates them
89
+ bot_response = bot_response.replace("</s>", "").strip()
90
 
91
  except Exception as e:
92
+ # Displays exactly what failed directly on the UI
93
  bot_response = (
94
  f"⚠️ **Server/API Error Encountered:**\n\n"
95
  f"`{str(e)}`\n\n"
96
+ f"**Fix required:** Ensure your `HF_TOKEN` is saved in the Space settings. If the API is temporarily busy, try again in a few moments."
 
97
  )
98
 
99
  # Appending messages via Gradio 6.x specification