1- from abc import ABC
1+ from abc import ABC , abstractmethod
22from dataclasses import dataclass , field
33from typing import Dict
44
55from capabilities .capability import Capability , capabilities_to_simple_text_handler
66from usecases .common_patterns import RoundBasedUseCase
77
8+ from mako .template import Template
9+ from rich .panel import Panel
10+ from utils import llm_util
811
912@dataclass
1013class Agent (RoundBasedUseCase , ABC ):
@@ -25,3 +28,61 @@ def get_capability(self, name: str) -> Capability:
2528 def get_capability_block (self ) -> str :
2629 capability_descriptions , _parser = capabilities_to_simple_text_handler (self ._capabilities )
2730 return "You can either\n \n " + "\n " .join (f"- { description } " for description in capability_descriptions .values ())
31+
32+ @dataclass
33+ class AgentWorldview (ABC ):
34+
35+ @abstractmethod
36+ def to_template (self ):
37+ pass
38+
39+ @abstractmethod
40+ def update (self , capability , cmd , result ):
41+ pass
42+
43+ class TemplatedAgent (Agent ):
44+
45+ _state : AgentWorldview = None
46+ _template : Template = None
47+ _template_size : int = 0
48+
49+ def init (self ):
50+ super ().init ()
51+
52+ def set_initial_state (self , initial_state ):
53+ print ("setting state!" )
54+ self ._state = initial_state
55+
56+ def set_template (self , template ):
57+ self ._template = Template (filename = template )
58+ self ._template_size = self .llm .count_tokens (self ._template .source )
59+
60+ def perform_round (self , turn ):
61+ got_root : bool = False
62+
63+ with self .console .status ("[bold green]Asking LLM for a new command..." ):
64+ # TODO output/log state
65+ options = self ._state .to_template ()
66+ options .update ({
67+ 'capabilities' : self .get_capability_block ()
68+ })
69+
70+ print (str (options ))
71+
72+ # get the next command from the LLM
73+ answer = self .llm .get_response (self ._template , ** options )
74+ cmd = llm_util .cmd_output_fixer (answer .result )
75+
76+ with self .console .status ("[bold green]Executing that command..." ):
77+ self .console .print (Panel (answer .result , title = "[bold cyan]Got command from LLM:" ))
78+ capability = self .get_capability (cmd .split (" " , 1 )[0 ])
79+ result , got_root = capability (cmd )
80+
81+ # log and output the command and its result
82+ self .log_db .add_log_query (self ._run_id , turn , cmd , result , answer )
83+ self ._state .update (capability , cmd , result )
84+ # TODO output/log new state
85+ self .console .print (Panel (result , title = f"[bold cyan]{ cmd } " ))
86+
87+ # if we got root, we can stop the loop
88+ return got_root
0 commit comments