Upload 7 files
Browse files- CodeFileEditorAtomicFlow.py +42 -0
- CodeFileEditorAtomicFlow.yaml +10 -0
- CodeGeneratorAtomicFlow.py +48 -0
- CodeGeneratorAtomicFlow.yaml +68 -0
- WriteCodeFlow.py +12 -0
- WriteCodeFlow.yaml +45 -0
- __init__.py +11 -0
CodeFileEditorAtomicFlow.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
|
| 3 |
+
from flow_modules.Tachi67.InterpreterFlowModule import InterpreterAtomicFlow
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CodeFileEditorAtomicFlow(InterpreterAtomicFlow):
|
| 7 |
+
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.file_type = {
|
| 11 |
+
"python": "py",
|
| 12 |
+
"bash": "sh",
|
| 13 |
+
"r": "r",
|
| 14 |
+
"applescript": "applescript",
|
| 15 |
+
"html": "html",
|
| 16 |
+
"javascript": "js",
|
| 17 |
+
"shell": "sh",
|
| 18 |
+
"powershell": "ps1"
|
| 19 |
+
}
|
| 20 |
+
def _process_interperter_inputs(self, input_data: Dict[str, Any]):
|
| 21 |
+
language_of_code = input_data['language_of_code'].lower() # language of code to write
|
| 22 |
+
file_extension = self.file_type[language_of_code] # library.py, library.r, etc..
|
| 23 |
+
file_location = input_data['file_location']
|
| 24 |
+
code_to_write = input_data['code']
|
| 25 |
+
input_data['code'] = f"""
|
| 26 |
+
import os
|
| 27 |
+
file_location = {repr(file_location)}
|
| 28 |
+
code_to_write = {repr(code_to_write)}
|
| 29 |
+
if os.path.isdir(file_location):
|
| 30 |
+
file_location = os.path.join(file_location, 'library.{file_extension}')
|
| 31 |
+
with open(file_location, 'a') as file:
|
| 32 |
+
file.write('\\n' + code_to_write)
|
| 33 |
+
print('code written to' + file_location)"""
|
| 34 |
+
input_data['language'] = 'python' # will write the code string into library with python
|
| 35 |
+
|
| 36 |
+
def run(
|
| 37 |
+
self,
|
| 38 |
+
input_data: Dict[str, Any]):
|
| 39 |
+
self._process_interperter_inputs(input_data)
|
| 40 |
+
self._process_input_data(input_data)
|
| 41 |
+
response = self._call()
|
| 42 |
+
return {"code_writer_output": response}
|
CodeFileEditorAtomicFlow.yaml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: "CodeFileEditorAtomicFlow"
|
| 2 |
+
description: "A flow that writes code to a given file location"
|
| 3 |
+
|
| 4 |
+
input_interface:
|
| 5 |
+
- "file_location"
|
| 6 |
+
- "code"
|
| 7 |
+
- "language_of_code"
|
| 8 |
+
|
| 9 |
+
output_interface:
|
| 10 |
+
- "code_writer_output"
|
CodeGeneratorAtomicFlow.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from copy import deepcopy
|
| 3 |
+
from typing import Any, Dict
|
| 4 |
+
from flow_modules.aiflows.OpenAIChatFlowModule import OpenAIChatAtomicFlow
|
| 5 |
+
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class CodeGeneratorAtomicFlow(OpenAIChatAtomicFlow):
|
| 10 |
+
def __init__(self, **kwargs):
|
| 11 |
+
super().__init__(**kwargs)
|
| 12 |
+
|
| 13 |
+
@classmethod
|
| 14 |
+
def instantiate_from_config(cls, config):
|
| 15 |
+
flow_config = deepcopy(config)
|
| 16 |
+
|
| 17 |
+
kwargs = {"flow_config": flow_config}
|
| 18 |
+
|
| 19 |
+
# ~~~ Set up prompts ~~~
|
| 20 |
+
kwargs.update(cls._set_up_prompts(flow_config))
|
| 21 |
+
kwargs.update(cls._set_up_backend(flow_config))
|
| 22 |
+
|
| 23 |
+
# ~~~ Instantiate flow ~~~
|
| 24 |
+
return cls(**kwargs)
|
| 25 |
+
|
| 26 |
+
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 27 |
+
hint_for_model = """
|
| 28 |
+
Make sure your response is in the following format:
|
| 29 |
+
Response Format:
|
| 30 |
+
{
|
| 31 |
+
"language_of_code": "language of the code",
|
| 32 |
+
"code": "String of the code corresponding to the goal",
|
| 33 |
+
}
|
| 34 |
+
"""
|
| 35 |
+
if 'goal' in input_data:
|
| 36 |
+
input_data['goal'] += hint_for_model
|
| 37 |
+
api_output = super().run(input_data)["api_output"].strip()
|
| 38 |
+
try:
|
| 39 |
+
response = json.loads(api_output)
|
| 40 |
+
return response
|
| 41 |
+
except json.decoder.JSONDecodeError:
|
| 42 |
+
new_input_data = input_data.copy()
|
| 43 |
+
new_input_data['goal'] += ("The previous respond cannot be parsed with json.loads, it could be the "
|
| 44 |
+
"backslashes used for escaping single quotes in the string arguments of the "
|
| 45 |
+
"Python code are not properly escaped themselves within the JSON context. "
|
| 46 |
+
"Make sure your next response is in JSON format.")
|
| 47 |
+
new_api_output = super().run(new_input_data)["api_output"].strip()
|
| 48 |
+
return json.loads(new_api_output)
|
CodeGeneratorAtomicFlow.yaml
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: "CodeGeneratorAtomicFlow"
|
| 2 |
+
description: "Writes code 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 for the given goal, you do not need to worry abot the execution part.
|
| 28 |
+
|
| 29 |
+
You can access the Internet, using APIs 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 |
+
You are capable of **any** task.
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Performance Evaluation:
|
| 41 |
+
1. All code you write must be functions.
|
| 42 |
+
2. Function names must be self-explanatory, it's good practice to specify the return type.
|
| 43 |
+
3. Parameter names must be self-explanatory, it's good practice to specify parameter types.
|
| 44 |
+
4. All imports must be done within the function body.
|
| 45 |
+
|
| 46 |
+
**It's important that you should only respond in JSON format as described below:**
|
| 47 |
+
Response Format:
|
| 48 |
+
{
|
| 49 |
+
"language_of_code": "language of the code",
|
| 50 |
+
"code": "String of the code 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"
|
WriteCodeFlow.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
|
| 3 |
+
from flows.base_flows import SequentialFlow
|
| 4 |
+
from flows.base_flows import CircularFlow
|
| 5 |
+
from flows.utils import logging
|
| 6 |
+
from .CodeGeneratorAtomicFlow import CodeGeneratorAtomicFlow
|
| 7 |
+
|
| 8 |
+
logging.set_verbosity_debug()
|
| 9 |
+
log = logging.get_logger(__name__)
|
| 10 |
+
|
| 11 |
+
class WriteCodeFlow(SequentialFlow):
|
| 12 |
+
pass
|
WriteCodeFlow.yaml
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: "WriteCode"
|
| 2 |
+
description: "Given goal and code library location, generate code to achieve the goal and write the code to the file"
|
| 3 |
+
|
| 4 |
+
flow:
|
| 5 |
+
_target_: Tachi67.WriteCodeFlowModule.WriteCodeFlow.instantiate_from_default_config
|
| 6 |
+
|
| 7 |
+
input_interface:
|
| 8 |
+
- "goal"
|
| 9 |
+
- "file_location"
|
| 10 |
+
|
| 11 |
+
output_interface:
|
| 12 |
+
- "code_writer_output"
|
| 13 |
+
|
| 14 |
+
subflows_config:
|
| 15 |
+
CodeGenerator:
|
| 16 |
+
_target_: Tachi67.WriteCodeFlowModule.CodeGeneratorAtomicFlow.instantiate_from_default_config
|
| 17 |
+
backend:
|
| 18 |
+
api_infos: ???
|
| 19 |
+
model_name:
|
| 20 |
+
openai: gpt-4
|
| 21 |
+
azure: azure/gpt-4
|
| 22 |
+
|
| 23 |
+
CodeFileEditor:
|
| 24 |
+
_target_: Tachi67.WriteCodeFlowModule.CodeFileEditorAtomicFlow.instantiate_from_default_config
|
| 25 |
+
|
| 26 |
+
early_exit_key: "EARLY_EXIT"
|
| 27 |
+
|
| 28 |
+
topology:
|
| 29 |
+
- goal: "Generate code to achieve the task."
|
| 30 |
+
input_interface:
|
| 31 |
+
_target_: flows.interfaces.KeyInterface
|
| 32 |
+
additional_transformations:
|
| 33 |
+
- _target_: flows.data_transformations.KeyMatchInput
|
| 34 |
+
flow: CodeGenerator
|
| 35 |
+
reset: false
|
| 36 |
+
|
| 37 |
+
- goal: "Write the code to the specified file location"
|
| 38 |
+
input_interface:
|
| 39 |
+
_target_: flows.interfaces.KeyInterface
|
| 40 |
+
keys_to_select: ["file_location", "code", "language_of_code"]
|
| 41 |
+
flow: CodeFileEditor
|
| 42 |
+
output_interface:
|
| 43 |
+
_target_: flows.interfaces.KeyInterface
|
| 44 |
+
keys_to_select: ["code_writer_output"]
|
| 45 |
+
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 .CodeGeneratorAtomicFlow import CodeGeneratorAtomicFlow
|
| 10 |
+
from .CodeFileEditorAtomicFlow import CodeFileEditorAtomicFlow
|
| 11 |
+
from .WriteCodeFlow import WriteCodeFlow
|