Upload 6 files
Browse filesFiles changed (6)
- InteractiveCodeGenFlow.pyAdded+66
- InteractiveCodeGenFlow.yamlAdded+67
- README.md+79-3
- __init__.pyAdded+12
- pip_requirements.txtAdded+15
- run.pyAdded+57
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566from copy import deepcopyfrom typing import Any, Dict
from aiflows.base_flows import SequentialFlowfrom aiflows.utils import logging
logging.set_verbosity_debug()log = logging.get_logger(__name__)
class InteractiveCodeGenFlow(SequentialFlow): """This flow writes code in an interactive manner. It is a sequential flow composed of: 1. MemoryReading: reads in the code library. 2. CodeGenerator: generates code based on the goal and functions in the code library. 3. CodeFileEditor: writes the generated code to a temp file for the user to see, edit and provide feedback. 4. ParseFeedback: opens up the temp file with vscode and parses the feedback from the user. *Input Interface*: - `goal` *Output Interface*: - `code` - `feedback` - `temp_code_file_location` """ REQUIRED_KEYS_CONFIG = ["max_rounds", "early_exit_key", "topology", "memory_files"]
def __init__( self, memory_files: Dict[str, Any], **kwargs ): super().__init__(**kwargs) self.memory_files = memory_files
@classmethod def instantiate_from_config(cls, config): flow_config = deepcopy(config)
kwargs = {"flow_config": flow_config}
# ~~~ Set up memory file ~~~ memory_files = flow_config["memory_files"] kwargs.update({"memory_files": memory_files})
# ~~~ Set up subflows ~~~ kwargs.update({"subflows": cls._set_up_subflows(flow_config)})
# ~~~ Instantiate flow ~~~ return cls(**kwargs)
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]: # ~~~ sets the input_data in the flow_state dict ~~~ self._state_update_dict(update_data=input_data)
# ~~~ set the memory file to the flow state ~~~ self._state_update_dict(update_data={"memory_files": self.memory_files})
max_rounds = self.flow_config.get("max_rounds", 1) if max_rounds is None: log.info(f"Running {self.flow_config['name']} without `max_rounds` until the early exit condition is met.")
self._sequential_run(max_rounds=max_rounds)
output = self._get_output_from_state()
return output12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667name: "InteractiveCodeGenFlow"description: "Generate code as goal requests, open up a temp file for the user to examine and provide feedback"
_target_: flow_modules.aiflows.InteractiveCodeGenFlowModule.InteractiveCodeGenFlow.instantiate_from_default_config
memory_files: ???
input_interface: - "goal"
output_interface: - "code" - "feedback" - "temp_code_file_location"
subflows_config: MemoryReading: _target_: flow_modules.aiflows.MemoryReadingFlowModule.MemoryReadingAtomicFlow.instantiate_from_default_config
CodeGenerator: _target_: flow_modules.aiflows.CodeGeneratorFlowModule.CodeGeneratorAtomicFlow.instantiate_from_default_config backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
CodeFileEditor: _target_: flow_modules.aiflows.CodeFileEditFlowModule.CodeFileEditAtomicFlow.instantiate_from_default_config
ParseFeedback: _target_: flow_modules.aiflows.ParseFeedbackFlowModule.ParseFeedbackAtomicFlow.instantiate_from_default_config
early_exit_key: "EARLY_EXIT"
topology: - goal: "Read in necessary memory" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: MemoryReading reset: false
- goal: "Generate code to achieve the task." input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: CodeGenerator reset: false
- goal: "Write the code generated to a temp file with instructions to the user" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: CodeFileEditor reset: false
- goal: "Parse user feedback from the temp file" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: ParseFeedback reset: false123---license: mit---12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879### Structure of InteractiveCodeGenFlow```goal|||v+------------------+| MemReading | Reads in the memory in the memory files. (https://huggingface.co/Tachi67/MemoryReadingFlowModule)https://huggingface.co/Tachi67/MemoryReadingFlowModule+------------------+|| (code_library: function signatures of the library)|v+------------------+| CodeGenerator | Generates code given goal, function signatures of the library. (https://huggingface.co/Tachi67/CodeGeneratorFlowModule)+------------------+|| (code, language_of_code)|v+-------------------+| CodeFileEdit | Edit a temp code file with the code generated. (https://huggingface.co/Tachi67/CodeFileEditFlowModule)+-------------------+|| (temp_code_file_location)|v+------------------+| ParseFeedback | Opens up the temp file until user closes the file, parse for user feedback. (https://huggingface.co/Tachi67/ParseFeedbackFlowModule)+------------------+|| (code, feedback)|vcode, feedback```Memory_files:- library.py# Table of Contents* [InteractiveCodeGenFlow](#InteractiveCodeGenFlow)* [InteractiveCodeGenFlow](#InteractiveCodeGenFlow.InteractiveCodeGenFlow)* [\_\_init\_\_](#__init__)<a id="InteractiveCodeGenFlow"></a># InteractiveCodeGenFlow<a id="InteractiveCodeGenFlow.InteractiveCodeGenFlow"></a>## InteractiveCodeGenFlow Objects```pythonclass InteractiveCodeGenFlow(SequentialFlow)```This flow writes code in an interactive manner. It is a sequential flow composed of:1. MemoryReading: reads in the code library.2. CodeGenerator: generates code based on the goal and functions in the code library.3. CodeFileEditor: writes the generated code to a temp file for the user to see, edit and provide feedback.4. ParseFeedback: opens up the temp file with vscode and parses the feedback from the user.*Input Interface*:- `goal`*Output Interface*:- `code`- `feedback`- `temp_code_file_location`<a id="__init__"></a># \_\_init\_\_
123456789101112# ~~~ Specify the dependencies ~~~dependencies = [ {"url": "aiflows/MemoryReadingFlowModule", "revision": "main"}, {"url": "aiflows/CodeGeneratorFlowModule", "revision": "main"}, {"url": "aiflows/CodeFileEditFlowModule", "revision": "main"}, {"url": "aiflows/ParseFeedbackFlowModule", "revision": "main"},]from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
from .InteractiveCodeGenFlow import InteractiveCodeGenFlow123456789101112131415colorama==0.4.6pytest==7.3.1pytest-cov==4.1.0hydra-core==1.3.2hydra-colorlog==1.1.0wrapt-timeout-decorator==1.3.12.2diskcache==5.6.1openai==1.0.0huggingface_hub==0.19.4jsonlines==3.1.0jinja2==3.1.2mock==5.0.2rich==12.6.0litellm==1.0.0aiflows123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import os
import hydra
from aiflows.backends.api_info import ApiInfofrom aiflows.messages import InputMessagefrom aiflows.utils.general_helpers import read_yaml_file, quick_load
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
logging.set_verbosity_debug()logging.auto_set_dir()
dependencies = [ {"url": "aiflows/CodeGeneratorFlowModule", "revision": "main"},]
from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
if __name__ == "__main__": # ~~~ make sure to set the openai api key in the envs ~~~ key = os.getenv("OPENAI_API_KEY") api_information = [ApiInfo(backend_used="openai", api_key=os.getenv("OPENAI_API_KEY"))] current_dir = os.getcwd() cfg_path = os.path.join(current_dir, "InteractiveCodeGenFlow.yaml") cfg = read_yaml_file(cfg_path)
# configuring memory files with open(os.path.join(current_dir, "example_library.py"), 'w') as f: pass memory_files = {"code_library": os.path.join(current_dir, "example_library.py")} cfg["memory_files"] = memory_files
# configuring api information quick_load(cfg, api_information)
InteractiveCodeGenFlow = hydra.utils.instantiate(cfg, _recursive_=False, _convert_="partial")
input_data = { "goal": "write a function to add up two numbers" } input_message = InputMessage.build( data_dict=input_data, src_flow="Launcher", dst_flow=InteractiveCodeGenFlow.name )
# ~~~ calling the flow ~~~ output_message = InteractiveCodeGenFlow(input_message)
print(output_message.data)InteractiveCodeGenFlow.pyAdded+66
InteractiveCodeGenFlow.yamlAdded+67
README.md+79-3
__init__.pyAdded+12
pip_requirements.txtAdded+15
run.pyAdded+57