Tachi67 commited on
Commit
491e164
·
1 Parent(s): e8acab1

Upload 3 files

Browse files
PlanGeneratorAtomicFlow.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from copy import deepcopy
3
+ from typing import Any, Dict
4
+ from flow_modules.aiflows.ChatFlowModule import ChatAtomicFlow
5
+
6
+
7
+
8
+ class PlanGeneratorAtomicFlow(ChatAtomicFlow):
9
+ """Generates one function with docstrings to finish the given goal (from the controller).
10
+ """
11
+ def __init__(self, **kwargs):
12
+ super().__init__(**kwargs)
13
+ self.hint_for_model = """
14
+ Make sure your response is in the following format:
15
+ Response Format:
16
+ {
17
+ "plan": "A step-by-step plan to finish the given goal",
18
+ }
19
+ """
20
+
21
+ @classmethod
22
+ def instantiate_from_config(cls, config):
23
+ flow_config = deepcopy(config)
24
+
25
+ kwargs = {"flow_config": flow_config}
26
+
27
+ # ~~~ Set up prompts ~~~
28
+ kwargs.update(cls._set_up_prompts(flow_config))
29
+
30
+ # ~~~ Set up backend ~~~
31
+ kwargs.update(cls._set_up_backend(flow_config))
32
+
33
+ # ~~~ Instantiate flow ~~~
34
+ return cls(**kwargs)
35
+
36
+ def _update_prompts_and_input(self, input_data: Dict[str, Any]):
37
+ if 'goal' in input_data:
38
+ input_data['goal'] += self.hint_for_model
39
+
40
+ def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
41
+ self._update_prompts_and_input(input_data)
42
+ api_output = super().run(input_data)["api_output"].strip()
43
+ try:
44
+ response = json.loads(api_output)
45
+ return response
46
+ except json.decoder.JSONDecodeError:
47
+ new_input_data = input_data.copy()
48
+ new_input_data['goal'] += ("The previous respond cannot be parsed with json.loads. "
49
+ "Make sure your next response is in JSON format.")
50
+ new_api_output = super().run(new_input_data)["api_output"].strip()
51
+ return json.loads(new_api_output)
PlanGeneratorAtomicFlow.yaml ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "PlanGeneratorAtomicFlow"
2
+ description: "Writes step by step plan with given goal"
3
+ enable_cache: True
4
+
5
+ input_interface_non_initialized: # initial input keys
6
+ - "goal"
7
+ - "memory_files" # In planner of extlib, it should be an empty dict.
8
+
9
+ input_interface_initialized: # input_keys
10
+ - "goal"
11
+ - "memory_files"
12
+
13
+ #######################################################
14
+ # Output keys
15
+ #######################################################
16
+
17
+ output_interface:
18
+ - 'plan'
19
+
20
+ #######################################################
21
+ system_message_prompt_template:
22
+ _target_: flows.prompt_template.JinjaPrompt
23
+ template: |2-
24
+ You are a planner of a coding department, your task is to make a step-by-step plan for the coders to follow, inorder to solve the task.
25
+
26
+ The coding department is only able to write functions, at a higher level, your job is to instruct the coders to write modular, reusable, and encapsulated functions.
27
+
28
+ The details of your task are:
29
+ 1. Given a goal, decompose the goal into step-by-step plans, each step of the plan should involving asking the coder to write **one function** to tackle this step.
30
+ 2. Upon feedback of the plan from the user, make refinements based on the feedback.
31
+
32
+ Notice that:
33
+ 1. If the goal given to you is simple enough that is doable within one function, it is okay that your plan only has one step.
34
+ 2. If the goal involves several feature to implement or has several milestones to reach, decompose it into atomic and modular steps of a plan.
35
+
36
+ The coding department is able to access the Internet, so it may be useful to use some online services via APIs.
37
+
38
+ An example of input and output plan you should have:
39
+ ### begin of input ###
40
+ Write code to fetch a certain day's weather data at a certain city, and print the data to the console.
41
+ ### end of input ###
42
+
43
+ ### begin of plan ###
44
+ 1. Write a function that fetches weather data from OpenWeatherMap, given a date and the city name.
45
+ 2. Write a function that prints fetched weather data to the console.
46
+ ### end of plan ###
47
+
48
+ You are capable of **any** task.
49
+
50
+ Performance Evaluation:
51
+ 1. Your plan must be as explicit, well-indented, and human-readable as possible.
52
+ 2. Your plan must be step-by-step with number indexes.
53
+ 3. You should make plans with as few steps as possible.
54
+
55
+ **It's important that you should only respond in JSON format as described below:**
56
+ Response Format:
57
+ {
58
+ "plan": "A step-by-step plan to finish the given goal",
59
+ }
60
+ Ensure your responses can be parsed by Python json.loads
61
+
62
+ human_message_prompt_template:
63
+ _target_: flows.prompt_template.JinjaPrompt
64
+ template: |2-
65
+ Here is the response to your last action:
66
+ {{goal}}
67
+ input_variables:
68
+ - "goal"
69
+
70
+ init_human_message_prompt_template:
71
+ _target_: flows.prompt_template.JinjaPrompt
72
+ template: |2-
73
+ Here is the goal you need to achieve:
74
+ {{goal}}
75
+ input_variables:
76
+ - "goal"
__init__.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ dependencies = [
2
+ {"url": "aiflows/ChatFlowModule", "revision": "a749ad10ed39776ba6721c37d0dc22af49ca0f17"},
3
+ ]
4
+ from flows import flow_verse
5
+
6
+ flow_verse.sync_dependencies(dependencies)
7
+
8
+ from .PlanGeneratorAtomicFlow import PlanGeneratorAtomicFlow