Upload 3 files
Browse files- MemoryReadingAtomicFlow.py +58 -0
- MemoryReadingAtomicFlow.yaml +5 -0
- __init__.py +1 -0
MemoryReadingAtomicFlow.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
import os
|
3 |
+
|
4 |
+
from flows.base_flows import AtomicFlow
|
5 |
+
from flows.utils.general_helpers import get_pyfile_functions_metadata_from_file
|
6 |
+
|
7 |
+
|
8 |
+
class MemoryReadingAtomicFlow(AtomicFlow):
|
9 |
+
"""A flow to read memory from given files.
|
10 |
+
|
11 |
+
Any composite flow that uses this flow should have
|
12 |
+
memory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state
|
13 |
+
|
14 |
+
*Input Interface*: - `memory_files` : name of the Dict which maps the memory name to its file location e.g.
|
15 |
+
{"plan": "examples/JARVIS/plan.txt"}
|
16 |
+
"""
|
17 |
+
|
18 |
+
def __init__(self):
|
19 |
+
super().__init__()
|
20 |
+
self.supported_mem_name = ["plan", "logs", "code_library"]
|
21 |
+
|
22 |
+
def _check_input_data(self, input_data: Dict[str, Any]):
|
23 |
+
"""input data sanity check"""
|
24 |
+
for mem_name, mem_path in input_data.items():
|
25 |
+
assert mem_name in self.supported_mem_name, (f"{mem_name} is not supported in MemoryReadingAtomicFlow, "
|
26 |
+
f"supported names are: {self.supported_mem_name}")
|
27 |
+
assert os.path.exists(mem_path), f"{mem_path} does not exist."
|
28 |
+
assert os.path.isfile(mem_path), f"{mem_path} is not a file."
|
29 |
+
|
30 |
+
def _read_text(self, file_location):
|
31 |
+
with open(file_location, 'r', encoding='utf-8') as file:
|
32 |
+
content = file.read()
|
33 |
+
return content
|
34 |
+
|
35 |
+
def _read_py_code_library(self, file_location):
|
36 |
+
metadata = get_pyfile_functions_metadata_from_file(file_location)
|
37 |
+
if len(metadata) == 0:
|
38 |
+
return "No functions yet."
|
39 |
+
descriptions = []
|
40 |
+
for function in metadata:
|
41 |
+
description = f"Function Name: {function['name']}\n"
|
42 |
+
description += f"Documentation: {function.get('docstring', 'No documentation')}\n"
|
43 |
+
description += f"Parameters: {', '.join(function.get('parameters', []))}\n"
|
44 |
+
description += f"Default Values: {function.get('defaults', 'None')}"
|
45 |
+
descriptions.append(description)
|
46 |
+
return "\n\n".join(descriptions)
|
47 |
+
|
48 |
+
def run(
|
49 |
+
self,
|
50 |
+
input_data: Dict[str, Any]):
|
51 |
+
self._check_input_data(input_data)
|
52 |
+
response = {}
|
53 |
+
for mem_name, mem_path in input_data.items():
|
54 |
+
if mem_name in ['plan', 'logs']:
|
55 |
+
response[mem_name] = self._read_text(mem_path)
|
56 |
+
elif mem_name == 'code_library' and mem_path.endswith('.py'):
|
57 |
+
response[mem_name] = self._read_py_code_library(mem_path)
|
58 |
+
return response
|
MemoryReadingAtomicFlow.yaml
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: "MemoryReadingAtomicFlow"
|
2 |
+
description: "A flow that reads memory from given file locations"
|
3 |
+
|
4 |
+
input_interface:
|
5 |
+
- "memory_files"
|
__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .MemoryReadingAtomicFlow import MemoryReadingAtomicFlow
|