Tachi67 commited on
Commit
dc1a80a
·
1 Parent(s): abeef9a

init commit, migrate from hf/tachi67

Browse files
CodeFileEditAtomicFlow.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+ from aiflows.base_flows.atomic import AtomicFlow
3
+ import os
4
+
5
+
6
+ class CodeFileEditAtomicFlow(AtomicFlow):
7
+ """This class is used to write code to a temp code file, with commented instructions to give information
8
+ to the user.
9
+
10
+ *Input Interface*:
11
+ - `code`: str
12
+ - `language_of_code`: str
13
+ - `memory_files`: Dict[str, str]
14
+
15
+ *Output Interface*:
16
+ - `code_editor_output`: str
17
+ - `temp_code_file_location`: str
18
+ """
19
+ def _generate_content(self, code_lib_location, code_str) -> str:
20
+ """This function generates the content to be written to the temp file.
21
+ :param code_lib_location (str): The location of the code library file.
22
+ :param code_str (str): The code to be written to the temp file.
23
+ :return: (str) The content to be written to the temp file.
24
+ """
25
+ content = (
26
+ "# The below code will be appended to " +
27
+ code_lib_location + "\n"
28
+ "# Edit the code directly or provide your thoughts down below if you have any suggestions.\n"
29
+ "# When you are done editing code and providing feedback, save file and close the current VSCode session to continue. \n"
30
+ "###########\n"
31
+ "# Code:\n" +
32
+ code_str +
33
+ "\n############\n"
34
+ "# Thoughts:"
35
+ )
36
+ return content
37
+
38
+ def _generate_temp_file_location(self, code_lib_location):
39
+ """This function generates the location of the temp file.
40
+ :param code_lib_location (str): The location of the code library file.
41
+ :return: (str) The location of the temp file.
42
+ """
43
+ directory = os.path.dirname(code_lib_location)
44
+ ret = os.path.join(directory, 'temp.py')
45
+ return ret
46
+
47
+ def _write_code_content_to_file(self, file_location, content: str):
48
+ """This function writes the content to the temp file.
49
+ :param file_location (str): The location of the temp file.
50
+ :param content (str): The content to be written to the temp file.
51
+ :return: (bool, str, str) The first value is a boolean indicating if the write was successful.
52
+ The second value is a string containing the output of the write operation.
53
+ The third value is a string containing the location of the temp file.
54
+ """
55
+ try:
56
+ with open(file_location, "w") as file:
57
+ file.write(content)
58
+
59
+ return True, f"Code written to {file_location}", file_location
60
+
61
+ except Exception as e:
62
+ return False, str(e), file_location
63
+
64
+ def _check_input(self, input_data: Dict[str, Any]):
65
+ """This function checks if the input data is valid.
66
+ :param input_data (Dict[str, Any]): The input data.
67
+ :return: None
68
+ """
69
+ assert "code" in input_data, "code is not passed to CodeFileEditAtomicFlow"
70
+ assert "language_of_code" in input_data, "language_of_code is not passed to CodeFileEditAtomicFlow"
71
+ assert "memory_files" in input_data, "memory_files is not passed to CodeFileEditAtomicFlow"
72
+ assert "code_library" in input_data["memory_files"], "code_library not in memory files"
73
+ code_lib_loc = input_data["memory_files"]["code_library"]
74
+ assert os.path.exists(code_lib_loc), f"{code_lib_loc} does not exist"
75
+ assert os.path.isfile(code_lib_loc), f"{code_lib_loc} is not a file"
76
+ language_of_code = input_data["language_of_code"]
77
+ assert language_of_code.lower() == 'python', "sorry!! only writing python code is supported."
78
+
79
+ def _generate_input_to_writer(self, input_data: Dict[str, Any]):
80
+ """This function generates the input to the writer function.
81
+ :param input_data (Dict[str, Any]): The input data.
82
+ :return: (str, str) The first value is a string containing the content to be written to the temp file.
83
+ The second value is a string containing the location of the temp file.
84
+ """
85
+ code_str = input_data['code']
86
+ code_lib_location = input_data["memory_files"]["code_library"]
87
+ content_to_write = self._generate_content(code_lib_location, code_str)
88
+ file_location_to_write = self._generate_temp_file_location(code_lib_location)
89
+ return content_to_write, file_location_to_write
90
+
91
+ def run(
92
+ self,
93
+ input_data: Dict[str, Any]
94
+ ):
95
+ """This function runs the atomic flow.
96
+ :param input_data (Dict[str, Any]): The input data.
97
+ :return: Dict[str, Any] The output data.
98
+ """
99
+ self._check_input(input_data)
100
+
101
+ # ~~~ Getting input data to the file editor ~~~
102
+ content_to_write, file_location_to_write = self._generate_input_to_writer(input_data)
103
+
104
+ # ~~~ Calling the writer function ~~~
105
+ result, code_editor_output, temp_file_location = self._write_code_content_to_file(
106
+ file_location_to_write, content_to_write)
107
+
108
+ # ~~~ Generating return variables ~~~
109
+ response = {}
110
+ response["code_editor_output"] = code_editor_output
111
+ response["temp_code_file_location"] = temp_file_location
112
+ return response
CodeFileEditAtomicFlow.yaml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "CodeFileEditAtomicFlow"
2
+ description: "A flow that writes code to a temp file"
3
+ _target_: flow_modules.aiflows.CodeFileEditFlowModule.CodeFileEditAtomicFlow.instantiate_from_default_config
4
+
5
+ input_interface:
6
+ - "code"
7
+ - "language_of_code"
8
+ - "memory_files"
9
+
10
+ output_interface:
11
+ - "code_editor_output"
12
+ - "temp_code_file_location"
README.md CHANGED
@@ -1,3 +1,49 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Table of Contents
2
+
3
+ * [CodeFileEditAtomicFlow](#CodeFileEditAtomicFlow)
4
+ * [CodeFileEditAtomicFlow](#CodeFileEditAtomicFlow.CodeFileEditAtomicFlow)
5
+ * [run](#CodeFileEditAtomicFlow.CodeFileEditAtomicFlow.run)
6
+ * [\_\_init\_\_](#__init__)
7
+
8
+ <a id="CodeFileEditAtomicFlow"></a>
9
+
10
+ # CodeFileEditAtomicFlow
11
+
12
+ <a id="CodeFileEditAtomicFlow.CodeFileEditAtomicFlow"></a>
13
+
14
+ ## CodeFileEditAtomicFlow Objects
15
+
16
+ ```python
17
+ class CodeFileEditAtomicFlow(AtomicFlow)
18
+ ```
19
+
20
+ This class is used to write code to a temp code file, with commented instructions to give information
21
+ to the user.
22
+
23
+ *Input Interface*:
24
+ - `code`: str
25
+ - `language_of_code`: str
26
+ - `memory_files`: Dict[str, str]
27
+
28
+ *Output Interface*:
29
+ - `code_editor_output`: str
30
+ - `temp_code_file_location`: str
31
+
32
+ <a id="CodeFileEditAtomicFlow.CodeFileEditAtomicFlow.run"></a>
33
+
34
+ #### run
35
+
36
+ ```python
37
+ def run(input_data: Dict[str, Any])
38
+ ```
39
+
40
+ This function runs the atomic flow.
41
+
42
+ :param input_data (Dict[str, Any]): The input data.
43
+ :return: Dict[str, Any] The output data.
44
+
45
+
46
+ <a id="__init__"></a>
47
+
48
+ # \_\_init\_\_
49
+
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .CodeFileEditAtomicFlow import CodeFileEditAtomicFlow
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