File size: 8,956 Bytes
fecab9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee15f4a
 
 
 
fecab9d
 
 
 
 
ee15f4a
fecab9d
 
ee15f4a
 
fecab9d
 
 
 
 
 
 
 
 
ee15f4a
fecab9d
 
 
 
ee15f4a
 
 
 
 
 
 
 
fecab9d
 
 
 
 
 
 
ee15f4a
 
fecab9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee15f4a
 
fecab9d
 
ee15f4a
fecab9d
 
ee15f4a
 
 
fecab9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee15f4a
 
 
 
fecab9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee15f4a
fecab9d
 
 
 
 
 
 
 
 
 
 
 
ee15f4a
fecab9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee15f4a
fecab9d
 
 
 
 
 
 
 
 
 
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
""" app.py

Question / answer over a collection of PDF documents from OECD.org.

PDF text extraction:
    - pypdf

Retrieval model:
    - LanceDB: support for hybrid search search with reranking of results.
    - Full text search (lexical): BM25
    - Vector search (semantic dense vectors): BAAI/bge-m3

Rerankers:
    - ColBERT, cross encoder, reciprocal rank fusion, AnswerDotAI

Generation:
    - Mistral

:author: Didier Guillevic
:date: 2024-12-28
"""

import gradio as gr
import lancedb

import llm_utils

import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

#
# LanceDB with the indexed documents
#

# Connect to the database
lance_db = lancedb.connect("lance.db")
lance_tbl = lance_db.open_table("documents")

# Document schema
class Document(lancedb.pydantic.LanceModel):
    text: str
    vector: lancedb.pydantic.Vector(1024)
    file_name: str
    num_pages: int
    creation_date: str
    modification_date: str

#
# Retrieval: query types and reranker types
#

query_types = {
    'lexical': 'fts',
    'semantic': 'vector',
    'hybrid': 'hybrid',
}

# Define a few rerankers
colbert_reranker = lancedb.rerankers.ColbertReranker(column='text')
answerai_reranker = lancedb.rerankers.AnswerdotaiRerankers(column='text')
crossencoder_reranker = lancedb.rerankers.CrossEncoderReranker(column='text')
reciprocal_rank_fusion_reranker = lancedb.rerankers.RRFReranker() # hybrid search only

reranker_types = {
    'ColBERT': colbert_reranker,
    'cross encoder': crossencoder_reranker,
    'AnswerAI': answerai_reranker,
    'Reciprocal Rank Fusion': reciprocal_rank_fusion_reranker
}

def search_table(
        table: lancedb.table,
        query: str,
        query_type: str,
        reranker_name: str,
        filter_year: int,
        top_k: int=5,
        overfetch_factor: int=2
    ):
    # Get the instance of reranker
    reranker = reranker_types.get(reranker_name)
    if reranker is None:
        logger.error(f"Invalid reranker name: {reranker_name}")
        raise ValueError(f"Invalid reranker selected: {reranker_name}")
    
    if query_type in ["vector", "fts"]:
        if reranker == reciprocal_rank_fusion_reranker:
            # reciprocal is for 'hybrid' search type only
            reranker = crossencoder_reranker
        results = (
            table.search(query, query_type=query_type)
            .where(f"creation_date >= '{filter_year}'", prefilter=True)
            .limit(top_k * overfetch_factor)
            .rerank(reranker=reranker)
            .limit(top_k)
            .to_list() # to get access to '_relevance_score'
            #.to_pydantic(Document)
        )
    elif query_type == "hybrid":
        results = (
            table.search(query, query_type=query_type)
            .where(f"creation_date >= '{filter_year}'", prefilter=True)
            .limit(top_k * overfetch_factor)
            .rerank(reranker=reranker)
            .limit(top_k)
            .to_list() # to get access to '_relevance_score'
            #.to_pydantic(Document)
        )

    return results[:top_k]


#
# Generatton: query + context --> response
#

def create_bulleted_list(texts: list[str], scores: list[float]=None) -> str:
    """
    This function takes a list of strings and returns HTML with a bulleted list.
    """
    html_items = []

    if scores is not None:
        for text, score in zip(texts, scores):
            html_items.append(f"<li>(Score={score:.2f})\t{text}</li>")
    else:
        for text in texts:
            html_items.append(f"<li>{text}</li>")

    return "<ul>" + "".join(html_items) + "</ul>"


