Tachi67 commited on
Commit
237937e
·
1 Parent(s): db6a065

Upload 3 files

Browse files
ParseFeedbackAtomicFlow.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+ from flows.base_flows.atomic import AtomicFlow
3
+
4
+
5
+ class ParseFeedbackAtomicFlow(AtomicFlow):
6
+ def _read_content(self, file_location, file_type):
7
+ if file_type == "code":
8
+ header_string = "# Code:\n"
9
+ elif file_type == "plan":
10
+ header_string = "Plan:\n"
11
+ else:
12
+ raise NotImplemented
13
+
14
+ with open(file_location, "r") as file:
15
+ content = file.read()
16
+
17
+ start_index = content.find(header_string)
18
+ if start_index == -1:
19
+ raise ValueError(f"Format error: {header_string} tag not found.")
20
+
21
+ end_index = content.find("\n############\n")
22
+ if end_index == -1 or end_index < start_index:
23
+ raise ValueError(f"Format error: {file_type} section is malformed.")
24
+
25
+ ret = content[start_index + len(header_string):end_index].strip()
26
+
27
+ return ret
28
+
29
+ def _parse_user_thoughts(self, file_location):
30
+ with open(file_location, "r") as file:
31
+ content = file.read()
32
+
33
+ delimiter = "\n############\n"
34
+ parts = content.split(delimiter)
35
+ if len(parts) < 2:
36
+ raise ValueError("Format error: Unable to find the thoughts section.")
37
+
38
+ thoughts_section = parts[1].strip()
39
+
40
+ thoughts_index = thoughts_section.find("Thoughts:")
41
+ if thoughts_index == -1:
42
+ raise ValueError("Format error: 'Thoughts:' tag not found.")
43
+
44
+ thoughts = thoughts_section[thoughts_index + len("Thoughts:"):].strip()
45
+
46
+ if thoughts == '':
47
+ thoughts = 'Looks good, go on.'
48
+
49
+ return thoughts
50
+
51
+ def _check_input(self, input_data: Dict[str, Any]):
52
+ code_file_exists = "temp_code_file_location" in input_data
53
+ plan_file_exists = "plan_file_location" in input_data
54
+ if code_file_exists == False and plan_file_exists == False:
55
+ raise AssertionError("Neither code file nor plan file is passed to ParseFeedbackAtomicFlow")
56
+ elif code_file_exists and plan_file_exists:
57
+ raise AssertionError("Both code file and plan file are passed to ParseFeedbackAtomic, which one to parse?")
58
+
59
+ def run(
60
+ self,
61
+ input_data: Dict[str, Any]
62
+ ):
63
+ response = {}
64
+ self._check_input(input_data)
65
+ if "temp_code_file_location" in input_data:
66
+ temp_code_file_location = input_data['plan_file_location']
67
+ code_content = self._read_content(temp_code_file_location, "code")
68
+ user_thoughts = self._parse_user_thoughts(temp_code_file_location)
69
+ response = {"code": code_content, "human_feedback": user_thoughts}
70
+
71
+ elif "plan_file_location" in input_data:
72
+ plan_file_location = input_data['plan_file_location']
73
+ plan_content = self._read_content(plan_file_location, "plan")
74
+ user_thoughts = self._parse_user_thoughts(plan_file_location)
75
+ response = {"plan": plan_content, "human_feedback": user_thoughts}
76
+
77
+ return response
ParseFeedbackAtomicFlow.yaml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ name: "ParseFeedbackAtomicFlow"
2
+ description: "A flow used to parse user feedback from a plan file or a code file"
3
+
4
+
5
+ # Should be changed when the ParseFeedbackAtomicFlow is used to parse a plan file.
6
+ input_interface:
7
+ - "temp_code_file_location"
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .ParseFeedbackAtomicFlow import ParseFeedbackAtomicFlow