Tachi67 commited on
Commit
d832d4e
·
1 Parent(s): 342b568

Upload 6 files

Browse files
InteractiveCodeGenFlow.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from copy import deepcopy
2
+ from typing import Any, Dict
3
+
4
+ from aiflows.base_flows import SequentialFlow
5
+ from aiflows.utils import logging
6
+
7
+ logging.set_verbosity_debug()
8
+ log = logging.get_logger(__name__)
9
+
10
+ class InteractiveCodeGenFlow(SequentialFlow):
11
+ """This flow writes code in an interactive manner. It is a sequential flow composed of:
12
+ 1. MemoryReading: reads in the code library.
13
+ 2. CodeGenerator: generates code based on the goal and functions in the code library.
14
+ 3. CodeFileEditor: writes the generated code to a temp file for the user to see, edit and provide feedback.
15
+ 4. ParseFeedback: opens up the temp file with vscode and parses the feedback from the user.
16
+
17
+ *Input Interface*:
18
+ - `goal`
19
+
20
+ *Output Interface*:
21
+ - `code`
22
+ - `feedback`
23
+ - `temp_code_file_location`
24
+ """
25
+ REQUIRED_KEYS_CONFIG = ["max_rounds", "early_exit_key", "topology", "memory_files"]
26
+
27
+ def __init__(
28
+ self,
29
+ memory_files: Dict[str, Any],
30
+ **kwargs
31
+ ):
32
+ super().__init__(**kwargs)
33
+ self.memory_files = memory_files
34
+
35
+ @classmethod
36
+ def instantiate_from_config(cls, config):
37
+ flow_config = deepcopy(config)
38
+
39
+ kwargs = {"flow_config": flow_config}
40
+
41
+ # ~~~ Set up memory file ~~~
42
+ memory_files = flow_config["memory_files"]
43
+ kwargs.update({"memory_files": memory_files})
44
+
45
+ # ~~~ Set up subflows ~~~
46
+ kwargs.update({"subflows": cls._set_up_subflows(flow_config)})
47
+
48
+ # ~~~ Instantiate flow ~~~
49
+ return cls(**kwargs)
50
+
51
+ def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
52
+ # ~~~ sets the input_data in the flow_state dict ~~~
53
+ self._state_update_dict(update_data=input_data)
54
+
55
+ # ~~~ set the memory file to the flow state ~~~
56
+ self._state_update_dict(update_data={"memory_files": self.memory_files})
57
+
58
+ max_rounds = self.flow_config.get("max_rounds", 1)
59
+ if max_rounds is None:
60
+ log.info(f"Running {self.flow_config['name']} without `max_rounds` until the early exit condition is met.")
61
+
62
+ self._sequential_run(max_rounds=max_rounds)
63
+
64
+ output = self._get_output_from_state()
65
+
66
+ return output
InteractiveCodeGenFlow.yaml ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "InteractiveCodeGenFlow"
2
+ description: "Generate code as goal requests, open up a temp file for the user to examine and provide feedback"
3
+
4
+ _target_: flow_modules.aiflows.InteractiveCodeGenFlowModule.InteractiveCodeGenFlow.instantiate_from_default_config
5
+
6
+ memory_files: ???
7
+
8
+ input_interface:
9
+ - "goal"
10
+
11
+ output_interface:
12
+ - "code"
13
+ - "feedback"
14
+ - "temp_code_file_location"
15
+
16
+ subflows_config:
17
+ MemoryReading:
18
+ _target_: flow_modules.aiflows.MemoryReadingFlowModule.MemoryReadingAtomicFlow.instantiate_from_default_config
19
+
20
+ CodeGenerator:
21
+ _target_: flow_modules.aiflows.CodeGeneratorFlowModule.CodeGeneratorAtomicFlow.instantiate_from_default_config
22
+ backend:
23
+ api_infos: ???
24
+ model_name:
25
+ openai: gpt-4
26
+ azure: azure/gpt-4
27
+
28
+ CodeFileEditor:
29
+ _target_: flow_modules.aiflows.CodeFileEditFlowModule.CodeFileEditAtomicFlow.instantiate_from_default_config
30
+
31
+ ParseFeedback:
32
+ _target_: flow_modules.aiflows.ParseFeedbackFlowModule.ParseFeedbackAtomicFlow.instantiate_from_default_config
33
+
34
+ early_exit_key: "EARLY_EXIT"
35
+
36
+ topology:
37
+ - goal: "Read in necessary memory"
38
+ input_interface:
39
+ _target_: aiflows.interfaces.KeyInterface
40
+ additional_transformations:
41
+ - _target_: aiflows.data_transformations.KeyMatchInput
42
+ flow: MemoryReading
43
+ reset: false
44
+
45
+ - goal: "Generate code to achieve the task."
46
+ input_interface:
47
+ _target_: aiflows.interfaces.KeyInterface
48
+ additional_transformations:
49
+ - _target_: aiflows.data_transformations.KeyMatchInput
50
+ flow: CodeGenerator
51
+ reset: false
52
+
53
+ - goal: "Write the code generated to a temp file with instructions to the user"
54
+ input_interface:
55
+ _target_: aiflows.interfaces.KeyInterface
56
+ additional_transformations:
57
+ - _target_: aiflows.data_transformations.KeyMatchInput
58
+ flow: CodeFileEditor
59
+ reset: false
60
+
61
+ - goal: "Parse user feedback from the temp file"
62
+ input_interface:
63
+ _target_: aiflows.interfaces.KeyInterface
64
+ additional_transformations:
65
+ - _target_: aiflows.data_transformations.KeyMatchInput
66
+ flow: ParseFeedback
67
+ reset: false
README.md CHANGED
@@ -1,3 +1,79 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Structure of InteractiveCodeGenFlow
2
+
3
+ ```
4
+ goal
5
+ |
6
+ |
7
+ |
8
+ v
9
+ +------------------+
10
+ | MemReading | Reads in the memory in the memory files. (https://huggingface.co/Tachi67/MemoryReadingFlowModule)https://huggingface.co/Tachi67/MemoryReadingFlowModule
11
+ +------------------+
12
+ |
13
+ | (code_library: function signatures of the library)
14
+ |
15
+ v
16
+ +------------------+
17
+ | CodeGenerator | Generates code given goal, function signatures of the library. (https://huggingface.co/Tachi67/CodeGeneratorFlowModule)
18
+ +------------------+
19
+ |
20
+ | (code, language_of_code)
21
+ |
22
+ v
23
+ +-------------------+
24
+ | CodeFileEdit | Edit a temp code file with the code generated. (https://huggingface.co/Tachi67/CodeFileEditFlowModule)
25
+ +-------------------+
26
+ |
27
+ | (temp_code_file_location)
28
+ |
29
+ v
30
+ +------------------+
31
+ | ParseFeedback | Opens up the temp file until user closes the file, parse for user feedback. (https://huggingface.co/Tachi67/ParseFeedbackFlowModule)
32
+ +------------------+
33
+ |
34
+ | (code, feedback)
35
+ |
36
+ v
37
+ code, feedback
38
+
39
+ ```
40
+
41
+ Memory_files:
42
+ - library.py
43
+
44
+ # Table of Contents
45
+
46
+ * [InteractiveCodeGenFlow](#InteractiveCodeGenFlow)
47
+ * [InteractiveCodeGenFlow](#InteractiveCodeGenFlow.InteractiveCodeGenFlow)
48
+ * [\_\_init\_\_](#__init__)
49
+
50
+ <a id="InteractiveCodeGenFlow"></a>
51
+
52
+ # InteractiveCodeGenFlow
53
+
54
+ <a id="InteractiveCodeGenFlow.InteractiveCodeGenFlow"></a>
55
+
56
+ ## InteractiveCodeGenFlow Objects
57
+
58
+ ```python
59
+ class InteractiveCodeGenFlow(SequentialFlow)
60
+ ```
61
+
62
+ This flow writes code in an interactive manner. It is a sequential flow composed of:
63
+ 1. MemoryReading: reads in the code library.
64
+ 2. CodeGenerator: generates code based on the goal and functions in the code library.
65
+ 3. CodeFileEditor: writes the generated code to a temp file for the user to see, edit and provide feedback.
66
+ 4. ParseFeedback: opens up the temp file with vscode and parses the feedback from the user.
67
+
68
+ *Input Interface*:
69
+ - `goal`
70
+
71
+ *Output Interface*:
72
+ - `code`
73
+ - `feedback`
74
+ - `temp_code_file_location`
75
+
76
+ <a id="__init__"></a>
77
+
78
+ # \_\_init\_\_
79
+
__init__.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ~~~ Specify the dependencies ~~~
2
+ dependencies = [
3
+ {"url": "aiflows/MemoryReadingFlowModule", "revision": "main"},
4
+ {"url": "aiflows/CodeGeneratorFlowModule", "revision": "main"},
5
+ {"url": "aiflows/CodeFileEditFlowModule", "revision": "main"},
6
+ {"url": "aiflows/ParseFeedbackFlowModule", "revision": "main"},
7
+ ]
8
+ from aiflows import flow_verse
9
+
10
+ flow_verse.sync_dependencies(dependencies)
11
+
12
+ from .InteractiveCodeGenFlow import InteractiveCodeGenFlow
pip_requirements.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ colorama==0.4.6
2
+ pytest==7.3.1
3
+ pytest-cov==4.1.0
4
+ hydra-core==1.3.2
5
+ hydra-colorlog==1.1.0
6
+ wrapt-timeout-decorator==1.3.12.2
7
+ diskcache==5.6.1
8
+ openai==1.0.0
9
+ huggingface_hub==0.19.4
10
+ jsonlines==3.1.0
11
+ jinja2==3.1.2
12
+ mock==5.0.2
13
+ rich==12.6.0
14
+ litellm==1.0.0
15
+ aiflows
run.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import hydra
4
+
5
+ from aiflows.backends.api_info import ApiInfo
6
+ from aiflows.messages import InputMessage
7
+ from aiflows.utils.general_helpers import read_yaml_file, quick_load
8
+
9
+ from aiflows import logging
10
+ from aiflows.flow_cache import CACHING_PARAMETERS, clear_cache
11
+
12
+ CACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching
13
+
14
+ logging.set_verbosity_debug()
15
+ logging.auto_set_dir()
16
+
17
+ dependencies = [
18
+ {"url": "aiflows/CodeGeneratorFlowModule", "revision": "main"},
19
+ ]
20
+
21
+ from aiflows import flow_verse
22
+
23
+ flow_verse.sync_dependencies(dependencies)
24
+
25
+ if __name__ == "__main__":
26
+ # ~~~ make sure to set the openai api key in the envs ~~~
27
+ key = os.getenv("OPENAI_API_KEY")
28
+ api_information = [ApiInfo(backend_used="openai", api_key=os.getenv("OPENAI_API_KEY"))]
29
+ current_dir = os.getcwd()
30
+ cfg_path = os.path.join(current_dir, "InteractiveCodeGenFlow.yaml")
31
+ cfg = read_yaml_file(cfg_path)
32
+
33
+ # configuring memory files
34
+ with open(os.path.join(current_dir, "example_library.py"), 'w') as f:
35
+ pass
36
+ memory_files = {"code_library": os.path.join(current_dir, "example_library.py")}
37
+ cfg["memory_files"] = memory_files
38
+
39
+ # configuring api information
40
+ quick_load(cfg, api_information)
41
+
42
+ InteractiveCodeGenFlow = hydra.utils.instantiate(cfg, _recursive_=False, _convert_="partial")
43
+
44
+
45
+ input_data = {
46
+ "goal": "write a function to add up two numbers"
47
+ }
48
+ input_message = InputMessage.build(
49
+ data_dict=input_data,
50
+ src_flow="Launcher",
51
+ dst_flow=InteractiveCodeGenFlow.name
52
+ )
53
+
54
+ # ~~~ calling the flow ~~~
55
+ output_message = InteractiveCodeGenFlow(input_message)
56
+
57
+ print(output_message.data)