Gourisankar Padihary
commited on
Commit
·
afa7a1b
1
Parent(s):
79dcf63
Logging enabled
Browse files- data/load_dataset.py +2 -1
- generator/create_prompt.py +8 -38
- generator/extract_attributes.py +1 -0
- generator/generate_response.py +2 -2
data/load_dataset.py
CHANGED
@@ -3,6 +3,7 @@ from datasets import load_dataset
|
|
3 |
|
4 |
def load_data():
|
5 |
logging.info("Loading dataset")
|
6 |
-
dataset = load_dataset("rungalileo/ragbench", 'covidqa', split="
|
7 |
logging.info("Dataset loaded successfully")
|
|
|
8 |
return dataset
|
|
|
3 |
|
4 |
def load_data():
|
5 |
logging.info("Loading dataset")
|
6 |
+
dataset = load_dataset("rungalileo/ragbench", 'covidqa', split="test")
|
7 |
logging.info("Dataset loaded successfully")
|
8 |
+
logging.info(dataset)
|
9 |
return dataset
|
generator/create_prompt.py
CHANGED
@@ -3,7 +3,7 @@ from langchain.docstore.document import Document
|
|
3 |
def create_prompt(documents, question, response):
|
4 |
prompt = f""" I asked someone to answer a question based on one or more documents. Your task is to review their response and assess whether or not each sentence in that response is supported by text in the documents. If supported, identify which sentences in the documents provide that support. Additionally, identify which documents contain useful information for answering the question, and which documents the answer was sourced from.
|
5 |
|
6 |
-
Here are the documents, each split into sentences
|
7 |
{documents}
|
8 |
|
9 |
The question was:
|
@@ -33,23 +33,26 @@ def create_prompt(documents, question, response):
|
|
33 |
"all_utilized_sentence_keys": ["string"]
|
34 |
}}
|
35 |
|
36 |
-
|
37 |
contain useful information for answering the question. Provide a step-by-step
|
38 |
breakdown of information provided in the documents and how it is useful for
|
39 |
answering the question.
|
|
|
40 |
The all_relevant_sentence_keys field is a list of all document sentences keys
|
41 |
-
(e.g.
|
42 |
useful and relevant to the question, even if it was not used in the response,
|
43 |
or if only parts of the sentence are useful. Ignore the provided response when
|
44 |
making this judgement and base your judgement solely on the provided documents
|
45 |
and question. Omit sentences that, if removed from the document, would not
|
46 |
-
impact someone
|
|
|
47 |
The overall_supported_explanation field is a string explaining why the response
|
48 |
*as a whole* is or is not supported by the documents. In this field, provide a
|
49 |
step-by-step breakdown of the claims made in the response and the support (or
|
50 |
lack thereof) for those claims in the documents. Begin by assessing each claim
|
51 |
separately, one by one; don’t make any remarks about the response as a whole
|
52 |
until you have assessed all the claims in isolation.
|
|
|
53 |
The overall_supported field is a boolean indicating whether the response as a
|
54 |
whole is supported by the documents. This value should reflect the conclusion
|
55 |
you drew at the end of your step-by-step breakdown in overall_supported_explanation.
|
@@ -62,7 +65,7 @@ def create_prompt(documents, question, response):
|
|
62 |
16
|
63 |
- explanation: a string explaining why the sentence is or is not supported by the
|
64 |
documents.
|
65 |
-
- supporting_sentence_keys: keys (e.g.
|
66 |
support the response sentence. If the sentence is not supported, this list MUST
|
67 |
be empty. If the sentence is supported, this list MUST contain one or more keys.
|
68 |
In special cases where the sentence is supported, but not by any specific sentence,
|
@@ -98,37 +101,4 @@ def create_prompt(documents, question, response):
|
|
98 |
is supported by the text in the documents.
|
99 |
|
100 |
"""
|
101 |
-
return prompt
|
102 |
-
prompt = f""" I asked someone to answer a question based on one or more documents. Your task is to review their response and assess whether or not each sentence in that response is supported by text in the documents. If supported, identify which sentences in the documents provide that support. Additionally, identify which documents contain useful information for answering the question, and which documents the answer was sourced from.
|
103 |
-
|
104 |
-
Here are the documents, each split into sentences with associated keys:
|
105 |
-
{documents}
|
106 |
-
|
107 |
-
The question was:
|
108 |
-
{question}
|
109 |
-
|
110 |
-
Here is their response, split into sentences with associated keys:
|
111 |
-
{response}
|
112 |
-
|
113 |
-
Provide a JSON response with an answer , relevance_explanation, all_relevant_sentence_keys, overall_supported_explanation, overall_supported, sentence_support_information(this can be a json inside a json and with these fields response_sentence_key, explanation, supporting_sentence_keys, fully_supported) and all_utilized_sentence_keys below are the definitions
|
114 |
-
|
115 |
-
You must respond with a JSON object matching this schema:
|
116 |
-
|
117 |
-
{{
|
118 |
-
"answer": "string",
|
119 |
-
"relevance_explanation": "string",
|
120 |
-
"all_relevant_sentence_keys": ["string"],
|
121 |
-
"overall_supported_explanation": "string",
|
122 |
-
"overall_supported": "boolean",
|
123 |
-
"sentence_support_information": [
|
124 |
-
{{
|
125 |
-
"response_sentence_key": "string",
|
126 |
-
"explanation": "string",
|
127 |
-
"supporting_sentence_keys": ["string"],
|
128 |
-
"fully_supported": "boolean"
|
129 |
-
}},
|
130 |
-
],
|
131 |
-
"all_utilized_sentence_keys": ["string"]
|
132 |
-
}}
|
133 |
-
"""
|
134 |
return prompt
|
|
|
3 |
def create_prompt(documents, question, response):
|
4 |
prompt = f""" I asked someone to answer a question based on one or more documents. Your task is to review their response and assess whether or not each sentence in that response is supported by text in the documents. If supported, identify which sentences in the documents provide that support. Additionally, identify which documents contain useful information for answering the question, and which documents the answer was sourced from.
|
5 |
|
6 |
+
Here are the documents, each of which is split into sentences. Alongside each sentence is associated key, such as '0a.' or '0b.' that you can use to refer to it:
|
7 |
{documents}
|
8 |
|
9 |
The question was:
|
|
|
33 |
"all_utilized_sentence_keys": ["string"]
|
34 |
}}
|
35 |
|
36 |
+
The relevance_explanation field is a string explaining which documents
|
37 |
contain useful information for answering the question. Provide a step-by-step
|
38 |
breakdown of information provided in the documents and how it is useful for
|
39 |
answering the question.
|
40 |
+
|
41 |
The all_relevant_sentence_keys field is a list of all document sentences keys
|
42 |
+
(e.g. '0a') that are relevant to the question. Include every sentence that is
|
43 |
useful and relevant to the question, even if it was not used in the response,
|
44 |
or if only parts of the sentence are useful. Ignore the provided response when
|
45 |
making this judgement and base your judgement solely on the provided documents
|
46 |
and question. Omit sentences that, if removed from the document, would not
|
47 |
+
impact someone's ability to answer the question.
|
48 |
+
|
49 |
The overall_supported_explanation field is a string explaining why the response
|
50 |
*as a whole* is or is not supported by the documents. In this field, provide a
|
51 |
step-by-step breakdown of the claims made in the response and the support (or
|
52 |
lack thereof) for those claims in the documents. Begin by assessing each claim
|
53 |
separately, one by one; don’t make any remarks about the response as a whole
|
54 |
until you have assessed all the claims in isolation.
|
55 |
+
|
56 |
The overall_supported field is a boolean indicating whether the response as a
|
57 |
whole is supported by the documents. This value should reflect the conclusion
|
58 |
you drew at the end of your step-by-step breakdown in overall_supported_explanation.
|
|
|
65 |
16
|
66 |
- explanation: a string explaining why the sentence is or is not supported by the
|
67 |
documents.
|
68 |
+
- supporting_sentence_keys: keys (e.g. '0a') of sentences from the documents that
|
69 |
support the response sentence. If the sentence is not supported, this list MUST
|
70 |
be empty. If the sentence is supported, this list MUST contain one or more keys.
|
71 |
In special cases where the sentence is supported, but not by any specific sentence,
|
|
|
101 |
is supported by the text in the documents.
|
102 |
|
103 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
return prompt
|
generator/extract_attributes.py
CHANGED
@@ -10,6 +10,7 @@ def extract_attributes(question, relevant_docs, response):
|
|
10 |
# Format documents into a string by accessing the `page_content` attribute of each Document
|
11 |
formatted_documents = "\n".join([f"Doc {i+1}: {doc.page_content}" for i, doc in enumerate(relevant_docs)])
|
12 |
|
|
|
13 |
attribute_prompt = create_prompt(formatted_documents, question, response)
|
14 |
|
15 |
# Instead of using BaseMessage, pass the formatted prompt directly to invoke
|
|
|
10 |
# Format documents into a string by accessing the `page_content` attribute of each Document
|
11 |
formatted_documents = "\n".join([f"Doc {i+1}: {doc.page_content}" for i, doc in enumerate(relevant_docs)])
|
12 |
|
13 |
+
#print(f'Formated documents: {formatted_documents}')
|
14 |
attribute_prompt = create_prompt(formatted_documents, question, response)
|
15 |
|
16 |
# Instead of using BaseMessage, pass the formatted prompt directly to invoke
|
generator/generate_response.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
from langchain.chains import RetrievalQA
|
2 |
|
3 |
-
def generate_response(llm, vector_store, question):
|
4 |
# Create a retrieval-based question-answering chain using the relevant documents
|
5 |
qa_chain = RetrievalQA.from_chain_type(
|
6 |
llm=llm,
|
7 |
retriever=vector_store.as_retriever(),
|
8 |
return_source_documents=True
|
9 |
)
|
10 |
-
result = qa_chain.invoke(question)
|
11 |
response = result['result']
|
12 |
source_docs = result['source_documents']
|
13 |
return response, source_docs
|
|
|
1 |
from langchain.chains import RetrievalQA
|
2 |
|
3 |
+
def generate_response(llm, vector_store, question, relevant_docs):
|
4 |
# Create a retrieval-based question-answering chain using the relevant documents
|
5 |
qa_chain = RetrievalQA.from_chain_type(
|
6 |
llm=llm,
|
7 |
retriever=vector_store.as_retriever(),
|
8 |
return_source_documents=True
|
9 |
)
|
10 |
+
result = qa_chain.invoke(question, documents=relevant_docs)
|
11 |
response = result['result']
|
12 |
source_docs = result['source_documents']
|
13 |
return response, source_docs
|