Upload 3 files
Browse files- CodeGeneratorAtomicFlow.py +53 -0
- CodeGeneratorAtomicFlow.yaml +84 -0
- __init__.py +8 -0
CodeGeneratorAtomicFlow.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 CodeGeneratorAtomicFlow(ChatAtomicFlow):
|
9 |
+
"""Generates code and docstrings with given goal (from controller)"""
|
10 |
+
def __init__(self, **kwargs):
|
11 |
+
super().__init__(**kwargs)
|
12 |
+
self.hint_for_model = """
|
13 |
+
Make sure your response is in the following format:
|
14 |
+
Response Format:
|
15 |
+
{
|
16 |
+
"language_of_code": "language of the code",
|
17 |
+
"code": "String of the code and docstrings corresponding to the 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_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_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, it could be the "
|
49 |
+
"backslashes used for escaping single quotes in the string arguments of the "
|
50 |
+
"Python code are not properly escaped themselves within the JSON context. "
|
51 |
+
"Make sure your next response is in JSON format.")
|
52 |
+
new_api_output = super().run(new_input_data)["api_output"].strip()
|
53 |
+
return json.loads(new_api_output)
|
CodeGeneratorAtomicFlow.yaml
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: "CodeGeneratorAtomicFlow"
|
2 |
+
description: "Writes code with given instruction"
|
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 |
+
- 'language_of_code'
|
17 |
+
- 'code'
|
18 |
+
|
19 |
+
#######################################################
|
20 |
+
system_message_prompt_template:
|
21 |
+
_target_: flows.prompt_template.JinjaPrompt
|
22 |
+
template: |2-
|
23 |
+
You are a world class programmer that can complete any goal with code. All code you write should be functions.
|
24 |
+
|
25 |
+
Your function will then be imported and called by an executor to finish the goal.
|
26 |
+
|
27 |
+
Your task is to write functions and their respective docstrings for the given goal, you do not need to worry abot the execution part.
|
28 |
+
|
29 |
+
You can access the Internet, using APIs of online services could be useful.
|
30 |
+
|
31 |
+
**It's important that all code you write must be functions to achieve certain goals.**
|
32 |
+
|
33 |
+
**It's important that you make imports within functions.**
|
34 |
+
|
35 |
+
**Make sure the function names, parameter names are self-explanatory.**
|
36 |
+
|
37 |
+
**When you write a function, also write its docstring right after the function name, so that the user can understand the function.**
|
38 |
+
|
39 |
+
An example of a function you write:
|
40 |
+
### begin of example ###
|
41 |
+
def verify_path_exists(path: str) -> bool:
|
42 |
+
"""Verifies if the input path exists, returns True if path exists, otherwise return False"""
|
43 |
+
import os
|
44 |
+
return os.path.exists(path)
|
45 |
+
### end of example ###
|
46 |
+
|
47 |
+
You are capable of **any** task.
|
48 |
+
|
49 |
+
|
50 |
+
Performance Evaluation:
|
51 |
+
1. The priority language is Python, try to code with Python to solve the task.
|
52 |
+
2. All code you write must be functions.
|
53 |
+
3. Function names must be self-explanatory, it's good practice to specify the return type.
|
54 |
+
4. Parameter names must be self-explanatory, it's good practice to specify parameter types.
|
55 |
+
5. All imports must be done within the function body.
|
56 |
+
6. All functions must have docstrings.
|
57 |
+
|
58 |
+
**It's important that you should only respond in JSON format as described below:**
|
59 |
+
Response Format:
|
60 |
+
{
|
61 |
+
"language_of_code": "language of the code",
|
62 |
+
"code": "String of the code and docstrings corresponding to the goal",
|
63 |
+
}
|
64 |
+
Ensure your responses can be parsed by Python json.loads
|
65 |
+
|
66 |
+
**It's important that you make imports inside of the function body, but not outside of it.**
|
67 |
+
|
68 |
+
**It's important that the code you generate can be written by Python write, and is human-readable. The written file must also be indented and formatted, so that it is human-readable.**
|
69 |
+
|
70 |
+
human_message_prompt_template:
|
71 |
+
_target_: flows.prompt_template.JinjaPrompt
|
72 |
+
template: |2-
|
73 |
+
Here is the response to your last action:
|
74 |
+
{{goal}}
|
75 |
+
input_variables:
|
76 |
+
- "goal"
|
77 |
+
|
78 |
+
init_human_message_prompt_template:
|
79 |
+
_target_: flows.prompt_template.JinjaPrompt
|
80 |
+
template: |2-
|
81 |
+
Here is the goal you need to achieve:
|
82 |
+
{{goal}}
|
83 |
+
input_variables:
|
84 |
+
- "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 .CodeGeneratorAtomicFlow import CodeGeneratorAtomicFlow
|