Upload 9 files
Browse files- MemoryReadingAtomicFlow.py +91 -3
- README.md +69 -2
MemoryReadingAtomicFlow.py
CHANGED
|
@@ -13,19 +13,35 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 13 |
memory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state
|
| 14 |
|
| 15 |
*Input Interface*:
|
| 16 |
-
- `memory_files` : name of the Dict which maps the memory name to its file location e.g.
|
| 17 |
-
{"plan": "examples/JARVIS/plan.txt"}
|
| 18 |
|
| 19 |
*Output_Interface*:
|
| 20 |
- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
|
| 23 |
def __init__(self, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
super().__init__(**kwargs)
|
| 25 |
self.supported_mem_name = ["plan", "logs", "code_library"]
|
| 26 |
|
| 27 |
def _check_input_data(self, input_data: Dict[str, Any]):
|
| 28 |
-
"""input data sanity check
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
assert "memory_files" in input_data, "memory_files not passed to MemoryReadingAtomicFlow"
|
| 30 |
|
| 31 |
for mem_name, mem_path in input_data["memory_files"].items():
|
|
@@ -35,15 +51,43 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 35 |
assert os.path.isfile(mem_path), f"{mem_path} is not a file."
|
| 36 |
|
| 37 |
def _read_text(self, file_location):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with open(file_location, 'r', encoding='utf-8') as file:
|
| 39 |
content = file.read()
|
| 40 |
return content
|
| 41 |
|
| 42 |
def _get_pyfile_functions_metadata_from_file(self, file_location):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def python_file_path_to_module_name(file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
return os.path.basename(file_path).replace('.py', '')
|
| 45 |
|
| 46 |
def extract_top_level_function_names(python_file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with open(python_file_path, 'r') as file:
|
| 48 |
file_content = file.read()
|
| 49 |
tree = ast.parse(file_content)
|
|
@@ -51,6 +95,13 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 51 |
return [node.name for node in functions]
|
| 52 |
|
| 53 |
def load_module_from_file(file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
module_name = python_file_path_to_module_name(file_path)
|
| 55 |
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 56 |
module = importlib.util.module_from_spec(spec)
|
|
@@ -58,9 +109,25 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 58 |
return module
|
| 59 |
|
| 60 |
def get_function_from_name(function_name, module):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
return getattr(module, function_name)
|
| 62 |
|
| 63 |
def function_to_dict(function):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if not callable(function):
|
| 65 |
raise ValueError("Provided object is not a function.")
|
| 66 |
|
|
@@ -87,6 +154,13 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 87 |
return [function_to_dict(function) for function in functions]
|
| 88 |
|
| 89 |
def _format_metadata(self, metadata):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
lines = []
|
| 91 |
for function_data in metadata:
|
| 92 |
lines.append(f"Function: {function_data['name']}")
|
|
@@ -105,6 +179,13 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 105 |
|
| 106 |
return '\n'.join(lines)
|
| 107 |
def _read_py_code_library(self, file_location):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
metadata = self._get_pyfile_functions_metadata_from_file(file_location)
|
| 109 |
if len(metadata) == 0:
|
| 110 |
return "No functions yet."
|
|
@@ -114,6 +195,13 @@ class MemoryReadingAtomicFlow(AtomicFlow):
|
|
| 114 |
def run(
|
| 115 |
self,
|
| 116 |
input_data: Dict[str, Any]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
self._check_input_data(input_data)
|
| 118 |
response = {}
|
| 119 |
for mem_name, mem_path in input_data["memory_files"].items():
|
|
|
|
| 13 |
memory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state
|
| 14 |
|
| 15 |
*Input Interface*:
|
| 16 |
+
- `memory_files` : name of the Dict which maps the memory name to its file location e.g. {"plan": "examples/JARVIS/plan.txt"}
|
|
|
|
| 17 |
|
| 18 |
*Output_Interface*:
|
| 19 |
- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.
|
| 20 |
+
|
| 21 |
+
*Configuration Parameters*:
|
| 22 |
+
- `input_interface`: input interface of the flow
|
| 23 |
+
- `output_interface`: output interface of the flow
|
| 24 |
+
|
| 25 |
"""
|
| 26 |
|
| 27 |
def __init__(self, **kwargs):
|
| 28 |
+
"""
|
| 29 |
+
This is the constructor of the :class:`MemoryReadingAtomicFlow` class.
|
| 30 |
+
:param kwargs: additional arguments to pass to the :class:`AtomicFlow` constructor
|
| 31 |
+
:type kwargs: Dict[str, Any]
|
| 32 |
+
"""
|
| 33 |
super().__init__(**kwargs)
|
| 34 |
self.supported_mem_name = ["plan", "logs", "code_library"]
|
| 35 |
|
| 36 |
def _check_input_data(self, input_data: Dict[str, Any]):
|
| 37 |
+
"""input data sanity check
|
| 38 |
+
:param input_data: input data to the flow
|
| 39 |
+
:type input_data: Dict[str, Any]
|
| 40 |
+
:raises AssertionError: if the input data does not contain the required keys
|
| 41 |
+
:raises AssertionError: if the memory file does not exist
|
| 42 |
+
:raises AssertionError: if the memory file is not a file
|
| 43 |
+
:raises AssertionError: if the memory name is not supported
|
| 44 |
+
"""
|
| 45 |
assert "memory_files" in input_data, "memory_files not passed to MemoryReadingAtomicFlow"
|
| 46 |
|
| 47 |
for mem_name, mem_path in input_data["memory_files"].items():
|
|
|
|
| 51 |
assert os.path.isfile(mem_path), f"{mem_path} is not a file."
|
| 52 |
|
| 53 |
def _read_text(self, file_location):
|
| 54 |
+
"""
|
| 55 |
+
Read text from a file.
|
| 56 |
+
:param file_location: the file location
|
| 57 |
+
:type file_location: str
|
| 58 |
+
:return: the content of the file
|
| 59 |
+
:rtype: str
|
| 60 |
+
"""
|
| 61 |
with open(file_location, 'r', encoding='utf-8') as file:
|
| 62 |
content = file.read()
|
| 63 |
return content
|
| 64 |
|
| 65 |
def _get_pyfile_functions_metadata_from_file(self, file_location):
|
| 66 |
+
"""
|
| 67 |
+
Get the metadata of all the functions in a python file.
|
| 68 |
+
:param file_location: the file location
|
| 69 |
+
:type file_location: str
|
| 70 |
+
:return: the metadata of all the functions in a python file
|
| 71 |
+
:rtype: List[Dict[str, Any]]
|
| 72 |
+
"""
|
| 73 |
def python_file_path_to_module_name(file_path):
|
| 74 |
+
"""
|
| 75 |
+
Convert a python file path to its module name.
|
| 76 |
+
:param file_path: the file path
|
| 77 |
+
:type file_path: str
|
| 78 |
+
:return: the module name
|
| 79 |
+
:rtype: str
|
| 80 |
+
"""
|
| 81 |
return os.path.basename(file_path).replace('.py', '')
|
| 82 |
|
| 83 |
def extract_top_level_function_names(python_file_path):
|
| 84 |
+
"""
|
| 85 |
+
Extract the top level function names from a python file.
|
| 86 |
+
:param python_file_path: the python file path
|
| 87 |
+
:type python_file_path: str
|
| 88 |
+
:return: the top level function names
|
| 89 |
+
:rtype: List[str]
|
| 90 |
+
"""
|
| 91 |
with open(python_file_path, 'r') as file:
|
| 92 |
file_content = file.read()
|
| 93 |
tree = ast.parse(file_content)
|
|
|
|
| 95 |
return [node.name for node in functions]
|
| 96 |
|
| 97 |
def load_module_from_file(file_path):
|
| 98 |
+
"""
|
| 99 |
+
Load a module from a file.
|
| 100 |
+
:param file_path: the file path
|
| 101 |
+
:type file_path: str
|
| 102 |
+
:return: the module
|
| 103 |
+
:rtype: ModuleType
|
| 104 |
+
"""
|
| 105 |
module_name = python_file_path_to_module_name(file_path)
|
| 106 |
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 107 |
module = importlib.util.module_from_spec(spec)
|
|
|
|
| 109 |
return module
|
| 110 |
|
| 111 |
def get_function_from_name(function_name, module):
|
| 112 |
+
"""
|
| 113 |
+
Get a function from its name.
|
| 114 |
+
:param function_name: the function name
|
| 115 |
+
:type function_name: str
|
| 116 |
+
:param module: the module
|
| 117 |
+
:type module: ModuleType
|
| 118 |
+
:return: the function
|
| 119 |
+
:rtype: Callable
|
| 120 |
+
"""
|
| 121 |
return getattr(module, function_name)
|
| 122 |
|
| 123 |
def function_to_dict(function):
|
| 124 |
+
"""
|
| 125 |
+
Convert a function to a dictionary.
|
| 126 |
+
:param function: the function
|
| 127 |
+
:type function: Callable
|
| 128 |
+
:return: the dictionary
|
| 129 |
+
:rtype: Dict[str, Any]
|
| 130 |
+
"""
|
| 131 |
if not callable(function):
|
| 132 |
raise ValueError("Provided object is not a function.")
|
| 133 |
|
|
|
|
| 154 |
return [function_to_dict(function) for function in functions]
|
| 155 |
|
| 156 |
def _format_metadata(self, metadata):
|
| 157 |
+
"""
|
| 158 |
+
Format the metadata.
|
| 159 |
+
:param metadata: the metadata
|
| 160 |
+
:type metadata: List[Dict[str, Any]]
|
| 161 |
+
:return: the formatted metadata
|
| 162 |
+
:rtype: str
|
| 163 |
+
"""
|
| 164 |
lines = []
|
| 165 |
for function_data in metadata:
|
| 166 |
lines.append(f"Function: {function_data['name']}")
|
|
|
|
| 179 |
|
| 180 |
return '\n'.join(lines)
|
| 181 |
def _read_py_code_library(self, file_location):
|
| 182 |
+
"""
|
| 183 |
+
Read the python code library from a file.
|
| 184 |
+
:param file_location: the file location
|
| 185 |
+
:type file_location: str
|
| 186 |
+
:return: the python code library
|
| 187 |
+
:rtype: str
|
| 188 |
+
"""
|
| 189 |
metadata = self._get_pyfile_functions_metadata_from_file(file_location)
|
| 190 |
if len(metadata) == 0:
|
| 191 |
return "No functions yet."
|
|
|
|
| 195 |
def run(
|
| 196 |
self,
|
| 197 |
input_data: Dict[str, Any]):
|
| 198 |
+
"""
|
| 199 |
+
Run the flow.
|
| 200 |
+
:param input_data: the input data
|
| 201 |
+
:type input_data: Dict[str, Any]
|
| 202 |
+
:return: the output data
|
| 203 |
+
:rtype: Dict[str, Any]
|
| 204 |
+
"""
|
| 205 |
self._check_input_data(input_data)
|
| 206 |
response = {}
|
| 207 |
for mem_name, mem_path in input_data["memory_files"].items():
|
README.md
CHANGED
|
@@ -1,9 +1,37 @@
|
|
| 1 |
# Table of Contents
|
| 2 |
|
|
|
|
|
|
|
| 3 |
* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow)
|
| 4 |
* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow)
|
|
|
|
|
|
|
|
|
|
| 5 |
* [\_\_init\_\_](#__init__)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
<a id="MemoryReadingAtomicFlow"></a>
|
| 8 |
|
| 9 |
# MemoryReadingAtomicFlow
|
|
@@ -22,12 +50,51 @@ Any composite flow that uses this flow should have
|
|
| 22 |
memory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state
|
| 23 |
|
| 24 |
*Input Interface*:
|
| 25 |
-
- `memory_files` : name of the Dict which maps the memory name to its file location e.g.
|
| 26 |
-
{"plan": "examples/JARVIS/plan.txt"}
|
| 27 |
|
| 28 |
*Output_Interface*:
|
| 29 |
- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
<a id="__init__"></a>
|
| 32 |
|
| 33 |
# \_\_init\_\_
|
|
|
|
| 1 |
# Table of Contents
|
| 2 |
|
| 3 |
+
* [example\_library](#example_library)
|
| 4 |
+
* [add\_two\_numbers](#example_library.add_two_numbers)
|
| 5 |
* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow)
|
| 6 |
* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow)
|
| 7 |
+
* [\_\_init\_\_](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.__init__)
|
| 8 |
+
* [run](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.run)
|
| 9 |
+
* [run](#run)
|
| 10 |
* [\_\_init\_\_](#__init__)
|
| 11 |
|
| 12 |
+
<a id="example_library"></a>
|
| 13 |
+
|
| 14 |
+
# example\_library
|
| 15 |
+
|
| 16 |
+
<a id="example_library.add_two_numbers"></a>
|
| 17 |
+
|
| 18 |
+
#### add\_two\_numbers
|
| 19 |
+
|
| 20 |
+
```python
|
| 21 |
+
def add_two_numbers(a, b)
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
Add two numbers together and return the result
|
| 25 |
+
|
| 26 |
+
**Arguments**:
|
| 27 |
+
|
| 28 |
+
- `a`: first number
|
| 29 |
+
- `b`: second number
|
| 30 |
+
|
| 31 |
+
**Returns**:
|
| 32 |
+
|
| 33 |
+
sum of a and b
|
| 34 |
+
|
| 35 |
<a id="MemoryReadingAtomicFlow"></a>
|
| 36 |
|
| 37 |
# MemoryReadingAtomicFlow
|
|
|
|
| 50 |
memory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state
|
| 51 |
|
| 52 |
*Input Interface*:
|
| 53 |
+
- `memory_files` : name of the Dict which maps the memory name to its file location e.g. {"plan": "examples/JARVIS/plan.txt"}
|
|
|
|
| 54 |
|
| 55 |
*Output_Interface*:
|
| 56 |
- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.
|
| 57 |
|
| 58 |
+
*Configuration Parameters*:
|
| 59 |
+
- `input_interface`: input interface of the flow
|
| 60 |
+
- `output_interface`: output interface of the flow
|
| 61 |
+
|
| 62 |
+
<a id="MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.__init__"></a>
|
| 63 |
+
|
| 64 |
+
#### \_\_init\_\_
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
def __init__(**kwargs)
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
This is the constructor of the :class:`MemoryReadingAtomicFlow` class.
|
| 71 |
+
|
| 72 |
+
**Arguments**:
|
| 73 |
+
|
| 74 |
+
- `kwargs` (`Dict[str, Any]`): additional arguments to pass to the :class:`AtomicFlow` constructor
|
| 75 |
+
|
| 76 |
+
<a id="MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.run"></a>
|
| 77 |
+
|
| 78 |
+
#### run
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
def run(input_data: Dict[str, Any])
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
Run the flow.
|
| 85 |
+
|
| 86 |
+
**Arguments**:
|
| 87 |
+
|
| 88 |
+
- `input_data` (`Dict[str, Any]`): the input data
|
| 89 |
+
|
| 90 |
+
**Returns**:
|
| 91 |
+
|
| 92 |
+
`Dict[str, Any]`: the output data
|
| 93 |
+
|
| 94 |
+
<a id="run"></a>
|
| 95 |
+
|
| 96 |
+
# run
|
| 97 |
+
|
| 98 |
<a id="__init__"></a>
|
| 99 |
|
| 100 |
# \_\_init\_\_
|