"""
Exemplo básico de uso do AtomicOptimizer com LangChain
"""
from langchain_community.llms import FakeListLLM
from atomic_agents.integrations.langchain import AtomicOptimizer

def main():
    # Configurar o otimizador
    optimizer = AtomicOptimizer(
        compression=True,
        policies={
            "blockedTools": ["dangerous_tool"],
            "maxCost": 100.0,
            "maxTokens": 10000
        },
        ledger=True
    )

    # Criar um LLM fake para demonstração
    llm = FakeListLLM(
        responses=["I understand, here is the summary."],
        callbacks=[optimizer]
    )

    # Teste 1: Compressão
    print("=== Teste de Compressão ===")
    prompt = "Please help me. The goal is to summarize this text."
    print(f"Prompt original: {prompt}")
    result = llm.invoke(prompt)
    print(f"Resposta: {result}\n")

    # Teste 2: Verificar Ledger
    print("=== Auditoria (Ledger) ===")
    for entry in optimizer.ledger.entries:
        print(f"Event: {entry['data'].get('event')}")
        print(f"Hash: {entry['hash'][:16]}...")
        print(f"Timestamp: {entry['timestamp']}\n")

    # Teste 3: Verificar métricas de compressão
    print("=== Métricas de Compressão ===")
    for run_id, metrics in optimizer.run_metrics.items():
        if metrics:
            original = metrics.get("original", 0)
            compressed = metrics.get("compressed", 0)
            savings = original - compressed
            percentage = (savings / original * 100) if original > 0 else 0
            print(f"Run ID: {run_id[:8]}...")
            print(f"Original: {original} chars")
            print(f"Comprimido: {compressed} chars")
            print(f"Economia: {savings} chars ({percentage:.1f}%)\n")

if __name__ == "__main__":
    main()


