VectorV17 commited on
Commit
1b6eaa0
·
verified ·
1 Parent(s): 7a0ca52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -7,8 +7,9 @@ import gradio as gr
7
  warnings.filterwarnings("ignore", category=UserWarning, module="langchain_community")
8
 
9
  from langchain_community.vectorstores import Chroma
10
- from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
11
- from langchain_core.prompts import PromptTemplate
 
12
  from langchain_core.output_parsers import StrOutputParser
13
 
14
  # 1. Fetching the Hugging Face Space security token
@@ -31,33 +32,31 @@ simplilearn_faq_data = [
31
  vectorstore = Chroma.from_texts(texts=simplilearn_faq_data, embedding=embeddings)
32
  retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
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,
42
- do_sample=False,
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)
@@ -85,15 +84,12 @@ def process_chat(message, history):
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
 
7
  warnings.filterwarnings("ignore", category=UserWarning, module="langchain_community")
8
 
9
  from langchain_community.vectorstores import Chroma
10
+ from langchain_huggingface import HuggingFaceEmbeddings
11
+ from langchain_openai import ChatOpenAI # Re-introducing this to use HF's OpenAI proxy
12
+ from langchain_core.prompts import ChatPromptTemplate
13
  from langchain_core.output_parsers import StrOutputParser
14
 
15
  # 1. Fetching the Hugging Face Space security token
 
32
  vectorstore = Chroma.from_texts(texts=simplilearn_faq_data, embedding=embeddings)
33
  retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
34
 
35
+ # 3. Setup Open-Source LLM via Hugging Face's OpenAI-Compatible API
36
+ # This completely bypasses the "task: conversational" error by natively using the Messages API
37
  print("Configuring LLM Interface...")
38
+ chatModel = ChatOpenAI(
39
+ model="Qwen/Qwen2.5-1.5B-Instruct",
40
+ base_url="https://api-inference.huggingface.co/v1/", # Pointing LangChain directly to HF Servers
41
+ api_key=hf_token,
42
+ max_tokens=500,
43
+ temperature=0.3
 
 
44
  )
45
 
46
+ # 4. Constructing the Chain using ChatPromptTemplate (Clean & Native)
47
+ system_prompt = (
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.\n\n"
51
+ "Context:\n{context}"
52
+ )
 
 
 
 
 
 
53
 
54
+ prompt = ChatPromptTemplate.from_messages(
55
+ [
56
+ ("system", system_prompt),
57
+ ("human", "{input}"),
58
+ ]
59
+ )
60
 
61
  def format_docs(docs):
62
  return "\n\n".join(doc.page_content for doc in docs)
 
84
  # Executes the RAG Chain
85
  bot_response = rag_chain.invoke({"input": message})
86
 
 
 
 
87
  except Exception as e:
88
  # Displays exactly what failed directly on the UI
89
  bot_response = (
90
  f"⚠️ **Server/API Error Encountered:**\n\n"
91
  f"`{str(e)}`\n\n"
92
+ f"**Fix required:** Ensure your `HF_TOKEN` is saved in the Space settings. If it says 'Rate Limit', the free serverless API is temporarily busy."
93
  )
94
 
95
  # Appending messages via Gradio 6.x specification