Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,12 @@ import re
|
|
| 7 |
import json
|
| 8 |
from auditqa.sample_questions import QUESTIONS
|
| 9 |
from auditqa.reports import POSSIBLE_REPORTS, files
|
| 10 |
-
from auditqa.engine.prompts import audience_prompts, answer_prompt_template, llama_propmt
|
| 11 |
from auditqa.doc_process import process_pdf
|
| 12 |
-
from langchain_core.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from langchain_core.output_parsers import StrOutputParser
|
| 14 |
from langchain.llms import HuggingFaceEndpoint
|
| 15 |
from dotenv import load_dotenv
|
|
@@ -70,29 +73,32 @@ async def chat(query,history,sources,reports):
|
|
| 70 |
|
| 71 |
|
| 72 |
# get prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#prompt = ChatPromptTemplate.from_template(answer_prompt_template)
|
| 75 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 76 |
-
(
|
| 77 |
-
"system",
|
| 78 |
-
"""You are AuditQ&A, an AI Assistant created by Auditors and Data Scientist. You are given a question and extracted passages of the consolidated/departmental/thematic focus audit reports. Provide a clear and structured answer based on the passages provided, the context and the guidelines.
|
| 79 |
-
Guidelines:
|
| 80 |
-
- If the passages have useful facts or numbers, use them in your answer.
|
| 81 |
-
- When you use information from a passage, mention where it came from by using [Doc i] at the end of the sentence. i stands for the number of the document.
|
| 82 |
-
- Do not use the sentence 'Doc i says ...' to say where information came from.
|
| 83 |
-
- If the same thing is said in more than one document, you can mention all of them like this: [Doc i, Doc j, Doc k]
|
| 84 |
-
- Do not just summarize each passage one by one. Group your summaries to highlight the key parts in the explanation.
|
| 85 |
-
- If it makes sense, use bullet points and lists to make your answers easier to understand.
|
| 86 |
-
- You do not need to use every passage. Only use the ones that help answer the question.
|
| 87 |
-
- If the documents do not have the information needed to answer the question, just say you do not have enough information.""",
|
| 88 |
-
),
|
| 89 |
-
("user",
|
| 90 |
-
"""Passages:
|
| 91 |
-
{context}
|
| 92 |
-
-----------------------
|
| 93 |
-
Question: {question} - Explained to {audience}
|
| 94 |
-
Answer in {language} with the passages citations:"""),
|
| 95 |
-
])
|
| 96 |
|
| 97 |
# get llm_qa
|
| 98 |
# llm_qa = HuggingFaceEndpoint(
|
|
@@ -113,12 +119,13 @@ Answer in {language} with the passages citations:"""),
|
|
| 113 |
|
| 114 |
|
| 115 |
# create rag chain
|
| 116 |
-
|
|
|
|
| 117 |
# get answers
|
| 118 |
answer_lst = []
|
| 119 |
for question, context in zip(question_lst , context_retrieved_lst):
|
| 120 |
-
answer = chain.invoke(
|
| 121 |
-
answer_lst.append(answer)
|
| 122 |
docs_html = []
|
| 123 |
for i, d in enumerate(context_retrieved, 1):
|
| 124 |
docs_html.append(make_html_source(d, i))
|
|
|
|
| 7 |
import json
|
| 8 |
from auditqa.sample_questions import QUESTIONS
|
| 9 |
from auditqa.reports import POSSIBLE_REPORTS, files
|
|
|
|
| 10 |
from auditqa.doc_process import process_pdf
|
| 11 |
+
from langchain_core.messages import (
|
| 12 |
+
HumanMessage,
|
| 13 |
+
SystemMessage,
|
| 14 |
+
)
|
| 15 |
+
from langchain_huggingface import ChatHuggingFace
|
| 16 |
from langchain_core.output_parsers import StrOutputParser
|
| 17 |
from langchain.llms import HuggingFaceEndpoint
|
| 18 |
from dotenv import load_dotenv
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# get prompt
|
| 76 |
+
SYSTEM_PROMPT = """
|
| 77 |
+
You are AuditQ&A, an AI Assistant created by Auditors and Data Scientist. You are given a question and extracted passages of the consolidated/departmental/thematic focus audit reports. Provide a clear and structured answer based on the passages provided, the context and the guidelines.
|
| 78 |
+
Guidelines:
|
| 79 |
+
- If the passages have useful facts or numbers, use them in your answer.
|
| 80 |
+
- When you use information from a passage, mention where it came from by using [Doc i] at the end of the sentence. i stands for the number of the document.
|
| 81 |
+
- Do not use the sentence 'Doc i says ...' to say where information came from.
|
| 82 |
+
- If the same thing is said in more than one document, you can mention all of them like this: [Doc i, Doc j, Doc k]
|
| 83 |
+
- Do not just summarize each passage one by one. Group your summaries to highlight the key parts in the explanation.
|
| 84 |
+
- If it makes sense, use bullet points and lists to make your answers easier to understand.
|
| 85 |
+
- You do not need to use every passage. Only use the ones that help answer the question.
|
| 86 |
+
- If the documents do not have the information needed to answer the question, just say you do not have enough information.
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
USER_PROMPT = """Passages:
|
| 90 |
+
{context}
|
| 91 |
+
-----------------------
|
| 92 |
+
Question: {question} - Explained to audit expert
|
| 93 |
+
Answer in english with the passages citations:
|
| 94 |
+
""".format(context = context_retrieved_lst, question=query)
|
| 95 |
+
|
| 96 |
+
messages = [
|
| 97 |
+
SystemMessage(content=SYSTEM_PROMPT),
|
| 98 |
+
HumanMessage(
|
| 99 |
+
content=USER_PROMPT
|
| 100 |
+
),]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# get llm_qa
|
| 104 |
# llm_qa = HuggingFaceEndpoint(
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
# create rag chain
|
| 122 |
+
chat_model = ChatHuggingFace(llm=llm_qa)
|
| 123 |
+
chain = chat_model| StrOutputParser()
|
| 124 |
# get answers
|
| 125 |
answer_lst = []
|
| 126 |
for question, context in zip(question_lst , context_retrieved_lst):
|
| 127 |
+
answer = chain.invoke(messages)
|
| 128 |
+
answer_lst.append(answer.content)
|
| 129 |
docs_html = []
|
| 130 |
for i, d in enumerate(context_retrieved, 1):
|
| 131 |
docs_html.append(make_html_source(d, i))
|