File size: 9,963 Bytes
12cca3e
05e0faf
 
 
 
12cca3e
 
 
05e0faf
 
 
 
 
12cca3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e0faf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12cca3e
 
 
 
 
 
 
 
 
f8e7b59
12cca3e
 
 
 
 
 
 
 
 
f8e7b59
12cca3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8e7b59
12cca3e
 
 
 
 
 
 
 
 
f8e7b59
12cca3e
 
 
 
 
 
f8e7b59
12cca3e
 
f8e7b59
12cca3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05e0faf
12cca3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# reranks the top articles from a given csv file
# from langchain_openai import ChatOpenAI
# from langchain.chains import RetrievalQA
# from langchain_community.document_loaders.csv_loader import CSVLoader
# from langchain_community.vectorstores import DocArrayInMemorySearch
from sentence_transformers import CrossEncoder
import pandas as pd
import time
import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.tokenize import sent_tokenize


"""

This function rerank top articles (15 -> 4) from a given csv, then sends to LLM

Input:

    csv_path: str

    question: str

    top_n: int

Output:

    response: str

    links: list of str

    titles: list of str

    

Other functions in this file does not send articles to LLM. This is an exception.

Created using langchain RAG functions. Deprecated.

Update: Use langchain_RAG instead.

"""


# def langchain_rerank_answer(csv_path, question, source='url', top_n=4):
#     llm = ChatOpenAI(temperature=0.0)
#     loader = CSVLoader(csv_path, source_column="url")

#     index = VectorstoreIndexCreator(
#         vectorstore_cls=DocArrayInMemorySearch,
#     ).from_loaders([loader])

#     # prompt_template = """You are an a chatbot that answers tobacco related questions with source. Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
#     # {context}
#     # Question: {question}"""
#     # PROMPT = PromptTemplate(
#     # template=prompt_template, input_variables=["context", "question"]
#     # )
#     # chain_type_kwargs = {"prompt": PROMPT}

#     qa = RetrievalQA.from_chain_type(
#         llm=llm,
#         chain_type="stuff",
#         retriever=index.vectorstore.as_retriever(),
#         verbose=False,
#         return_source_documents=True,
#         # chain_type_kwargs=chain_type_kwargs,
#         # chain_type_kwargs = {
#         #     "document_separator": "<<<<>>>>>"
#         # },
#     )

#     answer = qa({"query": question})
#     sources = answer['source_documents']
#     sources_out = [source.metadata['source'] for source in sources]

#     return answer['result'], sources_out


# """
#     Langchain with sources.
#     This function is deprecated. Use langchain_RAG instead.
# """


# def langchain_with_sources(csv_path, question, top_n=4):
#     llm = ChatOpenAI(temperature=0.0)
#     loader = CSVLoader(csv_path, source_column="uuid")
#     index = VectorstoreIndexCreator(
#         vectorstore_cls=DocArrayInMemorySearch,
#     ).from_loaders([loader])

#     qa = RetrievalQAWithSourcesChain.from_chain_type(
#         llm=llm,
#         chain_type="stuff",
#         retriever=index.vectorstore.as_retriever(),
#     )
#     output = qa({"question": question}, return_only_outputs=True)
#     return output['answer'], output['sources']


# """
#     Reranks the top articles using crossencoder. 
#     Uses cross-encoder/ms-marco-MiniLM-L-6-v2 for embedding / reranking.
#     Input:
#         csv_path: str
#         question: str
#         top_n: int
#     Output:
#         out_values: list of [content, uuid, title]
# """


# returns list of top n similar articles using crossencoder
def crossencoder_rerank_answer(csv_path: str, question: str, top_n=4) -> list:
    cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
    articles = pd.read_csv(csv_path)
    contents = articles['content'].tolist()
    uuids = articles['uuid'].tolist()
    titles = articles['title'].tolist()
    published_dates = articles['published_date'].tolist()

    # biencoder retrieval does not have domain
    if 'domain' not in articles:
        domain = [""] * len(contents)
    else:
        domain = articles['domain'].tolist()

    cross_inp = [[question, content] for content in contents]
    cross_scores = cross_encoder.predict(cross_inp)
    scores_sentences = list(zip(cross_scores, contents, uuids, titles, domain, published_dates))
    scores_sentences = sorted(scores_sentences, key=lambda x: x[0], reverse=True)

    out_values = scores_sentences[:top_n]

    # if score is less than 0, truncate
    for idx in range(len(out_values)):
        if out_values[idx][0] < 0:
            out_values = out_values[:idx]
            if len(out_values) == 0:
                out_values = scores_sentences[:1]

            break
    # print(out_values)
    return out_values


