added readmea + demo
Browse files- ControllerAtomicFlow.py +76 -2
- ControllerAtomicFlow.yaml +2 -0
- ControllerExecutorFlow.py +63 -0
- ControllerExecutorFlow.yaml +2 -7
- README.md +365 -4
- WikiSearchAtomicFlow.py +29 -0
- __init__.py +1 -1
- demo.yaml +27 -0
- pip_requirements.txt +1 -1
- run.py +77 -0
- wikipediaAPI.py +46 -3
ControllerAtomicFlow.py
CHANGED
@@ -1,19 +1,72 @@
|
|
1 |
import json
|
2 |
from copy import deepcopy
|
3 |
from typing import Any, Dict, List
|
4 |
-
from flow_modules.aiflows.
|
5 |
|
6 |
from dataclasses import dataclass
|
7 |
|
8 |
|
9 |
@dataclass
|
10 |
class Command:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
name: str
|
12 |
description: str
|
13 |
input_args: List[str]
|
14 |
|
15 |
|
16 |
-
class ControllerAtomicFlow(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def __init__(self, commands: List[Command], **kwargs):
|
18 |
super().__init__(**kwargs)
|
19 |
self.system_message_prompt_template = self.system_message_prompt_template.partial(
|
@@ -22,6 +75,13 @@ class ControllerAtomicFlow(OpenAIChatAtomicFlow):
|
|
22 |
|
23 |
@staticmethod
|
24 |
def _build_commands_manual(commands: List[Command]) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
ret = ""
|
26 |
for i, command in enumerate(commands):
|
27 |
command_input_json_schema = json.dumps(
|
@@ -31,6 +91,13 @@ class ControllerAtomicFlow(OpenAIChatAtomicFlow):
|
|
31 |
|
32 |
@classmethod
|
33 |
def instantiate_from_config(cls, config):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
flow_config = deepcopy(config)
|
35 |
|
36 |
kwargs = {"flow_config": flow_config}
|
@@ -51,6 +118,13 @@ class ControllerAtomicFlow(OpenAIChatAtomicFlow):
|
|
51 |
return cls(**kwargs)
|
52 |
|
53 |
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
api_output = super().run(input_data)["api_output"].strip()
|
55 |
response = json.loads(api_output)
|
56 |
return response
|
|
|
1 |
import json
|
2 |
from copy import deepcopy
|
3 |
from typing import Any, Dict, List
|
4 |
+
from flow_modules.aiflows.ChatFlowModule import ChatAtomicFlow
|
5 |
|
6 |
from dataclasses import dataclass
|
7 |
|
8 |
|
9 |
@dataclass
|
10 |
class Command:
|
11 |
+
""" The command class is used to store the information about the commands that the user can give to the controller.
|
12 |
+
|
13 |
+
:param name: The name of the command.
|
14 |
+
:type name: str
|
15 |
+
:param description: The description of the command.
|
16 |
+
:type description: str
|
17 |
+
:param input_args: The input arguments of the command.
|
18 |
+
:type input_args: List[str]
|
19 |
+
"""
|
20 |
name: str
|
21 |
description: str
|
22 |
input_args: List[str]
|
23 |
|
24 |
|
25 |
+
class ControllerAtomicFlow(ChatAtomicFlow):
|
26 |
+
""" The ControllerAtomicFlow is an atomic flow that, given an observation and a goal, can call a set of commands and arguments which are then usually executed by an ExecutorAtomicFlow (branching flow).
|
27 |
+
|
28 |
+
*Configuration Parameters*
|
29 |
+
|
30 |
+
- `name` (str): The name of the flow. Default: "ControllerFlow"
|
31 |
+
- `description` (str): A description of the flow. This description is used to generate the help message of the flow.
|
32 |
+
Default: "Proposes the next action to take towards achieving the goal, and prepares the input for the executor."
|
33 |
+
- `enable_cache` (bool): Whether to enable caching or not. Default: True
|
34 |
+
- `commands` (List[Dict[str,Any]]): A list of commands that the controller can call. Default: []
|
35 |
+
- `finish` (Dict[str,Any]): The configuration of the finish command. Default parameters: No default parameters.
|
36 |
+
- `system_message_prompt_template` (Dict[str, Any]): The prompt template used to generate the system message.
|
37 |
+
By default, it's type is flows.prompt_template.JinjaPrompt. It's default parameters are:
|
38 |
+
- `template` (str): The template of the prompt. Default: see ControllerAtomicFlow.yaml for the default template.
|
39 |
+
- `input_variables` (List[str]): The input variables of the prompt. Default: ["commands"]. Note that the commands are the commands of the executor
|
40 |
+
(subflows of branching flow) and are actually to the system prompt template via the `_build_commands_manual` function of this class.
|
41 |
+
- `human_message_prompt_template` (Dict[str, Any]): The prompt template of the human/user message (message used everytime the except the first time in).
|
42 |
+
It's passed as the user message to the LLM. By default its of type flows.prompt_template.JinjaPrompt and has the following parameters:
|
43 |
+
- `template` (str): The template of the prompt. Default: see ControllerAtomicFlow.yaml for the default template.
|
44 |
+
- `input_variables` (List[str]): The input variables of the prompt. Default: ["observation"]
|
45 |
+
- init_human_message_prompt_template` (Dict[str, Any]): The prompt template of the human/user message used to initialize the conversation
|
46 |
+
(first time in). It is used to generate the human message. It's passed as the user message to the LLM.
|
47 |
+
By default its of type flows.prompt_template.JinjaPrompt and has the following parameters:
|
48 |
+
- `template` (str): The template of the prompt. Default: see ControllerAtomicFlow.yaml for the default template.
|
49 |
+
- `input_variables` (List[str]): The input variables of the prompt. Default: ["goal"]
|
50 |
+
- All other parameters are inherited from the default configuration of ChatAtomicFlow (see Flowcard, i.e. README.md, of ChatAtomicFlowModule).
|
51 |
+
|
52 |
+
*Initial Input Interface (this is the interface used the first time the flow is called)*:
|
53 |
+
- `goal` (str): The goal of the controller. Usually asked by the user/human (e.g. "I want to know the occupation and birth date of Michael Jordan.")
|
54 |
+
|
55 |
+
*Input Interface (this is the interface used after the first time the flow is called)*:
|
56 |
+
- `observation` (str): The observation of the controller's previous action. Usually the response of the ExecutorAtomicFlow (e.g. "The result of a wikipedia search (if the ExecutorAtomicFlow has a WikipediaExecutorAtomicFlow).")
|
57 |
+
|
58 |
+
*Output Interface:*
|
59 |
+
- `thought` (str): The thought of the controller on what to do next (which command to call)
|
60 |
+
- `reasoning` (str): The reasoning of the controller on why it thinks the command it wants to call is the right one
|
61 |
+
- `criticism` (str): The criticism of the controller of it's thinking process
|
62 |
+
- `command` (str): The command to the executor chooses to call
|
63 |
+
- `command_args` (Dict[str, Any]): The arguments of the command to call
|
64 |
+
|
65 |
+
:param commands: The commands that the controller can call (typically the commands of the executor).
|
66 |
+
:type commands: List[Command]
|
67 |
+
:param \**kwargs: The parameters specific to the ChatAtomicFlow.
|
68 |
+
:type \**kwargs: Dict[str, Any]
|
69 |
+
"""
|
70 |
def __init__(self, commands: List[Command], **kwargs):
|
71 |
super().__init__(**kwargs)
|
72 |
self.system_message_prompt_template = self.system_message_prompt_template.partial(
|
|
|
75 |
|
76 |
@staticmethod
|
77 |
def _build_commands_manual(commands: List[Command]) -> str:
|
78 |
+
""" This method writes the commands that the ControllerAtomicFlow in string to pass it to the system_message_prompt_template.
|
79 |
+
|
80 |
+
:param commands: The commands that the controller can call.
|
81 |
+
:type commands: List[Command]
|
82 |
+
:return: The string containing the commands.
|
83 |
+
:rtype: str
|
84 |
+
"""
|
85 |
ret = ""
|
86 |
for i, command in enumerate(commands):
|
87 |
command_input_json_schema = json.dumps(
|
|
|
91 |
|
92 |
@classmethod
|
93 |
def instantiate_from_config(cls, config):
|
94 |
+
""" This method instantiates the flow from a configuration file.
|
95 |
+
|
96 |
+
:param config: The configuration of the flow.
|
97 |
+
:type config: Dict[str, Any]
|
98 |
+
:return: The instantiated flow.
|
99 |
+
:rtype: ControllerAtomicFlow
|
100 |
+
"""
|
101 |
flow_config = deepcopy(config)
|
102 |
|
103 |
kwargs = {"flow_config": flow_config}
|
|
|
118 |
return cls(**kwargs)
|
119 |
|
120 |
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
121 |
+
""" This method runs the flow. Note that the response of the LLM is in the JSON format, but it's not a hard constraint (it can hallucinate and return an invalid JSON)
|
122 |
+
|
123 |
+
:param input_data: The input data of the flow.
|
124 |
+
:type input_data: Dict[str, Any]
|
125 |
+
:return: The output data of the flow (thought, reasoning, criticism, command, command_args)
|
126 |
+
:rtype: Dict[str, Any]
|
127 |
+
"""
|
128 |
api_output = super().run(input_data)["api_output"].strip()
|
129 |
response = json.loads(api_output)
|
130 |
return response
|
ControllerAtomicFlow.yaml
CHANGED
@@ -58,6 +58,8 @@ system_message_prompt_template:
|
|
58 |
"thought": "thought",
|
59 |
"reasoning": "reasoning",
|
60 |
"plan": "- short bulleted\n- list that conveys\n- long-term plan",
|
|
|
|
|
61 |
"command": "command name",
|
62 |
"command_args": {
|
63 |
"arg name": "value"
|
|
|
58 |
"thought": "thought",
|
59 |
"reasoning": "reasoning",
|
60 |
"plan": "- short bulleted\n- list that conveys\n- long-term plan",
|
61 |
+
"criticism": "constructive self-criticism",
|
62 |
+
"speak": "thoughts summary to say to user",
|
63 |
"command": "command name",
|
64 |
"command_args": {
|
65 |
"arg name": "value"
|
ControllerExecutorFlow.py
CHANGED
@@ -10,7 +10,62 @@ log = logging.get_logger(__name__)
|
|
10 |
|
11 |
|
12 |
class ControllerExecutorFlow(CircularFlow):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def _on_reach_max_round(self):
|
|
|
14 |
self._state_update_dict({
|
15 |
"answer": "The maximum amount of rounds was reached before the model found an answer.",
|
16 |
"status": "unfinished"
|
@@ -18,6 +73,14 @@ class ControllerExecutorFlow(CircularFlow):
|
|
18 |
|
19 |
@CircularFlow.output_msg_payload_processor
|
20 |
def detect_finish_or_continue(self, output_payload: Dict[str, Any], src_flow: ControllerAtomicFlow) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
command = output_payload["command"]
|
22 |
if command == "finish":
|
23 |
return {
|
|
|
10 |
|
11 |
|
12 |
class ControllerExecutorFlow(CircularFlow):
|
13 |
+
""" This class implements a ControllerExecutorFlow. It's composed of a ControllerAtomicFlow and an ExecutorFlow.
|
14 |
+
Where typically the ControllerAtomicFlow is uses a LLM to decide which command to call and the ExecutorFlow (branching flow) is used to execute the command.
|
15 |
+
|
16 |
+
It contains the following subflows:
|
17 |
+
|
18 |
+
- A Controller Atomic Flow: It is a flow that to decides which command to get closer to completing it's task of accomplishing a given goal.
|
19 |
+
- An Executor Flow: It is a branching flow that uses the executes the command instructed by the ControllerAtomicFlow.
|
20 |
+
|
21 |
+
An illustration of the flow is as follows:
|
22 |
+
|
23 |
+
goal -----|-----> ControllerFlow----->|-----> (anwser,status)
|
24 |
+
^ |
|
25 |
+
| |
|
26 |
+
| v
|
27 |
+
|<----- ExecutorFlow <------|
|
28 |
+
|
29 |
+
*Configuration Parameters*:
|
30 |
+
|
31 |
+
- `name` (str): The name of the flow. Default: "CtrlEx"
|
32 |
+
- `description` (str): A description of the flow. This description is used to generate the help message of the flow.
|
33 |
+
Default: "ControllerExecutor (i.e., MRKL, ReAct) interaction implementation with Flows
|
34 |
+
that approaches the problem solving in two phases: one Flow chooses the next step and another Flow executes it.
|
35 |
+
This is repeated until the controller Flow concludes on an answer."
|
36 |
+
- `max_rounds` (int): The maximum number of rounds the flow can run for.
|
37 |
+
Default: 30.
|
38 |
+
- `subflows_config` (Dict[str,Any]): A dictionary of the subflows configurations. Default:
|
39 |
+
- `Controller`: The configuration of the Controller Flow. By default, it a ControllerAtomicFlow. Default parameters:
|
40 |
+
- `finish` (Dict[str,Any]): The configuration of the finish command. Default parameters:
|
41 |
+
- `description` (str): The description of the command.
|
42 |
+
Default: "Signal that the objective has been satisfied, and returns the answer to the user."
|
43 |
+
- `input_args` (List[str]): The input arguments of the command. Default: ["answer"]
|
44 |
+
- All other parameters are inherited from the default configuration of ControllerAtomicFlow (see ControllerAtomicFlow)
|
45 |
+
- `Executor`: The configuration of the Executor Flow. By default, it's a BranchingFlow. There are no default parameters, the flow
|
46 |
+
parameter to to be defined is:
|
47 |
+
- `subflows_config` (Dict[str,Any]): A dictionary of the configuration of the subflows of the branching flow.
|
48 |
+
These subflows are typically also the possible commands of the Controller Flow. Default: []
|
49 |
+
- `early_exit_key` (str): The key that is used to exit the flow. Default: "EARLY_EXIT"
|
50 |
+
- `topology` (str): The topology of the flow which is "circular".
|
51 |
+
By default, the topology is the one shown in the illustration above
|
52 |
+
(the topology is also described in ControllerExecutorFlow.yaml).
|
53 |
+
|
54 |
+
|
55 |
+
*Input Interface*:
|
56 |
+
|
57 |
+
- `goal` (str): The goal of the controller. Usually asked by the user/human (e.g. "I want to know the occupation and birth date of Michael Jordan.")
|
58 |
+
|
59 |
+
*Output Interface*:
|
60 |
+
|
61 |
+
- `answer` (str): The answer of the flow to the query (e.g. "Michael Jordan is a basketball player and business man. He was born on February 17, 1963.")
|
62 |
+
- `status` (str): The status of the flow. It can be "finished" or "unfinished". If the status is "unfinished", it's usually because the maximum amount of rounds was reached before the model found an answer.
|
63 |
+
|
64 |
+
:param flow_config: The configuration of the flow (see Configuration Parameters).
|
65 |
+
:param subflows: A list of subflows. Required when instantiating the subflow programmatically (it replaces subflows_config from flow_config).
|
66 |
+
"""
|
67 |
def _on_reach_max_round(self):
|
68 |
+
""" This method is called when the flow reaches the maximum amount of rounds. It updates the state of the flow and starts the process of terminating the flow."""
|
69 |
self._state_update_dict({
|
70 |
"answer": "The maximum amount of rounds was reached before the model found an answer.",
|
71 |
"status": "unfinished"
|
|
|
73 |
|
74 |
@CircularFlow.output_msg_payload_processor
|
75 |
def detect_finish_or_continue(self, output_payload: Dict[str, Any], src_flow: ControllerAtomicFlow) -> Dict[str, Any]:
|
76 |
+
""" This method is called when the ExecutorAtomicFlow receives a message from the ControllerAtomicFlow. It checks if the flow should finish or continue.
|
77 |
+
|
78 |
+
:param output_payload: The output payload of the ControllerAtomicFlow.
|
79 |
+
:type output_payload: Dict[str, Any]
|
80 |
+
:param src_flow: The ControllerAtomicFlow.
|
81 |
+
:type src_flow: ControllerAtomicFlow
|
82 |
+
:return: The output payload of the ControllerAtomicFlow.
|
83 |
+
"""
|
84 |
command = output_payload["command"]
|
85 |
if command == "finish":
|
86 |
return {
|
ControllerExecutorFlow.yaml
CHANGED
@@ -21,9 +21,7 @@ subflows_config:
|
|
21 |
# wiki_search:
|
22 |
# description: "Performs a search on Wikipedia."
|
23 |
# input_args: ["search_term"]
|
24 |
-
|
25 |
-
# description: "Query the search engine DuckDuckGo."
|
26 |
-
# input_args: ["query"]
|
27 |
|
28 |
Executor:
|
29 |
_target_: flows.base_flows.BranchingFlow.instantiate_from_default_config
|
@@ -31,10 +29,7 @@ subflows_config:
|
|
31 |
# subflows_config:
|
32 |
# wiki_search:
|
33 |
# _target_: .WikiSearchAtomicFlow.instantiate_from_default_config
|
34 |
-
|
35 |
-
# _target_: flows.application_flows.LCToolFlowModule.LCToolFlow.instantiate_from_default_config
|
36 |
-
# backend:
|
37 |
-
# _target_: langchain.tools.DuckDuckGoSearchRun
|
38 |
|
39 |
early_exit_key: "EARLY_EXIT"
|
40 |
|
|
|
21 |
# wiki_search:
|
22 |
# description: "Performs a search on Wikipedia."
|
23 |
# input_args: ["search_term"]
|
24 |
+
|
|
|
|
|
25 |
|
26 |
Executor:
|
27 |
_target_: flows.base_flows.BranchingFlow.instantiate_from_default_config
|
|
|
29 |
# subflows_config:
|
30 |
# wiki_search:
|
31 |
# _target_: .WikiSearchAtomicFlow.instantiate_from_default_config
|
32 |
+
|
|
|
|
|
|
|
33 |
|
34 |
early_exit_key: "EARLY_EXIT"
|
35 |
|
README.md
CHANGED
@@ -1,5 +1,366 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
# ToDo
|
|
|
1 |
+
# Table of Contents
|
2 |
+
|
3 |
+
* [ControllerAtomicFlow](#ControllerAtomicFlow)
|
4 |
+
* [Command](#ControllerAtomicFlow.Command)
|
5 |
+
* [ControllerAtomicFlow](#ControllerAtomicFlow.ControllerAtomicFlow)
|
6 |
+
* [instantiate\_from\_config](#ControllerAtomicFlow.ControllerAtomicFlow.instantiate_from_config)
|
7 |
+
* [run](#ControllerAtomicFlow.ControllerAtomicFlow.run)
|
8 |
+
* [\_\_init\_\_](#__init__)
|
9 |
+
* [WikiSearchAtomicFlow](#WikiSearchAtomicFlow)
|
10 |
+
* [WikiSearchAtomicFlow](#WikiSearchAtomicFlow.WikiSearchAtomicFlow)
|
11 |
+
* [run](#WikiSearchAtomicFlow.WikiSearchAtomicFlow.run)
|
12 |
+
* [wikipediaAPI](#wikipediaAPI)
|
13 |
+
* [WikipediaAPIWrapper](#wikipediaAPI.WikipediaAPIWrapper)
|
14 |
+
* [validate\_environment](#wikipediaAPI.WikipediaAPIWrapper.validate_environment)
|
15 |
+
* [run](#wikipediaAPI.WikipediaAPIWrapper.run)
|
16 |
+
* [search\_page\_titles](#wikipediaAPI.WikipediaAPIWrapper.search_page_titles)
|
17 |
+
* [ControllerExecutorFlow](#ControllerExecutorFlow)
|
18 |
+
* [ControllerExecutorFlow](#ControllerExecutorFlow.ControllerExecutorFlow)
|
19 |
+
* [detect\_finish\_or\_continue](#ControllerExecutorFlow.ControllerExecutorFlow.detect_finish_or_continue)
|
20 |
+
|
21 |
+
<a id="ControllerAtomicFlow"></a>
|
22 |
+
|
23 |
+
# ControllerAtomicFlow
|
24 |
+
|
25 |
+
<a id="ControllerAtomicFlow.Command"></a>
|
26 |
+
|
27 |
+
## Command Objects
|
28 |
+
|
29 |
+
```python
|
30 |
+
@dataclass
|
31 |
+
class Command()
|
32 |
+
```
|
33 |
+
|
34 |
+
The command class is used to store the information about the commands that the user can give to the controller.
|
35 |
+
|
36 |
+
**Arguments**:
|
37 |
+
|
38 |
+
- `name` (`str`): The name of the command.
|
39 |
+
- `description` (`str`): The description of the command.
|
40 |
+
- `input_args` (`List[str]`): The input arguments of the command.
|
41 |
+
|
42 |
+
<a id="ControllerAtomicFlow.ControllerAtomicFlow"></a>
|
43 |
+
|
44 |
+
## ControllerAtomicFlow Objects
|
45 |
+
|
46 |
+
```python
|
47 |
+
class ControllerAtomicFlow(ChatAtomicFlow)
|
48 |
+
```
|
49 |
+
|
50 |
+
The ControllerAtomicFlow is an atomic flow that, given an observation and a goal, can call a set of commands and arguments which are then usually executed by an ExecutorAtomicFlow (branching flow).
|
51 |
+
|
52 |
+
*Configuration Parameters*
|
53 |
+
|
54 |
+
- `name` (str): The name of the flow. Default: "ControllerFlow"
|
55 |
+
- `description` (str): A description of the flow. This description is used to generate the help message of the flow.
|
56 |
+
Default: "Proposes the next action to take towards achieving the goal, and prepares the input for the executor."
|
57 |
+
- `enable_cache` (bool): Whether to enable caching or not. Default: True
|
58 |
+
- `commands` (List[Dict[str,Any]]): A list of commands that the controller can call. Default: []
|
59 |
+
- `finish` (Dict[str,Any]): The configuration of the finish command. Default parameters: No default parameters.
|
60 |
+
- `system_message_prompt_template` (Dict[str, Any]): The prompt template used to generate the system message.
|
61 |
+
By default, it's type is flows.prompt_template.JinjaPrompt. It's default parameters are:
|
62 |
+
- `template` (str): The template of the prompt. Default: see ControllerAtomicFlow.yaml for the default template.
|
63 |
+
- `input_variables` (List[str]): The input variables of the prompt. Default: ["commands"]. Note that the commands are the commands of the executor
|
64 |
+
(subflows of branching flow) and are actually to the system prompt template via the `_build_commands_manual` function of this class.
|
65 |
+
- `human_message_prompt_template` (Dict[str, Any]): The prompt template of the human/user message (message used everytime the except the first time in).
|
66 |
+
It's passed as the user message to the LLM. By default its of type flows.prompt_template.JinjaPrompt and has the following parameters:
|
67 |
+
- `template` (str): The template of the prompt. Default: see ControllerAtomicFlow.yaml for the default template.
|
68 |
+
- `input_variables` (List[str]): The input variables of the prompt. Default: ["observation"]
|
69 |
+
- init_human_message_prompt_template` (Dict[str, Any]): The prompt template of the human/user message used to initialize the conversation
|
70 |
+
(first time in). It is used to generate the human message. It's passed as the user message to the LLM.
|
71 |
+
By default its of type flows.prompt_template.JinjaPrompt and has the following parameters:
|
72 |
+
- `template` (str): The template of the prompt. Default: see ControllerAtomicFlow.yaml for the default template.
|
73 |
+
- `input_variables` (List[str]): The input variables of the prompt. Default: ["goal"]
|
74 |
+
- All other parameters are inherited from the default configuration of ChatAtomicFlow (see Flowcard, i.e. README.md, of ChatAtomicFlowModule).
|
75 |
+
|
76 |
+
*Initial Input Interface (this is the interface used the first time the flow is called)*:
|
77 |
+
- `goal` (str): The goal of the controller. Usually asked by the user/human (e.g. "I want to know the occupation and birth date of Michael Jordan.")
|
78 |
+
|
79 |
+
*Input Interface (this is the interface used after the first time the flow is called)*:
|
80 |
+
- `observation` (str): The observation of the controller's previous action. Usually the response of the ExecutorAtomicFlow (e.g. "The result of a wikipedia search (if the ExecutorAtomicFlow has a WikipediaExecutorAtomicFlow).")
|
81 |
+
|
82 |
+
*Output Interface:*
|
83 |
+
- `thought` (str): The thought of the controller on what to do next (which command to call)
|
84 |
+
- `reasoning` (str): The reasoning of the controller on why it thinks the command it wants to call is the right one
|
85 |
+
- `criticism` (str): The criticism of the controller of it's thinking process
|
86 |
+
- `command` (str): The command to the executor chooses to call
|
87 |
+
- `command_args` (Dict[str, Any]): The arguments of the command to call
|
88 |
+
|
89 |
+
**Arguments**:
|
90 |
+
|
91 |
+
- `commands` (`List[Command]`): The commands that the controller can call (typically the commands of the executor).
|
92 |
+
- `\**kwargs` (`Dict[str, Any]`): The parameters specific to the ChatAtomicFlow.
|
93 |
+
|
94 |
+
<a id="ControllerAtomicFlow.ControllerAtomicFlow.instantiate_from_config"></a>
|
95 |
+
|
96 |
+
#### instantiate\_from\_config
|
97 |
+
|
98 |
+
```python
|
99 |
+
@classmethod
|
100 |
+
def instantiate_from_config(cls, config)
|
101 |
+
```
|
102 |
+
|
103 |
+
This method instantiates the flow from a configuration file.
|
104 |
+
|
105 |
+
**Arguments**:
|
106 |
+
|
107 |
+
- `config` (`Dict[str, Any]`): The configuration of the flow.
|
108 |
+
|
109 |
+
**Returns**:
|
110 |
+
|
111 |
+
`ControllerAtomicFlow`: The instantiated flow.
|
112 |
+
|
113 |
+
<a id="ControllerAtomicFlow.ControllerAtomicFlow.run"></a>
|
114 |
+
|
115 |
+
#### run
|
116 |
+
|
117 |
+
```python
|
118 |
+
def run(input_data: Dict[str, Any]) -> Dict[str, Any]
|
119 |
+
```
|
120 |
+
|
121 |
+
This method runs the flow. Note that the response of the LLM is in the JSON format, but it's not a hard constraint (it can hallucinate and return an invalid JSON)
|
122 |
+
|
123 |
+
**Arguments**:
|
124 |
+
|
125 |
+
- `input_data` (`Dict[str, Any]`): The input data of the flow.
|
126 |
+
|
127 |
+
**Returns**:
|
128 |
+
|
129 |
+
`Dict[str, Any]`: The output data of the flow (thought, reasoning, criticism, command, command_args)
|
130 |
+
|
131 |
+
<a id="__init__"></a>
|
132 |
+
|
133 |
+
# \_\_init\_\_
|
134 |
+
|
135 |
+
<a id="WikiSearchAtomicFlow"></a>
|
136 |
+
|
137 |
+
# WikiSearchAtomicFlow
|
138 |
+
|
139 |
+
<a id="WikiSearchAtomicFlow.WikiSearchAtomicFlow"></a>
|
140 |
+
|
141 |
+
## WikiSearchAtomicFlow Objects
|
142 |
+
|
143 |
+
```python
|
144 |
+
class WikiSearchAtomicFlow(AtomicFlow)
|
145 |
+
```
|
146 |
+
|
147 |
+
This class implements a WikiSearch Atomic Flow. It's used to execute a Wikipedia search and get page summaries.
|
148 |
+
|
149 |
+
*Configuration Parameters*:
|
150 |
+
|
151 |
+
- `name` (str): The name of the flow. Default: "WikiSearchAtomicFlow"
|
152 |
+
- `description` (str): A description of the flow. This description is used to generate the help message of the flow.
|
153 |
+
Default: "A Flow that queries the wikipedia API for a page content."
|
154 |
+
- `lang` (str): The language of the Wikipedia page. Default: "en"
|
155 |
+
- `top_k_results` (int): The number of top results to return. Default: 5
|
156 |
+
- `doc_content_chars_max` (int): The maximum number of characters of the content of the Wikipedia page. Default: 3000
|
157 |
+
- Other parameters are inherited from the default configuration of AtomicFlow (see AtomicFlow)
|
158 |
+
|
159 |
+
*input_interface*:
|
160 |
+
|
161 |
+
- `search_term` (str): The search term to search for.
|
162 |
+
|
163 |
+
*output_interface*:
|
164 |
+
|
165 |
+
- `wiki_content` (str): The content of the Wikipedia page.
|
166 |
+
|
167 |
+
**Arguments**:
|
168 |
+
|
169 |
+
- `\**kwargs`: The keyword arguments passed to the AtomicFlow constructor
|
170 |
+
|
171 |
+
<a id="WikiSearchAtomicFlow.WikiSearchAtomicFlow.run"></a>
|
172 |
+
|
173 |
+
#### run
|
174 |
+
|
175 |
+
```python
|
176 |
+
def run(input_data: Dict[str, Any]) -> Dict[str, Any]
|
177 |
+
```
|
178 |
+
|
179 |
+
Runs the WikiSearch Atomic Flow. It's used to execute a Wikipedia search and get page summaries.
|
180 |
+
|
181 |
+
**Arguments**:
|
182 |
+
|
183 |
+
- `input_data` (`Dict[str, Any]`): The input data dictionary
|
184 |
+
|
185 |
+
**Returns**:
|
186 |
+
|
187 |
+
`Dict[str, Any]`: The output data dictionary
|
188 |
+
|
189 |
+
<a id="wikipediaAPI"></a>
|
190 |
+
|
191 |
+
# wikipediaAPI
|
192 |
+
|
193 |
+
Util that calls Wikipedia. references: https://github.com/hwchase17/langchain/blob/9b615022e2b6a3591347ad77a3e21aad6cf24c49/docs/extras/modules/agents/tools/integrations/wikipedia.ipynb#L36
|
194 |
+
|
195 |
+
<a id="wikipediaAPI.WikipediaAPIWrapper"></a>
|
196 |
+
|
197 |
+
## WikipediaAPIWrapper Objects
|
198 |
+
|
199 |
+
```python
|
200 |
+
class WikipediaAPIWrapper(BaseModel)
|
201 |
+
```
|
202 |
+
|
203 |
+
Wrapper around WikipediaAPI.
|
204 |
+
|
205 |
+
To use, you should have the ``wikipedia`` python package installed.
|
206 |
+
This wrapper will use the Wikipedia API to conduct searches and
|
207 |
+
fetch page summaries. By default, it will return the page summaries
|
208 |
+
of the top-k results.
|
209 |
+
It limits the Document content by doc_content_chars_max.
|
210 |
+
|
211 |
+
**Arguments**:
|
212 |
+
|
213 |
+
- `top_k_results` (`int`): The number of results to return.
|
214 |
+
- `lang` (`str`): The language to use for the Wikipedia API.
|
215 |
+
- `doc_content_chars_max` (`int`): The maximum number of characters in the Document content.
|
216 |
+
|
217 |
+
<a id="wikipediaAPI.WikipediaAPIWrapper.validate_environment"></a>
|
218 |
+
|
219 |
+
#### validate\_environment
|
220 |
+
|
221 |
+
```python
|
222 |
+
@root_validator()
|
223 |
+
def validate_environment(cls, values: Dict) -> Dict
|
224 |
+
```
|
225 |
+
|
226 |
+
Validate that the python package exists in environment.
|
227 |
+
|
228 |
+
**Arguments**:
|
229 |
+
|
230 |
+
- `values` (`Dict`): The values to validate.
|
231 |
+
|
232 |
+
**Raises**:
|
233 |
+
|
234 |
+
- `ImportError`: If the package is not installed.
|
235 |
+
|
236 |
+
**Returns**:
|
237 |
+
|
238 |
+
`Dict`: The validated values.
|
239 |
+
|
240 |
+
<a id="wikipediaAPI.WikipediaAPIWrapper.run"></a>
|
241 |
+
|
242 |
+
#### run
|
243 |
+
|
244 |
+
```python
|
245 |
+
def run(query: str) -> str
|
246 |
+
```
|
247 |
+
|
248 |
+
Run Wikipedia search and get page summaries.
|
249 |
+
|
250 |
+
**Arguments**:
|
251 |
+
|
252 |
+
- `query` (`str`): The query to search for.
|
253 |
+
|
254 |
+
**Returns**:
|
255 |
+
|
256 |
+
`str`: The page summaries.
|
257 |
+
|
258 |
+
<a id="wikipediaAPI.WikipediaAPIWrapper.search_page_titles"></a>
|
259 |
+
|
260 |
+
#### search\_page\_titles
|
261 |
+
|
262 |
+
```python
|
263 |
+
def search_page_titles(query: str) -> List[str]
|
264 |
+
```
|
265 |
+
|
266 |
+
Run Wikipedia search and get page summaries.
|
267 |
+
|
268 |
+
**Arguments**:
|
269 |
+
|
270 |
+
- `query` (`str`): The query to search for.
|
271 |
+
|
272 |
+
**Returns**:
|
273 |
+
|
274 |
+
`List[str]`: The page titles.
|
275 |
+
|
276 |
+
<a id="ControllerExecutorFlow"></a>
|
277 |
+
|
278 |
+
# ControllerExecutorFlow
|
279 |
+
|
280 |
+
<a id="ControllerExecutorFlow.ControllerExecutorFlow"></a>
|
281 |
+
|
282 |
+
## ControllerExecutorFlow Objects
|
283 |
+
|
284 |
+
```python
|
285 |
+
class ControllerExecutorFlow(CircularFlow)
|
286 |
+
```
|
287 |
+
|
288 |
+
This class implements a ControllerExecutorFlow. It's composed of a ControllerAtomicFlow and an ExecutorFlow.
|
289 |
+
|
290 |
+
Where typically the ControllerAtomicFlow is uses a LLM to decide which command to call and the ExecutorFlow (branching flow) is used to execute the command.
|
291 |
+
|
292 |
+
It contains the following subflows:
|
293 |
+
|
294 |
+
- A Controller Atomic Flow: It is a flow that to decides which command to get closer to completing it's task of accomplishing a given goal.
|
295 |
+
- An Executor Flow: It is a branching flow that uses the executes the command instructed by the ControllerAtomicFlow.
|
296 |
+
|
297 |
+
An illustration of the flow is as follows:
|
298 |
+
|
299 |
+
goal -----|-----> ControllerFlow----->|-----> (anwser,status)
|
300 |
+
^ |
|
301 |
+
| |
|
302 |
+
| v
|
303 |
+
|<----- ExecutorFlow <------|
|
304 |
+
|
305 |
+
*Configuration Parameters*:
|
306 |
+
|
307 |
+
- `name` (str): The name of the flow. Default: "CtrlEx"
|
308 |
+
- `description` (str): A description of the flow. This description is used to generate the help message of the flow.
|
309 |
+
Default: "ControllerExecutor (i.e., MRKL, ReAct) interaction implementation with Flows
|
310 |
+
that approaches the problem solving in two phases: one Flow chooses the next step and another Flow executes it.
|
311 |
+
This is repeated until the controller Flow concludes on an answer."
|
312 |
+
- `max_rounds` (int): The maximum number of rounds the flow can run for.
|
313 |
+
Default: 30.
|
314 |
+
- `subflows_config` (Dict[str,Any]): A dictionary of the subflows configurations. Default:
|
315 |
+
- `Controller`: The configuration of the Controller Flow. By default, it a ControllerAtomicFlow. Default parameters:
|
316 |
+
- `finish` (Dict[str,Any]): The configuration of the finish command. Default parameters:
|
317 |
+
- `description` (str): The description of the command.
|
318 |
+
Default: "Signal that the objective has been satisfied, and returns the answer to the user."
|
319 |
+
- `input_args` (List[str]): The input arguments of the command. Default: ["answer"]
|
320 |
+
- All other parameters are inherited from the default configuration of ControllerAtomicFlow (see ControllerAtomicFlow)
|
321 |
+
- `Executor`: The configuration of the Executor Flow. By default, it's a BranchingFlow. There are no default parameters, the flow
|
322 |
+
parameter to to be defined is:
|
323 |
+
- `subflows_config` (Dict[str,Any]): A dictionary of the configuration of the subflows of the branching flow.
|
324 |
+
These subflows are typically also the possible commands of the Controller Flow. Default: []
|
325 |
+
- `early_exit_key` (str): The key that is used to exit the flow. Default: "EARLY_EXIT"
|
326 |
+
- `topology` (str): The topology of the flow which is "circular".
|
327 |
+
By default, the topology is the one shown in the illustration above
|
328 |
+
(the topology is also described in ControllerExecutorFlow.yaml).
|
329 |
+
|
330 |
+
|
331 |
+
*Input Interface*:
|
332 |
+
|
333 |
+
- `goal` (str): The goal of the controller. Usually asked by the user/human (e.g. "I want to know the occupation and birth date of Michael Jordan.")
|
334 |
+
|
335 |
+
*Output Interface*:
|
336 |
+
|
337 |
+
- `answer` (str): The answer of the flow to the query (e.g. "Michael Jordan is a basketball player and business man. He was born on February 17, 1963.")
|
338 |
+
- `status` (str): The status of the flow. It can be "finished" or "unfinished". If the status is "unfinished", it's usually because the maximum amount of rounds was reached before the model found an answer.
|
339 |
+
|
340 |
+
**Arguments**:
|
341 |
+
|
342 |
+
- `flow_config`: The configuration of the flow (see Configuration Parameters).
|
343 |
+
- `subflows`: A list of subflows. Required when instantiating the subflow programmatically (it replaces subflows_config from flow_config).
|
344 |
+
|
345 |
+
<a id="ControllerExecutorFlow.ControllerExecutorFlow.detect_finish_or_continue"></a>
|
346 |
+
|
347 |
+
#### detect\_finish\_or\_continue
|
348 |
+
|
349 |
+
```python
|
350 |
+
@CircularFlow.output_msg_payload_processor
|
351 |
+
def detect_finish_or_continue(
|
352 |
+
output_payload: Dict[str, Any],
|
353 |
+
src_flow: ControllerAtomicFlow) -> Dict[str, Any]
|
354 |
+
```
|
355 |
+
|
356 |
+
This method is called when the ExecutorAtomicFlow receives a message from the ControllerAtomicFlow. It checks if the flow should finish or continue.
|
357 |
+
|
358 |
+
**Arguments**:
|
359 |
+
|
360 |
+
- `output_payload` (`Dict[str, Any]`): The output payload of the ControllerAtomicFlow.
|
361 |
+
- `src_flow` (`ControllerAtomicFlow`): The ControllerAtomicFlow.
|
362 |
+
|
363 |
+
**Returns**:
|
364 |
+
|
365 |
+
The output payload of the ControllerAtomicFlow.
|
366 |
|
|
WikiSearchAtomicFlow.py
CHANGED
@@ -11,6 +11,28 @@ log = logging.get_logger(__name__)
|
|
11 |
|
12 |
|
13 |
class WikiSearchAtomicFlow(AtomicFlow):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
REQUIRED_KEYS_CONFIG = ["lang", "top_k_results", "doc_content_chars_max"]
|
15 |
REQUIRED_KEYS_CONSTRUCTOR = []
|
16 |
|
@@ -23,6 +45,13 @@ class WikiSearchAtomicFlow(AtomicFlow):
|
|
23 |
|
24 |
def run(self,
|
25 |
input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# ~~~ Process input ~~~
|
28 |
term = input_data.get("search_term", None)
|
|
|
11 |
|
12 |
|
13 |
class WikiSearchAtomicFlow(AtomicFlow):
|
14 |
+
""" This class implements a WikiSearch Atomic Flow. It's used to execute a Wikipedia search and get page summaries.
|
15 |
+
|
16 |
+
*Configuration Parameters*:
|
17 |
+
|
18 |
+
- `name` (str): The name of the flow. Default: "WikiSearchAtomicFlow"
|
19 |
+
- `description` (str): A description of the flow. This description is used to generate the help message of the flow.
|
20 |
+
Default: "A Flow that queries the wikipedia API for a page content."
|
21 |
+
- `lang` (str): The language of the Wikipedia page. Default: "en"
|
22 |
+
- `top_k_results` (int): The number of top results to return. Default: 5
|
23 |
+
- `doc_content_chars_max` (int): The maximum number of characters of the content of the Wikipedia page. Default: 3000
|
24 |
+
- Other parameters are inherited from the default configuration of AtomicFlow (see AtomicFlow)
|
25 |
+
|
26 |
+
*input_interface*:
|
27 |
+
|
28 |
+
- `search_term` (str): The search term to search for.
|
29 |
+
|
30 |
+
*output_interface*:
|
31 |
+
|
32 |
+
- `wiki_content` (str): The content of the Wikipedia page.
|
33 |
+
|
34 |
+
:param \**kwargs: The keyword arguments passed to the AtomicFlow constructor
|
35 |
+
"""
|
36 |
REQUIRED_KEYS_CONFIG = ["lang", "top_k_results", "doc_content_chars_max"]
|
37 |
REQUIRED_KEYS_CONSTRUCTOR = []
|
38 |
|
|
|
45 |
|
46 |
def run(self,
|
47 |
input_data: Dict[str, Any]) -> Dict[str, Any]:
|
48 |
+
""" Runs the WikiSearch Atomic Flow. It's used to execute a Wikipedia search and get page summaries.
|
49 |
+
|
50 |
+
:param input_data: The input data dictionary
|
51 |
+
:type input_data: Dict[str, Any]
|
52 |
+
:return: The output data dictionary
|
53 |
+
:rtype: Dict[str, Any]
|
54 |
+
"""
|
55 |
|
56 |
# ~~~ Process input ~~~
|
57 |
term = input_data.get("search_term", None)
|
__init__.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# ~~~ Specify the dependencies ~~~
|
2 |
dependencies = [
|
3 |
-
{"url": "aiflows/
|
4 |
]
|
5 |
from flows import flow_verse
|
6 |
|
|
|
1 |
# ~~~ Specify the dependencies ~~~
|
2 |
dependencies = [
|
3 |
+
{"url": "aiflows/ChatFlowModule", "revision": "a749ad10ed39776ba6721c37d0dc22af49ca0f17"},
|
4 |
]
|
5 |
from flows import flow_verse
|
6 |
|
demo.yaml
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flow:
|
2 |
+
_target_: aiflows.ControllerExecutorFlowModule.ControllerExecutorFlow.instantiate_from_default_config
|
3 |
+
max_rounds: 30
|
4 |
+
|
5 |
+
### Subflows specification
|
6 |
+
subflows_config:
|
7 |
+
Controller:
|
8 |
+
_target_: aiflows.ControllerExecutorFlowModule.ControllerAtomicFlow.instantiate_from_default_config
|
9 |
+
commands:
|
10 |
+
wiki_search:
|
11 |
+
description: "Performs a search on Wikipedia."
|
12 |
+
input_args: [ "search_term" ]
|
13 |
+
finish:
|
14 |
+
description: "Signal that the objective has been satisfied, and returns the answer to the user."
|
15 |
+
input_args: [ "answer" ]
|
16 |
+
backend:
|
17 |
+
_target_: flows.backends.llm_lite.LiteLLMBackend
|
18 |
+
api_infos: ???
|
19 |
+
model_name:
|
20 |
+
openai: "gpt-3.5-turbo"
|
21 |
+
azure: "azure/gpt-4"
|
22 |
+
|
23 |
+
Executor:
|
24 |
+
_target_: flows.base_flows.BranchingFlow.instantiate_from_default_config
|
25 |
+
subflows_config:
|
26 |
+
wiki_search:
|
27 |
+
_target_: aiflows.ControllerExecutorFlowModule.WikiSearchAtomicFlow.instantiate_from_default_config
|
pip_requirements.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
wikipedia==1.4.0
|
run.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import hydra
|
4 |
+
|
5 |
+
import flows
|
6 |
+
from flows.flow_launchers import FlowLauncher
|
7 |
+
from flows.backends.api_info import ApiInfo
|
8 |
+
from flows.utils.general_helpers import read_yaml_file
|
9 |
+
|
10 |
+
from flows import logging
|
11 |
+
from flows.flow_cache import CACHING_PARAMETERS, clear_cache
|
12 |
+
|
13 |
+
CACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching
|
14 |
+
# clear_cache() # Uncomment this line to clear the cache
|
15 |
+
|
16 |
+
logging.set_verbosity_debug()
|
17 |
+
|
18 |
+
dependencies = [
|
19 |
+
{"url": "aiflows/ControllerExecutorFlowModule", "revision": os.getcwd()},
|
20 |
+
]
|
21 |
+
from flows import flow_verse
|
22 |
+
|
23 |
+
flow_verse.sync_dependencies(dependencies)
|
24 |
+
|
25 |
+
if __name__ == "__main__":
|
26 |
+
# ~~~ Set the API information ~~~
|
27 |
+
# OpenAI backend
|
28 |
+
api_information = [ApiInfo(backend_used="openai",
|
29 |
+
api_key = os.getenv("OPENAI_API_KEY"))]
|
30 |
+
# Azure backend
|
31 |
+
# api_information = ApiInfo(backend_used = "azure",
|
32 |
+
# api_base = os.getenv("AZURE_API_BASE"),
|
33 |
+
# api_key = os.getenv("AZURE_OPENAI_KEY"),
|
34 |
+
# api_version = os.getenv("AZURE_API_VERSION") )
|
35 |
+
|
36 |
+
path_to_output_file = None
|
37 |
+
# path_to_output_file = "output.jsonl" # Uncomment this line to save the output to disk
|
38 |
+
|
39 |
+
root_dir = "."
|
40 |
+
cfg_path = os.path.join(root_dir, "demo.yaml")
|
41 |
+
cfg = read_yaml_file(cfg_path)
|
42 |
+
# print(cfg["flow"].keys())
|
43 |
+
cfg["flow"]["subflows_config"]["Controller"]["backend"]["api_infos"] = api_information
|
44 |
+
|
45 |
+
# ~~~ Instantiate the Flow ~~~
|
46 |
+
flow_with_interfaces = {
|
47 |
+
"flow": hydra.utils.instantiate(cfg['flow'], _recursive_=False, _convert_="partial"),
|
48 |
+
"input_interface": (
|
49 |
+
None
|
50 |
+
if cfg.get( "input_interface", None) is None
|
51 |
+
else hydra.utils.instantiate(cfg['input_interface'], _recursive_=False)
|
52 |
+
),
|
53 |
+
"output_interface": (
|
54 |
+
None
|
55 |
+
if cfg.get( "output_interface", None) is None
|
56 |
+
else hydra.utils.instantiate(cfg['output_interface'], _recursive_=False)
|
57 |
+
),
|
58 |
+
}
|
59 |
+
|
60 |
+
# ~~~ Get the data ~~~
|
61 |
+
# This can be a list of samples
|
62 |
+
# data = {"id": 0, "goal": "Answer the following question: What is the population of Canada?"} # Uses wikipedia
|
63 |
+
data = {"id": 0, "goal": "Answer the following question: Who was the NBA champion in 2023?"}
|
64 |
+
|
65 |
+
# ~~~ Run inference ~~~
|
66 |
+
path_to_output_file = None
|
67 |
+
# path_to_output_file = "output.jsonl" # Uncomment this line to save the output to disk
|
68 |
+
|
69 |
+
_, outputs = FlowLauncher.launch(
|
70 |
+
flow_with_interfaces=flow_with_interfaces,
|
71 |
+
data=data,
|
72 |
+
path_to_output_file=path_to_output_file
|
73 |
+
)
|
74 |
+
|
75 |
+
# ~~~ Print the output ~~~
|
76 |
+
flow_output_data = outputs[0]
|
77 |
+
print(flow_output_data)
|
wikipediaAPI.py
CHANGED
@@ -17,6 +17,14 @@ class WikipediaAPIWrapper(BaseModel):
|
|
17 |
fetch page summaries. By default, it will return the page summaries
|
18 |
of the top-k results.
|
19 |
It limits the Document content by doc_content_chars_max.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
"""
|
21 |
|
22 |
wiki_client: Any
|
@@ -26,7 +34,14 @@ class WikipediaAPIWrapper(BaseModel):
|
|
26 |
|
27 |
@root_validator()
|
28 |
def validate_environment(cls, values: Dict) -> Dict:
|
29 |
-
"""Validate that the python package exists in environment.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
try:
|
31 |
import wikipedia
|
32 |
|
@@ -40,7 +55,13 @@ class WikipediaAPIWrapper(BaseModel):
|
|
40 |
return values
|
41 |
|
42 |
def run(self, query: str) -> str:
|
43 |
-
"""Run Wikipedia search and get page summaries.
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
page_titles = self.search_page_titles(query)
|
46 |
summaries = []
|
@@ -53,6 +74,13 @@ class WikipediaAPIWrapper(BaseModel):
|
|
53 |
return "\n\n".join(summaries)[: self.doc_content_chars_max]
|
54 |
|
55 |
def _fetch_page(self, page: str) -> Optional[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
try:
|
57 |
return self.wiki_client.page(title=page, auto_suggest=False).content[: self.doc_content_chars_max]
|
58 |
except (
|
@@ -62,7 +90,13 @@ class WikipediaAPIWrapper(BaseModel):
|
|
62 |
return None
|
63 |
|
64 |
def search_page_titles(self, query: str) -> List[str]:
|
65 |
-
"""Run Wikipedia search and get page summaries.
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
return self.wiki_client.search(query[:WIKIPEDIA_MAX_QUERY_LENGTH])[:self.top_k_results]
|
68 |
|
@@ -98,6 +132,15 @@ class WikipediaAPIWrapper(BaseModel):
|
|
98 |
|
99 |
@staticmethod
|
100 |
def _formatted_page_summary(page_title: str, wiki_page: Any) -> Optional[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
return f"Page: {page_title}\nSummary: {wiki_page.summary}"
|
102 |
|
103 |
# def load(self, query: str) -> List[Document]:
|
|
|
17 |
fetch page summaries. By default, it will return the page summaries
|
18 |
of the top-k results.
|
19 |
It limits the Document content by doc_content_chars_max.
|
20 |
+
|
21 |
+
:param top_k_results: The number of results to return.
|
22 |
+
:type top_k_results: int
|
23 |
+
:param lang: The language to use for the Wikipedia API.
|
24 |
+
:type lang: str
|
25 |
+
:param doc_content_chars_max: The maximum number of characters in the Document content.
|
26 |
+
:type doc_content_chars_max: int
|
27 |
+
:wiki_client: The Wikipedia API client.
|
28 |
"""
|
29 |
|
30 |
wiki_client: Any
|
|
|
34 |
|
35 |
@root_validator()
|
36 |
def validate_environment(cls, values: Dict) -> Dict:
|
37 |
+
"""Validate that the python package exists in environment.
|
38 |
+
|
39 |
+
:param values: The values to validate.
|
40 |
+
:type values: Dict
|
41 |
+
:return: The validated values.
|
42 |
+
:rtype: Dict
|
43 |
+
:raises ImportError: If the package is not installed.
|
44 |
+
"""
|
45 |
try:
|
46 |
import wikipedia
|
47 |
|
|
|
55 |
return values
|
56 |
|
57 |
def run(self, query: str) -> str:
|
58 |
+
"""Run Wikipedia search and get page summaries.
|
59 |
+
|
60 |
+
:param query: The query to search for.
|
61 |
+
:type query: str
|
62 |
+
:return: The page summaries.
|
63 |
+
:rtype: str
|
64 |
+
"""
|
65 |
|
66 |
page_titles = self.search_page_titles(query)
|
67 |
summaries = []
|
|
|
74 |
return "\n\n".join(summaries)[: self.doc_content_chars_max]
|
75 |
|
76 |
def _fetch_page(self, page: str) -> Optional[str]:
|
77 |
+
""" Fetch page content from Wikipedia.
|
78 |
+
|
79 |
+
:param page: The page to fetch.
|
80 |
+
:type page: str
|
81 |
+
:return: The page content.
|
82 |
+
:rtype: Optional[str]
|
83 |
+
"""
|
84 |
try:
|
85 |
return self.wiki_client.page(title=page, auto_suggest=False).content[: self.doc_content_chars_max]
|
86 |
except (
|
|
|
90 |
return None
|
91 |
|
92 |
def search_page_titles(self, query: str) -> List[str]:
|
93 |
+
"""Run Wikipedia search and get page summaries.
|
94 |
+
|
95 |
+
:param query: The query to search for.
|
96 |
+
:type query: str
|
97 |
+
:return: The page titles.
|
98 |
+
:rtype: List[str]
|
99 |
+
"""
|
100 |
|
101 |
return self.wiki_client.search(query[:WIKIPEDIA_MAX_QUERY_LENGTH])[:self.top_k_results]
|
102 |
|
|
|
132 |
|
133 |
@staticmethod
|
134 |
def _formatted_page_summary(page_title: str, wiki_page: Any) -> Optional[str]:
|
135 |
+
""" Format the page and summary in a single string.
|
136 |
+
|
137 |
+
:param page_title: The page title.
|
138 |
+
:type page_title: str
|
139 |
+
:param wiki_page: The Wikipedia page.
|
140 |
+
:type wiki_page: Any
|
141 |
+
:return: The formatted page summary.
|
142 |
+
:rtype: Optional[str]
|
143 |
+
"""
|
144 |
return f"Page: {page_title}\nSummary: {wiki_page.summary}"
|
145 |
|
146 |
# def load(self, query: str) -> List[Document]:
|