Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 04f017a

Browse files
authored
examples: dataflow: chatbot: Update docs to use dataflow run single
1 parent 9594c82 commit 04f017a

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

docs/tutorials/dataflows/chatbot.rst

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,56 @@ messages which are directed to our bot.
2222
`examples/dataflow/chatbot <https://github.com/intel/dffml/blob/master/examples/dataflow/chatbot>`_
2323
directory of the DFFML source code.
2424

25-
We'll write the operations for this dataflow in operations.py
25+
You'll need to install `aiohttp <https://github.com/aio-libs/aiohttp>`_
26+
and ``dffml-model-scikit`` (The model used for prediction).
27+
28+
.. code-block:: console
29+
30+
$ pip install aiohttp dffml-model-scikit
31+
32+
We'll write the operations for this dataflow in **operations.py**
33+
34+
Adding necessary imports and defining ``Definitions`` for operation
35+
inputs.
36+
37+
.. literalinclude:: /../examples/dataflow/chatbot/operations.py
38+
:lines: 1-16
39+
40+
Defining config for our operations
2641

2742
.. literalinclude:: /../examples/dataflow/chatbot/operations.py
28-
:lines: 24-51
43+
:lines: 19-21
2944

30-
All requests to Gitter's API requires the room id for our room.
45+
All requests to Gitter's API requires the room id of our room.
3146
``get_room_id`` gets the ``room id`` from room name (The input to
3247
our dataflow).
3348

3449
.. literalinclude:: /../examples/dataflow/chatbot/operations.py
35-
:lines: 52-87
50+
:lines: 24-49
3651

3752
We listen to new messages directed to our bot.
3853

3954
.. literalinclude:: /../examples/dataflow/chatbot/operations.py
40-
:lines: 90-122
55+
:lines: 52-86
4156

4257
We'll use this op to send replies back to the chatroom
4358

4459
.. literalinclude:: /../examples/dataflow/chatbot/operations.py
45-
:lines: 125-220
60+
:lines: 89-121
4661

4762
This is the operation where all the logic for interpreting the messages
4863
go. If you have a Natural Language Understanding module It'd go here, so
4964
that you can parse unstructered data.
5065

66+
.. literalinclude:: /../examples/dataflow/chatbot/operations.py
67+
:lines: 124-248
68+
5169
Our operations are ``get_room_id, stream_chat, send_message and interpret_message``.
5270
All of them use at least one config. The common config being INISecretConfig which
5371
loads secret token and bot name from the ini config file.
5472

73+
**configs.ini**
74+
5575
.. literalinclude:: /../examples/dataflow/chatbot/configs.ini
5676

5777
Detour: What are imp_enter and ctx_enter?
@@ -90,19 +110,21 @@ we can do
90110
Running the dataflow
91111
--------------------
92112

113+
**run.py**
114+
93115
.. literalinclude:: /../examples/dataflow/chatbot/run.py
94116

95117
set the room name, config file name and run the dataflow
96118

97119
.. code-block:: console
98120
99-
python run.py
121+
$ python run.py
100122
101123
Or using the command line to, create the dataflow
102124

103125
.. code-block:: console
104126
105-
dffml dataflow create \
127+
$ dffml dataflow create \
106128
operations:get_room_id \
107129
operations:stream_chat \
108130
operations:send_message \
@@ -116,14 +138,12 @@ Or using the command line to, create the dataflow
116138
configs.ini=operations:send_message.secret.config.filename \
117139
ini=operations:interpret_message.secret.plugin \
118140
configs.ini=operations:interpret_message.secret.config.filename \
119-
> chatbot_df.json
141+
> chatbot_df.json
120142
121143
And run it by providing the ``room_name`` as the input
122144

123145
.. code-block:: console
124146
125-
dffml dataflow run records all \
147+
$ dffml dataflow run single \
126148
-dataflow ./chatbot_df.json \
127-
-inputs test_community1/community=room_name \
128-
-sources m=memory \
129-
-source-records temp
149+
-inputs test_community1/community=room_name

docs/usage/webhook/deploy.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ we'll setup another HTTP service which waits on GitHub webhooks to rebuilt and d
1313
`examples/ffmpeg <https://github.com/intel/dffml/blob/master/examples/ffmpeg/>`_
1414
directory of the DFFML source code.
1515

