-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentire_pipeline.py
245 lines (189 loc) · 9.17 KB
/
entire_pipeline.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
from typing import Tuple, Any
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sentence_transformers import SentenceTransformer, util
import torch
# for not seeing a warning message
import logging
logging.getLogger("transformers.modeling_utils").setLevel(logging.ERROR)
from keras.models import model_from_json
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
@tf.keras.utils.register_keras_serializable()
class WeightedCosineSimilarity(tf.keras.layers.Layer):
def __init__(self, units = 128, activation=None, **kwargs):
'''Initializes the class and sets up the internal variables'''
super(WeightedCosineSimilarity, self).__init__(**kwargs)
self.units = units
self.activation = tf.keras.activations.get(activation)
def get_config(self):
config = super(WeightedCosineSimilarity, self).get_config()
return config
def build(self, input_shape):
'''Create the state of the layer (weights)'''
# W should be half the size of the input and should be ones
w_init = tf.ones_initializer()
w_init_val = w_init(shape=(int(input_shape[-1] / 2),), dtype='float32')
self.w = tf.Variable(initial_value=w_init_val, trainable='true')
def call(self, inputs):
'''Defines the computation from inputs to outputs'''
# Take the first half of the input which is U:
U = inputs[:, :int(inputs.shape[-1] / 2)] # (128, 768)
# Take the second half of the input which is V:
V = inputs[:, int(inputs.shape[-1] / 2):] # (128, 768)
# Compute the element wise product of U, V and W
UW = tf.multiply(U, tf.exp(self.w)) # (128, 768) * (768)
# Compute the multiplication of UW and V
VW = tf.multiply(V, tf.exp(self.w)) # (128, 768) * (768)
UWVW = tf.multiply(UW, VW) # (128, 768) * (768)
# Sum the result over the second axis
WUV = tf.reduce_sum(UWVW, axis=1) # (128, 768) -> (128, 1)
# Square UW and VW
WU_squared = tf.square(UW) # (128, 768) -> (128, 768)
WV_squared = tf.square(VW) # (128, 768) -> (128, 768)
# Sum the result over the second axis
WU_squared_sum = tf.reduce_sum(WU_squared, axis=1) # (128, 768) -> (128, 1)
WV_squared_sum = tf.reduce_sum(WV_squared, axis=1) # (128, 768) -> (128, 1)
# take the root of the sum of squares of WUV, WU_squared and WV_squared
WU_squared_root = tf.sqrt(WU_squared_sum) # (128, 1)
WV_squared_root = tf.sqrt(WV_squared_sum) # (128, 1)
denominator = tf.multiply(WU_squared_root, WV_squared_root) # (128, 1) * (128, 1) = (128, 1)
# divide WUV by the denominator
WUV_div_denominator = tf.divide(WUV, denominator)
return self.activation(WUV_div_denominator)
def pipeline(query: str, method: str = 'cs', n_contexts: int = 5, chatgpt_prompt = "You are a Teachers Assistant and you should answer the QUESTION using the information given in the CONTEXT, if the CONTEXT is unrelated, you should ignore it."):
"""
This function is the pipeline for the entire project. It takes in a query and finds the most relevant document.
and gives it to the OpenAI API to generate a answer
:param n_contexts: The number of contexts to return
:param semantic_search_model: The semantic search model to use
:param query: The query to search for
:param n_contexts: The number of contexts to return
:param method: The semantic search model to use
:return:
"""
# 1. Preprocess the query
embedding = get_text_embedding(query)
# 2. Semantic Search
best_ctx = semantic_search_model(embedding, method, n_contexts)
# 3. Answer Generation
answer = answer_generation(query, best_ctx, chatgpt_prompt=chatgpt_prompt)
# 4. Return the answer
return answer, best_ctx
def get_text_embedding(text: str):
# Load pre-trained model and tokenizer
model = SentenceTransformer('sentence-transformers/multi-qa-mpnet-base-cos-v1')
embedding = model.encode(text, convert_to_tensor=True)
return embedding
def semantic_search_model(embedding: np.ndarray, method: str = 'ann', n_contexts: int = 5) -> str:
"""
This function takes in a query embedding and finds the most relevant document by using the ANN
:param n_contexts: The number of contexts to return
:param embedding: The query embedding of the query of dimension 768
:return: The most relevant n contexts
"""
# load the dataset
df = pd.read_pickle('./Data_Generation/df_pickle/final_02450_emb.pkl')
# load the embeddings
# make the df only contain the unique contexts
df = df.drop_duplicates(subset=['context'])
context_embeddings = np.stack(df['context_embedding'].to_numpy())
# Concatonate the query embedding ontop of each element in the context_embeddings, so each row is the query embedding and the context embedding
model_input = np.concatenate((np.tile(embedding, (len(context_embeddings), 1)), context_embeddings),
axis=1)
if method == 'ann':
with open('ANN/ANN_resamp.json', 'r') as json_file:
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("ANN/ANN_resamp.h5")
print("Loaded model from disk")
# Predict the most relevant context
prediction = loaded_model.predict(model_input)
# Get the 2 most relevant contexts
index = np.argsort(prediction, axis=0)[-n_contexts:]
# Get the context
# best_ctx_lst = [df.iloc[i]['context'].to_numpy()[0] for i in index]
# best_ctx = '. '.join(best_ctx_lst)
best_ctx_lst = [df.iloc[i]["context"].to_numpy()[0] for i in index]
best_ctx = '\n'.join(best_ctx_lst)
return best_ctx
if method == 'weighted_cs':
with open('ANN/model_cos.json', 'r') as json_file:
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json, custom_objects={'CustomLayer': WeightedCosineSimilarity})
# load weights into new model
loaded_model.load_weights("ANN/model_cos.h5")
print("Loaded model from disk")
# Predict the most relevant context
prediction = loaded_model.predict(model_input)
# Get the 2 most relevant contexts
index = np.argsort(prediction, axis=0)[-n_contexts:]
# Get the context
best_ctx_lst = [df.iloc[i]['context'].to_numpy()[0] for i in index]
best_ctx = '\n'.join(best_ctx_lst)
return best_ctx
elif method == 'cs':
# Should simply be the cosine similarity
x = np.array(embedding)
y = context_embeddings
cos_sim = np.dot(x, y.T) / (np.linalg.norm(x) * np.linalg.norm(y, axis=1))
# Get the 2 most relevant contexts
index = np.argsort(cos_sim, axis=0)[-n_contexts:]
# Get the context
#best_ctx_lst = [df.iloc[i]['context'] for i in index]
#best_ctx = '. '.join(best_ctx_lst)
best_ctx_lst = [df.iloc[i]["context"] for i in index]
best_ctx = '\n'.join(best_ctx_lst)
return best_ctx
return "ERROR: Possible methods are 'ann', 'weighted_cs' and 'cs'"
def answer_generation(query: str, context: str = "", pipeline_mode=True, chatgpt_prompt = "You are a Teachers Assistant and you should answer the QUESTION using the information given in the CONTEXT, if the CONTEXT is unrelated, you should ignore it."):
"""
This function takes in a query and a context and uses the OpenAI API to generate an answer
:param query:
:param context:
:return:
"""
if pipeline_mode:
print(f'CONTEXT: ```{context}``` QUESTION: ```{query}``` ANSWER:')
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
temperature=0,
#messages=[
# {"role": "system",
# "content": chatgpt_prompt},
# {"role": "user", "content": f'CONTEXT: ```{context}``` QUESTION: ```{query}``` ANSWER:'},
#]
messages = [
{"role": "user", "content": f' {chatgpt_prompt},CONTEXT: ```{context}``` QUESTION: ```{query}``` ANSWER:'},
]
)
except Exception as e:
return "OPENAI_ERROR:", str(e)
else:
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
temperature=0,
messages=[
{"role": "user", "content": f'QUESTION: ```{query}``` ANSWER:'},
]
)
except Exception as e:
return "OPENAI_ERROR:", str(e)
return completion.choices[0].message.content
if __name__ == "__main__":
# Read in the data
query = "What is the purpose of the Introduction to Machine Learning and Data Mining Lecture notes?"
answer_pipeline = pipeline(query, method='cs', n_contexts=2)
answer_chatgpt = answer_generation(query, pipeline_mode=False)
print(answer_pipeline)
print(answer_chatgpt)