-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_web_search_educacao.py
More file actions
141 lines (117 loc) · 5.33 KB
/
test_web_search_educacao.py
File metadata and controls
141 lines (117 loc) · 5.33 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
#!/usr/bin/env python3
"""
Script para testar web_search com uma pergunta que terá resultados relevantes
"""
import sys
sys.path.insert(0, '/home/marcus/projects/ping')
from agent_system import RAGAgent
from config import DEFAULT_PROVIDER, DEEPSEEK_MODEL
import json
print("=" * 100)
print("🧪 TESTE COMPLETO: WEB SEARCH COM PERGUNTA SOBRE EDUCAÇÃO BRASILEIRA")
print("=" * 100)
print(f"\n📋 Configuração:")
print(f" Provider: {DEFAULT_PROVIDER}")
print(f" Modelo: {DEEPSEEK_MODEL}")
# Inicializa o agente
print(f"\n⏳ Inicializando RAGAgent...")
agent = RAGAgent(
embedding_model="mxbai-embed-large",
generation_model="qwen3:30b",
planning_model="qwen3:30b"
)
# Pergunta que terá bons resultados na web
query = "Qual é a situação da educação superior no Brasil em 2025?"
print("\n" + "=" * 100)
print("TESTE COMPLETO COM WEB SEARCH - PERGUNTA SOBRE EDUCAÇÃO")
print("=" * 100)
print(f"\n❓ Pergunta: \"{query}\"")
print(f"\n{'─' * 100}")
try:
print(f"\n⏳ ETAPA 1: DeepSeek analisando...")
print(f"{'─' * 100}\n")
# Chama o método de planejamento
actions = agent._plan_action(query)
# Processa a resposta
if isinstance(actions, list):
if len(actions) > 0:
first_action = actions[0]
if 'error' not in first_action:
print(f"✅ RACIOCÍNIO DO AGENTE:")
print(f"\n🔧 Ferramentas a usar:")
for j, action in enumerate(actions, 1):
tool = action.get('tool', 'unknown')
params = action.get('params', {})
print(f"\n {j}. {tool}")
if params:
print(f" Query: \"{params.get('query', '')}\"" if tool == 'web_search' else f" Parâmetros: {json.dumps(params, ensure_ascii=False, indent=10)}")
# Executa as ações
print(f"\n{'─' * 100}")
print(f"\n⏳ ETAPA 2: Executando as ferramentas...")
print(f"{'─' * 100}\n")
all_results = []
for i, action in enumerate(actions, 1):
tool = action.get('tool')
params = action.get('params', {})
if tool and tool != 'error':
print(f"\n ⚙️ [{i}] Executando {tool}...")
if tool == 'web_search':
print(f" Query: \"{params.get('query', '')}\"")
try:
results = agent._execute_action(action)
print(f" ✓ Obteve {len(results) if isinstance(results, list) else 1} resultado(s)\n")
# Mostra os resultados
if isinstance(results, list) and len(results) > 0:
for idx, result in enumerate(results, 1):
if isinstance(result, dict):
if 'document' in result:
print(f" 📄 Resultado {idx}:")
doc = result['document']
# Trunca se muito grande
if len(str(doc)) > 250:
print(f" {str(doc)[:250]}...")
else:
print(f" {doc}")
# Mostra metadata se tiver
if 'metadata' in result and result['metadata']:
meta = result['metadata']
if 'source' in meta:
print(f" 🔗 Fonte: {meta['source']}")
if 'date' in meta and meta['date']:
print(f" 📅 Data: {meta['date']}")
print()
elif 'metadata' in result:
print(f" 📊 Resultado {idx}: {str(result['metadata'])[:200]}...")
print()
all_results.extend(results if isinstance(results, list) else [results])
except Exception as e:
print(f" ❌ Erro: {str(e)[:100]}\n")
# Sintetiza a resposta
print(f"{'─' * 100}")
print(f"\n⏳ ETAPA 3: DeepSeek sintetizando a resposta...")
print(f"{'─' * 100}\n")
try:
# Prepara os resultados no formato esperado
formatted_results = []
for i, action in enumerate(actions):
tool = action.get('tool')
if tool and tool != 'error':
# Coleta resultados desta ferramenta
for result in all_results:
if isinstance(result, dict):
formatted_results.append((tool, [result]))
else:
formatted_results.append((tool, [{'document': str(result)}]))
final_response = agent._synthesize_response(
user_question=query,
all_results=formatted_results
)
print(f"✅ RESPOSTA FINAL DO AGENTE:\n")
print(f"{final_response}")
except Exception as e:
print(f"❌ Erro ao sintetizar: {str(e)[:200]}")
except Exception as e:
print(f"❌ Erro geral: {type(e).__name__}: {str(e)[:150]}")
print(f"\n{'=' * 100}")
print("✅ TESTE CONCLUÍDO")
print("=" * 100)