Upload 9 files
Browse filesFiles changed (9)
- ExecuteCodeAtomicFlow.pyAdded+56
- ExecuteCodeAtomicFlow.yamlAdded+12
- RunCodeAskUserFlow.pyAdded+39
- RunCodeAskUserFlow.yamlAdded+14
- RunCodeFileEditAtomicFlow.pyAdded+80
- RunCodeFileEditAtomicFlow.yamlAdded+12
- RunCodeFlow.pyAdded+8
- RunCodeFlow.yamlAdded+46
- __init__.pyAdded+14
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import osimport timefrom typing import Dict, Anyimport subprocessfrom flow_modules.Tachi67.InterpreterFlowModule import InterpreterAtomicFlow
class ExecuteCodeAtomicFlow(InterpreterAtomicFlow): 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): try: process = subprocess.Popen(["code", file_location]) except Exception: raise 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) code_ran = input_data["code"].deepcopy() response = {"interpreter_output": execution_output, "code_ran": code_ran} return response123456789101112_target_: Tachi67.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"123456789101112131415161718192021222324252627282930313233343536373839from flow_modules.aiflows.HumanStandardInputFlowModule import HumanStandardInputFlow
from typing import Dict, Any
from flows.messages import UpdateMessage_Generic
from flows.utils import logging
log = logging.get_logger(f"flows.{__name__}")
class RunCodeAskUserFlow(HumanStandardInputFlow): 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 response1234567891011121314_target_: Tachi67.RunCodeFlowModule.RunCodeAskUserFlow.instantiate_from_default_configrequest_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"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import osfrom typing import Dict, Anyfrom flow_modules.Tachi67.CodeFileEditFlowModule import CodeFileEditAtomicFlow
class RunCodeFileEditAtomicFlow(CodeFileEditAtomicFlow):
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"] 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_write
def run( self, input_data: Dict[str, Any] ): self._check_input(input_data)
# ~~~ Getting input data to the file editor ~~~ content_to_write, file_location_to_write = self._generate_input_to_writer(input_data)
# ~~~ Calling the writer function ~~~ _, code_editor_output, temp_file_location, _ = self._write_code_content_to_file( file_location_to_write, content_to_write)
# ~~~ Generating return variables ~~~ response = {} response["code_editor_output"] = code_editor_output response["temp_code_file_location"] = temp_file_location return response123456789101112_target_: Tachi67.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"12345678from flows.base_flows import SequentialFlowfrom flows.utils import logging
logging.set_verbosity_debug()log = logging.get_logger(__name__)
class RunCodeFlow(SequentialFlow): pass12345678910111213141516171819202122232425262728293031323334353637383940414243444546name: "RunCodeFlow"description: "Run code in an interactive fashion"
_target_: Tachi67.RunCodeFlowModule.RunCodeFlow.instantiate_from_default_config
input_interface: - "memory_files" - "language" - "code"
output_interface: - "summary" - "result"
subflows_config: RunCodeFileEdit: _target_: Tachi67.RunCodeFlowModule.RunCodeFileEditAtomicFlow.instantiate_from_default_config
ExecuteCode: _target_: Tachi67.RunCodeFlowModule.ExecuteCodeAtomicFlow.instantiate_from_default_config
AskUser: _target_: Tachi67.RunCodeFlowModule.RunCodeAskUserFlow.instantiate_from_default_config
early_exit_key: "EARLY_EXIT"
topology: - goal: "Write the code & instructions to a temp file" input_interface: _target_: flows.interfaces.KeyInterface additional_transformations: - _target_: flows.data_transformations.KeyMatchInput flow: RunCodeFileEdit reset: false
- goal: "Run the code" input_interface: _target_: flows.interfaces.KeyInterface additional_transformations: - _target_: flows.data_transformations.KeyMatchInput flow: ExecuteCode reset: false
- goal: "Ask user for feedback" flow: AskUser reset: false1234567891011121314dependencies = [ {"url": "Tachi67/CodeFileEditFlowModule", "revision": "main"}, {"url": "Tachi67/InterpreterFlowModule", "revision": "main"}, {"url": "aiflows/HumanStandardInputFlowModule", "revision": "5683a922372c5fa90be9f6447d6662d8d80341fc"},]
from flows import flow_verse
flow_verse.sync_dependencies(dependencies)
from .RunCodeFlow import RunCodeFlowfrom .ExecuteCodeAtomicFlow import ExecuteCodeAtomicFlowfrom .RunCodeFileEditAtomicFlow import RunCodeFileEditAtomicFlowfrom .RunCodeAskUserFlow import RunCodeAskUserFlowExecuteCodeAtomicFlow.pyAdded+56
ExecuteCodeAtomicFlow.yamlAdded+12
RunCodeAskUserFlow.pyAdded+39
RunCodeAskUserFlow.yamlAdded+14
RunCodeFileEditAtomicFlow.pyAdded+80
RunCodeFileEditAtomicFlow.yamlAdded+12
RunCodeFlow.pyAdded+8
RunCodeFlow.yamlAdded+46
__init__.pyAdded+14