File size: 9,999 Bytes
b879c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# """
# Script de test complet pour le système de mémoire
# """

# from langchain_groq import ChatGroq
# from langgraph.graph import StateGraph, END
# from langgraph.prebuilt import ToolNode
# from typing import TypedDict, Sequence, Annotated
# from langchain_core.messages import BaseMessage
# from langgraph.graph.message import add_messages
# from dotenv import load_dotenv
# import os

# # Importer les composants
# # from memory_integration import tools_with_memory
# # from enhanced_system_prompt import create_enhanced_model_call, ENHANCED_SYSTEM_PROMPT

# # ============================================================================
# # CONFIGURATION
# # ============================================================================

# class AgentState(TypedDict):
#     messages: Annotated[Sequence[BaseMessage], add_messages]

# load_dotenv()
# api_key = os.getenv("GROQ_API_KEY")
# if not api_key:
#     raise ValueError("GROQ_API_KEY non définie")

# model = ChatGroq(
#     model="llama-3.1-8b-instant",
#     temperature=0.3,
#     max_tokens=2048,
#     api_key=api_key
# ).bind_tools(tools_with_memory)

# # ============================================================================
# # CONSTRUCTION DU GRAPHE AMÉLIORÉ
# # ============================================================================

# def should_continue(state: AgentState) -> str:
#     messages = state["messages"]
#     last_message = messages[-1]
    
#     if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
#         return "continue"
#     else:
#         return "end"

# # Créer le graphe
# graph = StateGraph(AgentState)

# # Ajouter les nœuds
# model_call_enhanced = create_enhanced_model_call()
# graph.add_node("llm", model_call_enhanced)

# tool_node = ToolNode(tools=tools_with_memory)
# graph.add_node("tools", tool_node)

# # Définir les connexions
# graph.set_entry_point("llm")
# graph.add_conditional_edges(
#     "llm",
#     should_continue,
#     {
#         "continue": "tools",
#         "end": END,
#     },
# )
# graph.add_edge("tools", "llm")

# # Compiler
# app_with_memory = graph.compile()

# # ============================================================================
# # FONCTIONS UTILITAIRES
# # ============================================================================

# def print_stream(stream, show_tool_calls=True):
#     """Affiche le flux de messages de manière élégante"""
#     print("\n" + "="*70)
    
#     for i, s in enumerate(stream):
#         message = s["messages"][-1]
        
#         if hasattr(message, 'content') and message.content:
#             print(f"\n{'─'*70}")
#             if hasattr(message, 'type'):
#                 if message.type == 'human':
#                     print("👤 UTILISATEUR:")
#                 elif message.type == 'ai':
#                     print("🤖 ASSISTANT:")
#                 elif message.type == 'tool':
#                     if show_tool_calls:
#                         print("🔧 RÉSULTAT OUTIL:")
            
#             content = message.content
#             if isinstance(content, str):
#                 # Limiter l'affichage si trop long
#                 if len(content) > 1000:
#                     print(content[:1000] + "\n... (contenu tronqué)")
#                 else:
#                     print(content)
#             else:
#                 print(content)
        
#         if hasattr(message, 'tool_calls') and message.tool_calls and show_tool_calls:
#             print("\n🔧 APPELS D'OUTILS:")
#             for tool_call in message.tool_calls:
#                 print(f"  → {tool_call.get('name', 'unknown')}({tool_call.get('args', {})})")
    
#     print("\n" + "="*70)

# def run_test(user_query: str, test_name: str = ""):
#     """Exécute un test avec affichage formaté"""
#     if test_name:
#         print(f"\n\n{'#'*70}")
#         print(f"# TEST: {test_name}")
#         print(f"{'#'*70}")
    
#     inputs = {"messages": [("user", user_query)]}
#     print_stream(app_with_memory.stream(inputs, stream_mode="values"))

# # ============================================================================
# # SUITE DE TESTS
# # ============================================================================

# def run_all_tests():
#     """Exécute tous les tests du système"""
    
#     print("\n" + "="*70)
#     print(" 🧪 SUITE DE TESTS - SYSTÈME DE MÉMOIRE INTELLIGENT")
#     print("="*70)
    
#     # Test 1: Première recherche (création du cache)
#     run_test(
#         "Fais-moi un résumé complet sur l'impact de l'intelligence artificielle sur le marché du travail",
#         "Test 1 - Première recherche (cache vide)"
#     )
    
#     # Test 2: Même sujet (utilisation du cache)
#     run_test(
#         "Peux-tu me redonner les infos sur l'IA et l'emploi ?",
#         "Test 2 - Recherche dans le cache"
#     )
    
#     # Test 3: Recherche dans la mémoire
#     run_test(
#         "Qu'est-ce que tu as trouvé sur l'intelligence artificielle ?",
#         "Test 3 - Recherche sémantique dans la mémoire"
#     )
    
