Clean Versions
Browse filesFiles changed (6)
3 unmodified lines4567891039 unmodified lines50515253545556575859604 unmodified lines65666768697071727374757677787980818283848586873 unmodified linesfrom aiflows.utils import loggingfrom aiflows.interfaces import KeyInterfacelogging.set_verbosity_debug()log = logging.get_logger(__name__)39 unmodified lines)def set_up_flow_state(self):super().set_up_flow_state()self.flow_state["last_flow_called"] = Nonedef run(self,input_message):# #~~~~~~~~~~~Solution 1 - Blocking ~~~~~~~# future = self.subflows["demonstration_flow"].get_reply_future(input_message)4 unmodified lines# self.send_message(reply)# #~~~~~~~~~~~Solution 2 - Non-Blocking ~~~~~~~if self.flow_state["last_flow_called"] is None:self.flow_state["input_message"] = input_messageself.subflows["demonstration_flow"].get_reply(input_message,self.get_instance_id(),)self.flow_state["last_flow_called"] = "demonstration_flow"elif self.flow_state["last_flow_called"] == "demonstration_flow":self.subflows["chat_flow"].get_reply(input_message,self.get_instance_id(),)self.flow_state["last_flow_called"] = "chat_flow"else:self.flow_state["last_flow_called"] = Nonereply = self.package_output_message(self.flow_state["input_message"],3 unmodified lines4567891039 unmodified lines505152535455565758596061626364654 unmodified lines70717273747576777879808182838485868788899091923 unmodified linesfrom aiflows.utils import loggingfrom aiflows.interfaces import KeyInterfacelogging.set_verbosity_debug()from aiflows.messages import FlowMessagelog = logging.get_logger(__name__)39 unmodified lines)def set_up_flow_state(self):""" Set up the flow state. It sets the last state of the flow to None."""super().set_up_flow_state()self.flow_state["last_state"] = Nonedef run(self,input_message):""" Runs the flow.:param input_message: The input message of the flow.:type input_message: FlowMessage"""# #~~~~~~~~~~~Solution 1 - Blocking ~~~~~~~# future = self.subflows["demonstration_flow"].get_reply_future(input_message)4 unmodified lines# self.send_message(reply)# #~~~~~~~~~~~Solution 2 - Non-Blocking ~~~~~~~if self.flow_state["last_state"] is None:self.flow_state["input_message"] = input_messageself.subflows["demonstration_flow"].get_reply(input_message,self.get_instance_id(),)self.flow_state["last_state"] = "demonstration_flow"elif self.flow_state["last_state"] == "demonstration_flow":self.subflows["chat_flow"].get_reply(input_message,self.get_instance_id(),)self.flow_state["last_state"] = "chat_flow"else:self.flow_state["last_state"] = Nonereply = self.package_output_message(self.flow_state["input_message"],
4 unmodified lines56789101112134 unmodified linessubflows_config:demonstration_flow:_target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_configname: "DemonstrationsAtomicFlow"description: "A flow that answers questions with demonstrations"chat_flow:_target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_configname: "ChatAtomicFlow"description: "A flow that answers questions"4 unmodified lines56789101112131415161718194 unmodified linessubflows_config:demonstration_flow:_target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_configflow_class_name: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlowflow_endpoint: DemonstrationsAtomicFlowuser_id: localname: "DemonstrationsAtomicFlow"description: "A flow that answers questions with demonstrations"chat_flow:_target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_configname: "ChatAtomicFlow"description: "A flow that answers questions"flow_class_name: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlowflow_endpoint: ChatAtomicFlowuser_id: local
4 unmodified lines567891030 unmodified lines41424344454647134 unmodified lines1821831841851861871881891901911921931944 unmodified linesfrom typing import Dict,Any,Optional,Listfrom aiflows.prompt_template import JinjaPromptfrom copy import deepcopyimport osimport hydralog = logging.get_logger(__name__)30 unmodified lines*Output Interface*:- The input interface expected by its successor flow (e.g. typically ChatAtomicFlow so the input interface expected by ChatAtomicFlow))- `demonstrations` (List[Dict[str, Any]]): A list of demonstrations. Each demonstration is a dictionary with the following keys:- idx (int): The index of the demonstration- query (str): The query of the demonstration134 unmodified lineslog.info("Loaded the demonstrations for %d datapoints from %s", len(self.data), self.params["data_dir"])def run(self,input_message):""" This method runs the flow. It returns the input data of the flow with the demonstrations added to it.:param input_data: The input data of the flow.:type input_data: Dict[str, Any]:return: The input data of the flow with the demonstrations added to it.:rtype: Dict[str, Any]"""input_data = input_message.datareply = self.package_output_message(4 unmodified lines56789101130 unmodified lines42434445464748134 unmodified lines1831841851861871881891901911921934 unmodified linesfrom typing import Dict,Any,Optional,Listfrom aiflows.prompt_template import JinjaPromptfrom copy import deepcopyfrom aiflows.messages import FlowMessageimport osimport hydralog = logging.get_logger(__name__)30 unmodified lines*Output Interface*:- Whichever data that was passed in the input_message (e.g. typically ChatAtomicFlow so the input interface expected by ChatAtomicFlow))- `demonstrations` (List[Dict[str, Any]]): A list of demonstrations. Each demonstration is a dictionary with the following keys:- idx (int): The index of the demonstration- query (str): The query of the demonstration134 unmodified lineslog.info("Loaded the demonstrations for %d datapoints from %s", len(self.data), self.params["data_dir"])def run(self,input_message: FlowMessage):""" This method runs the flow. It returns the data of the input_message with the demonstrations added to it.:param input_message: The input message of the flow.:type input_message: FlowMessage"""input_data = input_message.datareply = self.package_output_message(
123456name: "DemonstrationAtomicFlow"description: "A flow which returns Demonstrations"data: ??? #e.g. [{"query_data": {"query": "What is the capital of France?"}, "response_data": {"response": "Paris, my sir."}}]params:data_dir: ???123456name: "DemonstrationAtomicFlow"description: "A flow which returns Demonstrations"_target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_configdata: ??? #e.g. [{"query_data": {"query": "What is the capital of France?"}, "response_data": {"response": "Paris, my sir."}}]params:data_dir: ???
8 unmodified lines910111213141516 unmodified lines323334353637388 unmodified linessubflows_config:demonstration_flow:_target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_configdata:- query_data:query: "What is the capital of Turkey?"16 unmodified lineschat_flow:_target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_configname: "SimpleQA_Flow"# ~~~ Input interface specification ~~~input_interface_non_initialized:- "question"8 unmodified lines91011121314151617181916 unmodified lines363738394041424344458 unmodified linessubflows_config:demonstration_flow:_target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_configname: "proxy of DemonstrationsAtomicFlow"flow_endpoint: DemonstrationsAtomicFlowuser_id: localdescription: "A flow that answers questions with demonstrations"data:- query_data:query: "What is the capital of Turkey?"16 unmodified lineschat_flow:_target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_configname: "proxy SimpleQA_Flow"description: "A flow that answers questions"flow_endpoint: ChatAtomicFlowuser_id: local# ~~~ Input interface specification ~~~input_interface_non_initialized:- "question"
40 unmodified lines41424344454647484950515253545556578 unmodified lines666768697071727374757677787980818283848586878889909140 unmodified linescfg_path = os.path.join(root_dir, "demo.yaml")cfg = read_yaml_file(cfg_path)#2.1 ~~~ Set the API information ~~~# OpenAI backendapi_information = [ApiInfo(backend_used="openai",api_key = os.getenv("OPENAI_API_KEY"))]# # Azure backend# api_information = ApiInfo(backend_used = "azure",# api_base = os.getenv("AZURE_API_BASE"),# api_key = os.getenv("AZURE_OPENAI_KEY"),# api_version = os.getenv("AZURE_API_VERSION") )#2.1 ~~~ Set the API information ~~~# OpenAI backendapi_information = [ApiInfo(backend_used="openai",8 unmodified lines#3. ~~~~ Serve The Flow ~~~~serve_utils.recursive_serve_flow(cl = cl,flow_type="ChatWithDemonstrationFlowModule",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="ChatWithDemonstrationFlowModule",config_overrides=None,initial_state=None,dispatch_point_override=None,)#6. ~~~ Get the data ~~~data = {"id": 0, "question": "What is the capital of France?"} # This can be a list of samples40 unmodified lines4142434445468 unmodified lines5556575859606162636465666768697071727374757677787940 unmodified linescfg_path = os.path.join(root_dir, "demo.yaml")cfg = read_yaml_file(cfg_path)#2.1 ~~~ Set the API information ~~~# OpenAI backendapi_information = [ApiInfo(backend_used="openai",8 unmodified lines#3. ~~~~ Serve The Flow ~~~~serve_utils.recursive_serve_flow(cl = cl,flow_class_name="flow_modules.aiflows.ChatWithDemonstrationsFlowModule.ChatWithDemonstrationsFlow",flow_endpoint="ChatWithDemonstrationsFlow",)#4. ~~~~~Start A Worker Thread~~~~~run_dispatch_worker_thread(cl)#5. ~~~~~Mount the flow and get its proxy~~~~~~proxy_flow= serve_utils.get_flow_instance(cl=cl,flow_endpoint="ChatWithDemonstrationsFlow",user_id="local",config_overrides = cfg)#6. ~~~ Get the data ~~~data = {"id": 0, "question": "What is the capital of France?"} # This can be a list of samples
ChatWithDemonstrationsFlow.py+13-8
ChatWithDemonstrationsFlow.yaml+6
DemonstrationsAtomicFlow.py+6-7
DemonstrationsAtomicFlow.yaml+1-1
demo.yaml+9-2
run.py+11-23