File size: 13,755 Bytes
4fa4aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c846f7b
 
 
 
4fa4aa5
 
09703e5
4fa4aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
08881ee
 
4fa4aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6f69d2
4fa4aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c65a5b
4fa4aa5
 
 
 
 
 
 
 
51ee6bd
 
 
 
 
 
c431a8b
 
 
 
8d99dd6
c431a8b
 
 
8131b59
 
c431a8b
 
 
 
 
 
51ee6bd
 
 
 
 
c431a8b
51ee6bd
1c65a5b
 
c9705de
64bcdab
c949211
 
 
 
 
 
64bcdab
51ee6bd
 
 
d03f63b
51ee6bd
d03f63b
 
 
 
 
 
 
 
 
51ee6bd
d03f63b
51ee6bd
d03f63b
 
 
 
 
51ee6bd
c431a8b
51ee6bd
dc3d0e6
51ee6bd
 
 
 
 
 
 
6e95c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51ee6bd
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core import QueryBundle
import gradio as gr
import pandas as pd
from llama_index.core.postprocessor import LLMRerank
from IPython.display import display, HTML
from llama_index.core.vector_stores import (
    MetadataFilter,
    MetadataFilters,
    FilterOperator,
    FilterOperator
)
from llama_index.core.tools import RetrieverTool
from llama_index.core.retrievers import RouterRetriever
from llama_index.core.selectors import PydanticSingleSelector
from llama_index.core import (
    VectorStoreIndex,
    SimpleKeywordTableIndex,
    SimpleDirectoryReader,
)
from llama_index.core import SummaryIndex, Settings
from llama_index.core.schema import IndexNode
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.llms.openai import OpenAI
from llama_index.core.callbacks import CallbackManager
from llama_index.core import Document
import os
from llama_index.embeddings.openai import OpenAIEmbedding 
import nest_asyncio
import pandas as pd
import hashlib
import tiktoken
from dotenv import load_dotenv

load_dotenv()


nest_asyncio.apply()
openai_key = os.getenv('openai_key_secret')
os.environ["OPENAI_API_KEY"] = openai_key


llm=OpenAI(temperature=0, model="gpt-4o")
Settings.llm = llm
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
ds=pd.read_excel("data_metropole 2.xlsx")

# df est la DATAFRAME qui contient le fichier source
df=ds.drop(columns=['Theme ID', 'SousTheme ID', 'Signataire Matricule',
       'Suppleant Matricule', 'Date Nomination', 'Date Commite Technique', 'Numero',
       'Libelle', 'Date Creation', 'Date Debut'])
#la DATAFRAME (filter_signataire) est celle qui contient les colonne relative au signataire
#la DATAFRAME (filter) est celle qui contient les colonne relative au département
df['Item Text'] = df['Item Text'].replace('signature', '', regex=True)
df['Item Text'] = df['Item Text'].replace('cosignature', '', regex=True)
filter_signataire = df[['Signataire', 'Fonction']]
filter_signataire = filter_signataire.drop_duplicates()
filter = df[['Collectivite', 'Direction DGA', 'Liste Service Text']]
filter = filter.drop_duplicates()

# pre traitement est cleaning des dataframe
df = df.dropna(subset=['Item Text'])
df_sorted = df.sort_values(by=['Collectivite', 'Direction DGA', 'Liste Service Text', 'Item Text','Theme Title','SousTheme Title','Item Text'])

#traietement des dataframe
df.loc[:, 'content'] = df.apply(lambda x: f'''
/ Theme : {x['Theme Title'] or ''}
/ Sous-Theme : {x['SousTheme Title'] or ''}
/ Item : {x['Item Text'] or ''}
/ Signataire : {x['Signataire'] or ''}
/ Suppleant : {x['Suppleant'] or ''}
/ Les services : {x['Liste Service Text'] or ''}
''', axis=1)

#############

df = df.fillna(value='')
filter = filter.fillna(value='')
filter_signataire = filter_signataire.fillna(value='')

#############

df.loc[:, 'description'] = df.apply(lambda x: f'''Collectivite : {x['Collectivite'] or ''}
Direction : {x['Direction DGA'] or ''}
Liste des Service : {x['Liste Service Text'] or ''}
''', axis=1)

filter.loc[:, 'description'] = filter.apply(lambda x: f'''Collectivite : {x['Collectivite'] or ''}
Direction : {x['Direction DGA'] or ''}
Liste des Service : {x['Liste Service Text'] or ''}
''', axis=1)

filter_signataire.loc[:, 'description'] = filter_signataire.apply(lambda x: f'''Signataire : {x['Signataire'] or ''}
Fonction : {x['Fonction'] or ''}
''', axis=1)

def hachage(row):
    return hashlib.sha1(row.encode("utf-8")).hexdigest()