#     # Test 4: Historique
#     run_test(
#         "Montre-moi l'historique de mes recherches",
#         "Test 4 - Consultation de l'historique"
#     )
    
#     # Test 5: Nouvelle recherche différente
#     run_test(
#         "Fais une analyse sur les énergies renouvelables",
#         "Test 5 - Nouvelle recherche (sujet différent)"
#     )
    
#     # Test 6: Question simple (pas de recherche)
#     run_test(
#         "Bonjour, comment ça va ?",
#         "Test 6 - Conversation simple (sans recherche)"
#     )
    
#     # Test 7: Recherche croisée
#     run_test(
#         "Compare ce que tu as trouvé sur l'IA et les énergies renouvelables",
#         "Test 7 - Recherche croisée dans la mémoire"
#     )
    
#     print("\n\n" + "="*70)
#     print(" ✅ TOUS LES TESTS TERMINÉS")
#     print("="*70)


# def demo_memory_stats():
#     """Affiche les statistiques de la mémoire"""
#     from memory_system import memory_system
    
#     print("\n" + "="*70)
#     print(" 📊 STATISTIQUES DU SYSTÈME DE MÉMOIRE")
#     print("="*70)
    
#     # Stats vectorielles
#     vector_count = memory_system.vector_memory.collection.count()
#     print(f"\n🗄️ Base Vectorielle:")
#     print(f"   Documents stockés: {vector_count}")
#     print(f"   Hashes en cache: {len(memory_system.vector_memory.content_hashes)}")
    
#     # Stats agent
#     conv_count = len(memory_system.agent_memory.conversation_history)
#     research_count = len(memory_system.agent_memory.research_cache)
    
#     print(f"\n🧠 Mémoire Agent:")
#     print(f"   Conversations: {conv_count}")
#     print(f"   Recherches en cache: {research_count}")
#     print(f"   Topics mémorisés: {len(memory_system.agent_memory.topic_keywords)}")
    
#     if research_count > 0:
#         print(f"\n📚 Topics en cache:")
#         for topic in list(memory_system.agent_memory.research_cache.keys())[:5]:
#             print(f"   • {topic}")
    
#     print("\n" + "="*70)


# # ============================================================================
# # MENU INTERACTIF
# # ============================================================================

# def interactive_menu():
#     """Menu interactif pour tester le système"""
    
#     while True:
#         print("\n" + "="*70)
#         print(" 🎯 ASSISTANT DE RECHERCHE INTELLIGENT")
#         print("="*70)
#         print("\n Options:")
#         print("  1. Poser une question / Lancer une recherche")
#         print("  2. Rechercher dans la mémoire")
#         print("  3. Voir l'historique")
#         print("  4. Statistiques de la mémoire")
#         print("  5. Lancer la suite de tests")
#         print("  6. Réinitialiser la mémoire")
#         print("  0. Quitter")
        
#         choice = input("\n👉 Votre choix: ").strip()
        
#         if choice == "1":
#             query = input("\n💬 Votre question: ")
#             run_test(query, "Recherche utilisateur")
        
#         elif choice == "2":
#             query = input("\n🔍 Recherche dans la mémoire: ")
#             run_test(f"Cherche dans ta mémoire: {query}", "Recherche mémoire")
        
#         elif choice == "3":
#             run_test("Montre-moi mon historique de recherches", "Historique")
        
#         elif choice == "4":
#             demo_memory_stats()
        
#         elif choice == "5":
#             run_all_tests()
        
#         elif choice == "6":
#             confirm = input("\n⚠️ Êtes-vous sûr de vouloir réinitialiser ? (oui/non): ")
#             if confirm.lower() == "oui":
#                 from memory_system import memory_system
#                 memory_system.agent_memory.clear_all()
#                 print("✅ Mémoire réinitialisée")
#             else:
#                 print("❌ Annulé")
        
#         elif choice == "0":
#             print("\n👋 Au revoir!")
#             break
        
#         else:
#             print("\n❌ Choix invalide")


# # ============================================================================
# # POINT D'ENTRÉE
# # ============================================================================

# if __name__ == "__main__":
#     import sys
    
#     print("\n" + "🚀"*35)
#     print(" SYSTÈME DE RECHERCHE INTELLIGENT AVEC MÉMOIRE")
#     print("🚀"*35 + "\n")
    
#     if len(sys.argv) > 1:
#         if sys.argv[1] == "test":
#             # Mode test automatique
#             run_all_tests()
#             demo_memory_stats()
#         elif sys.argv[1] == "stats":
#             # Afficher uniquement les stats
#             demo_memory_stats()
#         else:
#             # Exécuter une requête directe
#             query = " ".join(sys.argv[1:])
#             run_test(query, "Requête CLI")
#     else:
#         # Mode interactif
#         interactive_menu()