nbaldwin commited on
Commit
593a9f3
·
0 Parent(s):

coflows working version

Browse files
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python,java,c++,pycharm,visualstudiocode,macos,linux,windows
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python,java,c++,pycharm,visualstudiocode,macos,linux,windows
3
+
4
+ ### C++ ###
5
+ # Prerequisites
6
+ *.d
7
+
8
+ # Compiled Object files
9
+ *.slo
10
+ *.lo
11
+ *.o
12
+ *.obj
13
+
14
+ # Precompiled Headers
15
+ *.gch
16
+ *.pch
17
+
18
+ # Compiled Dynamic libraries
19
+ *.so
20
+ *.dylib
21
+ *.dll
22
+
23
+ # Fortran module files
24
+ *.mod
25
+ *.smod
26
+
27
+ # Compiled Static libraries
28
+ *.lai
29
+ *.la
30
+ *.a
31
+ *.lib
32
+
33
+ # Executables
34
+ *.exe
35
+ *.out
36
+ *.app
37
+
38
+ ### Java ###
39
+ # Compiled class file
40
+ *.class
41
+
42
+ # Log file
43
+ *.log
44
+
45
+ # BlueJ files
46
+ *.ctxt
47
+
48
+ # Mobile Tools for Java (J2ME)
49
+ .mtj.tmp/
50
+
51
+ # Package Files #
52
+ *.jar
53
+ *.war
54
+ *.nar
55
+ *.ear
56
+ *.zip
57
+ *.tar.gz
58
+ *.rar
59
+
60
+ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
61
+ hs_err_pid*
62
+ replay_pid*
63
+
64
+ ### Linux ###
65
+ *~
66
+
67
+ # temporary files which can be created if a process still has a handle open of a deleted file
68
+ .fuse_hidden*
69
+
70
+ # KDE directory preferences
71
+ .directory
72
+
73
+ # Linux trash folder which might appear on any partition or disk
74
+ .Trash-*
75
+
76
+ # .nfs files are created when an open file is removed but is still being accessed
77
+ .nfs*
78
+
79
+ ### macOS ###
80
+ # General
81
+ .DS_Store
82
+ .AppleDouble
83
+ .LSOverride
84
+
85
+ # Icon must end with two \r
86
+ Icon
87
+
88
+
89
+ # Thumbnails
90
+ ._*
91
+
92
+ # Files that might appear in the root of a volume
93
+ .DocumentRevisions-V100
94
+ .fseventsd
95
+ .Spotlight-V100
96
+ .TemporaryItems
97
+ .Trashes
98
+ .VolumeIcon.icns
99
+ .com.apple.timemachine.donotpresent
100
+
101
+ # Directories potentially created on remote AFP share
102
+ .AppleDB
103
+ .AppleDesktop
104
+ Network Trash Folder
105
+ Temporary Items
106
+ .apdisk
107
+
108
+ ### macOS Patch ###
109
+ # iCloud generated files
110
+ *.icloud
111
+
112
+ ### PyCharm ###
113
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
114
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
115
+
116
+ # User-specific stuff
117
+ .idea/**/workspace.xml
118
+ .idea/**/tasks.xml
119
+ .idea/**/usage.statistics.xml
120
+ .idea/**/dictionaries
121
+ .idea/**/shelf
122
+
123
+ # AWS User-specific
124
+ .idea/**/aws.xml
125
+
126
+ # Generated files
127
+ .idea/**/contentModel.xml
128
+
129
+ # Sensitive or high-churn files
130
+ .idea/**/dataSources/
131
+ .idea/**/dataSources.ids
132
+ .idea/**/dataSources.local.xml
133
+ .idea/**/sqlDataSources.xml
134
+ .idea/**/dynamic.xml
135
+ .idea/**/uiDesigner.xml
136
+ .idea/**/dbnavigator.xml
137
+
138
+ # Gradle
139
+ .idea/**/gradle.xml
140
+ .idea/**/libraries
141
+
142
+ # Gradle and Maven with auto-import
143
+ # When using Gradle or Maven with auto-import, you should exclude module files,
144
+ # since they will be recreated, and may cause churn. Uncomment if using
145
+ # auto-import.
146
+ # .idea/artifacts
147
+ # .idea/compiler.xml
148
+ # .idea/jarRepositories.xml
149
+ # .idea/modules.xml
150
+ # .idea/*.iml
151
+ # .idea/modules
152
+ # *.iml
153
+ # *.ipr
154
+
155
+ # CMake
156
+ cmake-build-*/
157
+
158
+ # Mongo Explorer plugin
159
+ .idea/**/mongoSettings.xml
160
+
161
+ # File-based project format
162
+ *.iws
163
+
164
+ # IntelliJ
165
+ out/
166
+
167
+ # mpeltonen/sbt-idea plugin
168
+ .idea_modules/
169
+
170
+ # JIRA plugin
171
+ atlassian-ide-plugin.xml
172
+
173
+ # Cursive Clojure plugin
174
+ .idea/replstate.xml
175
+
176
+ # SonarLint plugin
177
+ .idea/sonarlint/
178
+
179
+ # Crashlytics plugin (for Android Studio and IntelliJ)
180
+ com_crashlytics_export_strings.xml
181
+ crashlytics.properties
182
+ crashlytics-build.properties
183
+ fabric.properties
184
+
185
+ # Editor-based Rest Client
186
+ .idea/httpRequests
187
+
188
+ # Android studio 3.1+ serialized cache file
189
+ .idea/caches/build_file_checksums.ser
190
+
191
+ ### PyCharm Patch ###
192
+ # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
193
+
194
+ # *.iml
195
+ # modules.xml
196
+ # .idea/misc.xml
197
+ # *.ipr
198
+
199
+ # Sonarlint plugin
200
+ # https://plugins.jetbrains.com/plugin/7973-sonarlint
201
+ .idea/**/sonarlint/
202
+
203
+ # SonarQube Plugin
204
+ # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
205
+ .idea/**/sonarIssues.xml
206
+
207
+ # Markdown Navigator plugin
208
+ # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
209
+ .idea/**/markdown-navigator.xml
210
+ .idea/**/markdown-navigator-enh.xml
211
+ .idea/**/markdown-navigator/
212
+
213
+ # Cache file creation bug
214
+ # See https://youtrack.jetbrains.com/issue/JBR-2257
215
+ .idea/$CACHE_FILE$
216
+
217
+ # CodeStream plugin
218
+ # https://plugins.jetbrains.com/plugin/12206-codestream
219
+ .idea/codestream.xml
220
+
221
+ # Azure Toolkit for IntelliJ plugin
222
+ # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
223
+ .idea/**/azureSettings.xml
224
+
225
+ ### Python ###
226
+ # Byte-compiled / optimized / DLL files
227
+ __pycache__/
228
+ *.py[cod]
229
+ *$py.class
230
+
231
+ # C extensions
232
+
233
+ # Distribution / packaging
234
+ .Python
235
+ build/
236
+ develop-eggs/
237
+ dist/
238
+ downloads/
239
+ eggs/
240
+ .eggs/
241
+ lib/
242
+ lib64/
243
+ parts/
244
+ sdist/
245
+ var/
246
+ wheels/
247
+ share/python-wheels/
248
+ *.egg-info/
249
+ .installed.cfg
250
+ *.egg
251
+ MANIFEST
252
+
253
+ # PyInstaller
254
+ # Usually these files are written by a python script from a template
255
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
256
+ *.manifest
257
+ *.spec
258
+
259
+ # Installer logs
260
+ pip-log.txt
261
+ pip-delete-this-directory.txt
262
+
263
+ # Unit test / coverage reports
264
+ htmlcov/
265
+ .tox/
266
+ .nox/
267
+ .coverage
268
+ .coverage.*
269
+ .cache
270
+ nosetests.xml
271
+ coverage.xml
272
+ *.cover
273
+ *.py,cover
274
+ .hypothesis/
275
+ .pytest_cache/
276
+ cover/
277
+
278
+ # Translations
279
+ *.mo
280
+ *.pot
281
+
282
+ # Django stuff:
283
+ local_settings.py
284
+ db.sqlite3
285
+ db.sqlite3-journal
286
+
287
+ # Flask stuff:
288
+ instance/
289
+ .webassets-cache
290
+
291
+ # Scrapy stuff:
292
+ .scrapy
293
+
294
+ # Sphinx documentation
295
+ docs/_build/
296
+
297
+ # PyBuilder
298
+ .pybuilder/
299
+ target/
300
+
301
+ # Jupyter Notebook
302
+ .ipynb_checkpoints
303
+
304
+ # IPython
305
+ profile_default/
306
+ ipython_config.py
307
+
308
+ # pyenv
309
+ # For a library or package, you might want to ignore these files since the code is
310
+ # intended to run in multiple environments; otherwise, check them in:
311
+ # .python-version
312
+
313
+ # pipenv
314
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
315
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
316
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
317
+ # install all needed dependencies.
318
+ #Pipfile.lock
319
+
320
+ # poetry
321
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
322
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
323
+ # commonly ignored for libraries.
324
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
325
+ #poetry.lock
326
+
327
+ # pdm
328
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
329
+ #pdm.lock
330
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
331
+ # in version control.
332
+ # https://pdm.fming.dev/#use-with-ide
333
+ .pdm.toml
334
+
335
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
336
+ __pypackages__/
337
+
338
+ # Celery stuff
339
+ celerybeat-schedule
340
+ celerybeat.pid
341
+
342
+ # SageMath parsed files
343
+ *.sage.py
344
+
345
+ # Environments
346
+ .env
347
+ .venv
348
+ env/
349
+ venv/
350
+ ENV/
351
+ env.bak/
352
+ venv.bak/
353
+
354
+ # Spyder project settings
355
+ .spyderproject
356
+ .spyproject
357
+
358
+ # Rope project settings
359
+ .ropeproject
360
+
361
+ # mkdocs documentation
362
+ /site
363
+
364
+ # mypy
365
+ .mypy_cache/
366
+ .dmypy.json
367
+ dmypy.json
368
+
369
+ # Pyre type checker
370
+ .pyre/
371
+
372
+ # pytype static type analyzer
373
+ .pytype/
374
+
375
+ # Cython debug symbols
376
+ cython_debug/
377
+
378
+ # PyCharm
379
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
380
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
381
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
382
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
383
+ #.idea/
384
+
385
+ ### Python Patch ###
386
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
387
+ poetry.toml
388
+
389
+ # ruff
390
+ .ruff_cache/
391
+
392
+ # LSP config files
393
+ pyrightconfig.json
394
+
395
+ ### VisualStudioCode ###
396
+ .vscode/*
397
+ !.vscode/settings.json
398
+ !.vscode/tasks.json
399
+ !.vscode/launch.json
400
+ !.vscode/extensions.json
401
+ !.vscode/*.code-snippets
402
+
403
+ # Local History for Visual Studio Code
404
+ .history/
405
+
406
+ # Built Visual Studio Code Extensions
407
+ *.vsix
408
+
409
+ ### VisualStudioCode Patch ###
410
+ # Ignore all local history of files
411
+ .history
412
+ .ionide
413
+
414
+ ### Windows ###
415
+ # Windows thumbnail cache files
416
+ Thumbs.db
417
+ Thumbs.db:encryptable
418
+ ehthumbs.db
419
+ ehthumbs_vista.db
420
+
421
+ # Dump file
422
+ *.stackdump
423
+
424
+ # Folder config file
425
+ [Dd]esktop.ini
426
+
427
+ # Recycle Bin used on file shares
428
+ $RECYCLE.BIN/
429
+
430
+ # Windows Installer files
431
+ *.cab
432
+ *.msi
433
+ *.msix
434
+ *.msm
435
+ *.msp
436
+
437
+ # Windows shortcuts
438
+ *.lnk
439
+
440
+ # End of https://www.toptal.com/developers/gitignore/api/python,java,c++,pycharm,visualstudiocode,macos,linux,windows
441
+
442
+ .*
443
+ flow_modules/
ChatWithDemonstrationsFlow.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from aiflows.base_flows import CompositeFlow
4
+ from aiflows.utils import logging
5
+ from aiflows.interfaces import KeyInterface
6
+ logging.set_verbosity_debug()
7
+
8
+ log = logging.get_logger(__name__)
9
+
10
+
11
+ class ChatWithDemonstrationsFlow(CompositeFlow):
12
+ """ A Chat with Demonstrations Flow. It is a flow that consists of multiple sub-flows that are executed sequentially.
13
+ It's parent class is SequentialFlow.
14
+
15
+ It Contains the following subflows:
16
+ - A Demonstration Flow: It is a flow that passes demonstrations to the ChatFlow
17
+ - A Chat Flow: It is a flow that uses the demonstrations to answer queries asked by the user/human.
18
+
19
+ An illustration of the flow is as follows:
20
+
21
+ -------> Demonstration Flow -------> Chat Flow ------->
22
+
23
+ *Configuration Parameters*:
24
+
25
+ - `name` (str): The name of the flow. Default: "ChatAtomic_Flow_with_Demonstrations"
26
+ - `description` (str): A description of the flow. This description is used to generate the help message of the flow.
27
+ Default: "A sequential flow that answers questions with demonstrations"
28
+ - `subflows_config` (Dict[str,Any]): A dictionary of subflows configurations of the sequential Flow. Default:
29
+ - `Demonstration Flow`: The configuration of the Demonstration Flow. By default, it a DemonstrationsAtomicFlow.
30
+ Its default parmaters are defined in DemonstrationsAtomicFlow).
31
+ - `Chat Flow`: The configuration of the Chat Flow. By default, its a ChatAtomicFlow.
32
+ Its default parmaters are defined in ChatAtomicFlowModule (see Flowcard, i.e. README.md, of ChatAtomicFlowModule).
33
+ - `topology` (str): The topology of the flow which is "sequential". By default, the topology is the one shown in the
34
+ illustration above (the topology is also described in ChatWithDemonstrationsFlow.yaml).
35
+
36
+ *Input Interface*:
37
+
38
+ - `query` (str): A query asked to the flow (e.g. "What is the capital of France?")
39
+
40
+ Output Interface:
41
+
42
+ - `answer` (str): The answer of the flow to the query
43
+
44
+ :param \**kwargs: Arguments to be passed to the parent class SequentialFlow constructor.
45
+ """
46
+ def __init__(self,**kwargs):
47
+ super().__init__(**kwargs)
48
+ self.output_interface = KeyInterface(
49
+ keys_to_rename={"api_output": "answer"}
50
+ )
51
+
52
+ def set_up_flow_state(self):
53
+ super().set_up_flow_state()
54
+ self.flow_state["last_flow_called"] = None
55
+
56
+ def run(self,input_data):
57
+
58
+ #~~~~~~~~~~~Solution 1 - Blocking ~~~~~~~
59
+ future = self.ask_subflow("demonstration_flow",input_data)
60
+
61
+ answer = self.ask_subflow("chat_flow",future.get_data())
62
+ return self.output_interface(answer.get_data())
63
+
64
+ # #~~~~~~~~~~~Solution 2 - Non-Blocking ~~~~~~~
65
+ # if self.flow_state["last_flow_called"] is None:
66
+ # self.ask_pipe_subflow("demonstration_flow",input_data)
67
+ # self.flow_state["last_flow_called"] = "demonstration_flow"
68
+ # return {"answer": "Just Called the Demonstration Flow"}
69
+
70
+ # elif self.flow_state["last_flow_called"] == "demonstration_flow":
71
+ # self.ask_pipe_subflow("chat_flow",input_data)
72
+ # self.flow_state["last_flow_called"] = "chat_flow"
73
+ # return {"answer": "Just Called the Demonstration Flow"}
74
+
75
+ # self.flow_state["last_flow_called"] = None
76
+ return self.output_interface(input_data)
77
+
78
+
ChatWithDemonstrationsFlow.yaml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ name: "ChatAtomic_Flow_with_Demonstrations"
3
+ description: "A sequential flow that answers questions with demonstrations"
4
+
5
+ subflows_config:
6
+ demonstration_flow:
7
+ _target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_config
8
+ name: "DemonstrationsAtomicFlow"
9
+ description: "A flow that answers questions with demonstrations"
10
+ chat_flow:
11
+ _target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_config
12
+ name: "ChatAtomicFlow"
13
+ description: "A flow that answers questions"
DemonstrationsAtomicFlow.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import jinja2
2
+ from aiflows.base_flows import AtomicFlow
3
+ from aiflows.utils import logging
4
+ from aiflows.utils import general_helpers
5
+ from typing import Dict,Any,Optional,List
6
+ from aiflows.prompt_template import JinjaPrompt
7
+ from copy import deepcopy
8
+ import os
9
+ import hydra
10
+ log = logging.get_logger(__name__)
11
+
12
+ class DemonstrationsAtomicFlow(AtomicFlow):
13
+ """ This class implements a Demonstrations Atomic Flow. It is a flow which is usually used to pass demonstrations (of user assistant interactions)
14
+ to the ChatAtomicFlow.
15
+
16
+ *Configuration Parameters*:
17
+
18
+ - `name` (str): The name of the flow. Default: "DemonstrationsAtomicFlow"
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 passes demonstrations to the ChatFlow"
21
+ - `data` (List[Dict[str, Any]]): The data of the demonstrations.
22
+ If data is None, the data is loaded from the file specified in the params["data_dir"].
23
+ Default: No default value this field must be set.
24
+ - `params` (Dict[str, Any]): The parameters specific to the dataset of the demonstrations. Its default parameters are:
25
+ - `data_dir` (str): The directory where the demonstrations are stored. If the data is not directly passed to the flow through `data` then
26
+ the data is loaded from this directory. Default: No default value this field must be set.
27
+ - `demonstrations_id` (str): The id of the demonstrations (name of the data file). If the data is not directly passed to the flow through `data` then
28
+ the data is loaded from this file. Default: No default value this field must be set.
29
+ - `demonstrations_k` (int): The number of demonstrations to pass to the ChatFlow.
30
+ If None, all the demonstrations are passed to the ChatFlow. Default: None
31
+ - `query_prompt_template` (Dict[str, Any]): The prompt template used to generate the query of the demonstrations.
32
+ By default its of type flows.prompt_template.JinjaPrompt. None of the parameters of the prompt are defined by default and therefore need to be defined if one
33
+ wants to use the query_prompt_template. Default parameters are defined in flows.prompt_template.jinja2_prompts.JinjaPrompt.
34
+ - `response_prompt_template` (Dict[str, Any]): The prompt template used to generate the response of the demonstrations. By default its of type flows.prompt_template.JinjaPrompt.
35
+ None of the parameters of the prompt are defined by default and therefore need to be defined if one
36
+ wants to use the response_prompt_template. Default parameters are defined in flows.prompt_template.jinja2_prompts.JinjaPrompt.
37
+
38
+ *Input Interface*:
39
+
40
+ - The input interface expected by its successor flow (e.g. typically ChatAtomicFlow so the input interface is the one expected by ChatAtomicFlow)
41
+
42
+ *Output Interface*:
43
+
44
+ - The input interface expected by its successor flow (e.g. typically ChatAtomicFlow so the input interface expected by ChatAtomicFlow))
45
+ - `demonstrations` (List[Dict[str, Any]]): A list of demonstrations. Each demonstration is a dictionary with the following keys:
46
+ - idx (int): The index of the demonstration
47
+ - query (str): The query of the demonstration
48
+ - response (str): The response of the demonstration
49
+
50
+ :param params: The parameters specific to the dataset of the demonstrations. It must sould contain the following keys:
51
+ - 'data_dir' (str): The directory where the demonstrations are stored. This field is used if the data is not directly passed to the flow through the 'data' field.
52
+ - 'demonstrations_id' (str): The id of the demonstrations (name of the data file). This field is used if the data is not directly passed to the flow through the 'data' field.
53
+ - 'demonstrations_k' (int): The number of demonstrations to pass to the ChatFlow. If None, all the demonstrations are passed to the ChatFlow.
54
+ - 'ids_to_keep' (Optional[Union[str, List[str]]]): The ids of the demonstrations to keep. If None, all the demonstrations are kept.
55
+ :type params: Dict[str, Any]
56
+ :param query_prompt_template: The prompt template used to generate the query of the demonstrations.
57
+ :type query_prompt_template: JinjaPrompt
58
+ :param response_prompt_template: The prompt template used to generate the response of the demonstrations.
59
+ :type response_prompt_template: JinjaPrompt
60
+ :param data: The data of the demonstrations. If None, the data is loaded from the file specified in the params.
61
+ :type data: Optional[List[Dict[str, Any]]]
62
+ """
63
+ demonstrations_k: Optional[int] = None
64
+ query_prompt_template: JinjaPrompt
65
+ response_prompt_template: JinjaPrompt
66
+ params: Dict
67
+
68
+ def __init__(self,params,query_prompt_template,response_prompt_template, data=None,**kwargs):
69
+ super().__init__(**kwargs)
70
+ self.params = params
71
+ self.data = data
72
+ self.demonstrations_k = self.params.get("demonstrations_k", None)
73
+
74
+ #typically the query would be what the user (human) asks the assistant (LLM)
75
+ self.query_prompt_template = query_prompt_template
76
+ #typically the response would be what the assistant (LLM) should answer to the user (human)
77
+ self.response_prompt_template = response_prompt_template
78
+ if self.data is None:
79
+ self._load_data()
80
+
81
+ @classmethod
82
+ def _set_up_prompts(cls, config):
83
+ """ This method instantiates the prompt templates of the flow (used when instantiating the flow from a config file)
84
+
85
+ :param config: The configuration of the flow.
86
+ :type config: Dict[str, Any]
87
+ :return: A dictionary of keyword arguments to pass to the constructor of the flow.
88
+ :rtype: Dict[str, Any]
89
+ """
90
+ kwargs = {}
91
+ kwargs["query_prompt_template"] = \
92
+ hydra.utils.instantiate(config['query_prompt_template'], _convert_="partial")
93
+ kwargs["response_prompt_template"] = \
94
+ hydra.utils.instantiate(config['response_prompt_template'], _convert_="partial")
95
+ return kwargs
96
+
97
+ @classmethod
98
+ def instantiate_from_config(cls, config):
99
+ """ This method instantiates the flow from a config file.
100
+
101
+ :param config: The configuration of the flow.
102
+ :type config: Dict[str, Any]
103
+ :return: The instantiated flow.
104
+ :rtype: Flow
105
+ """
106
+ flow_config = deepcopy(config)
107
+
108
+ kwargs = {"flow_config": flow_config}
109
+
110
+ # ~~~ Set up prompts ~~~
111
+ kwargs.update(cls._set_up_prompts(flow_config))
112
+ kwargs.update({"params": flow_config["params"]})
113
+ kwargs.update({"data": flow_config["data"]})
114
+ # ~~~ Instantiate flow ~~~
115
+ return cls(**kwargs)
116
+
117
+ def _get_query_message_content(self, sample_data: Dict):
118
+ """ This method returns the query message content of a demonstration given the sample data (by rendering the query prompt template).
119
+
120
+ :param sample_data: The sample data of the demonstration.
121
+ :type sample_data: Dict[str, Any]
122
+ :return: The query message content of the demonstration.
123
+ :rtype: str
124
+ """
125
+ input_variables = self.query_prompt_template.input_variables
126
+ return self.query_prompt_template.format(**{k: sample_data[k] for k in input_variables})
127
+
128
+ def _get_response_message_content(self, sample_data: Dict):
129
+ """ This method returns the response message content of a demonstration given the sample data (by rendering the response prompt template).
130
+
131
+ :param sample_data: The sample data of the demonstration.
132
+ :type sample_data: Dict[str, Any]
133
+ :return: The response message content of the demonstration.
134
+ :rtype: str
135
+ """
136
+ input_variables = self.response_prompt_template.input_variables
137
+ return self.response_prompt_template.format(**{k: sample_data[k] for k in input_variables})
138
+
139
+ def _get_io_pair(self, idx):
140
+ """ This method, given the index of a demonstration, returns an query-response pair from the demonstrations data.
141
+
142
+ :param idx: The index of the demonstration.
143
+ :type idx: int
144
+ :return: The query-response pair at idx from the demonstrations data.
145
+ :rtype: Dict[str, Any]
146
+ """
147
+ dp = self.data[idx]
148
+
149
+ query_data = dp["query_data"]
150
+ response_data = dp["response_data"]
151
+
152
+ query = self._get_query_message_content(query_data)
153
+ response = self._get_response_message_content(response_data)
154
+
155
+ return {"idx": idx, "query": query,"response": response}
156
+
157
+ def _get_io_pairs(self,input_data: Dict[str, Any]) -> List[Any]:
158
+ """ This method returns the demonstrations that are passed to the destination flow (typically ChatAtomicFlow).
159
+
160
+ :param input_data: The input data of the flow.
161
+ :type input_data: Dict[str, Any]
162
+ :return: The demonstrations that are passed to the destination flow.
163
+ :rtype: List[Any]
164
+ """
165
+ demonstrations_k = self.demonstrations_k if self.demonstrations_k is not None else len(self.data)
166
+ io_pairs = [self._get_io_pair(idx) for idx in range(demonstrations_k)]
167
+ return io_pairs
168
+
169
+ def _load_data(self):
170
+ """ This method loads the demonstrations from the file specified in the params. It also filters the demonstrations if the ids_to_keep parameter is specified."""
171
+ demonstrations_file = os.path.join(self.params["data_dir"], f"{self.params['demonstrations_id']}.jsonl")
172
+ self.data = general_helpers.read_jsonlines(demonstrations_file)
173
+
174
+ if self.params.get("ids_to_keep", False):
175
+ if isinstance(self.params["ids_to_keep"], str):
176
+ ids_to_keep = set(self.params["ids_to_keep"].split(","))
177
+ else:
178
+ ids_to_keep = set(self.params["ids_to_keep"])
179
+
180
+ self.data = [d for d in self.data if d["id"] in ids_to_keep]
181
+
182
+ log.info("Loaded the demonstrations for %d datapoints from %s", len(self.data), self.params["data_dir"])
183
+
184
+ def run(self,
185
+ input_data: Dict[str, Any]) -> Dict[str, Any]:
186
+ """ This method runs the flow. It returns the input data of the flow with the demonstrations added to it.
187
+
188
+ :param input_data: The input data of the flow.
189
+ :type input_data: Dict[str, Any]
190
+ :return: The input data of the flow with the demonstrations added to it.
191
+ :rtype: Dict[str, Any]
192
+ """
193
+ return {**input_data,**{"demonstrations": self._get_io_pairs(input_data=input_data)}}
DemonstrationsAtomicFlow.yaml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "DemonstrationAtomicFlow"
2
+ description: "A flow which returns Demonstrations"
3
+
4
+ data: ??? #e.g. [{"query_data": {"query": "What is the capital of France?"}, "response_data": {"response": "Paris, my sir."}}]
5
+ params:
6
+ data_dir: ???
7
+ demonstrations_id: ???
8
+ ids_to_keep: null
9
+ demonstrations_k: null
10
+
11
+ query_prompt_template:
12
+ _target_: aiflows.prompt_template.JinjaPrompt
13
+
14
+ response_prompt_template:
15
+ _target_: aiflows.prompt_template.JinjaPrompt
README.md ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+
5
+ # Table of Contents
6
+
7
+ * [\_\_init\_\_](#__init__)
8
+ * [DemonstrationsAtomicFlow](#DemonstrationsAtomicFlow)
9
+ * [DemonstrationsAtomicFlow](#DemonstrationsAtomicFlow.DemonstrationsAtomicFlow)
10
+ * [instantiate\_from\_config](#DemonstrationsAtomicFlow.DemonstrationsAtomicFlow.instantiate_from_config)
11
+ * [run](#DemonstrationsAtomicFlow.DemonstrationsAtomicFlow.run)
12
+ * [ChatWithDemonstrationsFlow](#ChatWithDemonstrationsFlow)
13
+ * [ChatWithDemonstrationsFlow](#ChatWithDemonstrationsFlow.ChatWithDemonstrationsFlow)
14
+
15
+ <a id="__init__"></a>
16
+
17
+ # \_\_init\_\_
18
+
19
+ <a id="DemonstrationsAtomicFlow"></a>
20
+
21
+ # DemonstrationsAtomicFlow
22
+
23
+ <a id="DemonstrationsAtomicFlow.DemonstrationsAtomicFlow"></a>
24
+
25
+ ## DemonstrationsAtomicFlow Objects
26
+
27
+ ```python
28
+ class DemonstrationsAtomicFlow(AtomicFlow)
29
+ ```
30
+
31
+ This class implements a Demonstrations Atomic Flow. It is a flow which is usually used to pass demonstrations (of user assistant interactions)
32
+
33
+ to the ChatAtomicFlow.
34
+
35
+ *Configuration Parameters*:
36
+
37
+ - `name` (str): The name of the flow. Default: "DemonstrationsAtomicFlow"
38
+ - `description` (str): A description of the flow. This description is used to generate the help message of the flow.
39
+ Default: "A flow that passes demonstrations to the ChatFlow"
40
+ - `data` (List[Dict[str, Any]]): The data of the demonstrations.
41
+ If data is None, the data is loaded from the file specified in the params["data_dir"].
42
+ Default: No default value this field must be set.
43
+ - `params` (Dict[str, Any]): The parameters specific to the dataset of the demonstrations. Its default parameters are:
44
+ - `data_dir` (str): The directory where the demonstrations are stored. If the data is not directly passed to the flow through `data` then
45
+ the data is loaded from this directory. Default: No default value this field must be set.
46
+ - `demonstrations_id` (str): The id of the demonstrations (name of the data file). If the data is not directly passed to the flow through `data` then
47
+ the data is loaded from this file. Default: No default value this field must be set.
48
+ - `demonstrations_k` (int): The number of demonstrations to pass to the ChatFlow.
49
+ If None, all the demonstrations are passed to the ChatFlow. Default: None
50
+ - `query_prompt_template` (Dict[str, Any]): The prompt template used to generate the query of the demonstrations.
51
+ By default its of type aiflows.prompt_template.JinjaPrompt. None of the parameters of the prompt are defined by default and therefore need to be defined if one
52
+ wants to use the query_prompt_template. Default parameters are defined in aiflows.prompt_template.jinja2_prompts.JinjaPrompt.
53
+ - `response_prompt_template` (Dict[str, Any]): The prompt template used to generate the response of the demonstrations. By default its of type aiflows.prompt_template.JinjaPrompt.
54
+ None of the parameters of the prompt are defined by default and therefore need to be defined if one
55
+ wants to use the response_prompt_template. Default parameters are defined in aiflows.prompt_template.jinja2_prompts.JinjaPrompt.
56
+
57
+ *Input Interface*:
58
+
59
+ - The input interface expected by its successor flow (e.g. typically ChatAtomicFlow so the input interface is the one expected by ChatAtomicFlow)
60
+
61
+ *Output Interface*:
62
+
63
+ - The input interface expected by its successor flow (e.g. typically ChatAtomicFlow so the input interface expected by ChatAtomicFlow))
64
+ - `demonstrations` (List[Dict[str, Any]]): A list of demonstrations. Each demonstration is a dictionary with the following keys:
65
+ - idx (int): The index of the demonstration
66
+ - query (str): The query of the demonstration
67
+ - response (str): The response of the demonstration
68
+
69
+ **Arguments**:
70
+
71
+ - `params` (`Dict[str, Any]`): The parameters specific to the dataset of the demonstrations. It must sould contain the following keys:
72
+ - 'data_dir' (str): The directory where the demonstrations are stored. This field is used if the data is not directly passed to the flow through the 'data' field.
73
+ - 'demonstrations_id' (str): The id of the demonstrations (name of the data file). This field is used if the data is not directly passed to the flow through the 'data' field.
74
+ - 'demonstrations_k' (int): The number of demonstrations to pass to the ChatFlow. If None, all the demonstrations are passed to the ChatFlow.
75
+ - 'ids_to_keep' (Optional[Union[str, List[str]]]): The ids of the demonstrations to keep. If None, all the demonstrations are kept.
76
+ - `query_prompt_template` (`JinjaPrompt`): The prompt template used to generate the query of the demonstrations.
77
+ - `response_prompt_template` (`JinjaPrompt`): The prompt template used to generate the response of the demonstrations.
78
+ - `data` (`Optional[List[Dict[str, Any]]]`): The data of the demonstrations. If None, the data is loaded from the file specified in the params.
79
+
80
+ <a id="DemonstrationsAtomicFlow.DemonstrationsAtomicFlow.instantiate_from_config"></a>
81
+
82
+ #### instantiate\_from\_config
83
+
84
+ ```python
85
+ @classmethod
86
+ def instantiate_from_config(cls, config)
87
+ ```
88
+
89
+ This method instantiates the flow from a config file.
90
+
91
+ **Arguments**:
92
+
93
+ - `config` (`Dict[str, Any]`): The configuration of the flow.
94
+
95
+ **Returns**:
96
+
97
+ `Flow`: The instantiated flow.
98
+
99
+ <a id="DemonstrationsAtomicFlow.DemonstrationsAtomicFlow.run"></a>
100
+
101
+ #### run
102
+
103
+ ```python
104
+ def run(input_data: Dict[str, Any]) -> Dict[str, Any]
105
+ ```
106
+
107
+ This method runs the flow. It returns the input data of the flow with the demonstrations added to it.
108
+
109
+ **Arguments**:
110
+
111
+ - `input_data` (`Dict[str, Any]`): The input data of the flow.
112
+
113
+ **Returns**:
114
+
115
+ `Dict[str, Any]`: The input data of the flow with the demonstrations added to it.
116
+
117
+ <a id="ChatWithDemonstrationsFlow"></a>
118
+
119
+ # ChatWithDemonstrationsFlow
120
+
121
+ <a id="ChatWithDemonstrationsFlow.ChatWithDemonstrationsFlow"></a>
122
+
123
+ ## ChatWithDemonstrationsFlow Objects
124
+
125
+ ```python
126
+ class ChatWithDemonstrationsFlow(SequentialFlow)
127
+ ```
128
+
129
+ A Chat with Demonstrations Flow. It is a flow that consists of multiple sub-flows that are executed sequentially.
130
+
131
+ It's parent class is SequentialFlow.
132
+
133
+ It Contains the following subflows:
134
+ - A Demonstration Flow: It is a flow that passes demonstrations to the ChatFlow
135
+ - A Chat Flow: It is a flow that uses the demonstrations to answer queries asked by the user/human.
136
+
137
+ An illustration of the flow is as follows:
138
+
139
+ -------> Demonstration Flow -------> Chat Flow ------->
140
+
141
+ *Configuration Parameters*:
142
+
143
+ - `name` (str): The name of the flow. Default: "ChatAtomic_Flow_with_Demonstrations"
144
+ - `description` (str): A description of the flow. This description is used to generate the help message of the flow.
145
+ Default: "A sequential flow that answers questions with demonstrations"
146
+ - `subflows_config` (Dict[str,Any]): A dictionary of subflows configurations of the sequential Flow. Default:
147
+ - `Demonstration Flow`: The configuration of the Demonstration Flow. By default, it a DemonstrationsAtomicFlow.
148
+ Its default parmaters are defined in DemonstrationsAtomicFlow).
149
+ - `Chat Flow`: The configuration of the Chat Flow. By default, its a ChatAtomicFlow.
150
+ Its default parmaters are defined in ChatAtomicFlowModule (see Flowcard, i.e. README.md, of ChatAtomicFlowModule).
151
+ - `topology` (str): The topology of the flow which is "sequential". By default, the topology is the one shown in the
152
+ illustration above (the topology is also described in ChatWithDemonstrationsFlow.yaml).
153
+
154
+ *Input Interface*:
155
+
156
+ - `query` (str): A query asked to the flow (e.g. "What is the capital of France?")
157
+
158
+ Output Interface:
159
+
160
+ - `answer` (str): The answer of the flow to the query
161
+
162
+ **Arguments**:
163
+
164
+ - `\**kwargs`: Arguments to be passed to the parent class SequentialFlow constructor.
165
+
__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ~~~ Specify the dependencies ~~
2
+
3
+
4
+ dependencies = [
5
+ {"url": "aiflows/ChatFlowModule", "revision": "main"},
6
+ ]
7
+ from aiflows import flow_verse
8
+ flow_verse.sync_dependencies(dependencies)
9
+
10
+ from .ChatWithDemonstrationsFlow import ChatWithDemonstrationsFlow
11
+ from .DemonstrationsAtomicFlow import DemonstrationsAtomicFlow
demo.yaml ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ _target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.ChatWithDemonstrationsFlow.instantiate_from_default_config
2
+ name: "SimpleQA_Flow_with_Demonstrations"
3
+ description: "A sequential flow that answers questions with demonstrations"
4
+
5
+ input_interface: # Connector between the "input data" and the Flow
6
+ - "questions"
7
+ output_interface: # Connector between the Flow's output and the caller
8
+ - "answer"
9
+
10
+ subflows_config:
11
+ demonstration_flow:
12
+ _target_: flow_modules.aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_config
13
+ data:
14
+ - query_data:
15
+ query: "What is the capital of Turkey?"
16
+ response_data:
17
+ response: "Istambul, my sir."
18
+ - query_data:
19
+ query: "what is the capital of Germany?"
20
+ response_data:
21
+ response: "Berlin, my sir."
22
+ query_prompt_template:
23
+ template: |2-
24
+ Answer the following question: {{query}}
25
+ input_variables:
26
+ - "query"
27
+ response_prompt_template:
28
+ template: |2-
29
+ {{response}}
30
+ input_variables:
31
+ - response
32
+
33
+ chat_flow:
34
+ _target_: flow_modules.aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_config
35
+ name: "SimpleQA_Flow"
36
+ # ~~~ Input interface specification ~~~
37
+ input_interface_non_initialized:
38
+ - "question"
39
+
40
+ # ~~~ backend model parameters ~~
41
+ backend:
42
+ _target_: aiflows.backends.llm_lite.LiteLLMBackend
43
+ api_infos: ???
44
+ model_name:
45
+ openai: "gpt-3.5-turbo"
46
+ azure: "azure/gpt-4"
47
+
48
+ # ~~~ generation_parameters ~~
49
+ n: 1
50
+ max_tokens: 3000
51
+ temperature: 0.3
52
+
53
+ top_p: 0.2
54
+ frequency_penalty: 0
55
+ presence_penalty: 0
56
+
57
+ n_api_retries: 6
58
+ wait_time_between_retries: 20
59
+
60
+ # ~~~ Prompt specification ~~~
61
+ system_message_prompt_template:
62
+ _target_: aiflows.prompt_template.JinjaPrompt
63
+ template: |2-
64
+ You are a helpful chatbot that truthfully answers questions. Answer in a similar way to your previous replies.
65
+ input_variables: []
66
+ partial_variables: {}
67
+
68
+ init_human_message_prompt_template:
69
+ _target_: aiflows.prompt_template.JinjaPrompt
70
+ template: |2-
71
+ Answer the following question: {{question}}
72
+ input_variables: ["question"]
73
+ partial_variables: {}
pip_requirements.txt ADDED
File without changes
run.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import hydra
4
+
5
+ import aiflows
6
+ from aiflows.flow_launchers import FlowLauncher
7
+ from aiflows.backends.api_info import ApiInfo
8
+ from aiflows.utils.general_helpers import read_yaml_file, quick_load_api_keys
9
+
10
+ from aiflows import logging
11
+ from aiflows.flow_cache import CACHING_PARAMETERS, clear_cache
12
+ from aiflows.utils import serve_utils
13
+ from aiflows.workers import run_dispatch_worker_thread
14
+ from aiflows.messages import FlowMessage
15
+ from aiflows.interfaces import KeyInterface
16
+ CACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching
17
+ # clear_cache() # Uncomment this line to clear the cache
18
+
19
+ logging.set_verbosity_debug() # Uncomment this line to see verbose logs
20
+
21
+ from aiflows import flow_verse
22
+
23
+
24
+ dependencies = [
25
+ {"url": "aiflows/ChatWithDemonstrationsFlowModule", "revision": os.getcwd()}
26
+ ]
27
+
28
+ flow_verse.sync_dependencies(dependencies)
29
+ if __name__ == "__main__":
30
+ # ~~~ Set the API information ~~~
31
+ # OpenAI backend
32
+
33
+ api_information = [ApiInfo(backend_used="openai", api_key=os.getenv("OPENAI_API_KEY"))]
34
+
35
+
36
+ FLOW_MODULES_PATH = "./"
37
+
38
+ jwt = os.getenv("COLINK_JWT")
39
+ addr = os.getenv("LOCAL_COLINK_ADDRESS")
40
+
41
+ cl = serve_utils.start_colink_component(
42
+ "Reverse Number Demo",
43
+ {"jwt": jwt, "addr": addr}
44
+ )
45
+
46
+ # # Azure backend
47
+ # api_information = ApiInfo(backend_used = "azure",
48
+ # api_base = os.getenv("AZURE_API_BASE"),
49
+ # api_key = os.getenv("AZURE_OPENAI_KEY"),
50
+ # api_version = os.getenv("AZURE_API_VERSION") )
51
+
52
+ root_dir = "."
53
+ cfg_path = os.path.join(root_dir, "demo.yaml")
54
+ cfg = read_yaml_file(cfg_path)
55
+
56
+ serve_utils.recursive_serve_flow(
57
+ cl = cl,
58
+ flow_type="simpleDemonstrationQA",
59
+ default_config=cfg,
60
+ default_state=None,
61
+ default_dispatch_point="coflows_dispatch",
62
+ )
63
+
64
+ quick_load_api_keys(cfg, api_information, key="api_infos")
65
+
66
+ proxy_flow = serve_utils.recursive_mount(
67
+ cl=cl,
68
+ client_id="local",
69
+ flow_type="simpleDemonstrationQA",
70
+ config_overrides=cfg,
71
+ initial_state=None,
72
+ dispatch_point_override=None,
73
+ )
74
+
75
+
76
+ # ~~~ Get the data ~~~
77
+ data = {"id": 0, "question": "What's the capital of France?"} # This can be a list of samples
78
+ # data = {"id": 0, "question": "Who was the NBA champion in 2023?"} # This can be a list of samples
79
+ # ~~~ Run inference ~~~
80
+
81
+ input_message = FlowMessage(
82
+ data= data,
83
+ src_flow="Coflows team",
84
+ dst_flow=proxy_flow,
85
+ is_input_msg=True
86
+ )
87
+
88
+ future = proxy_flow.ask(input_message)
89
+
90
+ print(future.get_data())