Spaces:
Paused
Paused
Update app.py
Browse files
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
|
| 43 |
if not hf_token:
|
| 44 |
-
print("WARNING: HF_TOKEN is missing.
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
max_tokens=400,
|
| 51 |
temperature=0.3,
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
prompt =
|
| 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)
|