Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,10 +26,10 @@ from langchain.retrievers import EnsembleRetriever
|
|
| 26 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 27 |
from langchain_core.documents import Document
|
| 28 |
|
| 29 |
-
app = FastAPI(title="
|
| 30 |
|
| 31 |
class Question(BaseModel):
|
| 32 |
-
|
| 33 |
|
| 34 |
def semantic_chunk_with_embeddings(documents, embeddings, max_chunk_size=1000, min_sentences=2, overlap_sentences=1):
|
| 35 |
"""Chunk documents into semantically related groups using embeddings and clustering."""
|
|
@@ -136,7 +136,7 @@ def load_rag_system():
|
|
| 136 |
You are TechChat, an AI assistant created to provide accurate, concise, and helpful information about admissions to Kwame Nkrumah University of Science and Technology (KNUST). Your primary goal is to assist users with questions related to KNUST admissions, including application processes, requirements, deadlines, programs, and other relevant details.
|
| 137 |
|
| 138 |
### Instructions:
|
| 139 |
-
1. **KNUST Admissions Questions**: Use the provided context to answer questions about KNUST admissions clearly and accurately. If the context is sufficient, tailor your response to the specific details provided.
|
| 140 |
2. **Limited Context**: If the context lacks enough information to fully answer a KNUST admissions question, provide a general but accurate response based on your knowledge of KNUST admissions, and invite the user to provide more details for a more specific answer.
|
| 141 |
3. **Off-Topic Questions**: If the question is unrelated to KNUST admissions, respond politely with: "I'm sorry, that question is outside my focus on KNUST admissions. Feel free to ask about KNUST application processes, requirements, or programs, and I'll be happy to help!"
|
| 142 |
4. **Tone and Style**: Maintain a friendly, professional, and approachable tone. Avoid overly technical jargon unless necessary, and ensure responses are easy to understand.
|
|
@@ -160,27 +160,22 @@ def load_rag_system():
|
|
| 160 |
logger.info("Initializing RAG system...")
|
| 161 |
retriever, vectorstore, chain = load_rag_system()
|
| 162 |
|
| 163 |
-
|
| 164 |
-
if "dress" in question.lower() and "employees" in question.lower():
|
| 165 |
-
return "What is the dress code policy for employees regarding casual attire?"
|
| 166 |
-
return question
|
| 167 |
|
| 168 |
@app.post("/chat")
|
| 169 |
async def ask(question: Question):
|
| 170 |
print('ask route reached')
|
| 171 |
try:
|
| 172 |
-
logger.info(f"Received question: {question.
|
| 173 |
-
|
| 174 |
-
logger.info(f"Rewritten question: {rewritten_question}")
|
| 175 |
-
context_docs = retriever.invoke(rewritten_question)
|
| 176 |
logger.info(f"Retrieved {len(context_docs)} context documents")
|
| 177 |
-
max_similarity = max([vectorstore.similarity_search_with_score(
|
| 178 |
if max_similarity < 0.25: # Lowered threshold slightly
|
| 179 |
logger.info("Similarity too low, returning 'I don’t know'")
|
| 180 |
return {"answer": "I'm not sure about that, but I'd be happy to help if you provide more details!"}
|
| 181 |
context_text = "\n".join([doc.page_content for doc in context_docs])
|
| 182 |
logger.info("Generating response...")
|
| 183 |
-
response = chain.invoke({"context": context_text, "question":
|
| 184 |
answer = response['text'].split("Answer:")[-1].strip()
|
| 185 |
logger.info(f"Generated answer: {answer}")
|
| 186 |
return {"answer": answer}
|
|
|
|
| 26 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 27 |
from langchain_core.documents import Document
|
| 28 |
|
| 29 |
+
app = FastAPI(title="TechChat Rag")
|
| 30 |
|
| 31 |
class Question(BaseModel):
|
| 32 |
+
query: str
|
| 33 |
|
| 34 |
def semantic_chunk_with_embeddings(documents, embeddings, max_chunk_size=1000, min_sentences=2, overlap_sentences=1):
|
| 35 |
"""Chunk documents into semantically related groups using embeddings and clustering."""
|
|
|
|
| 136 |
You are TechChat, an AI assistant created to provide accurate, concise, and helpful information about admissions to Kwame Nkrumah University of Science and Technology (KNUST). Your primary goal is to assist users with questions related to KNUST admissions, including application processes, requirements, deadlines, programs, and other relevant details.
|
| 137 |
|
| 138 |
### Instructions:
|
| 139 |
+
1. **KNUST Admissions Questions**: Use the provided context as a guide to answer questions about KNUST admissions clearly and accurately but you are not mandated to stick to the context if it is inacurate. You are to refine the context for response. If the context is sufficient, tailor your response to the specific details provided.
|
| 140 |
2. **Limited Context**: If the context lacks enough information to fully answer a KNUST admissions question, provide a general but accurate response based on your knowledge of KNUST admissions, and invite the user to provide more details for a more specific answer.
|
| 141 |
3. **Off-Topic Questions**: If the question is unrelated to KNUST admissions, respond politely with: "I'm sorry, that question is outside my focus on KNUST admissions. Feel free to ask about KNUST application processes, requirements, or programs, and I'll be happy to help!"
|
| 142 |
4. **Tone and Style**: Maintain a friendly, professional, and approachable tone. Avoid overly technical jargon unless necessary, and ensure responses are easy to understand.
|
|
|
|
| 160 |
logger.info("Initializing RAG system...")
|
| 161 |
retriever, vectorstore, chain = load_rag_system()
|
| 162 |
|
| 163 |
+
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
@app.post("/chat")
|
| 166 |
async def ask(question: Question):
|
| 167 |
print('ask route reached')
|
| 168 |
try:
|
| 169 |
+
logger.info(f"Received question: {question.query}")
|
| 170 |
+
context_docs = retriever.invoke(question.query)
|
|
|
|
|
|
|
| 171 |
logger.info(f"Retrieved {len(context_docs)} context documents")
|
| 172 |
+
max_similarity = max([vectorstore.similarity_search_with_score(question.query, k=1)[0][1] for _ in context_docs], default=0)
|
| 173 |
if max_similarity < 0.25: # Lowered threshold slightly
|
| 174 |
logger.info("Similarity too low, returning 'I don’t know'")
|
| 175 |
return {"answer": "I'm not sure about that, but I'd be happy to help if you provide more details!"}
|
| 176 |
context_text = "\n".join([doc.page_content for doc in context_docs])
|
| 177 |
logger.info("Generating response...")
|
| 178 |
+
response = chain.invoke({"context": context_text, "question": question.query})
|
| 179 |
answer = response['text'].split("Answer:")[-1].strip()
|
| 180 |
logger.info(f"Generated answer: {answer}")
|
| 181 |
return {"answer": answer}
|