nbaldwin commited on
Commit
798fa73
1 Parent(s): 90178e8

readme and demo

Browse files
.gitignore CHANGED
@@ -1 +1,443 @@
1
- __pycache__/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 CHANGED
@@ -9,5 +9,39 @@ log = logging.get_logger(__name__)
9
 
10
 
11
  class ChatWithDemonstrationsFlow(SequentialFlow):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def __init__(self,**kwargs):
13
  super().__init__(**kwargs)
 
9
 
10
 
11
  class ChatWithDemonstrationsFlow(SequentialFlow):
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)
ChatWithDemonstrationsFlow.yaml CHANGED
@@ -6,7 +6,7 @@ subflows_config:
6
  _target_: aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_config
7
 
8
  chat_flow:
9
- _target_: aiflows.OpenAIChatFlowModule.OpenAIChatAtomicFlow.instantiate_from_default_config
10
 
11
  topology:
12
  - goal: Get Demonstrations
@@ -24,3 +24,10 @@ topology:
24
  _target_: flows.interfaces.KeyInterface
25
  keys_to_rename:
26
  api_output: answer # Rename the api_output to answer
 
 
 
 
 
 
 
 
6
  _target_: aiflows.ChatWithDemonstrationsFlowModule.DemonstrationsAtomicFlow.instantiate_from_default_config
7
 
8
  chat_flow:
9
+ _target_: aiflows.ChatFlowModule.ChatAtomicFlow.instantiate_from_default_config
10
 
11
  topology:
12
  - goal: Get Demonstrations
 
24
  _target_: flows.interfaces.KeyInterface
25
  keys_to_rename:
26
  api_output: answer # Rename the api_output to answer
27
+
28
+
29
+ input_interface_initialized:
30
+ - "query"
31
+
32
+ output_interface:
33
+ - "answer"
DemonstrationsAtomicFlow.py CHANGED
@@ -5,12 +5,61 @@ from flows.utils import general_helpers
5
  from typing import Dict,Any,Optional,List
6
  from flows.prompt_template import JinjaPrompt
7
  from copy import deepcopy
8
- import numpy as np
9
  import os
10
  import hydra
11
  log = logging.get_logger(__name__)
12
 
13
  class DemonstrationsAtomicFlow(AtomicFlow):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  demonstrations_k: Optional[int] = None
15
  query_prompt_template: JinjaPrompt
16
  response_prompt_template: JinjaPrompt
@@ -31,6 +80,13 @@ class DemonstrationsAtomicFlow(AtomicFlow):
31
 
32
  @classmethod
33
  def _set_up_prompts(cls, config):
 
 
 
 
 
 
 
34
  kwargs = {}
35
  kwargs["query_prompt_template"] = \
36
  hydra.utils.instantiate(config['query_prompt_template'], _convert_="partial")
@@ -40,6 +96,13 @@ class DemonstrationsAtomicFlow(AtomicFlow):
40
 
41
  @classmethod
42
  def instantiate_from_config(cls, config):
 
 
 
 
 
 
 
43
  flow_config = deepcopy(config)
44
 
45
  kwargs = {"flow_config": flow_config}
@@ -52,14 +115,35 @@ class DemonstrationsAtomicFlow(AtomicFlow):
52
  return cls(**kwargs)
53
 
54
  def _get_query_message_content(self, sample_data: Dict):
 
 
 
 
 
 
 
55
  input_variables = self.query_prompt_template.input_variables
56
  return self.query_prompt_template.format(**{k: sample_data[k] for k in input_variables})
57
 
58
  def _get_response_message_content(self, sample_data: Dict):
 
 
 
 
 
 
 
59
  input_variables = self.response_prompt_template.input_variables
60
  return self.response_prompt_template.format(**{k: sample_data[k] for k in input_variables})
61
 
62
  def _get_io_pair(self, idx):
 
 
 
 
 
 
 
63
  dp = self.data[idx]
64
 
65
  query_data = dp["query_data"]
@@ -71,11 +155,19 @@ class DemonstrationsAtomicFlow(AtomicFlow):
71
  return {"idx": idx, "query": query,"response": response}
72
 
73
  def _get_io_pairs(self,input_data: Dict[str, Any]) -> List[Any]:
 
 
 
 
 
 
 
74
  demonstrations_k = self.demonstrations_k if self.demonstrations_k is not None else len(self.data)
75
  io_pairs = [self._get_io_pair(idx) for idx in range(demonstrations_k)]
76
  return io_pairs
77
 
78
  def _load_data(self):
 
79
  demonstrations_file = os.path.join(self.params["data_dir"], f"{self.params['demonstrations_id']}.jsonl")
80
  self.data = general_helpers.read_jsonlines(demonstrations_file)
81
 
@@ -91,4 +183,11 @@ class DemonstrationsAtomicFlow(AtomicFlow):
91
 
92
  def run(self,
93
  input_data: Dict[str, Any]) -> Dict[str, Any]:
 
 
 
 
 
 
 
94
  return {**input_data,**{"demonstrations": self._get_io_pairs(input_data=input_data)}}
 
5
  from typing import Dict,Any,Optional,List
6
  from flows.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
 
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")
 
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}
 
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"]
 
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
 
 
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)}}
README.md DELETED
@@ -1,3 +0,0 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
__init__.py CHANGED
@@ -1,6 +1,6 @@
1
  # ~~~ Specify the dependencies ~~
2
  dependencies = [
3
- {"url": "aiflows/OpenAIChatFlowModule", "revision": "d69ba2125de99d2edb631dd51d22225ed9e3446c"},
4
  ]
