Commit
·
8cb7e14
1
Parent(s):
bf61a22
First commit.
Browse files- HumanStandardInputFlow.py +101 -0
- README.md +21 -0
- __init__.py +1 -0
- pip_requirements.py +1 -0
HumanStandardInputFlow.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from copy import deepcopy
|
2 |
+
from typing import Dict, Any
|
3 |
+
|
4 |
+
import hydra
|
5 |
+
from langchain import PromptTemplate
|
6 |
+
|
7 |
+
from flows.base_flows import AtomicFlow
|
8 |
+
from flows.messages import UpdateMessage_Generic
|
9 |
+
|
10 |
+
from flows.utils import logging
|
11 |
+
|
12 |
+
# logging.set_verbosity_debug() # ToDo: Has no effect on the logger for __name__. Level is warn, and info is not printed
|
13 |
+
log = logging.get_logger(f"flows.{__name__}") # ToDo: Is there a better fix?
|
14 |
+
|
15 |
+
|
16 |
+
class HumanStandardInputFlow(AtomicFlow):
|
17 |
+
REQUIRED_KEYS_CONFIG = ["request_multi_line_input_flag"]
|
18 |
+
|
19 |
+
query_message_prompt_template: PromptTemplate = None
|
20 |
+
|
21 |
+
__default_flow_config = {
|
22 |
+
"end_of_input_string": "EOI",
|
23 |
+
"input_keys": [],
|
24 |
+
"description": "Reads input from the user's standard input.",
|
25 |
+
"query_message_prompt_template": {
|
26 |
+
"_target_": "langchain.PromptTemplate",
|
27 |
+
"template": "",
|
28 |
+
"input_variables": [],
|
29 |
+
"partial_variables": {},
|
30 |
+
"template_format": "jinja2"
|
31 |
+
}
|
32 |
+
}
|
33 |
+
|
34 |
+
def __init__(self, query_message_prompt_template, **kwargs):
|
35 |
+
super().__init__(**kwargs)
|
36 |
+
self.query_message_prompt_template = query_message_prompt_template
|
37 |
+
|
38 |
+
@classmethod
|
39 |
+
def _set_up_prompts(cls, config):
|
40 |
+
kwargs = {}
|
41 |
+
|
42 |
+
kwargs["query_message_prompt_template"] = \
|
43 |
+
hydra.utils.instantiate(config['query_message_prompt_template'], _convert_="partial")
|
44 |
+
|
45 |
+
return kwargs
|
46 |
+
|
47 |
+
@classmethod
|
48 |
+
def instantiate_from_config(cls, config):
|
49 |
+
flow_config = deepcopy(config)
|
50 |
+
|
51 |
+
kwargs = {"flow_config": flow_config}
|
52 |
+
|
53 |
+
# ~~~ Set up prompts ~~~
|
54 |
+
kwargs.update(cls._set_up_prompts(flow_config))
|
55 |
+
|
56 |
+
# ~~~ Instantiate flow ~~~
|
57 |
+
return cls(**kwargs)
|
58 |
+
|
59 |
+
@staticmethod
|
60 |
+
def _get_message(prompt_template, input_data: Dict[str, Any]):
|
61 |
+
template_kwargs = {}
|
62 |
+
for input_variable in prompt_template.input_variables:
|
63 |
+
template_kwargs[input_variable] = input_data[input_variable]
|
64 |
+
|
65 |
+
msg_content = prompt_template.format(**template_kwargs)
|
66 |
+
return msg_content
|
67 |
+
|
68 |
+
def _read_input(self):
|
69 |
+
if not self.flow_config["request_multi_line_input_flag"]:
|
70 |
+
log.info("Please enter you single-line response and press enter.")
|
71 |
+
human_input = input()
|
72 |
+
return human_input
|
73 |
+
|
74 |
+
end_of_input_string = self.flow_config["end_of_input_string"]
|
75 |
+
log.info(f"Please enter your multi-line response below. "
|
76 |
+
f"To submit the response, write `{end_of_input_string}` on a new line and press enter.")
|
77 |
+
|
78 |
+
content = []
|
79 |
+
while True:
|
80 |
+
line = input()
|
81 |
+
if line == self.flow_config["end_of_input_string"]:
|
82 |
+
break
|
83 |
+
content.append(line)
|
84 |
+
human_input = "\n".join(content)
|
85 |
+
return human_input
|
86 |
+
|
87 |
+
def run(self,
|
88 |
+
input_data: Dict[str, Any]) -> Dict[str, Any]:
|
89 |
+
|
90 |
+
query_message = self._get_message(self.query_message_prompt_template, input_data)
|
91 |
+
state_update_message = UpdateMessage_Generic(
|
92 |
+
created_by=self.flow_config['name'],
|
93 |
+
updated_flow=self.flow_config["name"],
|
94 |
+
data={"query_message": query_message},
|
95 |
+
)
|
96 |
+
self._log_message(state_update_message)
|
97 |
+
|
98 |
+
log.info(query_message)
|
99 |
+
human_input = self._read_input()
|
100 |
+
|
101 |
+
return {"human_input": human_input}
|
README.md
CHANGED
@@ -1,3 +1,24 @@
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
4 |
+
## Description
|
5 |
+
|
6 |
+
Reads input from the user's standard input.
|
7 |
+
|
8 |
+
## Configuration parameters
|
9 |
+
|
10 |
+
request_multi_line_input_flag (Boolean): If true, the user will be prompted to enter multiple lines of input. If false, the user will be prompted to enter a single line of input.
|
11 |
+
|
12 |
+
query_message_prompt_template (Dict):
|
13 |
+
template (String): The template for the message to be presented to the user (e.g., "The last `{{action}}` completed successfully. Do you have any feedback that should be considered before selecting the next action?"). Default value is "".
|
14 |
+
input_variables (List): The list of variables to be used in the template (e.g., ["action"]). Default value is [].
|
15 |
+
|
16 |
+
input_keys (List): The list of input keys that should be passed to the Flow as input. Default value is [].
|
17 |
+
|
18 |
+
## Input interface
|
19 |
+
|
20 |
+
By default, this Flow does not expect any input.
|
21 |
+
|
22 |
+
## Output interface
|
23 |
+
|
24 |
+
human_input (String): The user's input.
|
__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .HumanStandardInputFlow import HumanStandardInputFlow
|
pip_requirements.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# ToDo
|