Optimization: Improved context handling and lowered Ollama temperature for better local performance

This commit is contained in:
Marcos
2026-03-22 13:08:52 -03:00
parent 3cfb6fb3d2
commit 03b1793be9

View File

@@ -21,7 +21,11 @@ def get_llm_response(prompt: str, provider: str, cfg: dict) -> str:
res = requests.post(f"{ollama_host}/api/generate", json={ res = requests.post(f"{ollama_host}/api/generate", json={
"model": os.getenv("OLLAMA_MODEL", "qwen2.5-coder:1.5b"), "model": os.getenv("OLLAMA_MODEL", "qwen2.5-coder:1.5b"),
"prompt": prompt, "prompt": prompt,
"stream": False "stream": False,
"options": {
"temperature": 0.1,
"num_ctx": 4096
}
}) })
if res.status_code == 200: if res.status_code == 200:
return res.json().get("response", "") return res.json().get("response", "")
@@ -78,54 +82,44 @@ Resposta: A memória RAM está operando com 20% de uso.
""" """
system_prompt = system_prompt_base.replace("{TOOLS_LIST}", tools_desc) system_prompt = system_prompt_base.replace("{TOOLS_LIST}", tools_desc)
# Constrói o histórico da conversa (memória de curto prazo) # Preparação de Histórico Limpa
history_str = "" hist_list = chat_history[-5:] if chat_history else []
if chat_history: hist_txt = "\n".join([f"Usuário: {m['user']}\nAgente: {m['bot']}" for m in hist_list])
for msg in chat_history[-5:]: # Pega as últimas 5 interações
history_str += f"\nUsuário: {msg['user']}\nAgente: {msg['bot']}\n"
history_str += f"\nUsuário: {prompt}\n" # Cabeçalho de Execução
execution_context = f"\n--- HISTÓRICO RECENTE ---\n{hist_txt}\n------------------------\n\nORDEM ATUAL: {prompt}\n"
current_reasoning = ""
current_iteration_history = history_str
max_loops = 12 max_loops = 12
print(f"--- INICIANDO AGENTE ({provider}) ---") print(f"--- INICIANDO AGENTE ({provider}) ---")
for i in range(max_loops): for i in range(max_loops):
print(f"\n[LOOP {i+1}/{max_loops}]") print(f"\n[LOOP {i+1}/{max_loops}]")
full_prompt = system_prompt + current_iteration_history
response = get_llm_response(full_prompt, provider, cfg)
# Constrói o Prompt Final separando REGRAS, PASSADO e PENSAMENTO ATUAL
full_prompt = f"{system_prompt}\n{execution_context}\n\nSEU PENSAMENTO ATUAL (Ações e Observações):\n{current_reasoning}\n"
response = get_llm_response(full_prompt, provider, cfg)
print(f"PENSAMENTO:\n{response}") print(f"PENSAMENTO:\n{response}")
# Procura por chamadas de ferramentas na resposta
match = re.search(r"\[TOOL:(.*?)\](.*?)\[/TOOL\]", response, re.IGNORECASE | re.DOTALL) match = re.search(r"\[TOOL:(.*?)\](.*?)\[/TOOL\]", response, re.IGNORECASE | re.DOTALL)
if match: if match:
tool_name = match.group(1).strip() tool_name = match.group(1).strip()
arg = match.group(2).strip() arg = match.group(2).strip()
print(f"EXECUTANDO: {tool_name}")
print(f"EXECUTANDO: {tool_name} | ARGS: {arg}")
if tool_name in AVAILABLE_TOOLS: if tool_name in AVAILABLE_TOOLS:
func = AVAILABLE_TOOLS[tool_name]["func"] func = AVAILABLE_TOOLS[tool_name]["func"]
# Caso a ferramenta não aceite argumentos (ex: get_system_health) observation = func() if tool_name in ["get_system_health", "get_docker_stats"] else func(arg)
if tool_name in ["get_system_health", "get_docker_stats"]: print(f"OBSERVAÇÃO (suprimida): {str(observation)[:150]}...")
observation = func()
else:
observation = func(arg)
print(f"OBSERVAÇÃO (suprimida): {str(observation)[:200]}...")
else: else:
observation = f"Erro: Ferramenta '{tool_name}' não encontrada." observation = f"Erro: Ferramenta '{tool_name}' não encontrada."
print(f"ERRO: {observation}")
# Adiciona ao histórico do loop atual # Adiciona ao PENSAMENTO ATUAL apenas, sem sujar o histórico original
current_iteration_history += f"\nAgente (Ação): {response}\nSISTEMA (Saída de {tool_name}): {observation}\n" current_reasoning += f"\nAgente (Ação): {response}\nSISTEMA (Saída): {observation}\n"
else: else:
# Se não tem comando, é a resposta final
print("--- RESPOSTA FINAL ENCONTRADA ---") print("--- RESPOSTA FINAL ENCONTRADA ---")
return response return response
print("!!! ERRO: LIMITE DE TENTATIVAS ATINGIDO !!!") return "O agente atingiu o limite de tentativas."
return "O agente atingiu o limite de tentativas para esta tarefa."