Update Controller_JarvisFlow.py
Browse files- Controller_JarvisFlow.py +35 -1
Controller_JarvisFlow.py
CHANGED
@@ -16,10 +16,41 @@ class Command:
|
|
16 |
|
17 |
# TODO: controller should be generalized
|
18 |
class Controller_JarvisFlow(ChatAtomicFlow):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def __init__(
|
20 |
self,
|
21 |
commands: List[Command],
|
22 |
**kwargs):
|
|
|
23 |
super().__init__(**kwargs)
|
24 |
self.system_message_prompt_template = self.system_message_prompt_template.partial(
|
25 |
commands=self._build_commands_manual(commands),
|
@@ -53,6 +84,7 @@ class Controller_JarvisFlow(ChatAtomicFlow):
|
|
53 |
return content
|
54 |
@staticmethod
|
55 |
def _build_commands_manual(commands: List[Command]) -> str:
|
|
|
56 |
ret = ""
|
57 |
for i, command in enumerate(commands):
|
58 |
command_input_json_schema = json.dumps(
|
@@ -63,6 +95,7 @@ class Controller_JarvisFlow(ChatAtomicFlow):
|
|
63 |
|
64 |
@classmethod
|
65 |
def instantiate_from_config(cls, config):
|
|
|
66 |
flow_config = deepcopy(config)
|
67 |
|
68 |
kwargs = {"flow_config": flow_config}
|
@@ -85,6 +118,7 @@ class Controller_JarvisFlow(ChatAtomicFlow):
|
|
85 |
return cls(**kwargs)
|
86 |
|
87 |
def _update_prompts_and_input(self, input_data: Dict[str, Any]):
|
|
|
88 |
if 'goal' in input_data:
|
89 |
input_data['goal'] += self.hint_for_model
|
90 |
if 'result' in input_data:
|
@@ -107,7 +141,7 @@ class Controller_JarvisFlow(ChatAtomicFlow):
|
|
107 |
self._state_update_add_chat_message(content=updated_system_message_content,
|
108 |
role=self.flow_config["system_name"])
|
109 |
|
110 |
-
|
111 |
while True:
|
112 |
api_output = super().run(input_data)["api_output"].strip()
|
113 |
try:
|
|
|
16 |
|
17 |
# TODO: controller should be generalized
|
18 |
class Controller_JarvisFlow(ChatAtomicFlow):
|
19 |
+
"""This class is a controller for JarvisFlow, it takes the plan generated by the planner, logs of previous executions,
|
20 |
+
depending on the initial goal or the subsequent feedback from the branching executors (and the human), to decide which
|
21 |
+
executor to call next (or to exit by calling finish).
|
22 |
+
|
23 |
+
*Configuration Parameters*:
|
24 |
+
- `commands` (dict): a dictionary of commands that the controller can call, each command has a name, a description, and a list of input arguments.
|
25 |
+
The commands will be injected into the system message prompt template.
|
26 |
+
- `system_message_prompt_template` (str): the template for the system message prompt, there are several components needs to be injected into the
|
27 |
+
template, including the commands, plan, plan_file_location, logs, and the goal. The injection of commands is done then initalizing the flow,
|
28 |
+
the rest of the components are injected at the beginning of each run.
|
29 |
+
- `previous_messages` (int): a sliding window of previous messages that will be passed to the model. This is the central part of short-term memory management.
|
30 |
+
|
31 |
+
*Input Interface Non Initialized*:
|
32 |
+
- `goal` (str): the initial goal of the conversation, this is the input to the model.
|
33 |
+
- `memory_files` (dict): a dictionary of file locations that contains the plan, logs.
|
34 |
+
- `plan` (str): the plan generated by the planner, the plan will change (marked as done, or re-plan) as execution preceeds.
|
35 |
+
- `logs` (str): the logs of previous executions, the logs will be appended as execution preceeds.
|
36 |
+
|
37 |
+
*Input Interface Initialized*:
|
38 |
+
- `result` (str): the result of the previous execution, this is the input to the model.
|
39 |
+
- `memory_files` (dict): a dictionary of file locations that contains the plan, logs.
|
40 |
+
- `plan` (str): the plan generated by the planner, the plan will change (marked as done, or re-plan) as execution preceeds.
|
41 |
+
- `logs` (str): the logs of previous executions, the logs will be appended as execution preceeds.
|
42 |
+
- `goal` (str): the initial goal, this is kept because the goal is also injected into the system prompts so that Jarvis does not
|
43 |
+
forget what the goal is, when the memory sliding window is implemented.
|
44 |
+
|
45 |
+
*Output Interface*:
|
46 |
+
- `command` (str): the command to be executed by the executor.
|
47 |
+
- `command_args` (dict): the arguments of the command to be executed by the executor.
|
48 |
+
"""
|
49 |
def __init__(
|
50 |
self,
|
51 |
commands: List[Command],
|
52 |
**kwargs):
|
53 |
+
"""Initialize the flow, inject the commands into the system message prompt template."""
|
54 |
super().__init__(**kwargs)
|
55 |
self.system_message_prompt_template = self.system_message_prompt_template.partial(
|
56 |
commands=self._build_commands_manual(commands),
|
|
|
84 |
return content
|
85 |
@staticmethod
|
86 |
def _build_commands_manual(commands: List[Command]) -> str:
|
87 |
+
"""Build the manual for the commands."""
|
88 |
ret = ""
|
89 |
for i, command in enumerate(commands):
|
90 |
command_input_json_schema = json.dumps(
|
|
|
95 |
|
96 |
@classmethod
|
97 |
def instantiate_from_config(cls, config):
|
98 |
+
"""Setting up the flow from the config file. In particular, setting up the prompts, backend, and commands."""
|
99 |
flow_config = deepcopy(config)
|
100 |
|
101 |
kwargs = {"flow_config": flow_config}
|
|
|
118 |
return cls(**kwargs)
|
119 |
|
120 |
def _update_prompts_and_input(self, input_data: Dict[str, Any]):
|
121 |
+
"""Hinting the model to output in json format, updating the plan, logs to the system prompts."""
|
122 |
if 'goal' in input_data:
|
123 |
input_data['goal'] += self.hint_for_model
|
124 |
if 'result' in input_data:
|
|
|
141 |
self._state_update_add_chat_message(content=updated_system_message_content,
|
142 |
role=self.flow_config["system_name"])
|
143 |
|
144 |
+
# ~~~run the model, special mechanism to deal with situations where the output is not in json format. ~~~
|
145 |
while True:
|
146 |
api_output = super().run(input_data)["api_output"].strip()
|
147 |
try:
|