Upload 9 files
Browse filesFiles changed (2)
- MemoryReadingAtomicFlow.py+91-3
- README.md+69-2
12 unmodified lines131415161718192021222324252627282930313 unmodified lines3536373839404142434445464748491 unmodified line5152535455561 unmodified line58596061626364656620 unmodified lines87888990919212 unmodified lines1051061071081091103 unmodified lines11411511611711811912 unmodified linesmemory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state*Input Interface*:- `memory_files` : name of the Dict which maps the memory name to its file location e.g.{"plan": "examples/JARVIS/plan.txt"}*Output_Interface*:- corresponding memory content, for example, `code_library`. There could be multiple memory content returned."""def __init__(self, **kwargs):super().__init__(**kwargs)self.supported_mem_name = ["plan", "logs", "code_library"]def _check_input_data(self, input_data: Dict[str, Any]):"""input data sanity check"""assert "memory_files" in input_data, "memory_files not passed to MemoryReadingAtomicFlow"for mem_name, mem_path in input_data["memory_files"].items():3 unmodified linesassert os.path.isfile(mem_path), f"{mem_path} is not a file."def _read_text(self, file_location):with open(file_location, 'r', encoding='utf-8') as file:content = file.read()return contentdef _get_pyfile_functions_metadata_from_file(self, file_location):def python_file_path_to_module_name(file_path):return os.path.basename(file_path).replace('.py', '')def extract_top_level_function_names(python_file_path):with open(python_file_path, 'r') as file:file_content = file.read()tree = ast.parse(file_content)1 unmodified linereturn [node.name for node in functions]def load_module_from_file(file_path):module_name = python_file_path_to_module_name(file_path)spec = importlib.util.spec_from_file_location(module_name, file_path)module = importlib.util.module_from_spec(spec)1 unmodified linereturn moduledef get_function_from_name(function_name, module):return getattr(module, function_name)def function_to_dict(function):if not callable(function):raise ValueError("Provided object is not a function.")20 unmodified linesreturn [function_to_dict(function) for function in functions]def _format_metadata(self, metadata):lines = []for function_data in metadata:lines.append(f"Function: {function_data['name']}")12 unmodified linesreturn '\n'.join(lines)def _read_py_code_library(self, file_location):metadata = self._get_pyfile_functions_metadata_from_file(file_location)if len(metadata) == 0:return "No functions yet."3 unmodified linesdef run(self,input_data: Dict[str, Any]):self._check_input_data(input_data)response = {}for mem_name, mem_path in input_data["memory_files"].items():12 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546473 unmodified lines515253545556575859606162636465666768697071727374757677787980818283848586878889909192931 unmodified line95969798991001011021031041051061071 unmodified line10911011111211311411511611711811912012112212312412512612712812913013113213320 unmodified lines15415515615715815916016116216316416516612 unmodified lines1791801811821831841851861871881891901913 unmodified lines19519619719819920020120220320420520620712 unmodified linesmemory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state*Input Interface*:- `memory_files` : name of the Dict which maps the memory name to its file location e.g. {"plan": "examples/JARVIS/plan.txt"}*Output_Interface*:- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.*Configuration Parameters*:- `input_interface`: input interface of the flow- `output_interface`: output interface of the flow"""def __init__(self, **kwargs):"""This is the constructor of the :class:`MemoryReadingAtomicFlow` class.:param kwargs: additional arguments to pass to the :class:`AtomicFlow` constructor:type kwargs: Dict[str, Any]"""super().__init__(**kwargs)self.supported_mem_name = ["plan", "logs", "code_library"]def _check_input_data(self, input_data: Dict[str, Any]):"""input data sanity check:param input_data: input data to the flow:type input_data: Dict[str, Any]:raises AssertionError: if the input data does not contain the required keys:raises AssertionError: if the memory file does not exist:raises AssertionError: if the memory file is not a file:raises AssertionError: if the memory name is not supported"""assert "memory_files" in input_data, "memory_files not passed to MemoryReadingAtomicFlow"for mem_name, mem_path in input_data["memory_files"].items():3 unmodified linesassert os.path.isfile(mem_path), f"{mem_path} is not a file."def _read_text(self, file_location):"""Read text from a file.:param file_location: the file location:type file_location: str:return: the content of the file:rtype: str"""with open(file_location, 'r', encoding='utf-8') as file:content = file.read()return contentdef _get_pyfile_functions_metadata_from_file(self, file_location):"""Get the metadata of all the functions in a python file.:param file_location: the file location:type file_location: str:return: the metadata of all the functions in a python file:rtype: List[Dict[str, Any]]"""def python_file_path_to_module_name(file_path):"""Convert a python file path to its module name.:param file_path: the file path:type file_path: str:return: the module name:rtype: str"""return os.path.basename(file_path).replace('.py', '')def extract_top_level_function_names(python_file_path):"""Extract the top level function names from a python file.:param python_file_path: the python file path:type python_file_path: str:return: the top level function names:rtype: List[str]"""with open(python_file_path, 'r') as file:file_content = file.read()tree = ast.parse(file_content)1 unmodified linereturn [node.name for node in functions]def load_module_from_file(file_path):"""Load a module from a file.:param file_path: the file path:type file_path: str:return: the module:rtype: ModuleType"""module_name = python_file_path_to_module_name(file_path)spec = importlib.util.spec_from_file_location(module_name, file_path)module = importlib.util.module_from_spec(spec)1 unmodified linereturn moduledef get_function_from_name(function_name, module):"""Get a function from its name.:param function_name: the function name:type function_name: str:param module: the module:type module: ModuleType:return: the function:rtype: Callable"""return getattr(module, function_name)def function_to_dict(function):"""Convert a function to a dictionary.:param function: the function:type function: Callable:return: the dictionary:rtype: Dict[str, Any]"""if not callable(function):raise ValueError("Provided object is not a function.")20 unmodified linesreturn [function_to_dict(function) for function in functions]def _format_metadata(self, metadata):"""Format the metadata.:param metadata: the metadata:type metadata: List[Dict[str, Any]]:return: the formatted metadata:rtype: str"""lines = []for function_data in metadata:lines.append(f"Function: {function_data['name']}")12 unmodified linesreturn '\n'.join(lines)def _read_py_code_library(self, file_location):"""Read the python code library from a file.:param file_location: the file location:type file_location: str:return: the python code library:rtype: str"""metadata = self._get_pyfile_functions_metadata_from_file(file_location)if len(metadata) == 0:return "No functions yet."3 unmodified linesdef run(self,input_data: Dict[str, Any]):"""Run the flow.:param input_data: the input data:type input_data: Dict[str, Any]:return: the output data:rtype: Dict[str, Any]"""self._check_input_data(input_data)response = {}for mem_name, mem_path in input_data["memory_files"].items():
12345678912 unmodified lines222324252627282930313233# Table of Contents* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow)* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow)* [\_\_init\_\_](#__init__)<a id="MemoryReadingAtomicFlow"></a># MemoryReadingAtomicFlow12 unmodified linesmemory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state*Input Interface*:- `memory_files` : name of the Dict which maps the memory name to its file location e.g.{"plan": "examples/JARVIS/plan.txt"}*Output_Interface*:- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.<a id="__init__"></a># \_\_init\_\_1234567891011121314151617181920212223242526272829303132333435363712 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100# Table of Contents* [example\_library](#example_library)* [add\_two\_numbers](#example_library.add_two_numbers)* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow)* [MemoryReadingAtomicFlow](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow)* [\_\_init\_\_](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.__init__)* [run](#MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.run)* [run](#run)* [\_\_init\_\_](#__init__)<a id="example_library"></a># example\_library<a id="example_library.add_two_numbers"></a>#### add\_two\_numbers```pythondef add_two_numbers(a, b)```Add two numbers together and return the result**Arguments**:- `a`: first number- `b`: second number**Returns**:sum of a and b<a id="MemoryReadingAtomicFlow"></a># MemoryReadingAtomicFlow12 unmodified linesmemory_files: Dict[str, str] which maps memory name to its memory file location in the flow_state*Input Interface*:- `memory_files` : name of the Dict which maps the memory name to its file location e.g. {"plan": "examples/JARVIS/plan.txt"}*Output_Interface*:- corresponding memory content, for example, `code_library`. There could be multiple memory content returned.*Configuration Parameters*:- `input_interface`: input interface of the flow- `output_interface`: output interface of the flow<a id="MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.__init__"></a>#### \_\_init\_\_```pythondef __init__(**kwargs)```This is the constructor of the :class:`MemoryReadingAtomicFlow` class.**Arguments**:- `kwargs` (`Dict[str, Any]`): additional arguments to pass to the :class:`AtomicFlow` constructor<a id="MemoryReadingAtomicFlow.MemoryReadingAtomicFlow.run"></a>#### run```pythondef run(input_data: Dict[str, Any])```Run the flow.**Arguments**:- `input_data` (`Dict[str, Any]`): the input data**Returns**:`Dict[str, Any]`: the output data<a id="run"></a># run<a id="__init__"></a># \_\_init\_\_
MemoryReadingAtomicFlow.py+91-3
README.md+69-2