Citations Support: Now Surfsense responses will include Citations.

This commit is contained in:
DESKTOP-RTLN3BA\$punk 2024-09-20 16:30:48 -07:00
parent 06152827c1
commit e93abed6d5
2 changed files with 42 additions and 7 deletions

View file

@ -215,9 +215,7 @@ class HIndices:
db.commit()
return "success"
def is_query_answerable(self, query, context):
prompt = PromptTemplate(
template="""You are a grader assessing relevance of a retrieved document to a user question. \n
@ -263,7 +261,18 @@ class HIndices:
context_to_answer = ""
for i, doc in enumerate(contextdocs):
context_to_answer += "DOCUMENT " + str(i) + " PAGECONTENT CHUCK: \n\n ===================================== \n\n" + doc.page_content + '\n\n ==============================================='
content = f":DOCUMENT {str(i)}\n"
content += f"=======================================METADATA==================================== \n"
content += f"Webpage Url : {doc.metadata['VisitedWebPageURL']} \n"
content += f"Webpage Title : {doc.metadata['VisitedWebPageTitle']} \n"
content += f"Accessed on (Date With Time In ISO String): {doc.metadata['VisitedWebPageDateWithTimeInISOString']} \n"
content += f"===================================================================================== \n"
content += f"Webpage CONTENT CHUCK: \n\n {doc.page_content} \n\n"
content += f"===================================================================================== \n"
context_to_answer += content
content = ""
if(self.is_query_answerable(query=query, context=context_to_answer).lower() == 'yes'):
ans_chain = CONTEXT_ANSWER_PROMPT | self.llm
@ -290,8 +299,17 @@ class HIndices:
context_to_answer = ""
for i, doc in enumerate(top_summaries_compressed_docs):
context_to_answer += "DOCUMENT " + str(i) + " PAGECONTENT: \n\n ===================================== \n\n" + doc.page_content + '\n\n ==============================================='
content = f":DOCUMENT {str(i)}\n"
content += f"=======================================METADATA==================================== \n"
content += f"Webpage Url : {doc.metadata['VisitedWebPageURL']} \n"
content += f"Webpage Title : {doc.metadata['VisitedWebPageTitle']} \n"
content += f"Accessed on (Date With Time In ISO String): {doc.metadata['VisitedWebPageDateWithTimeInISOString']} \n"
content += f"===================================================================================== \n"
content += f"Webpage CONTENT CHUCK: \n\n {doc.page_content} \n\n"
content += f"===================================================================================== \n"
context_to_answer += content
ans_chain = CONTEXT_ANSWER_PROMPT | self.llm
finalans = ans_chain.invoke({"query": query, "context": context_to_answer})

View file

@ -38,7 +38,7 @@ SUBQUERY_DECOMPOSITION_PROMT = PromptTemplate(
template=SUBQUERY_DECOMPOSITION_TEMPLATE
)
CONTEXT_ANSWER_TEMPLATE = DATE_TODAY + """You are a phd in english litrature. You are given the task to give detailed report and explanation to the user query based on the given context.
CONTEXT_ANSWER_TEMPLATE = DATE_TODAY + """You are a phd in english litrature. You are given the task to give detailed research report and explanation to the user query based on the given context.
IMPORTANT INSTRUCTION: Only return answer if you can find it in given context otherwise just say you don't know.
@ -47,10 +47,27 @@ Context: {context}
User Query: {query}
Detailed Report:"""
ANSWER_WITH_CITATIONS = DATE_TODAY + """You're a helpful AI assistant. Given a user question and some Webpage article snippets, \
answer the user question and provide citations. If none of the articles answer the question, just say you don't know.
Remember, you must return both an answer and citations. Citation information is in given Document Metadata.
A citation consists of a Web Page Title. Website Name, URL. Accessed Day Month Year.
Citations Example:
Citations
1. Citing Sources in Academic Writing. Scribbr. www.scribbr.com/category/citing-sources/. Accessed 4 March 2021.
2. What is SEO? Backlinko. www.backlinko.com/seo. Accessed 10 March 2022.
Here are the Webpage article snippets:
{context}
User Query: {query}
Your Answer:"""
CONTEXT_ANSWER_PROMPT = PromptTemplate(
input_variables=["context","query"],
template=CONTEXT_ANSWER_TEMPLATE
template=ANSWER_WITH_CITATIONS
)