coflows compatible
Browse filesFiles changed (3)
- HumanStandardInputFlow.py+8-4
- demo.yaml+14-16
- run.py+71-31
7 unmodified lines891011121314127 unmodified lines1421431441451461471481 unmodified line1501511521531541551561574 unmodified lines1621631641657 unmodified linesfrom aiflows.messages import UpdateMessage_Genericfrom aiflows.utils import logging# logging.set_verbosity_debug() # ToDo: Has no effect on the logger for __name__. Level is warn, and info is not printedlog = logging.get_logger(f"aiflows.{__name__}") # ToDo: Is there a better fix?127 unmodified linesreturn human_inputdef run(self,input_data: Dict[str, Any]) -> Dict[str, Any]:""" Runs the HumanStandardInputFlow. It's used to read input from the user/human's standard input.:param input_data: The input data dictionary1 unmodified line:return: The output data dictionary:rtype: 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"],4 unmodified lineslog.info(query_message)human_input = self._read_input()return {"human_input": human_input}7 unmodified lines891011121314127 unmodified lines1421431441451461471481 unmodified line1501511521531541551561571581594 unmodified lines1641651661671681697 unmodified linesfrom aiflows.messages import UpdateMessage_Genericfrom aiflows.utils import loggingfrom aiflows.messages import FlowMessage# logging.set_verbosity_debug() # ToDo: Has no effect on the logger for __name__. Level is warn, and info is not printedlog = logging.get_logger(f"aiflows.{__name__}") # ToDo: Is there a better fix?127 unmodified linesreturn human_inputdef run(self,input_message: FlowMessage):""" Runs the HumanStandardInputFlow. It's used to read input from the user/human's standard input.:param input_data: The input data dictionary1 unmodified line:return: The output data dictionary:rtype: Dict[str, Any]"""input_data = input_message.dataquery_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"],4 unmodified lineslog.info(query_message)human_input = self._read_input()reply_message = self._package_output_message(input_message = input_message, response = {"human_input": human_input})self.reply_to_message(reply = reply_message, to = input_message)
1234567891011121314151617input_interface:_target_: aiflows.interfaces.KeyInterfaceflow:_target_: flow_modules.aiflows.HumanStandardInputFlowModule.HumanStandardInputFlow.instantiate_from_default_configname: "HumanStandardInputFlow"description: "A demo of the HumanStandardInputFlow."request_multi_line_input_flag: Trueend_of_input_string: EOIquery_message_prompt_template:_target_: aiflows.prompt_template.JinjaPrompttemplate: |2-Please enter your input.What's opinion on this statement?{{statement}}input_variables: []partial_variables: {"statement": "This Flow works well."}123456789101112131415_target_: flow_modules.aiflows.HumanStandardInputFlowModule.HumanStandardInputFlow.instantiate_from_default_configname: "HumanStandardInputFlow"description: "A demo of the HumanStandardInputFlow."request_multi_line_input_flag: Trueend_of_input_string: EOIquery_message_prompt_template:_target_: aiflows.prompt_template.JinjaPrompttemplate: |2-Please enter your input.What's opinion on this statement?{{statement}}input_variables: [statement]partial_variables: {}
1 unmodified line234567891011121314155 unmodified lines21222324252627282930313233343536373839404142434445464748495051525354555657581 unmodified lineimport hydrafrom aiflows.flow_launchers import FlowLauncherfrom aiflows.utils.general_helpers import read_yaml_filefrom aiflows import loggingfrom aiflows.flow_cache import CACHING_PARAMETERSCACHING_PARAMETERS.do_caching = False # Set to True to enable caching# clear_cache() # Uncomment this line to clear the cachelogging.set_verbosity_debug()5 unmodified linesflow_verse.sync_dependencies(dependencies)if __name__ == "__main__":root_dir = "."cfg_path = os.path.join(root_dir, "demo.yaml")cfg = read_yaml_file(cfg_path)flow_with_interfaces = {"flow": hydra.utils.instantiate(cfg['flow'], _recursive_=False, _convert_="partial"),"input_interface": (Noneif getattr(cfg, "input_interface", None) is Noneelse hydra.utils.instantiate(cfg['input_interface'], _recursive_=False)),"output_interface": (Noneif getattr(cfg, "output_interface", None) is Noneelse hydra.utils.instantiate(cfg['output_interface'], _recursive_=False)),}# ~~~ Get the data ~~~# This can be a list of samplesdata = {"id": 0} # Add your data here# ~~~ Run inference ~~~path_to_output_file = None# path_to_output_file = "output.jsonl" # Uncomment this line to save the output to disk_, outputs = FlowLauncher.launch(flow_with_interfaces=flow_with_interfaces,data=data,path_to_output_file=path_to_output_file,)# ~~~ Print the output ~~~flow_output_data = outputs[0]print(flow_output_data)1 unmodified line2345678910111213141516171819202122235 unmodified lines293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697981 unmodified lineimport hydraimport aiflowsfrom aiflows.flow_launchers import FlowLauncherfrom aiflows.backends.api_info import ApiInfofrom aiflows.utils.general_helpers import read_yaml_filefrom aiflows import loggingfrom aiflows.flow_cache import CACHING_PARAMETERS, clear_cachefrom aiflows.utils import serve_utilsfrom aiflows.workers import run_dispatch_worker_threadfrom aiflows.messages import FlowMessagefrom aiflows.interfaces import KeyInterfacefrom aiflows.utils.colink_utils import start_colink_serverfrom aiflows.workers import run_dispatch_worker_threadCACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching# clear_cache() # Uncomment this line to clear the cachelogging.set_verbosity_debug()5 unmodified linesflow_verse.sync_dependencies(dependencies)if __name__ == "__main__":#1. ~~~~~ Set up a colink server ~~~~FLOW_MODULES_PATH = "./"cl = start_colink_server()#2. ~~~~~Load flow config~~~~~~root_dir = "."cfg_path = os.path.join(root_dir, "demo.yaml")cfg = read_yaml_file(cfg_path)#3. ~~~~ Serve The Flow ~~~~serve_utils.serve_flow(cl = cl,flow_type="HumanStandardInputFlowModule",default_config=cfg,default_state=None,default_dispatch_point="coflows_dispatch")#4. ~~~~~Start A Worker Thread~~~~~run_dispatch_worker_thread(cl, dispatch_point="coflows_dispatch", flow_modules_base_path=FLOW_MODULES_PATH)#5. ~~~~~Mount the flow and get its proxy~~~~~~proxy_flow = serve_utils.recursive_mount(cl=cl,client_id="local",flow_type="HumanStandardInputFlowModule",config_overrides=None,initial_state=None,dispatch_point_override=None,)#6. ~~~ Get the data ~~~data = {"id": 0, "statement": "This flow works well"} # This can be a list of samples# data = {"id": 0, "question": "Who was the NBA champion in 2023?"} # This can be a list of samples#option1: use the FlowMessage classinput_message = FlowMessage(data=data,)#option2: use the proxy_flow#input_message = proxy_flow._package_input_message(data = data)#7. ~~~ Run inference ~~~future = proxy_flow.send_message_blocking(input_message)#uncomment this line if you would like to get the full message back#reply_message = future.get_message()reply_data = future.get_data()# ~~~ Print the output ~~~print("~~~~~~Reply~~~~~~")print(reply_data)#8. ~~~~ (Optional) apply output interface on reply ~~~~# output_interface = KeyInterface(# keys_to_rename={"api_output": "answer"},# )# print("Output: ", output_interface(reply_data))#9. ~~~~~Optional: Unserve Flow~~~~~~# serve_utils.delete_served_flow(cl, "ReverseNumberAtomicFlow_served")
HumanStandardInputFlow.py+8-4
demo.yaml+14-16
run.py+71-31