# le hashage
df['hash'] = df.apply(lambda x: hachage(f'''Collectivite : {x['Collectivite'] or ''}
Direction : {x['Direction DGA'] or ''}
Liste des Service : {x['Liste Service Text'] or ''}
'''), axis=1)
filter['hash'] = filter.apply(lambda x: hachage(f'''Collectivite : {x['Collectivite'] or ''}
Direction : {x['Direction DGA'] or ''}
Liste des Service : {x['Liste Service Text'] or ''}
'''), axis=1)
#################################################"
filter_signataire['hash'] = filter_signataire.apply(lambda x: hachage(f'''Signataire : {x['Signataire'] or ''}
'''), axis=1)

#construction des DOCUMENTS pour la vectorisation
description_docs = [Document(text=row['description'],metadata={"id_documents": row['hash']}) for index, row in filter.iterrows()]
content_docs = [Document(text=row['content'],metadata={"id_documents": row['hash']}) for index, row in df.iterrows()]
signataire_docs = [Document(text=row['Signataire'],metadata={"id_signataire": row['hash']}) for index, row in filter_signataire.iterrows()]
content_signataire = [Document(text=row['content'],metadata={"id_signataire": row['hash']}) for index, row in df.iterrows()]

index = VectorStoreIndex.from_documents(
    description_docs,
    show_progress = True
)
index_all = VectorStoreIndex.from_documents(
    content_docs,
    show_progress = True
)
index_signataire = VectorStoreIndex.from_documents(
    signataire_docs,
    show_progress = True
)
index_all_signataire = VectorStoreIndex.from_documents(
    content_signataire,
    show_progress = True
)

def get_retrieved_nodes(
    query_str, vector_top_k=10, reranker_top_n=3, with_reranker=False,index=index):
    query_bundle = QueryBundle(query_str)
    # configure retriever
    retriever = VectorIndexRetriever(
        index=index,
        similarity_top_k=vector_top_k,
    
    )
    retrieved_nodes = retriever.retrieve(query_bundle)

    if with_reranker:
        # configure reranker
        reranker = LLMRerank(
            choice_batch_size=5,
            top_n=reranker_top_n,
        )
        retrieved_nodes = reranker.postprocess_nodes(
            retrieved_nodes, query_bundle
        )

    return retrieved_nodes
def get_all_text(new_nodes):
    texts = []
    for i, node in enumerate(new_nodes, 1):
        texts.append(f"\nDocument {i} : {node.get_text()}")
    return ' '.join(texts)

def further_retrieve(query):
    # Retrieve new nodes based on the query
    new_nodes = get_retrieved_nodes(
        query,
        index=index,
        vector_top_k=10,
        reranker_top_n=5,
        with_reranker=False,
    )
    new_nodes_signataire = get_retrieved_nodes(
        query,
        index=index_all_signataire,
        vector_top_k=10,
        reranker_top_n=5,
        with_reranker=False,
    )
    filters = MetadataFilters(
        filters=[
            MetadataFilter(key="id_documents", value=[node.metadata['id_documents'] for node in new_nodes], operator=FilterOperator.IN)
        ],
    )
    filters_s = MetadataFilters(
        filters=[
            MetadataFilter(key="id_signataire", value=[node.metadata['id_signataire'] for node in new_nodes_signataire], operator=FilterOperator.IN)
       ],
    )

        # Create a retriever with the specified filters
    retriever_description = index_all.as_retriever(filters=filters, similarity_top_k=15)
    retriever_signataire= index_all_signataire.as_retriever(filters=filters_s,similarity_top_k=4)
            # initialize tools
    description_tool = RetrieverTool.from_defaults(
        retriever=retriever_description,
        description="Useful for retrieving specific context from direction, liste service and collectivite",
    )
    signataire_tool = RetrieverTool.from_defaults(
        retriever=retriever_signataire,
        description="Useful for retrieving specific context from signataire and fonction",
    )
    # define retriever
    retriever = RouterRetriever(
        selector=PydanticSingleSelector.from_defaults(llm=llm),
        retriever_tools=[
            description_tool,
            signataire_tool,
        ],
    )
    try : 
        query_bundle = QueryBundle(query)
            # Retrieve nodes based on the original query and filters
        retrieved_nodes = retriever.retrieve(query_bundle)
        reranker = LLMRerank(
            choice_batch_size=5,  # Process 5 nodes at a time
            top_n=10  # Return the top 7 reranked nodes
        )
        
        # Post-process the retrieved nodes by reranking them
        reranked_nodes = reranker.postprocess_nodes(retrieved_nodes, query_bundle)
        return get_all_text(reranked_nodes)
    except :
        print("No rerank")
        return get_all_text(retriever.retrieve(query))
        
