coflow compatible
Browse filesFiles changed (5)
- ChatHumanFlowModule.py+101-3
- ChatHumanFlowModule.yaml+15-49
- __init__.py+2-2
- demo.yaml+28-30
- run.py+78-41
1234567891037 unmodified lines4849505152535455from aiflows.base_flows import CircularFlowfrom aiflows.utils import logginglog = logging.get_logger(f"aiflows.{__name__}")class ChatHumanFlowModule(CircularFlow):""" This class implements a Chat Human Flow Module. It is a flow that consists of two sub-flows that are executed circularly. It Contains the following subflows:- A User Flow: A flow makes queries to the Assistant Flow. E.g. The user asks the assistant (LLM) a question.37 unmodified linesdef __init__(self, **kwargs):super().__init__(**kwargs)@classmethoddef type(cls):""" This method returns the type of the flow."""return "OpenAIChatHumanFlowModule"12345678910111237 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153from aiflows.base_flows import CompositeFlowfrom aiflows.utils import loggingfrom aiflows.messages import FlowMessagefrom aiflows.interfaces import KeyInterfacefrom aiflows.data_transformations import RegexFirstOccurrenceExtractor,EndOfInteractionlog = logging.get_logger(f"aiflows.{__name__}")class ChatHumanFlowModule(CompositeFlow):""" This class implements a Chat Human Flow Module. It is a flow that consists of two sub-flows that are executed circularly. It Contains the following subflows:- A User Flow: A flow makes queries to the Assistant Flow. E.g. The user asks the assistant (LLM) a question.37 unmodified linesdef __init__(self, **kwargs):super().__init__(**kwargs)self.regex_extractor = RegexFirstOccurrenceExtractor(**self.flow_config["regex_first_occurrence_extractor"])self.end_of_interaction = EndOfInteraction(**self.flow_config["end_of_interaction"])self.input_interface_assistant = KeyInterface(keys_to_rename = {"human_input": "query"},additional_transformations = [self.regex_extractor, self.end_of_interaction])def set_up_flow_state(self):""" This method sets up the flow state. It is called when the flow is executed."""super().set_up_flow_state()self.flow_state["last_flow_called"] = Noneself.flow_state["current_round"] = 0self.flow_state["user_inputs"] = []self.flow_state["assistant_outputs"] = []self.flow_state["input_message"] = Noneself.flow_state["end_of_interaction"] = False@classmethoddef type(cls):""" This method returns the type of the flow."""return "OpenAIChatHumanFlowModule"def max_rounds_reached(self):return self.flow_config["max_rounds"] is not None and self.flow_state["current_round"] >= self.flow_config["max_rounds"]def generate_reply(self):reply = self._package_output_message(input_message = self.flow_state["input_message"],response = {"user_inputs": self.flow_state["user_inputs"],"assistant_outputs": self.flow_state["assistant_outputs"],"end_of_interaction": self.flow_state["end_of_interaction"]},)self.reply_to_message(reply = reply,to = self.flow_state["input_message"])def call_to_user(self,input_message):self.flow_state["assistant_outputs"].append(input_message.data["api_output"])if self.max_rounds_reached():self.generate_reply()else:self.subflows["User"].send_message_async(input_message,pipe_to=self.flow_config["flow_ref"])self.flow_state["last_flow_called"] = "User"self.flow_state["current_round"] += 1def call_to_assistant(self,input_message):message = self.input_interface_assistant(input_message)if self.flow_state["last_flow_called"] is None:self.flow_state["input_message"] = input_messageelse:self.flow_state["user_inputs"].append(input_message.data["query"])if message.data["end_of_interaction"]:self.flow_state["end_of_interaction"] = Trueself.generate_reply()else:self.subflows["Assistant"].send_message_async(message,pipe_to=self.flow_config["flow_ref"])self.flow_state["last_flow_called"] = "Assistant"def run(self,input_message: FlowMessage):""" This method runs the flow. It is the main method of the flow and it is called when the flow is executed.:param input_message: The input message to the flow.:type input_message: FlowMessage"""last_flow_called = self.flow_state["last_flow_called"]if last_flow_called is None or last_flow_called == "User":self.call_to_assistant(input_message=input_message)elif last_flow_called == "Assistant":self.call_to_user(input_message=input_message)
3 unmodified lines45678910111213141 unmodified line161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566673 unmodified linesmax_rounds: null # Run until early exit is detectedinput_interface:_target_: aiflows.interfaces.KeyInterfaceoutput_interface:- "end_of_interaction"- "answer"subflows_config:Assistant:1 unmodified lineUser:_target_: flow_modules.aiflows.HumanStandardInputFlowModule.HumanStandardInputFlow.instantiate_from_default_configtopology:- goal: "Query the assistant"### Input Interfaceinput_interface:_target_: aiflows.interfaces.KeyInterfaceadditional_transformations:- _target_: aiflows.data_transformations.KeyMatchInput### Flow Specificationflow: Assistantreset_every_round: false### Output Interfaceoutput_interface:_target_: aiflows.interfaces.KeyInterfaceadditional_transformations:- _target_: aiflows.data_transformations.PrintPreviousMessages- goal: "Ask the user for input"### Input Interfaceinput_interface:_target_: aiflows.interfaces.KeyInterfaceadditional_transformations:- _target_: aiflows.data_transformations.KeyMatchInput### Flow Specificationflow: Userreset_every_round: true### Output Interfaceoutput_interface:_target_: aiflows.interfaces.KeyInterfacekeys_to_rename:human_input: queryadditional_transformations:- _target_: aiflows.data_transformations.RegexFirstOccurrenceExtractorregex: '(?<=```answer)([\s\S]*?)(?=```)'input_key: "query"output_key: "answer"strip: Trueassert_unique: True- _target_: aiflows.data_transformations.EndOfInteractionend_of_interaction_string: "<END>"input_key: "query"output_key: "end_of_interaction"early_exit_key: "end_of_interaction"3 unmodified lines4567891011121314151617181920212223242526271 unmodified line29303132333 unmodified linesmax_rounds: null # Run until early exit is detectedinput_interface:- "query"output_interface:- "end_of_interaction"- "user_inputs"- "assistant_outputs"regex_first_occurrence_extractor:regex: "(?<=```answer)([\\s\\S]*?)(?=```)"input_key: "query"output_key: "answer"strip: trueassert_unique: trueend_of_interaction:end_of_interaction_string: "<END>"input_key: "query"output_key: "end_of_interaction"subflows_config:Assistant:1 unmodified lineUser:_target_: flow_modules.aiflows.HumanStandardInputFlowModule.HumanStandardInputFlow.instantiate_from_default_config
1234567# ~~~ Specify the dependencies ~~~dependencies = [{"url": "aiflows/ChatFlowModule", "revision": "main"},{"url": "aiflows/HumanStandardInputFlowModule", "revision": "main"},]from aiflows import flow_verse1234567# ~~~ Specify the dependencies ~~~dependencies = [{"url": "aiflows/ChatFlowModule", "revision": "coflows"},{"url": "aiflows/HumanStandardInputFlowModule", "revision": "coflows"},]from aiflows import flow_verse
12345678910111213141516171819202122232425262728293031flow:max_rounds: 5_target_: flow_modules.aiflows.ChatInteractiveFlowModule.ChatHumanFlowModule.instantiate_from_default_configsubflows_config:Assistant:_target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_configbackend:_target_: aiflows.backends.llm_lite.LiteLLMBackendapi_infos: ???model_name:openai: "gpt-4"azure: "azure/gpt-4"input_interface_non_initialized: []User:_target_: flow_modules.aiflows.HumanStandardInputFlowModule.HumanStandardInputFlow.instantiate_from_default_configrequest_multi_line_input_flag: Falsequery_message_prompt_template:_target_: aiflows.prompt_template.JinjaPrompttemplate: |2-{{api_output}}To end an Interaction, type <END> and press enter.input_variables: ["api_output"]input_interface:- "api_output"output_interface:- "human_input"1234567891011121314151617181920212223242526272829max_rounds: 2_target_: flow_modules.aiflows.ChatInteractiveFlowModule.ChatHumanFlowModule.instantiate_from_default_configsubflows_config:Assistant:_target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_configbackend:_target_: aiflows.backends.llm_lite.LiteLLMBackendapi_infos: ???model_name:openai: "gpt-4"azure: "azure/gpt-4"input_interface_non_initialized: []User:_target_: flow_modules.aiflows.HumanStandardInputFlowModule.HumanStandardInputFlow.instantiate_from_default_configrequest_multi_line_input_flag: Falsequery_message_prompt_template:_target_: aiflows.prompt_template.JinjaPrompttemplate: |2-{{api_output}}To end an Interaction, type <END> and press enter.input_variables: ["api_output"]input_interface:- "api_output"output_interface:- "human_input"
123451 unmodified line7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677"""A simple script to run a Flow that can be used for development and debugging."""import osimport hydra1 unmodified lineimport 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_cacheCACHING_PARAMETERS.do_caching = False # Set to True to enable caching# clear_cache() # Uncomment this line to clear the cachelogging.set_verbosity_debug()dependencies = [{"url": "aiflows/ChatInteractiveFlowModule", "revision": os.getcwd()},]from aiflows import flow_verseflow_verse.sync_dependencies(dependencies)if __name__ == "__main__":# ~~~ 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") )root_dir = "."cfg_path = os.path.join(root_dir, "demo.yaml")cfg = read_yaml_file(cfg_path)cfg["flow"]["subflows_config"]["Assistant"]["backend"]["api_infos"] = api_information# ~~~ Instantiate the Flow ~~~flow_with_interfaces = {"flow": hydra.utils.instantiate(cfg['flow'], _recursive_=False, _convert_="partial"),"input_interface": (Noneif cfg.get( "input_interface", None) is Noneelse hydra.utils.instantiate(cfg['input_interface'], _recursive_=False)),"output_interface": (Noneif cfg.get( "output_interface", None) is Noneelse hydra.utils.instantiate(cfg['output_interface'], _recursive_=False)),}# ~~~ Get the data ~~~data = {"id": 0} # 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# ~~~ 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)1231 unmodified line56789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import osimport hydra1 unmodified lineimport aiflowsfrom aiflows.flow_launchers import FlowLauncherfrom aiflows.backends.api_info import ApiInfofrom aiflows.utils.general_helpers import read_yaml_file, quick_load_api_keysfrom 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()dependencies = [{"url": "aiflows/ChatInteractiveFlowModule", "revision": os.getcwd()}]from aiflows import flow_verseflow_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)#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") )quick_load_api_keys(cfg, api_information, key="api_infos")#3. ~~~~ Serve The Flow ~~~~serve_utils.recursive_serve_flow(cl = cl,flow_type="ChatInteractiveFlowModule",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="ChatInteractiveFlowModule",config_overrides=None,initial_state=None,dispatch_point_override=None,)#6. ~~~ Get the data ~~~data = {"id": 0, "query": "I want to ask you a few questions"} # 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, "ChatWithDemonstrationFlowModule") o_caching = False # Set to True to enable caching
ChatHumanFlowModule.py+101-3
ChatHumanFlowModule.yaml+15-49
__init__.py+2-2
demo.yaml+28-30
run.py+78-41