Tachi67 commited on
Commit
0660a86
·
1 Parent(s): ab0c22d

Upload 7 files

Browse files
PlanFileEditorAtomicFlow.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+
3
+ from flow_modules.Tachi67.InterpreterFlowModule import InterpreterAtomicFlow
4
+
5
+ class PlanFileEditorAtomicFlow(InterpreterAtomicFlow):
6
+
7
+ def _process_interperter_inputs(self, input_data: Dict[str, Any]):
8
+ input_data['language'] = 'python'
9
+ file_location = input_data['file_location']
10
+ plan_str = input_data['plan_str']
11
+ input_data['code'] = f"""
12
+ import os
13
+ file_location = {repr(file_location)}
14
+ plan_str = {repr(plan_str)}
15
+ if os.path.isdir(file_location):
16
+ file_location = os.path.join(file_location, 'plan.txt')
17
+ with open(file_location, 'w') as file:
18
+ file.write(plan_str)
19
+ print('plan written to' + file_location)"""
20
+
21
+ def run(
22
+ self,
23
+ input_data: Dict[str, Any]):
24
+ self._process_interperter_inputs(input_data)
25
+ self._process_input_data(input_data)
26
+ response = self._call()
27
+ return {"plan_writer_output": response}
PlanFileEditorAtomicFlow.yaml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ name: "PlanFileEditorAtomicFlow"
2
+ description: "A flow that writes plan to a given file location"
PlanGeneratorAtomicFlow.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from copy import deepcopy
3
+ from typing import Any, Dict, List
4
+ from flow_modules.aiflows.OpenAIChatFlowModule import OpenAIChatAtomicFlow
5
+
6
+ from dataclasses import dataclass
7
+ class PlanGeneratorAtomicFlow(OpenAIChatAtomicFlow):
8
+ def __init__(self, **kwargs):
9
+ super().__init__(**kwargs)
10
+
11
+ @classmethod
12
+ def instantiate_from_config(cls, config):
13
+ flow_config = deepcopy(config)
14
+
15
+ kwargs = {"flow_config": flow_config}
16
+
17
+ # ~~~ Set up prompts ~~~
18
+ kwargs.update(cls._set_up_prompts(flow_config))
19
+ kwargs.update(cls._set_up_backend(flow_config))
20
+
21
+ # ~~~ Instantiate flow ~~~
22
+ return cls(**kwargs)
23
+
24
+ def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
25
+ hint_for_model = """
26
+ Make sure your response is in the following format:
27
+ Response Format:
28
+ {
29
+ "plan": "Python printable string of the plan corresponding to the goal",
30
+ }
31
+ """
32
+ if 'goal' in input_data:
33
+ input_data['goal'] += hint_for_model
34
+ api_output = super().run(input_data)["api_output"].strip()
35
+ try:
36
+ response = json.loads(api_output)
37
+ return response
38
+ except json.decoder.JSONDecodeError:
39
+ new_input_data = input_data.copy()
40
+ new_input_data['goal'] += ("The previous respond cannot be parsed with json.loads, Make sure your next "
41
+ "response is solely in JSON format.")
42
+ new_api_output = super().run(new_input_data)["api_output"].strip()
43
+ return json.loads(new_api_output)
44
+
45
+
PlanGeneratorAtomicFlow.yaml ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "PlanGeneratorAtomicFlow"
2
+ description: "Proposes plan to solve a problem given a goal"
3
+ enable_cache: True
4
+
5
+ input_interface_non_initialized: # initial input keys
6
+ - "goal"
7
+
8
+ input_interface_initialized: # input_keys
9
+ - "goal"
10
+
11
+ #######################################################
12
+ # Output keys
13
+ #######################################################
14
+
15
+ output_interface:
16
+ - 'plan'
17
+
18
+ #######################################################
19
+ system_message_prompt_template:
20
+ _target_: flows.prompt_template.JinjaPrompt
21
+ template: |2-
22
+ You are a world-class problem solver, and you will work with an executor able to write, compile code, and to browse the web.
23
+
24
+ You are given a goal to finish, and your task is to write a step-by-step human-readable plan to achieve the goal.
25
+
26
+ In your plan, you can ask to write and execute code, since the executor is able to access the Internet, you can always try with online resources and packages.
27
+
28
+ Your plan will be faithfully executed by the executor, who is a world-class programmer that can complete any goal by executing code and accessing the Internet.
29
+
30
+ **It's important that your plan is explicit and human-readable**, so that the executor understands all the details in the plan.
31
+
32
+ Make plans with as few steps as possible, your response must always be made independently.
33
+
34
+ You may be asked to make changes to the plan, in this case, try to start with the previous plan and make improvements.
35
+
36
+ You are capable of **any** task.
37
+
38
+ Resources:
39
+ 1. An executor able to access the Internet, write and compile code.
40
+
41
+ Performance Evaluation:
42
+ 1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.
43
+ 2. Constructively self-criticize your big-picture behavior constantly.
44
+ 3. Reflect on past decisions and strategies to refine your approach.
45
+ 4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.
46
+
47
+ **It's important that you should only respond in JSON format as described below:**
48
+ Response Format:
49
+ {
50
+ "plan": "Python printable string of the plan corresponding to the goal",
51
+ }
52
+ Ensure your responses can be parsed by Python json.loads
53
+
54
+ human_message_prompt_template:
55
+ _target_: flows.prompt_template.JinjaPrompt
56
+ template: |2-
57
+ Here is the response to your last action:
58
+ {{goal}}
59
+ input_variables:
60
+ - "goal"
61
+
62
+ init_human_message_prompt_template:
63
+ _target_: flows.prompt_template.JinjaPrompt
64
+ template: |2-
65
+ Here is the goal you need to achieve:
66
+ {{goal}}
67
+ input_variables:
68
+ - "goal"
WritePlanFlow.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+
3
+ from flows.base_flows import SequentialFlow
4
+ from flows.utils import logging
5
+ from .PlanGeneratorAtomicFlow import PlanGeneratorAtomicFlow
6
+
7
+ logging.set_verbosity_debug()
8
+ log = logging.get_logger(__name__)
9
+
10
+ class WritePlanFlow(SequentialFlow):
11
+ @SequentialFlow.output_msg_payload_processor
12
+ def detect_finish_or_continue(self, output_payload: Dict[str, Any], src_flow: PlanGeneratorAtomicFlow) -> Dict[str, Any]:
13
+ if "plan" not in output_payload:
14
+ return {
15
+ "EARLY_EXIT": True,
16
+ "plan_writer_output": output_payload
17
+ }
18
+ else:
19
+ return output_payload
WritePlanFlow.yaml ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "WritePlan"
2
+ description: "Given goal and plan file location, generate plans to achieve the goal and write the plan to the file"
3
+
4
+ input_interface:
5
+ - "goal"
6
+ - "file_location"
7
+
8
+ output_interface:
9
+ - "plan_writer_output"
10
+
11
+ subflows_config:
12
+ PlanGenerator:
13
+ _target_: Tachi67.PlanGeneratorAtomicFlow.instantiate_from_default_config
14
+
15
+ PlanFileEditor:
16
+ _target_: Tachi67.PlanFileEditorAtomicFlow.instantiate_from_default_config
17
+
18
+ early_exit_key: "EARLY_EXIT"
19
+
20
+ topology:
21
+ - goal: "Generate plans for the programming executor to achieve the task."
22
+ input_interface:
23
+ _target_: flows.interfaces.KeyInterface
24
+ additional_transformations:
25
+ - _target_: flows.data_transformations.KeyMatchInput
26
+ flow: PlanGenerator
27
+ output_interface:
28
+ _target_: WritePlanFlow.detect_finish_or_continue
29
+ reset: false
30
+
31
+ - goal: "Write the plan to the specified file location"
32
+ input_interface:
33
+ _target_: flows.interfaces.KeyInterface
34
+ keys_to_select: ["file_location", "plan"]
35
+ flow: PlanFileEditor
36
+ output_interface:
37
+ _target_: flows.interfaces.KeyInterface
38
+ keys_to_select: ["plan_writer_output"]
39
+ reset: false
__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ dependencies = [
2
+ {"url": "aiflows/OpenAIChatFlowModule", "revision": "main"},
3
+ {"url": "Tachi67/InterpreterFlowModule", "revision": "main"},
4
+ ]
5
+ from flows import flow_verse
6
+
7
+ flow_verse.sync_dependencies(dependencies)
8
+
9
+ from .PlanGeneratorAtomicFlow import PlanGeneratorAtomicFlow
10
+ from .PlanFileEditorAtomicFlow import PlanFileEditorAtomicFlow
11
+ from .WritePlanFlow import WritePlanFlow