Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -94,7 +94,7 @@ except Exception as e:
|
|
94 |
raise RuntimeError("Something went wrong with the Together API setup. Please verify your API key and configuration.")
|
95 |
|
96 |
# ==========================
|
97 |
-
# Conversational Retrieval Chain
|
98 |
# ==========================
|
99 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
100 |
qa = ConversationalRetrievalChain.from_llm(
|
@@ -102,9 +102,8 @@ qa = ConversationalRetrievalChain.from_llm(
|
|
102 |
memory=memory,
|
103 |
retriever=db_retriever,
|
104 |
combine_docs_chain_kwargs={"prompt": prompt},
|
105 |
-
return_source_documents=True # This enables logging of retrieved content
|
106 |
)
|
107 |
-
logger.info("Conversational Retrieval Chain initialized
|
108 |
|
109 |
# ==========================
|
110 |
# FastAPI Backend
|
@@ -125,15 +124,23 @@ async def root():
|
|
125 |
async def chat(request: ChatRequest):
|
126 |
try:
|
127 |
logger.debug(f"Received user question: {request.question}")
|
128 |
-
result = qa.invoke(input=request.question)
|
129 |
|
130 |
-
#
|
131 |
-
|
132 |
-
logger.debug(
|
|
|
|
|
133 |
|
|
|
|
|
134 |
answer = result.get("answer")
|
|
|
135 |
if not answer or "The information is not available in the provided context" in answer:
|
136 |
answer = "I'm sorry, I couldn't find relevant information for your query. Please try rephrasing or providing more details."
|
|
|
|
|
|
|
|
|
137 |
return ChatResponse(answer=answer)
|
138 |
except Exception as e:
|
139 |
logger.error(f"Error during chat invocation: {e}")
|
|
|
94 |
raise RuntimeError("Something went wrong with the Together API setup. Please verify your API key and configuration.")
|
95 |
|
96 |
# ==========================
|
97 |
+
# Conversational Retrieval Chain
|
98 |
# ==========================
|
99 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
100 |
qa = ConversationalRetrievalChain.from_llm(
|
|
|
102 |
memory=memory,
|
103 |
retriever=db_retriever,
|
104 |
combine_docs_chain_kwargs={"prompt": prompt},
|
|
|
105 |
)
|
106 |
+
logger.info("Conversational Retrieval Chain initialized.")
|
107 |
|
108 |
# ==========================
|
109 |
# FastAPI Backend
|
|
|
124 |
async def chat(request: ChatRequest):
|
125 |
try:
|
126 |
logger.debug(f"Received user question: {request.question}")
|
|
|
127 |
|
128 |
+
# Retrieve documents and log them
|
129 |
+
retrieved_docs = db_retriever.get_relevant_documents(request.question)
|
130 |
+
logger.debug("Retrieved Documents:")
|
131 |
+
for i, doc in enumerate(retrieved_docs, start=1):
|
132 |
+
logger.debug(f"Document {i}: {doc.page_content[:500]}...") # Log first 500 characters of each document
|
133 |
|
134 |
+
# Invoke the QA chain with the user question
|
135 |
+
result = qa.invoke(input=request.question)
|
136 |
answer = result.get("answer")
|
137 |
+
|
138 |
if not answer or "The information is not available in the provided context" in answer:
|
139 |
answer = "I'm sorry, I couldn't find relevant information for your query. Please try rephrasing or providing more details."
|
140 |
+
|
141 |
+
# Log the final answer
|
142 |
+
logger.debug(f"Chatbot Answer: {answer}")
|
143 |
+
|
144 |
return ChatResponse(answer=answer)
|
145 |
except Exception as e:
|
146 |
logger.error(f"Error during chat invocation: {e}")
|