New version of atomic flow
Browse filessupports JARVIS with plan writer, code writer.
- ControllerAtomicFlow.py +61 -18
ControllerAtomicFlow.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
|
|
|
|
|
| 1 |
import json
|
|
|
|
| 2 |
from copy import deepcopy
|
| 3 |
from typing import Any, Dict, List
|
| 4 |
from flow_modules.aiflows.OpenAIChatFlowModule import OpenAIChatAtomicFlow
|
|
@@ -14,11 +17,35 @@ class Command:
|
|
| 14 |
|
| 15 |
|
| 16 |
class ControllerAtomicFlow(OpenAIChatAtomicFlow):
|
| 17 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
self.system_message_prompt_template = self.system_message_prompt_template.partial(
|
| 20 |
commands=self._build_commands_manual(commands)
|
| 21 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
@staticmethod
|
| 24 |
def _build_commands_manual(commands: List[Command]) -> str:
|
|
@@ -50,26 +77,42 @@ class ControllerAtomicFlow(OpenAIChatAtomicFlow):
|
|
| 50 |
# ~~~ Instantiate flow ~~~
|
| 51 |
return cls(**kwargs)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 54 |
-
hint_for_model = """
|
| 55 |
-
Make sure your response is in the following format:
|
| 56 |
-
Response Format:
|
| 57 |
-
{
|
| 58 |
-
"thought": "thought",
|
| 59 |
-
"reasoning": "reasoning",
|
| 60 |
-
"plan": "- short bulleted\n- list that conveys\n- long-term plan",
|
| 61 |
-
"criticism": "constructive self-criticism",
|
| 62 |
-
"speak": "thoughts summary to say to user",
|
| 63 |
-
"command": "the python function you would like to call",
|
| 64 |
-
"command_args": {
|
| 65 |
-
"arg name": "value"
|
| 66 |
-
}
|
| 67 |
-
}
|
| 68 |
-
"""
|
| 69 |
if 'goal' in input_data:
|
| 70 |
-
input_data['goal'] += hint_for_model
|
| 71 |
if 'human_feedback' in input_data:
|
| 72 |
-
input_data['human_feedback'] += hint_for_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
api_output = super().run(input_data)["api_output"].strip()
|
| 74 |
|
| 75 |
try:
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import importlib.util
|
| 3 |
import json
|
| 4 |
+
import os.path
|
| 5 |
from copy import deepcopy
|
| 6 |
from typing import Any, Dict, List
|
| 7 |
from flow_modules.aiflows.OpenAIChatFlowModule import OpenAIChatAtomicFlow
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
class ControllerAtomicFlow(OpenAIChatAtomicFlow):
|
| 20 |
+
def __init__(
|
| 21 |
+
self,
|
| 22 |
+
commands: List[Command],
|
| 23 |
+
plan_file_location: str,
|
| 24 |
+
code_file_location: str,
|
| 25 |
+
**kwargs):
|
| 26 |
super().__init__(**kwargs)
|
| 27 |
+
if os.path.isdir(plan_file_location):
|
| 28 |
+
plan_file_location = os.path.join(plan_file_location, "plan.txt")
|
| 29 |
+
self.plan_file_location = plan_file_location
|
| 30 |
+
self.code_file_location = code_file_location
|
| 31 |
self.system_message_prompt_template = self.system_message_prompt_template.partial(
|
| 32 |
commands=self._build_commands_manual(commands)
|
| 33 |
)
|
| 34 |
+
self.hint_for_model = """
|
| 35 |
+
Make sure your response is in the following format:
|
| 36 |
+
Response Format:
|
| 37 |
+
{
|
| 38 |
+
"thought": "thought",
|
| 39 |
+
"reasoning": "reasoning",
|
| 40 |
+
"criticism": "constructive self-criticism",
|
| 41 |
+
"speak": "thoughts summary to say to user",
|
| 42 |
+
"command": "the python function you would like to call",
|
| 43 |
+
"command_args": {
|
| 44 |
+
"arg name": "value"
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
"""
|
| 48 |
+
self.original_system_template = self.system_message_prompt_template.template
|
| 49 |
|
| 50 |
@staticmethod
|
| 51 |
def _build_commands_manual(commands: List[Command]) -> str:
|
|
|
|
| 77 |
# ~~~ Instantiate flow ~~~
|
| 78 |
return cls(**kwargs)
|
| 79 |
|
| 80 |
+
def _get_library_function_signatures(self):
|
| 81 |
+
try:
|
| 82 |
+
spec = importlib.util.spec_from_file_location("code_library", self.code_file_location)
|
| 83 |
+
module = importlib.util.module_from_spec(spec)
|
| 84 |
+
spec.loader.exec_module(module)
|
| 85 |
+
ret = ''
|
| 86 |
+
import inspect
|
| 87 |
+
for name, obj in inspect.getmembers(module):
|
| 88 |
+
if inspect.isfunction(obj):
|
| 89 |
+
ret += f"{name}: {inspect.signature(obj)}\n"
|
| 90 |
+
return ret
|
| 91 |
+
except FileNotFoundError:
|
| 92 |
+
return 'There is no function available yet.'
|
| 93 |
+
|
| 94 |
+
def _get_plan(self):
|
| 95 |
+
try:
|
| 96 |
+
with open(self.plan_file_location, 'r') as file:
|
| 97 |
+
return file.read()
|
| 98 |
+
except FileNotFoundError:
|
| 99 |
+
return "There is no plan yet"
|
| 100 |
+
|
| 101 |
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if 'goal' in input_data:
|
| 103 |
+
input_data['goal'] += self.hint_for_model
|
| 104 |
if 'human_feedback' in input_data:
|
| 105 |
+
input_data['human_feedback'] += self.hint_for_model
|
| 106 |
+
|
| 107 |
+
# self.system_message_prompt_template.template
|
| 108 |
+
plan_to_append = self._get_plan()
|
| 109 |
+
function_signatures_to_append = self._get_library_function_signatures()
|
| 110 |
+
self.system_message_prompt_template.template = \
|
| 111 |
+
self.original_system_template + "\n" + f"Here are the available functions at {self.code_file_location}\n" \
|
| 112 |
+
+ function_signatures_to_append + "\n" \
|
| 113 |
+
+ f"Here is the step-by-step plan to achieve the goal:\n" \
|
| 114 |
+
+ plan_to_append + "\n"
|
| 115 |
+
|
| 116 |
api_output = super().run(input_data)["api_output"].strip()
|
| 117 |
|
| 118 |
try:
|