Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import logging
|
2 |
-
from langchain_community.document_loaders import
|
3 |
from langchain_huggingface import HuggingFaceEmbeddings
|
4 |
-
from sentence_transformers import SentenceTransformer
|
5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
from langchain_community.vectorstores import FAISS
|
7 |
from langchain.prompts import PromptTemplate
|
@@ -22,7 +21,6 @@ logger.debug("Starting FastAPI app...")
|
|
22 |
|
23 |
# Suppress warnings
|
24 |
warnings.filterwarnings("ignore", message="You are using `torch.load` with `weights_only=False`")
|
25 |
-
warnings.filterwarnings("ignore", message="Tried to instantiate class '__path__._path'")
|
26 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
27 |
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
28 |
|
@@ -55,37 +53,44 @@ except Exception as e:
|
|
55 |
# Ensure FAISS vectorstore is loaded or created
|
56 |
try:
|
57 |
db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
|
58 |
-
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":
|
59 |
except Exception as e:
|
60 |
logger.error(f"Error loading FAISS vectorstore: {e}")
|
61 |
# If not found, create a new vectorstore
|
62 |
try:
|
63 |
loader = DirectoryLoader('./data')
|
64 |
-
text_splitter = RecursiveCharacterTextSplitter()
|
65 |
documents = text_splitter.split_documents(loader.load())
|
66 |
db = FAISS.from_documents(documents, embeddings)
|
67 |
db.save_local("ipc_vector_db")
|
68 |
-
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":
|
69 |
except Exception as inner_e:
|
70 |
logger.error(f"Error creating FAISS vectorstore: {inner_e}")
|
71 |
raise RuntimeError("FAISS vectorstore could not be created or loaded.")
|
72 |
|
73 |
# Define the prompt template
|
74 |
-
prompt_template = """
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
|
82 |
|
83 |
# Initialize the Together API
|
84 |
try:
|
85 |
llm = Together(
|
86 |
model="mistralai/Mistral-7B-Instruct-v0.2",
|
87 |
-
temperature=0.
|
88 |
-
max_tokens=
|
89 |
together_api_key=TOGETHER_AI_API,
|
90 |
)
|
91 |
except Exception as e:
|
@@ -120,9 +125,17 @@ async def root():
|
|
120 |
@app.post("/chat", response_model=ChatResponse)
|
121 |
async def chat(request: ChatRequest):
|
122 |
try:
|
123 |
-
|
124 |
result = qa.invoke(input=request.question)
|
|
|
|
|
|
|
125 |
answer = result.get("answer", "The chatbot could not generate a response.")
|
|
|
|
|
|
|
|
|
|
|
126 |
return ChatResponse(answer=answer)
|
127 |
except Exception as e:
|
128 |
logger.error(f"Error during chat invocation: {e}")
|
|
|
1 |
import logging
|
2 |
+
from langchain_community.document_loaders import DirectoryLoader
|
3 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
from langchain.prompts import PromptTemplate
|
|
|
21 |
|
22 |
# Suppress warnings
|
23 |
warnings.filterwarnings("ignore", message="You are using `torch.load` with `weights_only=False`")
|
|
|
24 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
25 |
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
26 |
|
|
|
53 |
# Ensure FAISS vectorstore is loaded or created
|
54 |
try:
|
55 |
db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
|
56 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 5, "score_threshold": 0.8})
|
57 |
except Exception as e:
|
58 |
logger.error(f"Error loading FAISS vectorstore: {e}")
|
59 |
# If not found, create a new vectorstore
|
60 |
try:
|
61 |
loader = DirectoryLoader('./data')
|
62 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
63 |
documents = text_splitter.split_documents(loader.load())
|
64 |
db = FAISS.from_documents(documents, embeddings)
|
65 |
db.save_local("ipc_vector_db")
|
66 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 5, "score_threshold": 0.8})
|
67 |
except Exception as inner_e:
|
68 |
logger.error(f"Error creating FAISS vectorstore: {inner_e}")
|
69 |
raise RuntimeError("FAISS vectorstore could not be created or loaded.")
|
70 |
|
71 |
# Define the prompt template
|
72 |
+
prompt_template = """
|
73 |
+
As a legal chatbot specializing in the Indian Penal Code (IPC), provide precise, fact-based answers to the user’s question based on the provided context.
|
74 |
+
Respond only if the answer can be derived from the given context; otherwise, say:
|
75 |
+
"The information is not available in the provided context."
|
76 |
+
Use plain, professional language in your response.
|
77 |
+
|
78 |
+
CONTEXT: {context}
|
79 |
+
|
80 |
+
CHAT HISTORY: {chat_history}
|
81 |
+
|
82 |
+
QUESTION: {question}
|
83 |
+
|
84 |
+
ANSWER:
|
85 |
+
"""
|
86 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
|
87 |
|
88 |
# Initialize the Together API
|
89 |
try:
|
90 |
llm = Together(
|
91 |
model="mistralai/Mistral-7B-Instruct-v0.2",
|
92 |
+
temperature=0.3, # Lower temperature ensures deterministic answers
|
93 |
+
max_tokens=512, # Shorter response for focus
|
94 |
together_api_key=TOGETHER_AI_API,
|
95 |
)
|
96 |
except Exception as e:
|
|
|
125 |
@app.post("/chat", response_model=ChatResponse)
|
126 |
async def chat(request: ChatRequest):
|
127 |
try:
|
128 |
+
logger.debug(f"User Question: {request.question}")
|
129 |
result = qa.invoke(input=request.question)
|
130 |
+
logger.debug(f"Retrieved Context: {result.get('context', '')}")
|
131 |
+
logger.debug(f"Model Response: {result.get('answer', '')}")
|
132 |
+
|
133 |
answer = result.get("answer", "The chatbot could not generate a response.")
|
134 |
+
confidence_score = result.get("score", 0) # Assuming LLM provides a score
|
135 |
+
|
136 |
+
if confidence_score < 0.7:
|
137 |
+
answer = "The answer is uncertain. Please consult a professional."
|
138 |
+
|
139 |
return ChatResponse(answer=answer)
|
140 |
except Exception as e:
|
141 |
logger.error(f"Error during chat invocation: {e}")
|