5
  from flows import flow_verse
6
  flow_verse.sync_dependencies(dependencies)
 
1
  # ~~~ Specify the dependencies ~~
2
  dependencies = [
3
+ {"url": "aiflows/ChatFlowModule", "revision": "a749ad10ed39776ba6721c37d0dc22af49ca0f17"},
4
  ]
5
  from flows import flow_verse
6
  flow_verse.sync_dependencies(dependencies)
demo.yaml ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ flow: # Overrides the OpenAIChatAtomicFlow config
2
+ _target_: aiflows.ChatWithDemonstrationsFlowModule.ChatWithDemonstrationsFlow.instantiate_from_default_config
3
+ name: "SimpleQA_Flow_with_Demonstrations"
4
+ description: "A sequential flow that answers questions with demonstrations"
5
+
6
+ input_interface: # Connector between the "input data" and the Flow
7
+ - "questions"
8
+ output_interface: # Connector between the Flow's output and the caller
9
+ - "answer"
10
+
11
+ subflows_config:
12
+ demonstration_flow:
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
+ params:
23
+ data_dir: null
24
+ demonstrations_id: my_sir_demo
25
+ query_prompt_template:
26
+ template: |2-
27
+ Answer the following question: {{query}}
28
+ input_variables:
29
+ - "query"
30
+ response_prompt_template:
31
+ template: |2-
32
+ {{response}}
33
+ input_variables:
34
+ - response
35
+
36
+ chat_flow:
37
+ name: "SimpleQA_Flow"
38
+ # ~~~ Input interface specification ~~~
39
+ input_interface_non_initialized:
40
+ - "question"
41
+
42
+ # ~~~ backend model parameters ~~
43
+ backend:
44
+ _target_: flows.backends.llm_lite.LiteLLMBackend
45
+ api_infos: ???
46
+ model_name:
47
+ openai: "gpt-3.5-turbo"
48
+ azure: "azure/gpt-4"
49
+
50
+ # ~~~ generation_parameters ~~
51
+ n: 1
52
+ max_tokens: 3000
53
+ temperature: 0.3
54
+
55
+ top_p: 0.2
56
+ frequency_penalty: 0
57
+ presence_penalty: 0
58
+
59
+ n_api_retries: 6
60
+ wait_time_between_retries: 20
61
+
62
+ # ~~~ Prompt specification ~~~
63
+ system_message_prompt_template:
64
+ _target_: flows.prompt_template.JinjaPrompt
65
+ template: |2-
66
+ You are a helpful chatbot that truthfully answers questions. Answer in a similar way to your previous replies.
67
+ input_variables: []
68
+ partial_variables: {}
69
+
70
+
71
+ init_human_message_prompt_template:
72
+ _target_: flows.prompt_template.JinjaPrompt
73
+ template: |2-
74
+ Answer the following question: {{question}}
75
+ input_variables: ["question"]
76
+ partial_variables: {}
run.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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() # Uncomment this line to see verbose logs
17
+
18
+ from flows import flow_verse
19
+
20
+
21
+ dependencies = [
22
+ {"url": "aiflows/ChatWithDemonstrationsFlowModule", "revision": os.getcwd()},
23
+ ]
24
+
25
+ flow_verse.sync_dependencies(dependencies)
26
+
27
+ if __name__ == "__main__":
28
+ # ~~~ Set the API information ~~~
29
+ # OpenAI backend
30
+
31
+ api_information = [ApiInfo(backend_used="openai",
32
+ api_key = os.getenv("OPENAI_API_KEY"))]
33
+
34
+
35
+ # # Azure backend
36
+ # api_information = ApiInfo(backend_used = "azure",
37
+ # api_base = os.getenv("AZURE_API_BASE"),
38
+ # api_key = os.getenv("AZURE_OPENAI_KEY"),
39
+ # api_version = os.getenv("AZURE_API_VERSION") )
40
+
41
+ root_dir = "."
42
+ cfg_path = os.path.join(root_dir, "demo.yaml")
43
+ cfg = read_yaml_file(cfg_path)
44
+
45
+ cfg["flow"]["subflows_config"]["chat_flow"]["backend"]["api_infos"] = api_information
46
+
47
+ # ~~~ Instantiate the Flow ~~~
48
+ flow_with_interfaces = {
49
+ "flow": hydra.utils.instantiate(cfg['flow'], _recursive_=False, _convert_="partial"),
50
+ "input_interface": (
51
+ None
52
+ if cfg.get( "input_interface", None) is None
53
+ else hydra.utils.instantiate(cfg['input_interface'], _recursive_=False)
54
+ ),
55
+ "output_interface": (
56
+ None
57
+ if cfg.get( "output_interface", None) is None
58
+ else hydra.utils.instantiate(cfg['output_interface'], _recursive_=False)
59
+ ),
60
+ }
61
+ # ~~~ Get the data ~~~
62
+ data = {"id": 0, "question": "What's the capital of France?"} # This can be a list of samples
63
+ # data = {"id": 0, "question": "Who was the NBA champion in 2023?"} # This can be a list of samples
64
+ # ~~~ Run inference ~~~
65
+ path_to_output_file = None
66
+ # path_to_output_file = "output.jsonl" # Uncomment this line to save the output to disk
67
+
68
+ _, outputs = FlowLauncher.launch(
69
+ flow_with_interfaces=flow_with_interfaces,
70
+ data=data,
71
+ path_to_output_file=path_to_output_file
72
+ )
73
+
74
+ # ~~~ Print the output ~~~
75
+ flow_output_data = outputs[0]
76
+ print(flow_output_data)