def crossencoder_rerank_sentencewise(csv_path: str, question: str, top_n=10) -> list:
    cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
    articles = pd.read_csv(csv_path)
    contents = articles['content'].tolist()
    uuids = articles['uuid'].tolist()
    titles = articles['title'].tolist()
    published_dates = articles['published_date'].tolist()
    if 'domain' not in articles:
        domain = [""] * len(contents)
    else:
        domain = articles['domain'].tolist()

    sentences = []
    new_uuids = []
    new_titles = []
    new_domains = []
    new_published_dates = []
    for idx in range(len(contents)):
        sents = sent_tokenize(contents[idx])
        sentences.extend(sents)
        new_uuids.extend([uuids[idx]] * len(sents))
        new_titles.extend([titles[idx]] * len(sents))
        new_domains.extend([domain[idx]] * len(sents))
        new_published_dates.extend([published_dates[idx]] * len(sents))
    cross_inp = [[question, sent] for sent in sentences]
    cross_scores = cross_encoder.predict(cross_inp)
    scores_sentences = list(zip(cross_scores, sentences, new_uuids, new_titles, new_domains, new_published_dates))
    scores_sentences = sorted(scores_sentences, key=lambda x: x[0], reverse=True)

    out_values = scores_sentences[:top_n]

    # if score is less than 0, truncate
    for idx in range(len(out_values)):
        if out_values[idx][0] < 0:
            out_values = out_values[:idx]
            if len(out_values) == 0:
                out_values = scores_sentences[:1]

            break

    return out_values


def crossencoder_rerank_sentencewise_sentence_chunks(csv_path, question, top_n=10, chunk_size=2):
    cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
    articles = pd.read_csv(csv_path)
    contents = articles['content'].tolist()
    uuids = articles['uuid'].tolist()
    titles = articles['title'].tolist()

    # embeddings do not have domain as column
    if 'domain' not in articles:
        domain = [""] * len(contents)
    else:
        domain = articles['domain'].tolist()

    sentences = []
    new_uuids = []
    new_titles = []
    new_domains = []
    
    for idx in range(len(contents)):
        sents = sent_tokenize(contents[idx])
        sents_merged = []

        # if the number of sentences is less than chunk size, merge and join
        if len(sents) < chunk_size:
            sents_merged.append(' '.join(sents))
        else:
            for i in range(0, len(sents) - chunk_size + 1):
                sents_merged.append(' '.join(sents[i:i + chunk_size]))

        sentences.extend(sents_merged)
        new_uuids.extend([uuids[idx]] * len(sents_merged))
        new_titles.extend([titles[idx]] * len(sents_merged))
        new_domains.extend([domain[idx]] * len(sents_merged))

    cross_inp = [[question, sent] for sent in sentences]
    cross_scores = cross_encoder.predict(cross_inp)
    scores_sentences = list(zip(cross_scores, sentences, new_uuids, new_titles, new_domains))
    scores_sentences = sorted(scores_sentences, key=lambda x: x[0], reverse=True)

    out_values = scores_sentences[:top_n]

    for idx in range(len(out_values)):
        if out_values[idx][0] < 0:
            out_values = out_values[:idx]
            if len(out_values) == 0:
                out_values = scores_sentences[:1]

            break

    return out_values


def crossencoder_rerank_sentencewise_articles(csv_path, question, top_n=4):
    cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
    contents, uuids, titles, domain = load_articles(csv_path)

    sentences = []
    contents_elongated = []
    new_uuids = []
    new_titles = []
    new_domains = []

    for idx in range(len(contents)):
        sents = sent_tokenize(contents[idx])
        sentences.extend(sents)
        new_uuids.extend([uuids[idx]] * len(sents))
        contents_elongated.extend([contents[idx]] * len(sents))
        new_titles.extend([titles[idx]] * len(sents))
        new_domains.extend([domain[idx]] * len(sents))

    cross_inp = [[question, sent] for sent in sentences]
    cross_scores = cross_encoder.predict(cross_inp)
    scores_sentences = list(zip(cross_scores, contents_elongated, new_uuids, new_titles, new_domains))
    scores_sentences = sorted(scores_sentences, key=lambda x: x[0], reverse=True)

    score_sentences_compressed = []
    for item in scores_sentences:
        if not score_sentences_compressed:
            score_sentences_compressed.append(item)
        else:
            if item[2] not in [x[2] for x in score_sentences_compressed]:
                score_sentences_compressed.append(item)

    scores_sentences = score_sentences_compressed
    return scores_sentences[:top_n]


def no_rerank(csv_path, question, top_n=4):
    contents, uuids, titles, domains = load_articles(csv_path)
    return list(zip(contents, uuids, titles, domains))[:top_n]


def load_articles(csv_path:str):
    articles = pd.read_csv(csv_path)
    contents = articles['content'].tolist()
    uuids = articles['uuid'].tolist()
    titles = articles['title'].tolist()
    if 'domain' not in articles:
        domain = [""] * len(contents)
    else:
        domain = articles['domain'].tolist()
    return contents, uuids, titles, domain