def estimate_tokens(text):
    # Encoder le texte pour obtenir les tokens
    encoding = tiktoken.get_encoding("cl100k_base")
    tokens = encoding.encode(text)
    return len(tokens)
    
def question_reformulation(question):
    from openai import OpenAI

    client = OpenAI(api_key=openai_key)
    
    stream = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "system", "content": "reformule la question en specifiant le domaine de la question."},
                  
                  {"role": "user", "content": question}
                 ],
    
    )
    resultat = stream.choices[0].message.content
    return resultat

history_with_docs = []
def process_final(user_prom, history):
    global history_with_docs
    documents = further_retrieve(user_prom)
    user_question = question_reformulation(user_prom)
    history_with_docs.append((user_prom, documents))
    system_p = f"""agit come un expert financier et un agent de la metropole expert dans la recherche des deleguation de signature . L'utilisateur posera une question et tu devras trouver la réponse dans les documents suivants.Focalise sur les service et la direction du signataire que l'utilisateur cherche. Tu ne dois pas poser de question en retour.Tu ne dois pas mentionner le numéro des documents. Tu t'exprimes dans la même langue que l'utilisateur., 
                                        
                                        DOCUMENTS : 
                                        {documents}
                                        instruction : 
                                        -donne les signataire et les supplient et reponds de facon directe.
                                        -ta reponse peut se trouver sur plusieurs document
                                        -justifie la raison de ta reponse
                                        -la question fait reference a un service tres precis 
                                        -reponds par une liste structuree 
                                        """
    print("PHASE 03 passing to LLM\n")
    sys_p = f"<|im_start|>system \n{system_p}\n<|im_end|>"
    prompt_f = ""
    # total_tokens = estimate_tokens(prompt_f)

    # for val in reversed(history):
    #     if val[0]:  # Si c'est une question utilisateur
    #         # Chercher le document correspondant dans history_with_docs
    #         for past_question, past_documents in reversed(history_with_docs):
    #             if past_question == val[0]:
    #                 user_p = f" <|im_start|>user \n Documents: \n {past_documents}\n Question :{val[0]}\n<|im_end|>"
    #                 break
    #     if val[1]:  # Si c'est une réponse de l'assistant
    #         assistant_p = f" <|im_start|>assistant \n {val[1]}\n<|im_end|>"
        
    #     current_tokens = estimate_tokens(user_p+assistant_p)
        
    #     if total_tokens + current_tokens > 3000:
    #         break
    #     else:
    #         prompt_f = user_p + assistant_p + prompt_f
    #         total_tokens += current_tokens
            
    prompt_f = f"{sys_p} <|im_start|>user\n {user_question} \n<|im_end|><|im_start|>assistant \n"
    gen = llm.stream_complete(formatted=True, prompt=prompt_f)
    # print(f"le nombre TOTAL de tokens : {total_tokens}\n")
    print("_"*100)
    print(prompt_f)
    print("o"*100)
    for response in gen:
        yield response.text

from gradio import gradio as gr
# mychatbot = gr.Chatbot(
#     avatar_images=["./user_icon.png", "./metro.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,
# )
# description = """
# <p>
# <center>
# <img src="https://www.nicecotedazur.org/wp-content/themes/mnca/images/logo-metropole-nca.png" alt="rick" width="250"/>
# </center>
# </p>
# <p style="text-align:right"> Made by KHEOPS AI</p>
# """   
# demo = gr.ChatInterface(
#     fn=process_final, 
#     chatbot=mychatbot,
#     title="METROPOLE SIGNATAIRE CHATBOT",
#     description=description,
# )
# demo.launch(share=True, debug =True)
# Gradio Interface
with gr.Blocks() as demo:
    with gr.Row():
        description = """
        <h1 style ="font-size: 36px;font-weight: bold;"><center>METROPOLE SIGNATAIRE CHATBOT</center></h1>
        <p>
        <center>
        <img src="https://www.nicecotedazur.org/wp-content/themes/mnca/images/logo-metropole-nca.png" alt="rick" width="250"/>
        </center>
        </p>
        <p style="text-align:right"> Développé par  KHEOPS AI</p>
        """ 
        gr.HTML(description)
    chatbot = gr.Chatbot(height = "20rem")
    msg = gr.Textbox(show_label=False,placeholder = "Poser votre question ...")
    clear = gr.Button("Réinitialiser")

    def user(user_message, history):
        # Capture the user message and pass it to 'process_final'
        return "", history + [[user_message, None]]

    def bot(history):
        # Get the last user message from the history
        user_message = history[-1][0]
        
        # Process it using the 'process_final' function
        gen = process_final(user_message, history)
        
        bot_message = ""
        for chunk in gen:
            bot_message += chunk
            history[-1][1] = bot_message  # Update bot response in the conversation history
            yield history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    
    clear.click(lambda: None, None, chatbot, queue=False)

demo.launch(share=True, debug =True)