16-
We'll be using additional plugins from dffml, ``dffml-yaml-config`` and ``dffml-http-service``.
16+
We'll be using additional plugins from dffml, ``dffml-config-yaml`` and ``dffml-http-service``.
1717

1818
.. code-block:: console
1919
20-
$ pip install dffml-yaml-config dffml-http-service
20+
$ pip install dffml-config-yaml dffml-http-service
2121
2222
Writing the function
2323
--------------------

examples/dataflow/chatbot/operations.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dffml.cli.cli import CLI
1010
from dffml import op, config, Definition, BaseSecret
1111

12-
ACCESSTOKEN = Definition(name="access_tok3n", primitive="str")
12+
ACCESSTOKEN = Definition(name="access_token", primitive="str")
1313
ROOMNAME = Definition(name="room_name", primitive="str")
1414
ROOMID = Definition(name="room_id", primitive="str")
1515
MESSAGE = Definition(name="message", primitive="str")
@@ -78,7 +78,6 @@ async def stream_chat(self, room_id):
7878
# Gitter sends " \n" at some intervals
7979
if data == " \n".encode():
8080
continue
81-
print(f"\n\n Got data {data} \n\n")
8281
data = json.loads(data.strip())
8382
message = data["text"]
8483
# Only listen to messages directed to bot
@@ -136,9 +135,45 @@ async def interpret_message(self, message):
136135
return {"message": "Hey Hooman ฅ^•ﻌ•^ฅ"}
137136

138137
def extract_data(raw_data):
139-
raw_data = raw_data.split("data:")
138+
"""
139+
Parses data from text
140+
141+
eg
142+
>>> raw_data = "
143+
details:
144+
features: Years:int:1 Expertise:int:1 Trust:float:1
145+
predict: Salary:float:1
146+
data:
147+
Years,Expertise,Trust,Salary
148+
0,1,0.1,10
149+
1,3,0.2,20
150+
2,5,0.3,30
151+
3,7,0.4,40
152+
"
153+
154+
>>> extract_data(raw_data)
155+
{
156+
model-data:
157+
"
158+
Years,Expertise,Trust,Salary
159+
0,1,0.1,10
160+
1,3,0.2,20
161+
2,5,0.3,30
162+
3,7,0.4,40
163+
"
164+
,
165+
features:
166+
Years:int:1 Expertise:int:1 Trust:float:1
167+
,
168+
predict: Salary:float:1
169+
}
170+
"""
171+
raw_data = raw_data.split("data:") # (Feature details, training data)
140172
data = {"model-data": raw_data[1]}
141-
raw_data = raw_data[0].split("\n")
173+
raw_data = raw_data[0].split(
174+
"\n"
175+
) # splits feautre details to seprate lines
176+
# Iterate and add to to dictionary `data`
142177
for x in raw_data:
143178
k, *v = x.split(":")
144179
if isinstance(v, list): # for features
@@ -157,7 +192,7 @@ def extract_data(raw_data):
157192
message = re.sub(r"(@[^\s]+)(.*)", r"\2", message).strip()
158193

159194
if message.lower().startswith("train model"):
160-
return {"message": "Gimme more details!!"}
195+
return {"message": "Gimme more info!!"}
161196

162197
elif message.lower().startswith("predict:"):
163198
# Only replace first occurence of predict
@@ -172,15 +207,9 @@ def extract_data(raw_data):
172207
else:
173208
return {"message": " Oops ,I didnt get that ᕙ(⇀‸↼‶)ᕗ "}
174209

175-
# If predict or train, extract data
210+
# We'll use scikit logistic regression
176211
data = extract_data(raw_data)
177-
if "model-type" in data:
178-
model_type = data["model-type"]
179-
if "model-name" in data:
180-
model_name = data["model-name"]
181-
else:
182-
model_name = "mymodel"
183-
212+
model_type = "scikitlr"
184213
features = data["features"].split(" ")
185214
predict = data["predict"]
186215
model_data = data["model-data"]
@@ -196,7 +225,7 @@ def extract_data(raw_data):
196225
"-model",
197226
model_type,
198227
"-model-directory",
199-
model_name,
228+
"tempModel",
200229
"-model-features",
201230
*features,
202231
"-model-predict",
@@ -206,7 +235,6 @@ def extract_data(raw_data):
206235
"-source-filename",
207236
fileobj.name,
208237
)
209-
sys.stdout.flush()
210238

211239
if "train" in cmds:
212240
return {"message": "Done!!"}

0 commit comments

Comments
 (0)