Upload 12 files
Browse filesFiles changed (12)
- ExecuteCodeAtomicFlow.pyAdded+64
- ExecuteCodeAtomicFlow.yamlAdded+12
- README.md+103-3
- RunCodeAskUserFlow.pyAdded+40
- RunCodeAskUserFlow.yamlAdded+22
- RunCodeFileEditAtomicFlow.pyAdded+70
- RunCodeFileEditAtomicFlow.yamlAdded+12
- RunCodeFlow.pyAdded+21
- RunCodeFlow.yamlAdded+46
- __init__.pyAdded+14
- library.pyAdded+2
- run.pyAdded+56
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import osimport timefrom typing import Dict, Anyimport subprocessfrom flow_modules.aiflows.InterpreterFlowModule import InterpreterAtomicFlow
class ExecuteCodeAtomicFlow(InterpreterAtomicFlow): """This class inherits from InterpreterAtomicFlow and is used to execute code in a file. It opens up the file in VSCode and waits for the user to save the file. Once the file is saved, it reads the code from the file and executes it. It then returns the output of the execution. *Input Interface*: - temp_code_file_location: The location of the file containing the code to be executed. - language: The language of the code to be executed. *Output Interface*: - interpreter_output: The output of the execution of the code. - code_ran: The code that was executed. """ def _prepare_code(self, input_data: Dict[str, Any]): file_location = input_data["temp_code_file_location"] start_marker = "# Code:\n" end_marker = "############" code_started = False code_str = "" with open(file_location, 'r') as file: for line in file: if line.strip() == start_marker.strip(): code_started = True continue if line.strip() == end_marker.strip(): break if code_started: code_str += line input_data["code"] = code_str
def _check_input(self, input_data: Dict[str, Any]): assert "temp_code_file_location" in input_data, "temp_code_file_location not passed to ExecuteCodeAtomicFlow" assert "language" in input_data, "language not passed to ExecuteCodeAtomicFlow"
def _delete_file(self, file_location): if os.path.exists(file_location): os.remove(file_location)
def _open_file_and_wait_for_upd(self, file_location): process = subprocess.Popen(["code", "--wait", file_location]) while True: if process.poll() is not None: break time.sleep(1)
def run( self, input_data: Dict[str, Any]): self._check_input(input_data) file_loc = input_data["temp_code_file_location"] self._open_file_and_wait_for_upd(file_loc) self._prepare_code(input_data) self._process_input_data(input_data) execution_output = self._call() self._delete_file(file_loc) response = {"interpreter_output": execution_output, "code_ran": input_data['code']} return response123456789101112_target_: flow_modules.aiflows.RunCodeFlowModule.ExecuteCodeAtomicFlow.instantiate_from_default_configname: "ExecuteCodeAtomicFlow"description: "A flow that opens up temp code file location, waits for update, runs code and deletes temp code file"
input_interface: - "temp_code_file_location" - "language"
output_interface: - "interpreter_output" - "code_ran"123123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103---license: mit---# Table of Contents
* [RunCodeFlow](#RunCodeFlow) * [RunCodeFlow](#RunCodeFlow.RunCodeFlow)* [ExecuteCodeAtomicFlow](#ExecuteCodeAtomicFlow) * [ExecuteCodeAtomicFlow](#ExecuteCodeAtomicFlow.ExecuteCodeAtomicFlow)* [RunCodeAskUserFlow](#RunCodeAskUserFlow) * [RunCodeAskUserFlow](#RunCodeAskUserFlow.RunCodeAskUserFlow)* [library](#library)* [run](#run)* [RunCodeFileEditAtomicFlow](#RunCodeFileEditAtomicFlow) * [RunCodeFileEditAtomicFlow](#RunCodeFileEditAtomicFlow.RunCodeFileEditAtomicFlow)* [\_\_init\_\_](#__init__)
<a id="RunCodeFlow"></a>
# RunCodeFlow
<a id="RunCodeFlow.RunCodeFlow"></a>
## RunCodeFlow Objects
```pythonclass RunCodeFlow(SequentialFlow)```
This flow is used to run code in a file. It opens up the file in VSCode and waits for the user to save the file.Once the file is saved, it reads the code from the file and executes it. It then returns the output of the execution.Finally, the flow asks the user for feedback on the execution of the code.
*Input Interface*:- code: The code to be executed.- language: The language of the code to be executed.- memory_files: The dictionary containing the location of the code library.
*Output Interface*:- summary: The summary of the execution of the code. (To be written to the logs of the caller flow)- result: The result of the execution of the code. (To be returned to the controller of the caller flow)
<a id="ExecuteCodeAtomicFlow"></a>
# ExecuteCodeAtomicFlow
<a id="ExecuteCodeAtomicFlow.ExecuteCodeAtomicFlow"></a>
## ExecuteCodeAtomicFlow Objects
```pythonclass ExecuteCodeAtomicFlow(InterpreterAtomicFlow)```
This class inherits from InterpreterAtomicFlow and is used to execute code in a file.It opens up the file in VSCode and waits for the user to save the file. Once the file is saved, it reads the codefrom the file and executes it. It then returns the output of the execution.
*Input Interface*:- temp_code_file_location: The location of the file containing the code to be executed.- language: The language of the code to be executed.
*Output Interface*:- interpreter_output: The output of the execution of the code.- code_ran: The code that was executed.
<a id="RunCodeAskUserFlow"></a>
# RunCodeAskUserFlow
<a id="RunCodeAskUserFlow.RunCodeAskUserFlow"></a>
## RunCodeAskUserFlow Objects
```pythonclass RunCodeAskUserFlow(HumanStandardInputFlow)```
Refer to: https://huggingface.co/Tachi67/ExtendLibraryFlowModule/blob/main/ExtLibAskUserFlow.py
<a id="library"></a>
# library
<a id="run"></a>
# run
<a id="RunCodeFileEditAtomicFlow"></a>
# RunCodeFileEditAtomicFlow
<a id="RunCodeFileEditAtomicFlow.RunCodeFileEditAtomicFlow"></a>
## RunCodeFileEditAtomicFlow Objects
```pythonclass RunCodeFileEditAtomicFlow(CodeFileEditAtomicFlow)```
Refer to: https://huggingface.co/Tachi67/CodeFileEditFlowModule
<a id="__init__"></a>
# \_\_init\_\_
12345678910111213141516171819202122232425262728293031323334353637383940from flow_modules.aiflows.HumanStandardInputFlowModule import HumanStandardInputFlow
from typing import Dict, Any
from aiflows.messages import UpdateMessage_Generic
from aiflows.utils import logging
log = logging.get_logger(f"aiflows.{__name__}")
class RunCodeAskUserFlow(HumanStandardInputFlow): """Refer to: https://huggingface.co/Tachi67/ExtendLibraryFlowModule/blob/main/ExtLibAskUserFlow.py""" def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
query_message = self._get_message(self.query_message_prompt_template, input_data) state_update_message = UpdateMessage_Generic( created_by=self.flow_config['name'], updated_flow=self.flow_config["name"], data={"query_message": query_message}, ) self._log_message(state_update_message)
log.info(query_message) human_input = self._read_input()
response = {} result = f""" The following code was ran: {input_data['code_ran']} The execution result is: {input_data['interpreter_output']} The user's feedback is: {human_input} """ response["result"] = result response["summary"] = f"Coder/run_code: \n" + result
return response12345678910111213141516171819202122_target_: flow_modules.aiflows.RunCodeFlowModule.RunCodeAskUserFlow.instantiate_from_default_config
input_interface: - "interpreter_output" - "code_ran"output_interface: - "summary" - "result"
request_multi_line_input_flag: Falseend_of_input_string: EOI
query_message_prompt_template: template: |2- The following code was ran by the interpreter: {{code_ran}} Here's the execution result of the code: {{interpreter_output}} Please provide your feedback. input_variables: - "code_ran" - "interpreter_output"12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import osfrom typing import Dict, Anyfrom flow_modules.aiflows.CodeFileEditFlowModule import CodeFileEditAtomicFlow
class RunCodeFileEditAtomicFlow(CodeFileEditAtomicFlow): """Refer to: https://huggingface.co/Tachi67/CodeFileEditFlowModule"""
def _generate_content(self, code_str, comment_sign) -> str: if comment_sign == "<!-- -->": instruction = ( "<!-- The below code will be executed.\n" "Change the code if you consider necessary.\n" "Shut this vscode session completely to start running the code. -->\n" ) else: instruction = ( f"{comment_sign} The below code will be executed.\n" f"{comment_sign} Change the code if you consider necessary.\n" f"{comment_sign} Shut this vscode session completely to start running the code.\n" ) content = ( instruction + "###########\n" "# Code:\n" + code_str + "\n############\n" ) return content
def _generate_temp_file_location(self, code_lib_location, file_extension): directory = os.path.dirname(code_lib_location) ret = os.path.join(directory, 'run_code_temp'+file_extension) return ret
def _check_input(self, input_data: Dict[str, Any]): assert "code" in input_data, "code is not passed to RunCodeFileEditAtomicFlow" assert "memory_files" in input_data, "memory_files is not passed to RunCodeFileEditAtomicFlow" assert "language" in input_data, "language is not passed to RunCodeFileEditAtomicFlow"
def _generate_file_extension_and_comment_sign(self, language: str): details = { "python": {"extension": ".py", "comment": "#"}, "bash": {"extension": ".sh", "comment": "#"}, "shell": {"extension": ".sh", "comment": "#"}, "javascript": {"extension": ".js", "comment": "//"}, "html": {"extension": ".html", "comment": "<!-- -->"}, "applescript": {"extension": ".scpt", "comment": "--"}, "r": {"extension": ".r", "comment": "#"}, "powershell": {"extension": ".ps1", "comment": "#"} } if language.lower() not in details: raise NotImplemented return details.get(language.lower())
def _generate_input_to_writer(self, input_data: Dict[str, Any]): code_str = input_data['code'] code_lib_location = input_data["memory_files"]["code_library"] # if we are running python code and if the code is importing the code library after one modification, we need # to re-import the library. code_lang = input_data["language"].lower() code_lib_name = os.path.splitext(os.path.basename(code_lib_location))[0] if code_lang == 'python' and code_lib_name in code_str and 'importlib.reload' not in code_str: prepend_code = f"import importlib\nimport {code_lib_name}\nimportlib.reload({code_lib_name})\n" code_str = prepend_code + code_str lang_details = self._generate_file_extension_and_comment_sign(input_data["language"]) content_to_write = self._generate_content(code_str, lang_details["comment"]) file_location_to_write = self._generate_temp_file_location(code_lib_location, lang_details["extension"]) return content_to_write, file_location_to_write123456789101112_target_: flow_modules.aiflows.RunCodeFlowModule.RunCodeFileEditAtomicFlow.instantiate_from_default_configname: "RunCodeFileEditorAtomicFlow"description: "A flow that writes code to be ran to a temp file"
input_interface: - "code" - "language" - "memory_files"
output_interface: - "code_editor_output" - "temp_code_file_location"123456789101112131415161718192021from aiflows.base_flows import SequentialFlowfrom aiflows.utils import logging
logging.set_verbosity_debug()log = logging.get_logger(__name__)
class RunCodeFlow(SequentialFlow): """This flow is used to run code in a file. It opens up the file in VSCode and waits for the user to save the file. Once the file is saved, it reads the code from the file and executes it. It then returns the output of the execution. Finally, the flow asks the user for feedback on the execution of the code. *Input Interface*: - code: The code to be executed. - language: The language of the code to be executed. - memory_files: The dictionary containing the location of the code library. *Output Interface*: - summary: The summary of the execution of the code. (To be written to the logs of the caller flow) - result: The result of the execution of the code. (To be returned to the controller of the caller flow) """ pass12345678910111213141516171819202122232425262728293031323334353637383940414243444546name: "RunCodeFlow"description: "Run code in an interactive fashion"
_target_: flow_modules.aiflows.RunCodeFlowModule.RunCodeFlow.instantiate_from_default_config
input_interface: - "memory_files" - "language" - "code"
output_interface: - "summary" - "result"
subflows_config: RunCodeFileEdit: _target_: flow_modules.aiflows.RunCodeFlowModule.RunCodeFileEditAtomicFlow.instantiate_from_default_config
ExecuteCode: _target_: flow_modules.aiflows.RunCodeFlowModule.ExecuteCodeAtomicFlow.instantiate_from_default_config
AskUser: _target_: flow_modules.aiflows.RunCodeFlowModule.RunCodeAskUserFlow.instantiate_from_default_config
early_exit_key: "EARLY_EXIT"
topology: - goal: "Write the code & instructions to a temp file" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: RunCodeFileEdit reset: false
- goal: "Run the code" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: ExecuteCode reset: false
- goal: "Ask user for feedback" flow: AskUser reset: false1234567891011121314dependencies = [ {"url": "aiflows/CodeFileEditFlowModule", "revision": "main"}, {"url": "aiflows/InterpreterFlowModule", "revision": "main"}, {"url": "aiflows/HumanStandardInputFlowModule", "revision": "4ff043522c89a964ea3a928ce09811c51a2b5b98"},]
from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
from .RunCodeFlow import RunCodeFlowfrom .ExecuteCodeAtomicFlow import ExecuteCodeAtomicFlowfrom .RunCodeFileEditAtomicFlow import RunCodeFileEditAtomicFlowfrom .RunCodeAskUserFlow import RunCodeAskUserFlow12def add_two_numbers(a, b): return a + b1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import os
import hydra
from aiflows.messages import InputMessagefrom aiflows.utils.general_helpers import read_yaml_file
from aiflows import loggingfrom aiflows.flow_cache import CACHING_PARAMETERS, clear_cache
CACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching# clear_cache() # Uncomment this line to clear the cache
logging.set_verbosity_debug()logging.auto_set_dir()
dependencies = [ {"url": "aiflows/CodeFileEditFlowModule", "revision": "main"}, {"url": "aiflows/InterpreterFlowModule", "revision": "main"}, {"url": "aiflows/HumanStandardInputFlowModule", "revision": "4ff043522c89a964ea3a928ce09811c51a2b5b98"}, {"url": "aiflows/RunCodeFlowModule", "revision": "main"}]
from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
if __name__ == "__main__": current_dir = os.getcwd() cfg_path = os.path.join(current_dir, "RunCodeFlow.yaml") cfg = read_yaml_file(cfg_path)
# ~~~ instantiating the flow and input data ~~~ RunCodeFlow = hydra.utils.instantiate(cfg, _recursive_=False, _convert_="partial") memory_files = {"code_library": os.path.join(current_dir, "library.py")} code = """from library import add_two_numbers
add_two_numbers(1,1) """ input_data = { "memory_files": memory_files, "language": "Python", "code": code, } input_message = InputMessage.build( data_dict=input_data, src_flow="Launcher", dst_flow=RunCodeFlow.name )
# ~~~ calling the flow ~~~ output_message = RunCodeFlow(input_message)
print(output_message)ExecuteCodeAtomicFlow.pyAdded+64
ExecuteCodeAtomicFlow.yamlAdded+12
README.md+103-3
RunCodeAskUserFlow.pyAdded+40
RunCodeAskUserFlow.yamlAdded+22
RunCodeFileEditAtomicFlow.pyAdded+70
RunCodeFileEditAtomicFlow.yamlAdded+12
RunCodeFlow.pyAdded+21
RunCodeFlow.yamlAdded+46
__init__.pyAdded+14
library.pyAdded+2
run.pyAdded+56