VectorV17 commited on
Commit
9c79af0
·
verified ·
1 Parent(s): 1b6eaa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -8,7 +8,7 @@ warnings.filterwarnings("ignore", category=UserWarning, module="langchain_commun
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
 
@@ -33,17 +33,18 @@ 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. "
@@ -85,11 +86,11 @@ def process_chat(message, history):
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
 
8
 
9
  from langchain_community.vectorstores import Chroma
10
  from langchain_huggingface import HuggingFaceEmbeddings
11
+ from langchain_openai import ChatOpenAI
12
  from langchain_core.prompts import ChatPromptTemplate
13
  from langchain_core.output_parsers import StrOutputParser
14
 
 
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
  print("Configuring LLM Interface...")
37
  chatModel = ChatOpenAI(
38
  model="Qwen/Qwen2.5-1.5B-Instruct",
39
+ # FIX: Pointing the URL directly to the model's dedicated instance to prevent connection drops
40
+ base_url="https://api-inference.huggingface.co/models/Qwen/Qwen2.5-1.5B-Instruct/v1/",
41
  api_key=hf_token,
42
  max_tokens=500,
43
+ temperature=0.3,
44
+ max_retries=2 # Adds a safety net if the connection flickers
45
  )
46
 
47
+ # 4. Constructing the Chain using ChatPromptTemplate
48
  system_prompt = (
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. "
 
86
  bot_response = rag_chain.invoke({"input": message})
87
 
88
  except Exception as e:
89
+ # Displays the error
90
  bot_response = (
91
+ f"⚠️ **Connection Status:**\n\n"
92
  f"`{str(e)}`\n\n"
93
+ f"**Note:** If the free inference API is 'waking up' the model, it can take about 30 seconds. Please try clicking the button again!"
94
  )
95
 
96
  # Appending messages via Gradio 6.x specification