Upload 14 files
Browse filesFiles changed (14)
- CoderFlow.pyAdded+54
- CoderFlow.yamlAdded+134
- Controller_CoderFlow.pyAdded+131
- Controller_CoderFlow.yamlAdded+167
- CtrlExMem_CoderFlow.pyAdded+67
- CtrlExMem_CoderFlow.yamlAdded+173
- Planner_CoderFlow.pyAdded+65
- Planner_CoderFlow.yamlAdded+138
- README.md+248-3
- UpdatePlanAtomicFlow.pyAdded+40
- UpdatePlanAtomicFlow.yamlAdded+9
- __init__.pyAdded+19
- pip_requirements.txtAdded+15
- run_coder.pyAdded+106
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354from typing import Dict, Anyfrom aiflows.utils import logginglog = logging.get_logger(__name__)
from flow_modules.aiflows.AbstractBossFlowModule import AbstractBossFlow
class CoderFlow(AbstractBossFlow): """Coder flow is one executor branch of the Jarvis flow. At a higher level, it is a flow that writes and runs code given a goal. In the Jarvis flow, the Coder flow in invoked by the controller, The Coder flow receives the goal generated by the controller, writes and runs code in an interactive fashion.
The Coder flow has the similar structure as the Jarvis flow (inherited from AbstractBossFlow).
*Input Interface (expected input)* - `goal` (str): The goal from the caller (source flow, i.e. JarvisFlow)
*Output Interface (expected output)* - `result` (str): The result of the flow, the result will be returned to the caller (i.e. JarvisFlow). - `summary` (str): The summary of the flow, the summary will be logged into the logs of the caller flow (i.e. JarvisFlow).
Typical workflow of Coder: 0. JarvisFlow calls Coder with a goal. 1. MemoryReading reads plans, logs and code library. 2. Planner makes plan based on goal. 3. Extend library with the goal given by the controller. 4. Run code with code (possibly calls the newly written function) given by the controller. 5. Finish and give an answer. """ def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]: """The run function of the Coder flow. :param input_data: The input data of the flow. :type input_data: Dict[str, Any] :return: The output data of the flow. :rtype: Dict[str, Any] """ # ~~~ sets the input_data in the flow_state dict ~~~ self._state_update_dict(update_data=input_data)
# ~~~ set the memory file to the flow state ~~~ self._state_update_dict(update_data={"memory_files": self.memory_files})
max_rounds = self.flow_config.get("max_rounds", 1) if max_rounds is None: log.info(f"Running {self.flow_config['name']} without `max_rounds` until the early exit condition is met.")
self._sequential_run(max_rounds=max_rounds)
output = self._get_output_from_state()
self.reset(full_reset=True, recursive=True, src_flow=self)
return output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134name: CoderFlowdescription: "A flow used to tackle goals with coding in a step-by-step fashion"
_target_: flow_modules.aiflows.CoderFlowModule.CoderFlow.instantiate_from_default_config
memory_files: ???
subflows_config: MemoryReading: _target_: flow_modules.aiflows.MemoryReadingFlowModule.MemoryReadingAtomicFlow.instantiate_from_default_config output_interface: - "plan" - "logs" - "code_library"
Planner: _target_: flow_modules.aiflows.CoderFlowModule.Planner_CoderFlow.instantiate_from_default_config subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
CtrlExMem: _target_: flow_modules.aiflows.CoderFlowModule.CtrlExMem_CoderFlow.instantiate_from_default_config subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: extend_library: memory_files: ??? subflows_config: Planner: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 CtrlExMem: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_code: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_code: memory_files: ??? subflows_config: CodeGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 test: memory_files: ??? re_plan: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 re_plan: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131import 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 Controller_CoderFlow(ChatAtomicFlow): """Refer to: https://huggingface.co/Tachi67/JarvisFlowModule/blob/main/Controller_JarvisFlow.py""" 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), plan="no plans yet", plan_file_location="no location yet", code_library="no code library yet", code_library_location="no location yet", logs="no logs yet", ) self.hint_for_model = """ Make sure your response is in the following format: Response Format: { "command": "call one of the subordinates", "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
def _get_content_file_location(self, input_data, content_name): # get the location of the file that contains the content: plan, logs, code_library assert "memory_files" in input_data, "memory_files not passed to Coder/Controller" assert content_name in input_data["memory_files"], f"{content_name} not in memory files" return input_data["memory_files"][content_name]
def _get_content(self, input_data, content_name): # get the content of the file that contains the content: plan, logs, code_library assert content_name in input_data, f"{content_name} not passed to Coder/Controller" content = input_data[content_name] if len(content) == 0: content = f'No {content_name} yet' return content
@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 'result' in input_data: input_data['result'] += self.hint_for_model plan_file_location = self._get_content_file_location(input_data, "plan") plan_content = self._get_content(input_data, "plan") code_library_location = self._get_content_file_location(input_data, "code_library") code_library_content = self._get_content(input_data, "code_library") logs_content = self._get_content(input_data, "logs") self.system_message_prompt_template = self.system_message_prompt_template.partial( plan_file_location=plan_file_location, plan=plan_content, code_library_location=code_library_location, code_library=code_library_content, logs=logs_content )
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]: self._update_prompts_and_input(input_data)
# ~~~when conversation is initialized, append the updated system prompts to the chat history ~~~ if self._is_conversation_initialized(): updated_system_message_content = self._get_message(self.system_message_prompt_template, input_data) self._state_update_add_chat_message(content=updated_system_message_content, role=self.flow_config["system_name"])
while True: api_output = super().run(input_data)["api_output"].strip() try: start = api_output.index("{") end = api_output.rindex("}") + 1 json_str = api_output[start:end] return json.loads(json_str) except (ValueError, json.decoder.JSONDecodeError, json.JSONDecodeError): updated_system_message_content = self._get_message(self.system_message_prompt_template, input_data) self._state_update_add_chat_message(content=updated_system_message_content, role=self.flow_config["system_name"]) new_goal = "The previous respond cannot be parsed with json.loads. Next time, do not provide any comments or code blocks. Make sure your next response is purely json parsable." new_input_data = input_data.copy() new_input_data['result'] = new_goal input_data = new_input_data
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167_target_: flow_modules.aiflows.CoderFlowModule.Controller_CoderFlow.instantiate_from_default_configname: "ControllerFlow_Coder"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" - "memory_files" - "plan" - "logs" - "code_library"
input_interface_initialized: - "goal" - "result" - "memory_files" - "plan" - "logs" - "code_library"
######################################################## Output keys#######################################################
output_interface: - 'command' - 'command_args'
backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
commands: extend_library: description: "Given goal, write functions to achieve the goal, and then extend the library with the functions" input_args: [ "goal" ] re_plan: description: "When something is wrong with current plan, draft another plan based on the old plan and information about why it's bad or how to refine it." input_args: [ "goal" ] finish: description: "Signal that the objective has been satisfied, return the summary of what was done" input_args: [ "summary" ] manual_finish: description: "The user demands to quit and terminate the current process" input_args: [ ] ask_user: description: "Ask user a question for confirmation or assistance" input_args: [ "question" ] update_plan: description: "When one step of the plan is done, pass the updated plan to edit plan file and override current plan" input_args: [ "updated_plan" ] run_code: description: "Run a code script written by yourself, e.g. downloading a package to the current environment, importing and calling a function from the code library" input_args: [ "code", "language" ]
system_message_prompt_template: _target_: langchain.PromptTemplate template: |2- Your department is in charge of achieving goals with writing and executing code. You are the leader of this department. Here is the goal you need to achieve: {{goal}} You have a code library located at {{code_library_location}}, which contains functions that are already written and saved by your subordinates. Here are the functions in the library: {{code_library}} You work with several subordinates, they will be in charge of the specific tasks, including extending the code library, running a code script (you need to write the code script yourself, e.g. importing and calling a function from the code library), re-planning, etc. To call one of the subordinates, you need to call the corresponding command with necessary arguments, here are the commands and their descriptions: {{commands}} You are given a step-by-step plan to finish the goal, **make sure you follow the plan**, **notice that, if you are informed that the plan is overriden, this plan is the new plan you should stick to.**, the plan is located in a plan file {{plan_file_location}}: {{plan}} You should execute the plan step-by-step, for each step, you should do the following workflows: 0.1 Whenever the user demands to quit or terminate the current process, call `manual_finish` command. 0.2 Whenever in doubt, or you have something to ask, or confirm to the user, call `ask_user` with your question. 0.3 During the execution of the plan, if something goes wrong, call the `re_plan` with detailed information about what was wrong. 1. If the current step of plan asks to extend the library, call `extend_library` with the goal of the current step of plan. 2. In one of the following situations, call `run_code` with the code script you write: 2.1. The current step of plan does not require to extend the library, but requires to run a code script. 2.2. The current step of plan requires to import and run some functions from the library. In this case you **MUST** make sure you import the library, something like `from LIBRARY_NAME import FUNCTION_NAME` should suffice. 2.3. You need to download a package to the current environment, e.g. `pip install PACKAGE_NAME`, in this case the language of code should be `shell`. After the code is run, the user will provide feedback about the execution, react to the feedback accordingly. 3. Whenever you have done one step of the plan, **it could be either extending the library or running the code**, do the following: 3.1 Reflect on what plan you are having right now. 3.2 Reflect on which step of the plan you just finished. 3.3 Generate a plan, **it is exactly the same as the plan you have now, but with the current step of plan marked as done** 3.4 Call `update_plan` with the plan you just generated. 4. Proceed to the next step of the plan, go back to workflow 1 or 2. 5. When every step of the plan is done, call the `finish` command, with a summary of what was done.
**You MUST call `update_plan` whenever you realize one step of the plan, for example, extending the library or running the code is done.** Here is an example of execution: ### Beginning of an example execution: ### Plan: 1. Extend the library with a function that reads the stock prices from yahoo finance for the last `k` months for a company given its `company_code` 2. Call this function for `company_code`=TSLA and `k`=6. You start from step 1 of the plan, execute workflow 1, the extend_library command is called with the goal of step 1 of the plan. You are informed of what functions are written and saved to the library. You realize that step 1 is done, you go to workflow 3 to update the plan: 3.1 You recall the plan you have. 3.2 You realize step 1 of the plan is done. 3.3 You generate the updated plan: 1. Extend the library with a function that reads the stock prices for the last k months for a company given its `company_code` (DONE) 2. Call this function for `company_code`=TSLA and k=6. 3.4 You call `update_plan` with the generated plan. The updated plan will override the existing plan, you are informed of the behaviour of override. You enter step 2 of the plan. You go to workflow 2 to run a code script, you write a code script that imports the function you just wrote and call it with the arguments specified in the plan. You are informed that package yahoo finance is not installed, the user instructs to install the yahoo finance package, you go to workflow 2 to run a code script, you write a code script that installs the package. You are informed that the package is installed, you go to workflow 2 to run a code script, you write a code script that imports the function you just wrote and call it with the arguments specified in the plan. You are informed that the code has been successfully ran, the user is satisfied with the result. You realize that step 2 is done, you go to workflow 3 to update the plan: 3.1 You recall the plan you have. 3.2 You realize step 2 of the plan is done. 3.3 You generate the updated plan: 1. Extend the library with a function that reads the stock prices for the last k months for a company given its `company_code` (DONE) 2. Call this function for `company_code`=TSLA and k=6. (DONE) 3.4 You call `update_plan` with the generated plan. The updated plan will override the existing plan, you are informed of the behaviour of override. All steps are done, you call `finish`, with a summary of what functions are written and saved to the library. ### End of an example of execution ### Here is a list of history actions you have taken, for your reference: {{logs}} 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 one of the subordinates", "command_args": { "arg name": "value" } } Ensure your responses can be parsed by Python json.loads
input_variables: ["commands", "plan_file_location", "plan", "logs", "code_library_location", "code_library", "goal"] template_format: jinja2
human_message_prompt_template: _target_: aiflows.prompt_template.JinjaPrompt template: |2- Here is the result of your previous action: {{result}} input_variables: - "result" template_format: jinja2
init_human_message_prompt_template: _target_: aiflows.prompt_template.JinjaPrompt template: |2- Here is the goal you need to achieve, follow your plan to finish the goal: {{goal}} input_variables: - "goal" template_format: jinja2
previous_messages: last_k: 312345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667from typing import Dict, Any
from flow_modules.aiflows.AbstractBossFlowModule import CtrlExMemFlowfrom aiflows.base_flows import CircularFlow
class CtrlExMem_CoderFlow(CtrlExMemFlow): """This class inherits from the CtrlExMemFlow class from AbstractBossFlowModule. See: https://huggingface.co/Tachi67/AbstractBossFlowModule/blob/main/CtrlExMemFlow.py
*Input Interface*: - `plan` - `logs` - `code_library`: the signatures and docstring of the functions in the code library. - `memory_files` - `goal`
*Output Interface* - `result` (str): The result of the flow, the result will be returned to the caller. - `summary` (str): The summary of the flow, the summary will be logged into the logs of the caller flow. """ def _on_reach_max_round(self): self._state_update_dict({ "result": "the maximum amount of rounds was reached before the coder flow has done the job", "summary": "CoderFlow: the maximum amount of rounds was reached before the flow has done the job", "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": return { "EARLY_EXIT": True, "result": output_payload["command_args"]["summary"], "summary": "Coder: " + output_payload["command_args"]["summary"], "status": "finished" } elif command == "manual_finish": # ~~~ return the manual quit status ~~~ return { "EARLY_EXIT": True, "result": "CoderFlow was terminated explicitly by the user, process is unfinished", "summary": "Coder: process terminated by the user explicitly, nothing generated", "status": "unfinished" } elif command == "update_plan": keys_to_fetch_from_state = ["memory_files"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) output_payload["command_args"]["memory_files"] = fetched_state["memory_files"] return output_payload
elif command == "re_plan": keys_to_fetch_from_state = ["plan", "memory_files"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) output_payload["command_args"]["plan_file_location"] = fetched_state["memory_files"]["plan"] output_payload["command_args"]["plan"] = fetched_state["plan"] return output_payload elif command == "run_code": keys_to_fetch_from_state = ["memory_files"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) output_payload["command_args"]["memory_files"] = fetched_state["memory_files"] return output_payload
else: return output_payload123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173_target_: flow_modules.aiflows.CoderFlowModule.CtrlExMem_CoderFlow.instantiate_from_default_configname: "CtrlExMem_CoderFlow"description: "MemorizedControllerExecutor flow for Coder flow"
input_interface: - "plan" - "logs" - "code_library" - "memory_files" - "goal"
subflows_config: Controller: _target_: flow_modules.aiflows.CoderFlowModule.Controller_CoderFlow.instantiate_from_default_config backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: _target_: aiflows.base_flows.BranchingFlow.instantiate_from_default_config subflows_config: extend_library: _target_: flow_modules.aiflows.ExtendLibraryFlowModule.ExtendLibraryFlow.instantiate_from_default_config memory_files: ??? subflows_config: Planner: _target_: flow_modules.aiflows.PlanWriterFlowModule.PlanWriterFlow.instantiate_from_default_config subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 CtrlExMem: _target_: flow_modules.aiflows.ExtendLibraryFlowModule.CtrlExMem_ExtLib.instantiate_from_default_config subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_code: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_code: memory_files: ??? subflows_config: CodeGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 test: memory_files: ??? re_plan: subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 ask_user: _target_: flow_modules.aiflows.ExtendLibraryFlowModule.ExtLibAskUserFlow.instantiate_from_default_config re_plan: _target_: flow_modules.aiflows.ReplanningFlowModule.ReplanningFlow.instantiate_from_default_config subflows_config: Controller: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 Executor: subflows_config: write_plan: subflows_config: PlanGenerator: backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 run_code: _target_: flow_modules.aiflows.RunCodeFlowModule.RunCodeFlow.instantiate_from_default_config update_plan: _target_: flow_modules.aiflows.CoderFlowModule.UpdatePlanAtomicFlow.instantiate_from_default_config MemoryReading: _target_: flow_modules.aiflows.MemoryReadingFlowModule.MemoryReadingAtomicFlow.instantiate_from_default_config output_interface: - "plan" - "logs" - "code_library"
topology: - goal: "Select the next action and prepare the input for the executor." input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: Controller output_interface: _target_: CtrlExMem_CoderFlow.detect_finish_or_continue reset: false
- goal: "Execute the action specified by the Controller." input_interface: _target_: aiflows.interfaces.KeyInterface keys_to_rename: command: branch command_args: branch_input_data keys_to_select: ["branch", "branch_input_data"] flow: Executor output_interface: _target_: aiflows.interfaces.KeyInterface keys_to_rename: branch_output_data.code_ran: code_ran branch_output_data.interpreter_output: interpreter_output branch_output_data.plan: plan branch_output_data.status: status branch_output_data.summary: summary branch_output_data.result: result keys_to_delete: ["branch_output_data"] reset: false
- goal: "Write memory to memory files" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: MemoryWriting reset: false
- goal: "Read memory from memory files (flow_state)" input_interface: _target_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: MemoryReading reset: false1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465from typing import Dict, Anyimport osfrom flow_modules.aiflows.PlanWriterFlowModule import PlanWriterFlowfrom aiflows.base_flows import CircularFlow
class Planner_CoderFlow(PlanWriterFlow): """Planner of the coder flow, it inherits from PlanWriterFlow, see: https://huggingface.co/Tachi67/PlanWriterFlowModule """ def _on_reach_max_round(self): self._state_update_dict({ "plan": "The maximum amount of rounds was reached before the model generated the plan.", "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": # ~~~ fetch temp file location, plan content, memory file (of upper level flow e.g. ExtLib) from flow state keys_to_fetch_from_state = ["temp_plan_file_location", "plan", "memory_files"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) temp_plan_file_location = fetched_state["temp_plan_file_location"] plan_content = fetched_state["plan"] plan_file_location = fetched_state["memory_files"]["plan"]
# ~~~ delete the temp plan file ~~~ if os.path.exists(temp_plan_file_location): os.remove(temp_plan_file_location)
# ~~~ write plan content to plan file ~~~ with open(plan_file_location, 'w') as file: file.write(plan_content)
# ~~~ return the plan content ~~~ return { "EARLY_EXIT": True, "plan": plan_content, "summary": "Coder/PlanWriter: " + output_payload["command_args"]["summary"], "status": "finished" } elif command == "manual_finish": # ~~~ delete the temp plan file ~~~ keys_to_fetch_from_state = ["temp_plan_file_location"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) temp_plan_file_location = fetched_state["temp_plan_file_location"] if os.path.exists(temp_plan_file_location): os.remove(temp_plan_file_location) # ~~~ return the manual quit status ~~~ return { "EARLY_EXIT": True, "plan": "no plan was generated", "summary": "Coder/PlanWriter: PlanWriter was terminated explicitly by the user, process is unfinished", "status": "unfinished" } elif command == "write_plan": keys_to_fetch_from_state = ["memory_files", "code_library"] fetched_state = self._fetch_state_attributes_by_keys(keys=keys_to_fetch_from_state) output_payload["command_args"]["plan_file_location"] = fetched_state["memory_files"]["plan"] output_payload["command_args"]["code_library_location"] = fetched_state["memory_files"]["code_library"] output_payload["command_args"]["code_library"] = fetched_state["code_library"] return output_payload else: return output_payload123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138name: "Planner/Coder"description: "Generates plan with interactions with the user, used for the coder class"
_target_: flow_modules.aiflows.CoderFlowModule.Planner_CoderFlow.instantiate_from_default_config
input_interface: - "goal" - "code_library" - "memory_files"
output_interface: - "plan" - "status" - "summary"
### Subflows specificationsubflows_config: Controller: _target_: flow_modules.aiflows.PlanWriterFlowModule.PlanWriterCtrlFlow.instantiate_from_default_config backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4
Executor: _target_: aiflows.base_flows.BranchingFlow.instantiate_from_default_config subflows_config: write_plan: _target_: flow_modules.aiflows.InteractivePlanGenFlowModule.InteractivePlanGenFlow.instantiate_from_default_config input_interface: - "goal" - "plan_file_location" - "code_library_location" - "code_library" subflows_config: PlanGenerator: _target_: flow_modules.aiflows.PlanGeneratorFlowModule.PlanGeneratorAtomicFlow.instantiate_from_default_config backend: api_infos: ??? model_name: openai: gpt-4 azure: azure/gpt-4 input_interface_non_initialized: - "goal" - "code_library" - "code_library_location" input_interface_initialized: - "goal" - "code_library" - "code_library_location" system_message_prompt_template: _target_: aiflows.prompt_template.JinjaPrompt template: |2- You are a planner for a coding department, you write step-by-step plans for the coders to follow and execute.
You and the coders work with a code library located at {{code_library_location}}, **it is important that when you make plans, make the best use of the functions in the library.** Here are the contents in the library: {{code_library}}
The coders are able to read and write the code library, namely, they can import and use the functions in the library, and they are also able to extend the library.
Apart from reading and writing code, the coders are also able to execute a code script written by themselves.
**You task is: given a goal to achieve, write step-by-step plans for the coders, each step of your plan should involve one of the three actions: 1. Extend the code library with a function, describe the purpose, input and output of this function so that the coders understand what you mean and write appropriate functions. 2. Run a code script, for example, downloading a package after you realize the current environment lacks it; import and call one function from the code library with specific inputs. 3. Give a final answer**
Here are some criteria you should consider in order to write a good plan: 1. The plan should be nicely separated into steps, each step should involve extending the library, or running code, or giving a final answer. 2. Always try to use the functions already existent in the code library. To use them, instruct the coders to import and run the function from the code library. 3. If the current milestone cannot be solved by the functions in the library, generalize the problem and instruct the coders to extend the library with a function to deal with this problem. For example, suppose the goal you need to achieve is "Download Google's stock prices from the last 6 months from Yahoo finance", there is no function available to solve this in the library. In this case, your step of plan is: "Extend the library with a function that reads the stock prices for the last k months for a company given its `company_code`" 4. On top of criterion 3, when you instruct the coders to extend the library, make sure that: 4.1. Each time you instruct to extend the library, ask for only one function. 4.2. If the problem you generalize involves several separated steps and should not be done in one function, decompose the problem into several sub-problems and instruct the coders to extend the library with a function for each sub-problem, this should be done in several steps of plan.
Performance Evaluation: 1. Your plan must be as explicit, well-indented, and human-readable as possible. 2. Your plan must be step-by-step with number indexes, each step involves extending the library, or running code, or answering. 3. You should make plans with as few steps as possible.
**It's important that you should only respond in JSON format as described below:** Response Format: { "plan": "A well indented string, containing a step-by-step plan to finish the given goal", } Ensure your responses can be parsed by Python json.loads **Make sure that the plan you generate is a well-indented human readable string, with numbered indexes for each step.** input_variables: - "code_library_location" - "code_library"
PlanFileEditor: _target_: flow_modules.aiflows.PlanFileEditFlowModule.PlanFileEditAtomicFlow.instantiate_from_default_config
ParseFeedback: _target_: flow_modules.aiflows.ParseFeedbackFlowModule.ParseFeedbackAtomicFlow.instantiate_from_default_config input_interface: - "temp_plan_file_location" output_interface: - "plan" - "feedback"
ask_user: _target_: flow_modules.aiflows.PlanWriterFlowModule.PlanWriterAskUserFlow.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_: aiflows.interfaces.KeyInterface additional_transformations: - _target_: aiflows.data_transformations.KeyMatchInput flow: Controller output_interface: _target_: Planner_CoderFlow.detect_finish_or_continue reset: false
- goal: "Execute the action specified by the Controller." input_interface: _target_: aiflows.interfaces.KeyInterface keys_to_rename: command: branch command_args: branch_input_data keys_to_select: ["branch", "branch_input_data"] flow: Executor output_interface: _target_: aiflows.interfaces.KeyInterface keys_to_rename: branch_output_data.plan: plan branch_output_data.feedback: feedback branch_output_data.temp_plan_file_location: temp_plan_file_location keys_to_delete: ["branch_output_data"] reset: false123---license: mit---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248### Structure of Coder```goal, memory_files (dict)|v+-------------------+| MemoryReading | Reads in the content of the memory files| Flow |+-------------------+|| (memory_content)|v+-------------------+| PlanWriter | Writes a step-by-step plan to achieve the goal+-------------------+|| (plan)|v+-------------------+| CtrlExMemFlow | Illustrated below. Carries out the plan in an controller-executor fashion,| | with memory management mechanisms.+-------------------+|(summary, result)```Here is the structure of the `CtrlExMemFlow`:```plan, memory_files, memory_content, goal|v+---------------+| Controller | --------<<<<-----------++---------------+ || || (command, command args) || |v |+------------------+ || Executor | Each branch is an || (Tree Structure) | executor |+------------------+ || ^| (execution results) ^| ^v ^+---------------+ ^| MemWriteFlow | Updates memory files ^+---------------+ ^| ^| (summary) || |v |+---------------+ || MemReadFlow | Reads updated memory |+---------------+ || || (updated memory content) || |+-> goes back to the Controller>-+```Structure of the Executors:```+-------------------+| Branching || Executor |+-------------------+/ | | | \/ | | | \/ | | | \/ | | | \Extend_library ask_user re_plan update_plan run_code```Memory files of Coder:- plan_coder.txt- logs_coder.txt- library.pyAbout the branches:- [ExtendLibrary](https://huggingface.co/Tachi67/ExtendLibraryFlowModule): Writes and tests code functions in an interactive fashion, finally saves the written function to the code library.- [ask_user](https://huggingface.co/Tachi67/ExtendLibraryFlowModule/blob/main/ExtLibAskUserFlow.py): Ask user for info / confirmation, etc.- [re_plan](https://huggingface.co/Tachi67/ReplanningFlowModule): One branch of the executors, when something goes wrong, re-draft the plan.- [update_plan](https://huggingface.co/Tachi67/JarvisFlowModule/blob/main/UpdatePlanAtomicFlow.py): One branch of the executors, when the controller realizes that one (or some, depending on the LLM's response) step of the plan is (are) done, it generates a new plan that marks the step(s) as done.- [run_code](https://huggingface.co/Tachi67/RunCodeFlowModule): Runs the code written by the Controller, will first open up a temp file with the code for user confirmation and editing, then the code is passed to the [InterpreterFlow](https://huggingface.co/Tachi67/InterpreterFlowModule).# Table of Contents* [CtrlExMem\_CoderFlow](#CtrlExMem_CoderFlow)* [CtrlExMem\_CoderFlow](#CtrlExMem_CoderFlow.CtrlExMem_CoderFlow)* [run\_coder](#run_coder)* [Planner\_CoderFlow](#Planner_CoderFlow)* [Planner\_CoderFlow](#Planner_CoderFlow.Planner_CoderFlow)* [Controller\_CoderFlow](#Controller_CoderFlow)* [Controller\_CoderFlow](#Controller_CoderFlow.Controller_CoderFlow)* [UpdatePlanAtomicFlow](#UpdatePlanAtomicFlow)* [UpdatePlanAtomicFlow](#UpdatePlanAtomicFlow.UpdatePlanAtomicFlow)* [CoderFlow](#CoderFlow)* [CoderFlow](#CoderFlow.CoderFlow)* [run](#CoderFlow.CoderFlow.run)* [\_\_init\_\_](#__init__)<a id="CtrlExMem_CoderFlow"></a># CtrlExMem\_CoderFlow<a id="CtrlExMem_CoderFlow.CtrlExMem_CoderFlow"></a>## CtrlExMem\_CoderFlow Objects```pythonclass CtrlExMem_CoderFlow(CtrlExMemFlow)```This class inherits from the CtrlExMemFlow class from AbstractBossFlowModule.See: https://huggingface.co/Tachi67/AbstractBossFlowModule/blob/main/CtrlExMemFlow.py*Input Interface*:- `plan`- `logs`- `code_library`: the signatures and docstring of the functions in the code library.- `memory_files`- `goal`*Output Interface*- `result` (str): The result of the flow, the result will be returned to the caller.- `summary` (str): The summary of the flow, the summary will be logged into the logs of the caller flow.<a id="run_coder"></a># run\_coder<a id="Planner_CoderFlow"></a># Planner\_CoderFlow<a id="Planner_CoderFlow.Planner_CoderFlow"></a>## Planner\_CoderFlow Objects```pythonclass Planner_CoderFlow(PlanWriterFlow)```Planner of the coder flow, it inherits from PlanWriterFlow, see: https://huggingface.co/Tachi67/PlanWriterFlowModule<a id="Controller_CoderFlow"></a># Controller\_CoderFlow<a id="Controller_CoderFlow.Controller_CoderFlow"></a>## Controller\_CoderFlow Objects```pythonclass Controller_CoderFlow(ChatAtomicFlow)```Refer to: https://huggingface.co/Tachi67/JarvisFlowModule/blob/main/Controller_JarvisFlow.py<a id="UpdatePlanAtomicFlow"></a># UpdatePlanAtomicFlow<a id="UpdatePlanAtomicFlow.UpdatePlanAtomicFlow"></a>## UpdatePlanAtomicFlow Objects```pythonclass UpdatePlanAtomicFlow(AtomicFlow)```This flow updates the plan file with the updated plan. The controller should pass the updated plan to this flow.This design (controller reflect on the existing plan--update plan) is intended to let the controller more aware of theplan it has. However one setback is that this process in then not deterministic.*Input Interface*- `updated_plan`: the updated plan in string format*Output Interface*- `result`: the result of the update plan operation<a id="CoderFlow"></a># CoderFlow<a id="CoderFlow.CoderFlow"></a>## CoderFlow Objects```pythonclass CoderFlow(AbstractBossFlow)```Coder flow is one executor branch of the Jarvis flow. At a higher level, it is a flow thatwrites and runs code given a goal. In the Jarvis flow, the Coder flow in invoked by the controller,The Coder flow receives the goal generated by the controller, writes and runs code in an interactive fashion.The Coder flow has the similar structure as the Jarvis flow (inherited from AbstractBossFlow).*Input Interface (expected input)*- `goal` (str): The goal from the caller (source flow, i.e. JarvisFlow)*Output Interface (expected output)*- `result` (str): The result of the flow, the result will be returned to the caller (i.e. JarvisFlow).- `summary` (str): The summary of the flow, the summary will be logged into the logs of the caller flow (i.e. JarvisFlow).Typical workflow of Coder:0. JarvisFlow calls Coder with a goal.1. MemoryReading reads plans, logs and code library.2. Planner makes plan based on goal.3. Extend library with the goal given by the controller.4. Run code with code (possibly calls the newly written function) given by the controller.5. Finish and give an answer.<a id="CoderFlow.CoderFlow.run"></a>#### run```pythondef run(input_data: Dict[str, Any]) -> Dict[str, Any]```The run function of the Coder flow.**Arguments**:- `input_data` (`Dict[str, Any]`): The input data of the flow.**Returns**:`Dict[str, Any]`: The output data of the flow.<a id="__init__"></a># \_\_init\_\_
12345678910111213141516171819202122232425262728293031323334353637383940#TODO: generalize updateplanatomicflow with the one in extendlibraryfrom typing import Dict, Anyfrom aiflows.base_flows.atomic import AtomicFlowclass UpdatePlanAtomicFlow(AtomicFlow): """This flow updates the plan file with the updated plan. The controller should pass the updated plan to this flow. This design (controller reflect on the existing plan--update plan) is intended to let the controller more aware of the plan it has. However one setback is that this process in then not deterministic. *Input Interface* - `updated_plan`: the updated plan in string format *Output Interface* - `result`: the result of the update plan operation """ def _check_input(self, input_data: Dict[str, Any]): assert "memory_files" in input_data, "memory_files not passed to UpdatePlanAtomicFlow.yaml" assert "plan" in input_data["memory_files"], "plan not in memory_files"
def _call(self, input_data: Dict[str, Any]): try: plan_file_location = input_data["memory_files"]["plan"] plan_to_write = input_data["updated_plan"] with open(plan_file_location, 'w') as file: file.write(plan_to_write + "\n") return { "result": "updated plan saved to the plan file and has overriden the previous plan", "summary": f"Coder/UpdatePlanFlow: updated plan saved to {plan_file_location}" } except Exception as e: return { "result": f"Error occurred: {str(e)}", "summary": f"Coder/UpdatePlanFlow: error occurred while writing updated plan: {str(e)}" }
def run( self, input_data: Dict[str, Any] ): self._check_input(input_data) return self._call(input_data)123456789_target_: aiflows.CoderFlowModule.UpdatePlanAtomicFlow.instantiate_from_default_configname: "UpdatePlanAtomicFlow"description: "Writes new plan to plan file"
input_interface: - "updated_plan"
output_interface: - "result"12345678910111213141516171819dependencies = [ {"url": "aiflows/AbstractBossFlowModule", "revision": "main"}, {"url": "aiflows/MemoryReadingFlowModule", "revision": "main"}, {"url": "aiflows/PlanWriterFlowModule", "revision": "main"}, {"url": "aiflows/ExtendLibraryFlowModule", "revision": "main"}, {"url": "aiflows/RunCodeFlowModule", "revision": "main"}, {"url": "aiflows/ReplanningFlowModule", "revision": "main"}, {"url": "aiflows/HumanStandardInputFlowModule", "revision": "4ff043522c89a964ea3a928ce09811c51a2b5b98"}, {"url": "aiflows/ChatFlowModule", "revision": "297c90d08087d9ff3139521f11d1a48d7dc63ed4"},]
from aiflows import flow_verseflow_verse.sync_dependencies(dependencies)
from .UpdatePlanAtomicFlow import UpdatePlanAtomicFlowfrom .CoderFlow import CoderFlowfrom .Controller_CoderFlow import Controller_CoderFlowfrom .CtrlExMem_CoderFlow import CtrlExMem_CoderFlowfrom .Planner_CoderFlow import Planner_CoderFlow123456789101112131415colorama==0.4.6pytest==7.3.1pytest-cov==4.1.0hydra-core==1.3.2hydra-colorlog==1.1.0wrapt-timeout-decorator==1.3.12.2diskcache==5.6.1openai==1.0.0huggingface_hub==0.19.4jsonlines==3.1.0jinja2==3.1.2mock==5.0.2rich==12.6.0litellm==1.0.0aiflows123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import os
import hydra
from aiflows.backends.api_info import ApiInfofrom aiflows.messages import InputMessagefrom aiflows.utils.general_helpers import read_yaml_filefrom aiflows.utils.general_helpers import quick_load
from aiflows import loggingfrom aiflows.flow_cache import CACHING_PARAMETERS, clear_cache
CACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching# clear_cache() # Uncomment this line to clear the cache
logging.set_verbosity_debug()logging.auto_set_dir()
dependencies = [ {"url": "aiflows/HumanStandardInputFlowModule", "revision": "4ff043522c89a964ea3a928ce09811c51a2b5b98"}, {"url": "aiflows/ChatFlowModule", "revision": "297c90d08087d9ff3139521f11d1a48d7dc63ed4"}, {"url": "aiflows/AbstractBossFlowModule", "revision": "main"}, {"url": "aiflows/MemoryReadingFlowModule", "revision": "main"}, {"url": "aiflows/PlanWriterFlowModule", "revision": "main"}, {"url": "aiflows/ExtendLibraryFlowModule", "revision": "main"}, {"url": "aiflows/RunCodeFlowModule", "revision": "main"}, {"url": "aiflows/ReplanningFlowModule", "revision": "main"}, {"url": "aiflows/CoderFlowModule", "revision": "main"},]
from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
def set_up_memfiles(): code_lib_file_loc = os.path.join(current_dir, "library.py") coder_plan_file_loc = os.path.join(current_dir, "plan_coder.txt") coder_logs_file_loc = os.path.join(current_dir, "logs_coder.txt") extlib_plan_file_loc = os.path.join(current_dir, "plan_extlib.txt") extlib_logs_file_loc = os.path.join(current_dir, "logs_extlib.txt") with open(code_lib_file_loc, 'w') as file: pass with open(coder_plan_file_loc, 'w') as file: pass with open(coder_logs_file_loc, 'w') as file: pass with open(extlib_plan_file_loc, 'w') as file: pass with open(extlib_logs_file_loc, 'w') as file: pass
memfiles_coder = {} memfiles_extlib = {} memfiles_writecode_interactivecoder = {} memfiles_writecode_test = {} memfiles_coder["plan"] = coder_plan_file_loc memfiles_coder["logs"] = coder_logs_file_loc memfiles_coder["code_library"] = code_lib_file_loc memfiles_extlib["plan"] = extlib_plan_file_loc memfiles_extlib["logs"] = extlib_logs_file_loc memfiles_extlib["code_library"] = code_lib_file_loc memfiles_writecode_interactivecoder["code_library"] = code_lib_file_loc memfiles_writecode_test["code_library"] = code_lib_file_loc cfg["memory_files"] = memfiles_coder cfg["subflows_config"]["CtrlExMem"]["subflows_config"]["Executor"]["subflows_config"]["extend_library"]["memory_files"] = memfiles_extlib cfg["subflows_config"]["CtrlExMem"]["subflows_config"]["Executor"]["subflows_config"]["extend_library"]["subflows_config"]["CtrlExMem"]["subflows_config"]["Executor"]["subflows_config"]["write_code"][ "subflows_config"]["Executor"]["subflows_config"]["write_code"][ "memory_files"] = memfiles_writecode_interactivecoder cfg["subflows_config"]["CtrlExMem"]["subflows_config"]["Executor"]["subflows_config"]["extend_library"]["subflows_config"]["CtrlExMem"]["subflows_config"]["Executor"]["subflows_config"]["write_code"][ "subflows_config"]["Executor"]["subflows_config"]["test"]["memory_files"] = memfiles_writecode_test
if __name__ == "__main__": # ~~~ make sure to set the openai api key in the envs ~~~ key = os.getenv("OPENAI_API_KEY") api_information = [ApiInfo(backend_used="openai", api_key=os.getenv("OPENAI_API_KEY"))] path_to_output_file = None
current_dir = os.getcwd() cfg_path = os.path.join(current_dir, "CoderFlow.yaml") cfg = read_yaml_file(cfg_path)
# ~~~ setting api information into config ~~~ quick_load(cfg, api_information)
# ~~~ setting memory files into config ~~~ set_up_memfiles()
# ~~~ instantiating the flow and input data ~~~ CoderFlow = hydra.utils.instantiate(cfg, _recursive_=False, _convert_="partial") input_data = { #"goal": "define a calculator that supports addition and multiplication on complex numbers", #"goal": "define a calculator that supports addition on complex numbers" "goal": "fetch google's stock prices using yfinance for the last 6 months and print the first 10 entries" } input_message = InputMessage.build( data_dict=input_data, src_flow="Launcher", dst_flow=CoderFlow.name )
# ~~~ calling the flow ~~~ output_message = CoderFlow(input_message)
# ~~~ printing the output ~~~ print(output_message)CoderFlow.pyAdded+54
CoderFlow.yamlAdded+134
Controller_CoderFlow.pyAdded+131
Controller_CoderFlow.yamlAdded+167
CtrlExMem_CoderFlow.pyAdded+67
CtrlExMem_CoderFlow.yamlAdded+173
Planner_CoderFlow.pyAdded+65
Planner_CoderFlow.yamlAdded+138
README.md+248-3
UpdatePlanAtomicFlow.pyAdded+40
UpdatePlanAtomicFlow.yamlAdded+9
__init__.pyAdded+19
pip_requirements.txtAdded+15
run_coder.pyAdded+106