-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_system.py
More file actions
1147 lines (955 loc) · 48.7 KB
/
agent_system.py
File metadata and controls
1147 lines (955 loc) · 48.7 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Sistema de Agente RAG com LLM que decide quais ferramentas usar.
O LLM recebe a pergunta do usuário e decide:
1. Se deve usar ferramentas estruturadas (query_tools)
2. Se deve usar busca semântica (RAG)
3. Quais parâmetros usar
4. Como combinar múltiplas ferramentas
Fluxo:
Usuario → LLM Planejador → Ferramentas → LLM Sintetizador → Resposta
"""
import json
from typing import Dict, Any, List, Tuple, Optional
import llm_chat
from config import DEFAULT_PROVIDER, DEEPSEEK_MODEL, OLLAMA_GENERATION_MODEL
from query_tools import QueryTools
from embedding_manager import EmbeddingManager
class RAGAgent:
"""
Agente inteligente que usa LLM para decidir quais ferramentas usar.
"""
# Perfis válidos no sistema
PERFIS_VALIDOS = ['reitor', 'vicereitor', 'dceuff']
def __init__(
self,
embedding_model: str = "mxbai-embed-large",
generation_model: str = "qwen3:30b",
planning_model: str = "qwen3:30b"
):
"""
Inicializa o agente RAG.
Args:
embedding_model: Modelo para embeddings
generation_model: Modelo para gerar resposta final
planning_model: Modelo para planejar ações (pode ser menor/mais rápido)
"""
self.embedding_manager = EmbeddingManager(embedding_model=embedding_model)
self.query_tools = QueryTools(self.embedding_manager, llm_model=generation_model)
self.generation_model = generation_model
self.planning_model = planning_model
print(f"✓ Agente RAG inicializado")
print(f" - Modelo de planejamento: {planning_model}")
print(f" - Modelo de geração: {generation_model}")
def validar_perfil(self, profile: Optional[str]) -> tuple[bool, str]:
"""
Valida se um perfil existe antes de buscar.
Args:
profile: Nome do perfil a validar
Returns:
Tupla (está_válido, mensagem)
"""
if not profile:
return True, ""
profile_clean = profile.replace('@', '').lower()
if profile_clean not in self.PERFIS_VALIDOS:
msg = f"Perfil @{profile_clean} não encontrado. Perfis disponíveis: {', '.join(['@' + p for p in self.PERFIS_VALIDOS])}"
return False, msg
return True, profile_clean
def _create_tools_description(self) -> str:
"""
Cria descrição das ferramentas disponíveis para o LLM.
Returns:
String com descrição de todas as ferramentas
"""
return """
## FERRAMENTAS DISPONÍVEIS:
1. **get_top_posts_by_likes**
- Uso: Encontrar posts com mais curtidas
- Parâmetros: limit (int), profile (str, opcional)
- Exemplo: {"tool": "get_top_posts_by_likes", "limit": 10, "profile": "reitor"}
2. **get_top_posts_by_comments**
- Uso: Encontrar posts com mais comentários
- Parâmetros: limit (int), profile (str, opcional)
- Exemplo: {"tool": "get_top_posts_by_comments", "limit": 5}
3. **get_posts_by_engagement**
- Uso: Encontrar posts com maior engajamento total (curtidas + comentários)
- Parâmetros: limit (int), profile (str, opcional)
- Exemplo: {"tool": "get_posts_by_engagement", "limit": 10, "profile": "dceuff"}
4. **get_bottom_posts_by_likes**
- Uso: Encontrar posts com MENOS curtidas
- Parâmetros: limit (int), profile (str, opcional)
- Exemplo: {"tool": "get_bottom_posts_by_likes", "limit": 10, "profile": "dceuff"}
5. **get_bottom_posts_by_comments**
- Uso: Encontrar posts com MENOS comentários
- Parâmetros: limit (int), profile (str, opcional)
- Exemplo: {"tool": "get_bottom_posts_by_comments", "limit": 5}
6. **get_recent_posts**
- Uso: Encontrar posts publicados recentemente
- Parâmetros: days (int), limit (int), profile (str, opcional)
- Exemplo: {"tool": "get_recent_posts", "days": 7, "limit": 5}
7. **get_profile_statistics**
- Uso: Obter estatísticas agregadas de um perfil
- Parâmetros: profile (str, opcional - se omitido, retorna todos)
- Exemplo: {"tool": "get_profile_statistics", "profile": "reitor"}
8. **compare_profiles**
- Uso: Comparar estatísticas entre todos os perfis
- Parâmetros: nenhum
- Exemplo: {"tool": "compare_profiles"}
9. **count_term_occurrences**
- Uso: QUANTIFICAR quantos posts mencionam um termo específico (consulta TODA a base)
- Quando usar: "quantos posts falam sobre X", "quantas vezes mencionaram Y", "frequência de Z"
- Parâmetros: term (str - termo a buscar), profile (str, opcional), case_sensitive (bool, default=False)
- Exemplo: {"tool": "count_term_occurrences", "term": "greve", "profile": "dceuff"}
- IMPORTANTE: Esta ferramenta CONTA ocorrências, não retorna os posts mais relevantes
10. **analyze_sentiment**
- Uso: ANALISAR SENTIMENTO e percepção sobre um tópico/entidade usando LLM
- Quando usar: "como é visto X?", "percepção sobre Y", "o que pensam sobre Z", "análise de sentimento", "avaliação de X"
- Parâmetros: topic (str - tópico/entidade), profile (str, opcional), n_posts (int, default=20)
- Exemplo: {"tool": "analyze_sentiment", "topic": "reitor", "profile": "dceuff", "n_posts": 20}
- Retorna: Contagem positivo/negativo/neutro, aspectos positivos/negativos, resumo qualitativo
11. **semantic_search**
- Uso: Buscar posts/notícias por CONTEÚDO/TEMA usando busca semântica vetorial
- Quando usar: Perguntas sobre "o que foi dito", "posts sobre X", "aparições", "mencionou", etc.
- Parâmetros: query (str - reformule para otimizar busca), n_results (int), profile (str, opcional), content_type_filter (str, opcional: 'news' ou 'instagram_post')
- Exemplo: {"tool": "semantic_search", "params": {"query": "HUAP hospital atendimento saúde", "n_results": 8}}
- Exemplo com filtro: {"tool": "semantic_search", "params": {"query": "cotas ações afirmativas", "n_results": 10, "content_type_filter": "news"}}
- IMPORTANTE: Reformule a query do usuário para termos mais específicos e relevantes
- IMPORTANTE: Use content_type_filter='news' quando buscar sobre ex-reitor ou período histórico
12. **get_news_articles**
- Uso: Buscar NOTÍCIAS filtradas por data e/ou publisher
- Quando usar: "notícias sobre", "reportagens", "imprensa", "mídia", "jornais"
- Parâmetros: limit (int), min_date (str ISO), max_date (str ISO), publisher (str, opcional)
- Exemplo: {"tool": "get_news_articles", "limit": 10, "min_date": "2009-01-01", "max_date": "2010-12-31"}
13. **search_news_by_person**
- Uso: Buscar notícias que mencionam uma PESSOA específica (ex: Roberto Salles, ex-reitor)
- Quando usar: "notícias sobre Roberto Salles", "o que a imprensa disse sobre X"
- Parâmetros: person_name (str), limit (int)
- Exemplo: {"tool": "search_news_by_person", "person_name": "Roberto Salles", "limit": 10}
14. **get_news_statistics**
- Uso: Estatísticas sobre notícias indexadas (total, publishers, período)
- Quando usar: "quantas notícias", "quais veículos", "período coberto"
- Parâmetros: nenhum
- Exemplo: {"tool": "get_news_statistics"}
15. **web_search**
- Uso: Buscar na INTERNET usando DuckDuckGo para contexto externo e atualizado
- Quando usar: "notícias sobre X 2025", "últimas informações sobre Y", "contexto atualizado de Z", "o que está acontecendo com W"
- IMPORTANTE: Use quando a base local NÃO é suficiente (perguntas sobre atualidades, eventos recentes, contexto nacional/internacional)
- Parâmetros: query (str - termo de busca), limit (int, default 5)
- Exemplo: {"tool": "web_search", "query": "educação Brasil 2025 notícias", "limit": 5}
- Retorna: Resultados com título, resumo, fonte e data (quando disponível)
- DICA: Combine com semantic_search quando a pergunta tem DUAS partes (contexto externo + conteúdo local)
## PERFIS DISPONÍVEIS (Instagram):
- @dceuff (Diretório Central dos Estudantes)
- @reitor (Reitor ATUAL da UFF - Antônio Cláudio Nóbrega 2023-2025)
- @vicereitor (Vice-Reitor ATUAL da UFF)
## FONTES DE DADOS AUXILIARES:
- Notícias/Arquivo: Conteúdo histórico sobre a UFF e ex-reitor Roberto Salles (2009-2018)
- Use para perguntas sobre Roberto Salles/Sales ou eventos passados
## CONTEXTO IMPORTANTE:
- **Roberto Salles (ou Roberto Sales)** foi REITOR DA UFF entre 2009-2018
- Notícias sobre "reitor da UFF" do período 2009-2018 referem-se a Roberto Salles
- Posts atuais (2023-2025) do perfil @reitor referem-se ao REITOR ATUAL (Antônio Cláudio Nóbrega)
- Para perguntas sobre Roberto Salles: SEMPRE buscar em NOTÍCIAS, nunca em posts atuais
## DIRETRIZES CRÍTICAS:
### Use SEMANTIC_SEARCH quando:
✅ Pergunta sobre CONTEÚDO: "o que foi dito sobre X", "posts que mencionam Y", "falar sobre Z"
✅ Busca por TEMA: "aparições públicas", "eventos", "anúncios", "opiniões sobre"
✅ Perguntas ABERTAS: "como X tratou Y", "qual posicionamento sobre Z"
✅ TEMPORAL + CONTEÚDO: "o que foi dito em 2024 sobre X"
✅ Contexto específico: "última aparição pública", "pronunciamento sobre"
### Use FERRAMENTAS DE NOTÍCIAS quando:
✅ Pergunta sobre IMPRENSA: "o que os jornais disseram", "reportagens sobre"
✅ Menção a PESSOA ESPECÍFICA: "Roberto Salles", "Roberto Sales", "ex-reitor"
✅ Veículos de mídia: "FAPERJ", "BBC", "O Globo", "G1"
✅ Período histórico: notícias de 2009-2018
✅ IMPORTANTE: Perguntas sobre POSICIONAMENTO/OPINIÃO do ex-reitor Roberto Salles SEMPRE usar search_news_by_person + semantic_search em notícias
✅ Exemplos: "o que Roberto Salles achava de X", "posição do ex-reitor sobre Y", "Roberto Salles falou sobre Z"
✅ GATILHOS CRÍTICOS: "ex-reitor", "Roberto", "Salles", "Sales" → buscar em notícias (2009-2018)
### Use WEB_SEARCH quando:
✅ CONTEXTO EXTERNO/ATUALIZADO: "como está X em 2025", "últimas notícias sobre Y", "o que está acontecendo com Z"
✅ NOTÍCIAS RECENTES: "notícias de hoje", "eventos recentes", "informação atualizada"
✅ CONTEXTO NACIONAL/INTERNACIONAL: "economia do Brasil", "educação em SP", "lei federal de X"
✅ BASE LOCAL INSUFICIENTE: Pergunta sobre tópicos não cobertos nos dados da UFF
✅ COMBINE com semantic_search: Quando pergunta tem DUAS partes (contexto externo + conteúdo local)
### Use FERRAMENTAS ESTRUTURADAS quando:
✅ RANKING MAIORES: "mais curtidos", "top 10", "maior engajamento"
✅ RANKING MENORES: "menos curtidos", "posts com menos comentários", "menor engajamento" (use get_bottom_*)
✅ MÉTRICAS: "quantos posts", "média de curtidas", "estatísticas"
✅ COMPARAÇÕES NUMÉRICAS: "qual perfil tem mais X"
✅ FILTROS TEMPORAIS PUROS: "posts da última semana" (sem contexto de conteúdo)
✅ QUANTIFICAÇÃO DE TERMOS: "quantos posts falam sobre X", "frequência de Y" (use count_term_occurrences)
✅ Precisa de CONTEXTO + RANKING: busca semântica + ordenação
### COMBINE FERRAMENTAS quando:
✅ Pergunta tem MÉTRICA + CONTEÚDO: use semantic_search primeiro, depois filtre por métrica
✅ Pergunta tem CONTEXTO EXTERNO + LOCAL: use web_search + semantic_search
✅ Precisa de CONTEXTO + RANKING: busca semântica + ordenação
## INSTRUÇÕES:
- Analise se a pergunta é sobre CONTEÚDO (use semantic_search), MÉTRICAS (use tools estruturadas) ou CONTEXTO EXTERNO (use web_search)
- Você pode usar MÚLTIPLAS ferramentas em sequência
- Ao usar semantic_search, REFORMULE a query para termos mais específicos
- Retorne APENAS um JSON válido, sem texto adicional
"""
def _plan_action(
self,
user_question: str,
profile_filter: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
LLM decide quais ferramentas usar e com quais parâmetros.
Args:
user_question: Pergunta do usuário
profile_filter: Filtro de perfil (opcional)
Returns:
Lista de ações (ferramentas) a executar
"""
# Validação de perfil
perfil_ok, perfil_msg = self.validar_perfil(profile_filter)
if not perfil_ok:
# Retorna ação especial para erro de perfil
return [{
"tool": "error",
"params": {"message": perfil_msg}
}]
tools_desc = self._create_tools_description()
planning_prompt = f"""{tools_desc}
## CONTEXTO:
Pergunta do usuário: "{user_question}"
{f'Perfil filtrado: {profile_filter}' if profile_filter else 'Sem filtro de perfil'}
## ⚠️ IMPORTANTE - PERFIS vs FONTES:
**PERFIS (Instagram - Posts atuais):**
- @dceuff (Diretório Central dos Estudantes)
- @reitor (Reitor ATUAL: Antônio Cláudio Nóbrega 2023-2025)
- @vicereitor (Vice-Reitor ATUAL: Fábio Barbosa Passos)
**FONTES AUXILIARES (NÃO são perfis):**
- Notícias/Arquivo: Para perguntas sobre ex-reitor Roberto Salles ou eventos históricos
- Use ferramentas: search_news_by_person, get_news_articles, semantic_search com content_type_filter='news'
## 📅 DATAS CRÍTICAS (NÃO CONFUNDA!):
- **Roberto de Souza Salles**: Reitor de **23 de novembro de 2006 até 18 de novembro de 2014**
- Saiu do cargo em 2014, NÃO foi reeleito
- Depois de 2014, foi apenas candidato em eleições (NÃO reitor)
- **Antônio Cláudio da Nóbrega**: Reitor de 2014-2018 (eleito em 2014)
- **Reitor ATUAL (2023-2025)**: Antônio Cláudio Nóbrega
INFORMAÇÃO CRÍTICA:
- Perguntas sobre "o que Roberto Salles fez" referem-se ao período 2006-2014
- Perguntas sobre "candidato Roberto Salles" referem-se a 2018 (quando disputou mas perdeu)
- Roberto Salles NUNCA foi reitor em 2015, 2016, 2017, 2018+ (alucinação comum)
- Perguntas sobre ele DEVEM buscar em NOTÍCIAS históricas
- NÃO confundir com o reitor atual (posts @reitor são de 2023-2025)
## SUA TAREFA:
Analise a pergunta e decida qual(is) ferramenta(s) usar para respondê-la da melhor forma.
RETORNE APENAS UM JSON no seguinte formato:
{{
"reasoning": "Breve explicação do seu raciocínio",
"actions": [
{{"tool": "nome_da_ferramenta", "params": {{"param1": "valor1"}}}}
]
}}
EXEMPLOS:
Pergunta: "Quais foram os 5 posts mais curtidos?"
{{
"reasoning": "Ranking numérico por curtidas - métrica pura",
"actions": [
{{"tool": "get_top_posts_by_likes", "params": {{"limit": 5}}}}
]
}}
Pergunta: "Quais posts do DCE tiveram menos curtidas?"
{{
"reasoning": "Ranking INVERSO - posts com MENOS curtidas, não mais",
"actions": [
{{"tool": "get_bottom_posts_by_likes", "params": {{"limit": 10, "profile": "dceuff"}}}}
]
}}
Pergunta: "Me fale sobre posts do HUAP"
{{
"reasoning": "Pergunta sobre CONTEÚDO específico (HUAP), precisa busca semântica",
"actions": [
{{"tool": "semantic_search", "params": {{"query": "HUAP hospital universitário atendimento saúde", "n_results": 8}}}}
]
}}
Pergunta: "Qual foi a última aparição pública do reitor?"
{{
"reasoning": "Pergunta sobre CONTEÚDO (aparição pública) com filtro temporal e de perfil. Usar busca semântica com query otimizada",
"actions": [
{{"tool": "semantic_search", "params": {{"query": "reitor aparição pública evento pronunciamento presença cerimônia", "n_results": 10, "profile": "reitor"}}}}
]
}}
Pergunta: "O que estão falando do reitor em 2024?"
{{
"reasoning": "Pergunta sobre CONTEÚDO relacionado ao reitor. Busca semântica com filtro de perfil",
"actions": [
{{"tool": "semantic_search", "params": {{"query": "reitor UFF ações gestão decisões anúncios", "n_results": 10}}}}
]
}}
Pergunta: "O que o ex-reitor Roberto Salles achava das cotas?"
{{
"reasoning": "Pergunta sobre OPINIÃO/POSICIONAMENTO do ex-reitor (2009-2018). Deve buscar em NOTÍCIAS, não em posts atuais. Combinar busca por pessoa + busca semântica APENAS EM NOTÍCIAS (content_type_filter='news')",
"actions": [
{{"tool": "search_news_by_person", "params": {{"person_name": "Roberto Salles", "limit": 15}}}},
{{"tool": "semantic_search", "params": {{"query": "cotas ações afirmativas política racial reserva vagas lei educação", "n_results": 15, "content_type_filter": "news"}}}}
]
}}
Pergunta: "O que Roberto Sales fez quando era reitor?"
{{
"reasoning": "Pergunta sobre AÇÕES do ex-reitor Roberto Sales (variação do nome Salles). Período histórico 2009-2018. Buscar em NOTÍCIAS",
"actions": [
{{"tool": "search_news_by_person", "params": {{"person_name": "Roberto Salles", "limit": 20}}}},
{{"tool": "search_news_by_person", "params": {{"person_name": "Roberto Sales", "limit": 20}}}}
]
}}
Pergunta: "Compare o engajamento entre reitor e DCE"
{{
"reasoning": "Comparação de MÉTRICAS estatísticas entre perfis",
"actions": [
{{"tool": "compare_profiles", "params": {{}}}}
]
}}
Pergunta: "Qual foi o post mais curtido recente do reitor?"
{{
"reasoning": "Combina MÉTRICA (curtidas) + temporal (recente) + perfil. Ferramenta estruturada resolve",
"actions": [
{{"tool": "get_top_posts_by_likes", "params": {{"limit": 1, "profile": "reitor"}}}}
]
}}
Pergunta: "Posts sobre pesquisa científica que tiveram mais engajamento"
{{
"reasoning": "Combina CONTEÚDO (pesquisa) + MÉTRICA (engajamento). Usar busca semântica primeiro",
"actions": [
{{"tool": "semantic_search", "params": {{"query": "pesquisa científica ciência laboratório estudo investigação", "n_results": 20}}}},
{{"tool": "get_posts_by_engagement", "params": {{"limit": 5}}}}
]
}}
Pergunta: "O que o DCE publicou sobre greve na última semana?"
{{
"reasoning": "CONTEÚDO (greve) + temporal (última semana) + perfil. Busca semântica com filtro temporal",
"actions": [
{{"tool": "get_recent_posts", "params": {{"days": 7, "limit": 30, "profile": "dceuff"}}}},
{{"tool": "semantic_search", "params": {{"query": "greve paralisação mobilização protesto reivindicação", "n_results": 10, "profile": "dceuff"}}}}
]
}}
Pergunta: "Qual é a situação da educação no Brasil?"
{{
"reasoning": "Pergunta vaga sobre tema amplo que requer contexto externo/nacional. Reformular para query específica de busca e usar web_search",
"actions": [
{{"tool": "web_search", "params": {{"query": "educação Brasil 2025 situação atualizado", "limit": 5}}}}
]
}}
Pergunta: "Quais são as notícias mais recentes sobre educação no Brasil?"
{{
"reasoning": "Pergunta sobre contexto EXTERNO (notícias atuais do Brasil). Base local não é suficiente, usar web_search para contexto atualizado",
"actions": [
{{"tool": "web_search", "params": {{"query": "educação Brasil notícias 2025", "limit": 5}}}}
]
}}
Pergunta: "Como está a inflação? E o DCE falou sobre isso?"
{{
"reasoning": "Pergunta com DUAS partes: 1) contexto externo (inflação) 2) conteúdo local. Combinar web_search + semantic_search",
"actions": [
{{"tool": "web_search", "params": {{"query": "inflação Brasil 2025 notícias economia", "limit": 3}}}},
{{"tool": "semantic_search", "params": {{"query": "inflação economia custo de vida preços", "n_results": 10, "profile": "dceuff"}}}}
]
}}
IMPORTANTE:
- Para CONTEÚDO/TEMA → semantic_search (sempre reformule query)
- Para MÉTRICAS/RANKING → ferramentas estruturadas
- Para CONTEXTO EXTERNO/ATUALIZADO → web_search (notícias recentes, eventos atuais)
- DICAS para web_search: transforme perguntas como "qual é a situação de X?" em "X Brasil 2025" ou "X notícias recentes"
- Retorne APENAS o JSON, nada mais!
"""
try:
# Chama o LLM para planejar (usa DeepSeek ou Ollama conforme config)
model_to_use = DEEPSEEK_MODEL if DEFAULT_PROVIDER == 'deepseek' else self.planning_model
response = llm_chat.chat(
model=model_to_use,
messages=[
{
'role': 'system',
'content': 'Você é um planejador especializado em análise de dados do Instagram. Retorne APENAS JSON válido, sem markdown ou texto adicional.'
},
{
'role': 'user',
'content': planning_prompt
}
]
)
# Parse da resposta
plan_text = response['message']['content']
# Tenta parsear o JSON
try:
plan = json.loads(plan_text)
except json.JSONDecodeError:
# Se falhar, tenta extrair JSON do texto
import re
json_match = re.search(r'\{.*\}', plan_text, re.DOTALL)
if json_match:
plan = json.loads(json_match.group())
else:
raise ValueError("LLM não retornou JSON válido")
print(f"\n🤔 Raciocínio do agente: {plan.get('reasoning', 'N/A')}")
print(f"🔧 Ações planejadas: {len(plan.get('actions', []))} ferramenta(s)")
return plan.get('actions', [])
except Exception as e:
print(f"⚠️ Erro no planejamento: {e}")
# Fallback: busca semântica simples
return [{
'tool': 'semantic_search',
'params': {
'query': user_question,
'n_results': 5,
'profile': profile_filter
}
}]
def _execute_action(
self,
action: Dict[str, Any]
) -> List[Dict[str, Any]]:
"""
Executa uma ação (ferramenta).
Args:
action: Dicionário com tool e params
Returns:
Resultados da ferramenta
"""
tool = action.get('tool')
params = action.get('params', {})
print(f" ⚙️ Executando: {tool} com params {params}")
try:
if tool == 'get_top_posts_by_likes':
return self.query_tools.get_top_posts_by_likes(
limit=params.get('limit', 10),
profile=params.get('profile')
)
elif tool == 'get_top_posts_by_comments':
return self.query_tools.get_top_posts_by_comments(
limit=params.get('limit', 10),
profile=params.get('profile')
)
elif tool == 'get_posts_by_engagement':
return self.query_tools.get_posts_by_engagement(
limit=params.get('limit', 10),
profile=params.get('profile')
)
elif tool == 'get_bottom_posts_by_likes':
return self.query_tools.get_bottom_posts_by_likes(
limit=params.get('limit', 10),
profile=params.get('profile')
)
elif tool == 'get_bottom_posts_by_comments':
return self.query_tools.get_bottom_posts_by_comments(
limit=params.get('limit', 10),
profile=params.get('profile')
)
elif tool == 'get_recent_posts':
return self.query_tools.get_recent_posts(
days=params.get('days', 30),
limit=params.get('limit', 10),
profile=params.get('profile')
)
elif tool == 'get_profile_statistics':
stats = self.query_tools.get_profile_statistics(
profile=params.get('profile')
)
return [{'metadata': stats, 'is_stats': True}]
elif tool == 'compare_profiles':
comparison = self.query_tools.compare_profiles()
return [{'metadata': comparison, 'is_comparison': True}]
elif tool == 'count_term_occurrences':
result = self.query_tools.count_term_occurrences(
term=params.get('term', ''),
profile=params.get('profile'),
case_sensitive=params.get('case_sensitive', False)
)
return [{'metadata': result, 'is_term_count': True}]
elif tool == 'analyze_sentiment':
result = self.query_tools.analyze_sentiment(
topic=params.get('topic', ''),
profile=params.get('profile'),
n_posts=params.get('n_posts', 20)
)
return [{'metadata': result, 'is_sentiment': True}]
elif tool == 'get_news_articles':
return self.query_tools.get_news_articles(
limit=params.get('limit', 10),
min_date=params.get('min_date'),
max_date=params.get('max_date'),
publisher=params.get('publisher')
)
elif tool == 'search_news_by_person':
return self.query_tools.search_news_by_person(
person_name=params.get('person_name', ''),
limit=params.get('limit', 10)
)
elif tool == 'get_news_statistics':
stats = self.query_tools.get_news_statistics()
return [{'metadata': stats, 'is_news_stats': True}]
elif tool == 'semantic_search':
query = params.get('query', '')
n_results = params.get('n_results', 5)
profile = params.get('profile')
content_type = params.get('content_type_filter') # Novo parâmetro
# Busca no embedding manager
raw_results = self.embedding_manager.search(
query=query,
n_results=n_results,
profile_filter=profile,
content_type_filter=content_type # Passa filtro de tipo
)
# Converte para formato padrão (lista de dicts)
if not raw_results or 'ids' not in raw_results or not raw_results['ids']:
return []
formatted_results = []
for i in range(len(raw_results['ids'][0])):
formatted_results.append({
'id': raw_results['ids'][0][i],
'metadata': raw_results['metadatas'][0][i],
'document': raw_results['documents'][0][i],
'distance': raw_results['distances'][0][i] if 'distances' in raw_results else None
})
return formatted_results
elif tool == 'web_search':
query = params.get('query', '')
limit = params.get('limit', 5)
# Executa busca na web
web_results = self.query_tools.web_search(
query=query,
limit=limit
)
# Converte para formato padrão com 'profile' para compatibilidade
formatted_results = []
for i, result in enumerate(web_results):
formatted_results.append({
'id': f'web_{i}',
'metadata': {
'source': result.get('source', ''),
'date': result.get('date', ''),
'type': 'web_search',
'profile': result.get('profile', 'web_search') # IMPORTANTE para evitar KeyError
},
'document': f"**{result.get('title', '')}**\n\n{result.get('body', '')}"
})
return formatted_results
else:
print(f"⚠️ Ferramenta desconhecida: {tool}")
return []
except Exception as e:
print(f"❌ Erro ao executar {tool}: {e}")
return []
def _format_results_for_llm(
self,
results: List[Dict[str, Any]],
tool_name: str
) -> str:
"""
Formata resultados das ferramentas para o LLM sintetizar.
Args:
results: Resultados das ferramentas
tool_name: Nome da ferramenta usada
Returns:
String formatada com os resultados
"""
if not results:
return f"## Resultado de {tool_name}:\nNenhum resultado encontrado."
# Verifica se é estatísticas
if results[0].get('is_stats'):
stats = results[0]['metadata']
return f"""## Estatísticas:
- Total de posts: {stats.get('total_posts', 0)}
- Total de curtidas: {stats.get('total_likes', 0)}
- Total de comentários: {stats.get('total_comments', 0)}
- Média de curtidas por post: {stats.get('avg_likes_per_post', 0):.2f}
- Média de comentários por post: {stats.get('avg_comments_per_post', 0):.2f}
- Engajamento total: {stats.get('total_engagement', 0)}
{f"- Post mais engajado: {stats['top_post']['url']} ({stats['top_post']['engagement']} engajamentos)" if stats.get('top_post') else ''}
"""
# Verifica se é comparação
if results[0].get('is_comparison'):
comparison = results[0]['metadata']
text = "## Comparação Entre Perfis:\n\n"
for profile, stats in comparison.items():
text += f"### @{profile}\n"
text += f"- Posts: {stats['total_posts']}\n"
text += f"- Curtidas: {stats['total_likes']} (média: {stats['avg_likes']:.1f})\n"
text += f"- Comentários: {stats['total_comments']} (média: {stats['avg_comments']:.1f})\n"
text += f"- Engajamento total: {stats['total_engagement']}\n\n"
return text
# Verifica se é contagem de termo
if results[0].get('is_term_count'):
data = results[0]['metadata']
text = f"## Contagem de Termo: '{data['term']}'\n"
text += f"- Perfil(s): {data['profile']}\n"
text += f"- Posts encontrados: {data['count']} de {data['total_posts']} ({data['percentage']}%)\n\n"
# Se houver erro
if data.get('error'):
text += f"⚠️ Erro: {data['error']}\n"
return text
# Lista alguns posts que contêm o termo
if data['matching_posts']:
text += "### Exemplos de posts que mencionam o termo:\n\n"
for i, post in enumerate(data['matching_posts'][:5], 1):
meta = post.get('metadata', {})
doc = post.get('document', '')
text += f"**Post {i}** (@{meta.get('profile', 'unknown')})\n"
text += f"- Curtidas: {meta.get('likesCount', 0)}, Comentários: {meta.get('commentsCount', 0)}\n"
text += f"- Data: {meta.get('timestamp', 'N/A')[:10]}\n"
text += f"- Link: {meta.get('url', 'N/A')}\n"
if doc:
# Mostra trecho com o termo
text += f"- Trecho: {doc[:250]}...\n"
text += "\n"
return text
# Verifica se é análise de sentimento
if results[0].get('is_sentiment'):
data = results[0]['metadata']
text = f"## Análise de Sentimento: '{data['topic']}'\n"
text += f"- Perfil(s): {data['profile']}\n"
text += f"- Posts analisados: {data['total_posts']}\n\n"
# Se houver erro
if data.get('error'):
text += f"⚠️ Erro: {data['error']}\n"
return text
# Resumo do sentimento
text += f"### Resumo Geral:\n{data['sentiment_summary']}\n\n"
# Distribuição de sentimentos
text += "### Distribuição de Sentimentos:\n"
text += f"- ✅ Positivos: {data['positive_count']} posts\n"
text += f"- ❌ Negativos: {data['negative_count']} posts\n"
text += f"- ⚪ Neutros: {data['neutral_count']} posts\n\n"
# Aspectos positivos
if data.get('positive_aspects'):
text += "### Aspectos Positivos Identificados:\n"
for aspect in data['positive_aspects']:
text += f"- {aspect}\n"
text += "\n"
# Aspectos negativos
if data.get('negative_aspects'):
text += "### Aspectos Negativos/Críticas:\n"
for aspect in data['negative_aspects']:
text += f"- {aspect}\n"
text += "\n"
# Pontos-chave
if data.get('key_points'):
text += "### Pontos-Chave:\n"
for point in data['key_points']:
text += f"- {point}\n"
text += "\n"
# Exemplos de posts
examples = data.get('examples', {})
if examples.get('positive'):
text += "### Exemplos de Posts Positivos:\n"
for i, post in enumerate(examples['positive'][:2], 1):
meta = post.get('metadata', {})
doc = post.get('document', '')[:200]
text += f"{i}. @{meta.get('profile')}: {doc}... [{meta.get('url')}]\n"
text += "\n"
if examples.get('negative'):
text += "### Exemplos de Posts Negativos:\n"
for i, post in enumerate(examples['negative'][:2], 1):
meta = post.get('metadata', {})
doc = post.get('document', '')[:200]
text += f"{i}. @{meta.get('profile')}: {doc}... [{meta.get('url')}]\n"
text += "\n"
return text
# Verifica se é estatística de notícias
if results[0].get('is_news_stats'):
stats = results[0]['metadata']
text = "## Estatísticas de Notícias:\n\n"
text += f"- Total de notícias: {stats.get('total_news', 0)}\n"
if stats.get('date_range'):
dr = stats['date_range']
text += f"- Período coberto: {dr.get('oldest', 'N/A')[:10]} até {dr.get('newest', 'N/A')[:10]}\n"
text += "\n### Publishers/Veículos:\n"
for pub in stats.get('publishers', [])[:10]:
text += f"- {pub['name']}: {pub['count']} notícias\n"
return text
# Posts/notícias normais
text = f"## Resultados de {tool_name}:\n\n"
for i, post in enumerate(results[:10], 1):
meta = post.get('metadata', {})
doc = post.get('document', '')
content_type = meta.get('content_type', 'instagram_post')
# Formatação específica por tipo de conteúdo
if content_type == 'news':
# Formatação para notícias
text += f"**Notícia {i}**\n"
text += f"- Título: {meta.get('title', 'N/A')}\n"
text += f"- Publisher: {meta.get('publisher_name', 'N/A')}\n"
text += f"- Data: {meta.get('timestamp', 'N/A')[:10]}\n"
text += f"- Link: {meta.get('url', 'N/A')}\n"
# Para notícias, incluir MAIS conteúdo para o LLM ter contexto completo
if meta.get('description'):
text += f"- Descrição: {meta['description'][:500]}...\n"
if doc:
text += f"- Conteúdo: {doc[:800]}...\n"
else:
# Formatação para posts do Instagram
text += f"**Post {i}** (@{meta.get('profile', 'unknown')})\n"
text += f"- Curtidas: {meta.get('likesCount', 0)}\n"
text += f"- Comentários: {meta.get('commentsCount', 0)}\n"
text += f"- Data: {meta.get('timestamp', 'N/A')[:10]}\n"
text += f"- Link: {meta.get('url', 'N/A')}\n"
# Adiciona trecho do texto/legenda
if doc:
text += f"- Conteúdo: {doc[:200]}...\n"
elif meta.get('caption'):
text += f"- Legenda: {meta['caption'][:200]}...\n"
text += "\n"
return text
def _clean_response(self, response: str) -> str:
"""
Remove possíveis vazamentos de prompt ou instruções da resposta.
Também detecta e marca alucinações de datas.
Args:
response: Resposta bruta do LLM
Returns:
Resposta limpa
"""
import re
# Remove seções de "Importante sobre Roberto Salles" que não devem estar na resposta
# Remove bloco inteiro de "⚠️ Importante" se não foi perguntado sobre Roberto Salles
pattern = r'\n⚠️\s+Importante sobre Roberto Salles.*?(?=\n##|\n[❌✅]|$)'
response = re.sub(pattern, '', response, flags=re.DOTALL | re.IGNORECASE)
# Remove "Dados exclusivamente baseados" se não é contexto relevante
pattern = r'\n✅\s+Dados exclusivamente baseados.*?(?=\n##|\n[❌✅]|$)'
response = re.sub(pattern, '', response, flags=re.DOTALL)
# VALIDADOR DE DATAS: Detecta erros temporais
date_errors = [
(r'Roberto Salles.*?reitor.*?2012.*?2018', 'Roberto Salles foi reitor de 2006-2014, NÃO 2012-2018'),
(r'Roberto Salles.*?reitor.*?2015.*?2019', 'Roberto Salles foi reitor de 2006-2014, NÃO 2015-2019'),
(r'Salles.*?reitor.*?eleito em 2015', 'Roberto Salles foi reitor de 2006-2014. Em 2014 saiu do cargo'),
(r'Salles.*?reeleito em 2018', 'Roberto Salles não foi reeleito em 2018 - seu mandato terminou em 2014'),
]
has_date_error = False
for pattern, correction in date_errors:
if re.search(pattern, response, re.IGNORECASE):
has_date_error = True
print(f"⚠️ DETECÇÃO: Alucinação de data encontrada - {correction}")
# Remove linhas que contêm o erro
response = re.sub(pattern, f'[CORRIGIDO: {correction}]', response, flags=re.IGNORECASE)
# Remove linhas de diretrizes que não devem estar na resposta final
lines = response.split('\n')
filtered_lines = []
for line in lines:
# Pula linhas que parecem ser instruções vazadas
if any(x in line.lower() for x in ['diretrizes:', 'sua tarefa:', 'regra importante', 'não invente']):
continue
filtered_lines.append(line)
response = '\n'.join(filtered_lines)
# Remove múltiplas linhas em branco consecutivas
response = re.sub(r'\n\n\n+', '\n\n', response)
# Se encontrou erro de data, adiciona aviso ao final
if has_date_error:
response += (
"\n\n⚠️ **AVISO DE CORRIGIR ALUCINAÇÃO**:\n"
"A resposta acima foi corrigida automaticamente por conter erros de datas.\n"
"**DATA CORRETA**: Roberto de Souza Salles foi reitor da UFF de **23 de novembro de 2006 até 18 de novembro de 2014**.\n"
"Qualquer menção a outros períodos contém alucinação do modelo."
)
return response.strip()
def _validate_source_relevance(
self,
user_question: str,
all_results: List[Tuple[str, List[Dict[str, Any]]]]
) -> Tuple[bool, Optional[str]]:
"""
Valida se as fontes recuperadas são REALMENTE relevantes para a pergunta.
Aceita cenários com pessoas reais nos dados, rejeita apenas fictícios.
Args:
user_question: Pergunta do usuário
all_results: Resultados retornados
Returns:
Tupla (é_válido, warning_message ou None)
"""
question_lower = user_question.lower()
# Pessoas REAIS mencionadas nos dados
real_people_patterns = [
(r'fabio\s+(?:barbosa\s+)?passos?', 'Fábio Barbosa Passos', 'vice-reitor'),
(r'fábio\s+(?:barbosa\s+)?passos?', 'Fábio Barbosa Passos', 'vice-reitor'),
(r'roberto\s+salles?', 'Roberto Salles', 'ex-reitor 2009-2018'),
(r'antonio\s+claudio', 'Antonio Claudio Nóbrega', 'reitor atual'),
]
# Detecta menção a pessoas reais
import re
mentioned_people = []
for pattern, name, role in real_people_patterns:
if re.search(pattern, question_lower):
mentioned_people.append((name, role))
# Se menciona pessoas reais, é um cenário válido
if mentioned_people:
# Verifica se recuperou dados sobre essas pessoas
all_context = ""
for tool_name, results in all_results:
for result in results:
if isinstance(result, dict):
all_context += str(result).lower()
# Se recuperou dados relevantes, é válido
if len(all_results) > 0 and all_context.strip():
return True, None
# Se não encontrou dados sobre pessoas reais, indica falta de informação
people_str = " e ".join([f"{name} ({role})" for name, role in mentioned_people])
return False, (
f"⚠️ **Sem informações suficientes**\n\n"
f"A pergunta menciona: {people_str}\n\n"
f"Porém, os dados indexados não contêm informações adequadas para responder sobre "
f"esse cenário específico.\n\n"
f"Tente reformular a pergunta com temas ou contextos disponíveis nos dados."
)
return True, None
def _synthesize_response(
self,
user_question: str,
all_results: List[Tuple[str, List[Dict[str, Any]]]],
stream: bool = False
) -> str:
"""
LLM sintetiza todos os resultados em uma resposta coerente.
Args:
user_question: Pergunta original do usuário
all_results: Lista de (nome_ferramenta, resultados)
stream: Se True, retorna generator para streaming
Returns:
Resposta final sintetizada
"""
# Valida relevância das fontes e detecta cenários fictícios
is_valid, simulation_warning = self._validate_source_relevance(user_question, all_results)
if not is_valid:
return "❌ Não encontrei informações suficientes para responder sua pergunta com os dados disponíveis."
return hallucination_warning
# Monta contexto com todos os resultados
context = ""
for tool_name, results in all_results:
context += self._format_results_for_llm(results, tool_name)
context += "\n---\n\n"
synthesis_prompt = f"""{context}
## Pergunta Original do Usuário:
{user_question}
## Sua Tarefa:
Sintetize os dados acima em uma resposta clara, completa e bem formatada para o usuário.
DIRETRIZES:
1. Use APENAS os dados fornecidos acima
2. Seja objetivo e direto
3. Cite números específicos e links quando relevante
4. Use formatação markdown para melhor legibilidade
5. Organize a informação de forma lógica
6. Se múltiplas ferramentas foram usadas, integre os resultados de forma coerente
7. Sempre inclua links dos posts quando disponível
8. NÃO repita instruções ou seções de "Diretrizes" na sua resposta
9. NÃO inclua avisos sobre Roberto Salles a menos que a pergunta mencione especificamente sobre ele
NÃO invente informações que não estão no contexto!