Optimization: Improved context handling and lowered Ollama temperature for better local performance
This commit is contained in:
48
ai_agent.py
48
ai_agent.py
@@ -21,7 +21,11 @@ def get_llm_response(prompt: str, provider: str, cfg: dict) -> str:
|
||||
res = requests.post(f"{ollama_host}/api/generate", json={
|
||||
"model": os.getenv("OLLAMA_MODEL", "qwen2.5-coder:1.5b"),
|
||||
"prompt": prompt,
|
||||
"stream": False
|
||||
"stream": False,
|
||||
"options": {
|
||||
"temperature": 0.1,
|
||||
"num_ctx": 4096
|
||||
}
|
||||
})
|
||||
if res.status_code == 200:
|
||||
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)
|
||||
|
||||
# Constrói o histórico da conversa (memória de curto prazo)
|
||||
history_str = ""
|
||||
if chat_history:
|
||||
for msg in chat_history[-5:]: # Pega as últimas 5 interações
|
||||
history_str += f"\nUsuário: {msg['user']}\nAgente: {msg['bot']}\n"
|
||||
# Preparação de Histórico Limpa
|
||||
hist_list = chat_history[-5:] if chat_history else []
|
||||
hist_txt = "\n".join([f"Usuário: {m['user']}\nAgente: {m['bot']}" for m in hist_list])
|
||||
|
||||
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
|
||||
|
||||
print(f"--- INICIANDO AGENTE ({provider}) ---")
|
||||
|
||||
for i in range(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}")
|
||||
|
||||
# Procura por chamadas de ferramentas na resposta
|
||||
match = re.search(r"\[TOOL:(.*?)\](.*?)\[/TOOL\]", response, re.IGNORECASE | re.DOTALL)
|
||||
|
||||
if match:
|
||||
tool_name = match.group(1).strip()
|
||||
arg = match.group(2).strip()
|
||||
|
||||
print(f"EXECUTANDO: {tool_name} | ARGS: {arg}")
|
||||
print(f"EXECUTANDO: {tool_name}")
|
||||
|
||||
if tool_name in AVAILABLE_TOOLS:
|
||||
func = AVAILABLE_TOOLS[tool_name]["func"]
|
||||
# Caso a ferramenta não aceite argumentos (ex: get_system_health)
|
||||
if tool_name in ["get_system_health", "get_docker_stats"]:
|
||||
observation = func()
|
||||
else:
|
||||
observation = func(arg)
|
||||
|
||||
print(f"OBSERVAÇÃO (suprimida): {str(observation)[:200]}...")
|
||||
observation = func() if tool_name in ["get_system_health", "get_docker_stats"] else func(arg)
|
||||
print(f"OBSERVAÇÃO (suprimida): {str(observation)[:150]}...")
|
||||
else:
|
||||
observation = f"Erro: Ferramenta '{tool_name}' não encontrada."
|
||||
print(f"ERRO: {observation}")
|
||||
|
||||
# Adiciona ao histórico do loop atual
|
||||
current_iteration_history += f"\nAgente (Ação): {response}\nSISTEMA (Saída de {tool_name}): {observation}\n"
|
||||
# Adiciona ao PENSAMENTO ATUAL apenas, sem sujar o histórico original
|
||||
current_reasoning += f"\nAgente (Ação): {response}\nSISTEMA (Saída): {observation}\n"
|
||||
else:
|
||||
# Se não tem comando, é a resposta final
|
||||
print("--- RESPOSTA FINAL ENCONTRADA ---")
|
||||
return response
|
||||
|
||||
print("!!! ERRO: LIMITE DE TENTATIVAS ATINGIDO !!!")
|
||||
return "O agente atingiu o limite de tentativas para esta tarefa."
|
||||
return "O agente atingiu o limite de tentativas."
|
||||
|
||||
Reference in New Issue
Block a user