Upload 7 files
Browse filesFiles changed (7)
- CodeWriterAskUserFlow.pyAdded+31
- CodeWriterAskUserFlow.yamlAdded+8
- CodeWriterCtrlFlow.pyAdded+84
- CodeWriterCtrlFlow.yamlAdded+111
- CodeWriterFlow.pyAdded+43
- CodeWriterFlow.yamlAdded+76
- __init__.pyAdded+14
12345678910111213141516171819202122232425262728293031from flow_modules.aiflows.HumanStandardInputFlowModule import HumanStandardInputFlow
from typing import Dict, Any
from flows.messages import UpdateMessage_Generic
from flows.utils import logging
log = logging.get_logger(f"flows.{__name__}")
class CodeWriterAskUserFlow(HumanStandardInputFlow): def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
query_message = self._get_message(self.query_message_prompt_template, input_data) state_update_message = UpdateMessage_Generic( created_by=self.flow_config['name'], updated_flow=self.flow_config["name"], data={"query_message": query_message}, ) self._log_message(state_update_message)
log.info(query_message) human_input = self._read_input()
response = {} response["human_feedback"] = human_input response["code"] = "no code was written"
return response12345678request_multi_line_input_flag: Falseend_of_input_string: EOI
query_message_prompt_template: template: |2- {{question}} input_variables: - "question"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import jsonfrom copy import deepcopyfrom typing import Any, Dict, List
from flow_modules.aiflows.ChatFlowModule import ChatAtomicFlow
from dataclasses import dataclass
@dataclassclass Command: name: str description: str input_args: List[str]
class CodeWriterCtrlFlow(ChatAtomicFlow): def __init__( self, commands: List[Command], **kwargs): super().__init__(**kwargs) self.system_message_prompt_template = self.system_message_prompt_template.partial( commands=self._build_commands_manual(commands), ) self.hint_for_model = """ Make sure your response is in the following format: Response Format: { "command": "call code writer, the tester, or to finish", "command_args": { "arg name": "value" } } """
@staticmethod def _build_commands_manual(commands: List[Command]) -> str: ret = "" for i, command in enumerate(commands): command_input_json_schema = json.dumps( {input_arg: f"YOUR_{input_arg.upper()}" for input_arg in command.input_args}) ret += f"{i + 1}. {command.name}: {command.description} Input arguments (given in the JSON schema): {command_input_json_schema}\n" return ret
@classmethod def instantiate_from_config(cls, config): flow_config = deepcopy(config)
kwargs = {"flow_config": flow_config}
# ~~~ Set up prompts ~~~ kwargs.update(cls._set_up_prompts(flow_config))
# ~~~Set up backend ~~~ kwargs.update(cls._set_up_backend(flow_config))
# ~~~ Set up commands ~~~ commands = flow_config["commands"] commands = [ Command(name, command_conf["description"], command_conf["input_args"]) for name, command_conf in commands.items() ] kwargs.update({"commands": commands})
# ~~~ Instantiate flow ~~~ return cls(**kwargs)
def _update_prompts_and_input(self, input_data: Dict[str, Any]): if 'goal' in input_data: input_data['goal'] += self.hint_for_model if 'feedback' in input_data: input_data['feedback'] += self.hint_for_model
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]: self._update_prompts_and_input(input_data) api_output = super().run(input_data)["api_output"].strip() try: response = json.loads(api_output) return response except json.decoder.JSONDecodeError: new_input_data = input_data.copy() new_input_data['feedback'] = "The previous respond cannot be parsed with json.loads. Make sure your next response is in JSON format." new_api_output = super().run(new_input_data)["api_output"].strip() return json.loads(new_api_output)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111name: "CodeWriterControllerFlow"description: "Proposes the next action to take towards achieving the goal, and prepares the input for the branching flow"enable_cache: True
######################################################## Input keys#######################################################
input_interface_non_initialized: # initial input keys - "goal"
input_interface_initialized: - "code" - "feedback"
######################################################## Output keys#######################################################
output_interface: - 'command' - 'command_args'
backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
commands: write_code: description: "Write code to finish the goal with user interaction" input_args: ["goal"] finish: description: "Signal that the objective has been satisfied, and returns the answer to the user." input_args: [] ask_user: description: "Ask user a question for confirmation or assistance" input_args: ["question"] test: description: "test the code generated from write_code" input_args: []
system_message_prompt_template: _target_: langchain.PromptTemplate template: |2- You are in charge of a department of writing code to solve a certain goal. You work with a coder, who does all the coding job; and a code tester, who does all the testing job. Your **ONLY** task is to take the user's goal for you, to decide whether to call the coder to write or re-write the code, to call the tester to test the code, or to finish the current task. When you need to call the code writer, call the `write_code` command with the goal specified. When you need to call the code tester, call the `test` command to test the code written. When the code is written and the user is satisfied, call the `finish` command to terminate the current process. Whenever you are in doubt, or need to confirm something to the user, call `ask_user` with the question. The coder will only write one function per goal, make sure you are not asking the coder to write more than one function. You **must not** write code yourself. You only decide whether to call the coder with specified goals or to finish. Your workflow: 1. Upon user request, call the `write_code` with the goal given. 2. The coder will write code, which is a function. The user will examine the code, and provide feedback. 3. Depending on the feedback of the user: 3.1. The user provides feedback on how to change the code, **call the coder with user's specific requirements again, to ask the coder to refine the code**. Go back to step 2. 3.2. The user does not provide details about refining the code, for example, just stating the fact that the user has updated the code, **this means the user is satisfied with the code written, call the `finish` command.** 4. If the user is satisfied with the code, call `test` to test the code 5. Depending on the result of the test: 5.1 Test passes, terminate the current process with the `finish command` 5.2 Test fails, **call the coder with details of the test results to instruct the coder to refine the code**, go back to step 2. If you have completed all your tasks, make sure to use the "finish" command. Constraints: 1. Exclusively use the commands listed in double quotes e.g. "command name" Your response **MUST** be in the following format: Response Format: { "command": "call code writer, the tester, or to finish", "command_args": { "arg name": "value" } } Ensure your responses can be parsed by Python json.loads Available Functions: {{commands}} input_variables: ["commands"] template_format: jinja2
human_message_prompt_template: _target_: flows.prompt_template.JinjaPrompt template: |2- Here is the code written by the coder, it might have been updated by the user, depending on the user's feedback: {{code}} Here is the feedback, depending on the last command you called, it either came from the user or the tester: {{feedback}} input_variables: - "code" - "feedback" template_format: jinja2
init_human_message_prompt_template: _target_: flows.prompt_template.JinjaPrompt template: |2- Here is the goal you need to achieve: {{goal}} input_variables: - "goal" template_format: jinja212345678910111213141516171819202122232425262728293031323334353637383940414243from typing import Dict, Anyimport os
from flow_modules.Tachi67.ContentWriterFlowModule import ContentWriterFlowfrom flows.base_flows import CircularFlow
class CodeWriterFlow(ContentWriterFlow): def _on_reach_max_round(self): self._state_update_dict({ "code": "The maximum amount of rounds was reached before the model generated the code.", "status": "unfinished" })
@CircularFlow.output_msg_payload_processor def detect_finish_or_continue(self, output_payload: Dict[str, Any], src_flow) -> Dict[str, Any]: command = output_payload["command"] if command == "finish": # ~~~ delete the temp code file ~~~ keys_to_fetch_from_state = ["temp_code_file_location", "code"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) temp_code_file_location = fetched_state["temp_code_file_location"] code_content = fetched_state["code"] if os.path.exists(temp_code_file_location): os.remove(temp_code_file_location) # ~~~ return the code content ~~~ return { "EARLY_EXIT": True, "code": code_content, "status": "finished" } elif command == "test": # ~~~ fetch code string from flow state ~~~ keys_to_fetch_from_state = ["code"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state)
# ~~~ add code content to the command args (branch input data) ~~~ code_content = fetched_state["code"] output_payload["command_args"]["code"] = code_content
return output_payload else: return output_payload12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576name: "CodeWriter"description: "Generates code with interactions with the user"
_target_: Tachi67.CodeWriterFlowModule.CodeWriterFlow.instantiate_from_default_config
output_interface: - "code" - "status"
### Subflows specificationsubflows_config: Controller: _target_: Tachi67.CodeWriterFlowModule.CodeWriterCtrlFlow.instantiate_from_default_config backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
Executor: _target_: flows.base_flows.BranchingFlow.instantiate_from_default_config subflows_config: write_code: _target_: Tachi67.InteractiveCodeGenFlowModule.InteractiveCodeGenFlow.instantiate_from_default_config subflows_config: MemoryReading: _target_: Tachi67.MemoryReadingFlowModule.MemoryReadingAtomicFlow.instantiate_from_default_config
CodeGenerator: _target_: Tachi67.CodeGeneratorFlowModule.CodeGeneratorAtomicFlow.instantiate_from_default_config backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
CodeFileEditor: _target_: Tachi67.CodeFileEditFlowModule.CodeFileEditAtomicFlow.instantiate_from_default_config
ParseFeedback: _target_: Tachi67.ParseFeedbackFlowModule.ParseFeedbackAtomicFlow.instantiate_from_default_config
ask_user: _target_: Tachi67.CodeWriterFlowModule.CodeWriterAskUserFlow.instantiate_from_default_config
test: _target_: Tachi67.TestCodeFlowModule.TestCodeFlow.instantiate_from_default_config
early_exit_key: "EARLY_EXIT"
topology: - goal: "Select the next action and prepare the input for the executor." input_interface: _target_: flows.interfaces.KeyInterface additional_transformations: - _target_: flows.data_transformations.KeyMatchInput flow: Controller output_interface: _target_: CodeWriterFlow.detect_finish_or_continue reset: false
- goal: "Execute the action specified by the Controller." input_interface: _target_: flows.interfaces.KeyInterface keys_to_rename: command: branch command_args: branch_input_data keys_to_select: ["branch", "branch_input_data"] flow: Executor output_interface: _target_: flows.interfaces.KeyInterface keys_to_rename: branch_output_data: observation keys_to_unpack: ["observation"] reset: false1234567891011121314dependencies = [ {"url": "Tachi67/ContentWriterFlowModule", "revision": "main"}, {"url": "Tachi67/InteractiveCodeGenFlowModule", "revision": "main"}, {"url": "Tachi67/TestCodeFlowModule", "revision": "main"}, {"url": "aiflows/ChatFlowModule", "revision": "a749ad10ed39776ba6721c37d0dc22af49ca0f17"}, {"url": "aiflows/HumanStandardInputFlowModule", "revision": "5683a922372c5fa90be9f6447d6662d8d80341fc"}]from flows import flow_verse
flow_verse.sync_dependencies(dependencies)
from .CodeWriterFlow import CodeWriterFlowfrom .CodeWriterCtrlFlow import CodeWriterCtrlFlowfrom .CodeWriterAskUserFlow import CodeWriterAskUserFlowCodeWriterAskUserFlow.pyAdded+31
CodeWriterAskUserFlow.yamlAdded+8
CodeWriterCtrlFlow.pyAdded+84
CodeWriterCtrlFlow.yamlAdded+111
CodeWriterFlow.pyAdded+43
CodeWriterFlow.yamlAdded+76
__init__.pyAdded+14