def generate_response(
        query: str,
        query_type: str,
        reranker_name: str,
        filter_year: int,
        top_k: int
    ) -> list[str, str, str]:
    """Generate a response given query, search type and reranker.

    Args:

    Returns:
        - the response given the snippets extracted from the database
        - (html string): the references (origin of the snippets of text used to generate the answer)
        - (html string): the snippets of text used to generate the answer
    """
    # Get results from LanceDB
    results = search_table(
        lance_tbl,
        query=query,
        query_type=query_type,
        reranker_name=reranker_name,
        filter_year=filter_year,
        top_k=top_k
    )
    
    references = [result['file_name'] for result in results]
    references_html = "<h4>References</h4>\n" + create_bulleted_list(references)

    snippets = [result['text'] for result in results]
    scores = [result['_relevance_score'] for result in results]
    snippets_html = "<h4>Snippets</h4>\n" + create_bulleted_list(snippets, scores)

    # Generate the reponse from the LLM
    stream_reponse = llm_utils.generate_chat_response_streaming(
        query, '\n\n'.join(snippets)
    )

    model_response = ""
    for chunk in stream_reponse:
        model_response += chunk.data.choices[0].delta.content
        yield model_response, references_html, snippets_html


#
# User interface
#

with gr.Blocks() as demo:
    gr.Markdown("""
        # Hybrid search / reranking / Mistral
        Document collection: OECD documents on international tax crimes.
    """)

    # Inputs: question
    question = gr.Textbox(
        label="Question to answer",
        placeholder=""
    )

    # Response / references / snippets
    response = gr.Textbox(
        label="Response",
        placeholder=""
    )
    with gr.Accordion("References & snippets", open=False):
        references = gr.HTML(label="References")
        snippets = gr.HTML(label="Snippets")
    
    # Button
    with gr.Row():
        response_button = gr.Button("Submit", variant='primary')
        clear_button = gr.Button("Clear", variant='secondary')
    
    # Additional inputs
    query_type = gr.Dropdown(
        choices=query_types.items(),
        value='hybrid',
        label='Query type',
        render=False
    )
    reranker_name = gr.Dropdown(
        choices=list(reranker_types.keys()),
        value='cross encoder',
        label='Reranker',
        render=False
    )
    filter_year = gr.Slider(
        minimum=2005, maximum=2020, value=2005, step=1,
        label='Creation date >=', render=False
    )
    top_k = gr.Slider(
        minimum=2, maximum=10, value=5, step=1,
        label='Top k result', render=False
    )

    with gr.Row():
        # Example questions given default provided PDF file
        with gr.Accordion("Sample questions", open=False):
            gr.Examples(
                [
                    ["What is the OECD's role in combating offshore tax evasion?",],
                    ["What are the key tools used in fighting offshore tax evasion?",],
                    ['What are "High Net Worth Individuals" (HNWIs) and how do they relate to tax compliance efforts?',],
                    ["What is the significance of international financial centers (IFCs) in the context of tax evasion?",],
                    ["What is being done to address the role of professional enablers in facilitating tax evasion?",],
                    ["How does the OECD measure the effectiveness of international efforts to fight offshore tax evasion?",],
                    ['What are the "Ten Global Principles" for fighting tax crime?',],
                    ["What are some recent developments in the fight against offshore tax evasion?",],
                ],
                inputs=[question, query_type, reranker_name, filter_year, top_k],
                outputs=[response, references, snippets],
                fn=generate_response,
                cache_examples=False,
                label="Sample questions"
            )
        
        # Additional inputs: search parameters
        with gr.Accordion("Search parameters", open=False):
            with gr.Row():
                query_type.render()
                reranker_name.render()
                filter_year.render()
                top_k.render()
    
    # Documentation
    with gr.Accordion("Documentation", open=False):
        gr.Markdown("""
            - Retrieval model
                - LanceDB: support for hybrid search search with reranking of results.
                - Full text search (lexical): BM25
                - Vector search (semantic dense vectors): BAAI/bge-m3
            - Rerankers
                - ColBERT, cross encoder, reciprocal rank fusion, AnswerDotAI
            - Generation
                - Mistral
            - Examples
                - Generated using Google NotebookLM
        """)

    # Click actions
    response_button.click(
        fn=generate_response,
        inputs=[question, query_type, reranker_name, filter_year, top_k],
        outputs=[response, references, snippets]
    )
    clear_button.click(
        fn=lambda: ('', '', '', ''),
        inputs=[],
        outputs=[question, response, references, snippets]
    )


demo.launch(show_api=False)