-
Notifications
You must be signed in to change notification settings - Fork 92
/
RetrievalAugmentation.py
306 lines (266 loc) · 11.4 KB
/
RetrievalAugmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import logging
import pickle
from .cluster_tree_builder import ClusterTreeBuilder, ClusterTreeConfig
from .EmbeddingModels import BaseEmbeddingModel
from .QAModels import BaseQAModel, GPT3TurboQAModel
from .SummarizationModels import BaseSummarizationModel
from .tree_builder import TreeBuilder, TreeBuilderConfig
from .tree_retriever import TreeRetriever, TreeRetrieverConfig
from .tree_structures import Node, Tree
# Define a dictionary to map supported tree builders to their respective configs
supported_tree_builders = {"cluster": (ClusterTreeBuilder, ClusterTreeConfig)}
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
class RetrievalAugmentationConfig:
def __init__(
self,
tree_builder_config=None,
tree_retriever_config=None, # Change from default instantiation
qa_model=None,
embedding_model=None,
summarization_model=None,
tree_builder_type="cluster",
# New parameters for TreeRetrieverConfig and TreeBuilderConfig
# TreeRetrieverConfig arguments
tr_tokenizer=None,
tr_threshold=0.5,
tr_top_k=5,
tr_selection_mode="top_k",
tr_context_embedding_model="OpenAI",
tr_embedding_model=None,
tr_num_layers=None,
tr_start_layer=None,
# TreeBuilderConfig arguments
tb_tokenizer=None,
tb_max_tokens=100,
tb_num_layers=5,
tb_threshold=0.5,
tb_top_k=5,
tb_selection_mode="top_k",
tb_summarization_length=100,
tb_summarization_model=None,
tb_embedding_models=None,
tb_cluster_embedding_model="OpenAI",
):
# Validate tree_builder_type
if tree_builder_type not in supported_tree_builders:
raise ValueError(
f"tree_builder_type must be one of {list(supported_tree_builders.keys())}"
)
# Validate qa_model
if qa_model is not None and not isinstance(qa_model, BaseQAModel):
raise ValueError("qa_model must be an instance of BaseQAModel")
if embedding_model is not None and not isinstance(
embedding_model, BaseEmbeddingModel
):
raise ValueError(
"embedding_model must be an instance of BaseEmbeddingModel"
)
elif embedding_model is not None:
if tb_embedding_models is not None:
raise ValueError(
"Only one of 'tb_embedding_models' or 'embedding_model' should be provided, not both."
)
tb_embedding_models = {"EMB": embedding_model}
tr_embedding_model = embedding_model
tb_cluster_embedding_model = "EMB"
tr_context_embedding_model = "EMB"
if summarization_model is not None and not isinstance(
summarization_model, BaseSummarizationModel
):
raise ValueError(
"summarization_model must be an instance of BaseSummarizationModel"
)
elif summarization_model is not None:
if tb_summarization_model is not None:
raise ValueError(
"Only one of 'tb_summarization_model' or 'summarization_model' should be provided, not both."
)
tb_summarization_model = summarization_model
# Set TreeBuilderConfig
tree_builder_class, tree_builder_config_class = supported_tree_builders[
tree_builder_type
]
if tree_builder_config is None:
tree_builder_config = tree_builder_config_class(
tokenizer=tb_tokenizer,
max_tokens=tb_max_tokens,
num_layers=tb_num_layers,
threshold=tb_threshold,
top_k=tb_top_k,
selection_mode=tb_selection_mode,
summarization_length=tb_summarization_length,
summarization_model=tb_summarization_model,
embedding_models=tb_embedding_models,
cluster_embedding_model=tb_cluster_embedding_model,
)
elif not isinstance(tree_builder_config, tree_builder_config_class):
raise ValueError(
f"tree_builder_config must be a direct instance of {tree_builder_config_class} for tree_builder_type '{tree_builder_type}'"
)
# Set TreeRetrieverConfig
if tree_retriever_config is None:
tree_retriever_config = TreeRetrieverConfig(
tokenizer=tr_tokenizer,
threshold=tr_threshold,
top_k=tr_top_k,
selection_mode=tr_selection_mode,
context_embedding_model=tr_context_embedding_model,
embedding_model=tr_embedding_model,
num_layers=tr_num_layers,
start_layer=tr_start_layer,
)
elif not isinstance(tree_retriever_config, TreeRetrieverConfig):
raise ValueError(
"tree_retriever_config must be an instance of TreeRetrieverConfig"
)
# Assign the created configurations to the instance
self.tree_builder_config = tree_builder_config
self.tree_retriever_config = tree_retriever_config
self.qa_model = qa_model or GPT3TurboQAModel()
self.tree_builder_type = tree_builder_type
def log_config(self):
config_summary = """
RetrievalAugmentationConfig:
{tree_builder_config}
{tree_retriever_config}
QA Model: {qa_model}
Tree Builder Type: {tree_builder_type}
""".format(
tree_builder_config=self.tree_builder_config.log_config(),
tree_retriever_config=self.tree_retriever_config.log_config(),
qa_model=self.qa_model,
tree_builder_type=self.tree_builder_type,
)
return config_summary
class RetrievalAugmentation:
"""
A Retrieval Augmentation class that combines the TreeBuilder and TreeRetriever classes.
Enables adding documents to the tree, retrieving information, and answering questions.
"""
def __init__(self, config=None, tree=None):
"""
Initializes a RetrievalAugmentation instance with the specified configuration.
Args:
config (RetrievalAugmentationConfig): The configuration for the RetrievalAugmentation instance.
tree: The tree instance or the path to a pickled tree file.
"""
if config is None:
config = RetrievalAugmentationConfig()
if not isinstance(config, RetrievalAugmentationConfig):
raise ValueError(
"config must be an instance of RetrievalAugmentationConfig"
)
# Check if tree is a string (indicating a path to a pickled tree)
if isinstance(tree, str):
try:
with open(tree, "rb") as file:
self.tree = pickle.load(file)
if not isinstance(self.tree, Tree):
raise ValueError("The loaded object is not an instance of Tree")
except Exception as e:
raise ValueError(f"Failed to load tree from {tree}: {e}")
elif isinstance(tree, Tree) or tree is None:
self.tree = tree
else:
raise ValueError(
"tree must be an instance of Tree, a path to a pickled Tree, or None"
)
tree_builder_class = supported_tree_builders[config.tree_builder_type][0]
self.tree_builder = tree_builder_class(config.tree_builder_config)
self.tree_retriever_config = config.tree_retriever_config
self.qa_model = config.qa_model
if self.tree is not None:
self.retriever = TreeRetriever(self.tree_retriever_config, self.tree)
else:
self.retriever = None
logging.info(
f"Successfully initialized RetrievalAugmentation with Config {config.log_config()}"
)
def add_documents(self, docs):
"""
Adds documents to the tree and creates a TreeRetriever instance.
Args:
docs (str): The input text to add to the tree.
"""
if self.tree is not None:
user_input = input(
"Warning: Overwriting existing tree. Did you mean to call 'add_to_existing' instead? (y/n): "
)
if user_input.lower() == "y":
# self.add_to_existing(docs)
return
self.tree = self.tree_builder.build_from_text(text=docs)
self.retriever = TreeRetriever(self.tree_retriever_config, self.tree)
def retrieve(
self,
question,
start_layer: int = None,
num_layers: int = None,
top_k: int = 10,
max_tokens: int = 3500,
collapse_tree: bool = True,
return_layer_information: bool = True,
):
"""
Retrieves information and answers a question using the TreeRetriever instance.
Args:
question (str): The question to answer.
start_layer (int): The layer to start from. Defaults to self.start_layer.
num_layers (int): The number of layers to traverse. Defaults to self.num_layers.
max_tokens (int): The maximum number of tokens. Defaults to 3500.
use_all_information (bool): Whether to retrieve information from all nodes. Defaults to False.
Returns:
str: The context from which the answer can be found.
Raises:
ValueError: If the TreeRetriever instance has not been initialized.
"""
if self.retriever is None:
raise ValueError(
"The TreeRetriever instance has not been initialized. Call 'add_documents' first."
)
return self.retriever.retrieve(
question,
start_layer,
num_layers,
top_k,
max_tokens,
collapse_tree,
return_layer_information,
)
def answer_question(
self,
question,
top_k: int = 10,
start_layer: int = None,
num_layers: int = None,
max_tokens: int = 3500,
collapse_tree: bool = True,
return_layer_information: bool = False,
):
"""
Retrieves information and answers a question using the TreeRetriever instance.
Args:
question (str): The question to answer.
start_layer (int): The layer to start from. Defaults to self.start_layer.
num_layers (int): The number of layers to traverse. Defaults to self.num_layers.
max_tokens (int): The maximum number of tokens. Defaults to 3500.
use_all_information (bool): Whether to retrieve information from all nodes. Defaults to False.
Returns:
str: The answer to the question.
Raises:
ValueError: If the TreeRetriever instance has not been initialized.
"""
# if return_layer_information:
context, layer_information = self.retrieve(
question, start_layer, num_layers, top_k, max_tokens, collapse_tree, True
)
answer = self.qa_model.answer_question(context, question)
if return_layer_information:
return answer, layer_information
return answer
def save(self, path):
if self.tree is None:
raise ValueError("There is no tree to save.")
with open(path, "wb") as file:
pickle.dump(self.tree, file)
logging.info(f"Tree successfully saved to {path}")