-
Notifications
You must be signed in to change notification settings - Fork 409
/
generation.py
1344 lines (1106 loc) · 53.7 KB
/
generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A set of actions for generating various types of completions using an LLMs."""
import asyncio
import logging
import random
import re
import sys
import threading
import uuid
from ast import literal_eval
from functools import lru_cache
from time import time
from typing import Callable, List, Optional, cast
from jinja2 import meta
from jinja2.sandbox import SandboxedEnvironment
from langchain_core.language_models.llms import BaseLLM
from nemoguardrails.actions.actions import ActionResult, action
from nemoguardrails.actions.llm.utils import (
flow_to_colang,
get_first_nonempty_line,
get_last_bot_intent_event,
get_last_user_intent_event,
get_last_user_utterance_event,
get_multiline_response,
get_retrieved_relevant_chunks,
get_top_k_nonempty_lines,
llm_call,
strip_quotes,
)
from nemoguardrails.colang import parse_colang_file
from nemoguardrails.colang.v2_x.lang.colang_ast import Flow, Spec, SpecOp
from nemoguardrails.colang.v2_x.runtime.eval import eval_expression
from nemoguardrails.context import (
generation_options_var,
llm_call_info_var,
raw_llm_request,
streaming_handler_var,
)
from nemoguardrails.embeddings.index import EmbeddingsIndex, IndexItem
from nemoguardrails.kb.kb import KnowledgeBase
from nemoguardrails.llm.params import llm_params
from nemoguardrails.llm.prompts import get_prompt
from nemoguardrails.llm.taskmanager import LLMTaskManager
from nemoguardrails.llm.types import Task
from nemoguardrails.logging.explain import LLMCallInfo
from nemoguardrails.patch_asyncio import check_sync_call_from_async_loop
from nemoguardrails.rails.llm.config import EmbeddingSearchProvider, RailsConfig
from nemoguardrails.rails.llm.options import GenerationOptions
from nemoguardrails.streaming import StreamingHandler
from nemoguardrails.utils import get_or_create_event_loop, new_event_dict, new_uuid
log = logging.getLogger(__name__)
local_streaming_handlers = {}
class LLMGenerationActions:
"""A container objects for multiple related actions."""
def __init__(
self,
config: RailsConfig,
llm: BaseLLM,
llm_task_manager: LLMTaskManager,
get_embedding_search_provider_instance: Callable[
[Optional[EmbeddingSearchProvider]], EmbeddingsIndex
],
verbose: bool = False,
):
self.config = config
self.llm = llm
self.verbose = verbose
# We extract the user/bot messages from the config as we might alter them.
self.user_messages = config.user_messages.copy()
self.bot_messages = config.bot_messages.copy()
# If we have user messages, we build an index with them
self.user_message_index = None
self.bot_message_index = None
self.flows_index = None
self.get_embedding_search_provider_instance = (
get_embedding_search_provider_instance
)
# There are still some edge cases not covered by nest_asyncio.
# Using a separate thread always for now.
loop = get_or_create_event_loop()
if True or check_sync_call_from_async_loop():
t = threading.Thread(target=asyncio.run, args=(self.init(),))
t.start()
t.join()
else:
loop.run_until_complete(self.init())
self.llm_task_manager = llm_task_manager
# We also initialize the environment for rendering bot messages
self.env = SandboxedEnvironment()
# If set, in passthrough mode, this function will be used instead of
# calling the LLM with the user input.
self.passthrough_fn = None
async def init(self):
# For Colang 2.x we need to do some initial processing
if self.config.colang_version == "2.x":
self._process_flows()
await asyncio.gather(
self._init_user_message_index(),
self._init_bot_message_index(),
self._init_flows_index(),
)
def _extract_user_message_example(self, flow: Flow):
"""Heuristic to extract user message examples from a flow."""
elements = [
item
for item in flow.elements
if item["_type"] != "doc_string_stmt" and item["_type"] != "stmt"
]
if len(elements) != 2:
return
el = elements[1]
if isinstance(el, SpecOp):
if el.op == "match":
spec = cast(SpecOp, el).spec
if (
not hasattr(spec, "name")
or spec.name != "UtteranceUserActionFinished"
):
return
if "final_transcript" not in spec.arguments:
return
# Extract the message and remove the double quotes
message = eval_expression(spec.arguments["final_transcript"], {})
if isinstance(message, str):
self.user_messages[flow.name] = [message]
elif el.op == "await":
spec = cast(SpecOp, el).spec
if isinstance(spec, dict) and spec.get("_type") == "spec_or":
specs = spec.get("elements")
else:
assert isinstance(spec, Spec)
specs = [spec]
for spec in specs:
if (
not spec.name.startswith("user ")
or not spec.arguments
or not spec.arguments["$0"]
):
continue
message = eval_expression(spec.arguments["$0"], {})
if isinstance(message, str):
if flow.name not in self.user_messages:
self.user_messages[flow.name] = []
self.user_messages[flow.name].append(message)
def _extract_bot_message_example(self, flow: Flow):
# Quick heuristic to identify the user utterance examples
if len(flow.elements) != 2:
return
el = flow.elements[1]
if (
not isinstance(el, SpecOp)
or not hasattr(el.spec, "name")
or el.spec.name != "UtteranceBotAction"
or "script" not in el.spec.arguments
):
return
# Extract the message and remove the double quotes
message = el.spec.arguments["script"][1:-1]
self.bot_messages[flow.name] = [message]
def _process_flows(self):
"""Process the provided flows to extract the user utterance examples."""
flow: Flow
for flow in self.config.flows:
if flow.name.startswith("user "):
self._extract_user_message_example(flow)
if flow.name.startswith("bot "):
self._extract_bot_message_example(flow)
async def _init_user_message_index(self):
"""Initializes the index of user messages."""
if not self.user_messages:
return
items = []
for intent, utterances in self.user_messages.items():
for text in utterances:
items.append(IndexItem(text=text, meta={"intent": intent}))
# If we have no patterns, we stop.
if len(items) == 0:
return
self.user_message_index = self.get_embedding_search_provider_instance(
self.config.core.embedding_search_provider
)
await self.user_message_index.add_items(items)
# NOTE: this should be very fast, otherwise needs to be moved to separate thread.
await self.user_message_index.build()
async def _init_bot_message_index(self):
"""Initializes the index of bot messages."""
if not self.bot_messages:
return
items = []
for intent, utterances in self.bot_messages.items():
for text in utterances:
items.append(IndexItem(text=intent, meta={"text": text}))
# If we have no patterns, we stop.
if len(items) == 0:
return
self.bot_message_index = self.get_embedding_search_provider_instance(
self.config.core.embedding_search_provider
)
await self.bot_message_index.add_items(items)
# NOTE: this should be very fast, otherwise needs to be moved to separate thread.
await self.bot_message_index.build()
async def _init_flows_index(self):
"""Initializes the index of flows."""
if not self.config.flows:
return
items = []
for flow in self.config.flows:
# We don't include the system flows in the index because we don't want
# the LLM to predict system actions.
if flow.get("is_system_flow", False):
continue
# TODO: check if the flow has system actions and ignore the flow.
colang_flow = flow.get("source_code") or flow_to_colang(flow)
# We index on the full body for now
# items.append(IndexItem(text=colang_flow, meta={"flow": colang_flow}))
# EXPERIMENTAL: We create an index entry for every line in the flow
for line in colang_flow.split("\n"):
if line.strip() != "":
items.append(IndexItem(text=line, meta={"flow": colang_flow}))
# If we have no patterns, we stop.
if len(items) == 0:
return
self.flows_index = self.get_embedding_search_provider_instance(
self.config.core.embedding_search_provider
)
await self.flows_index.add_items(items)
# NOTE: this should be very fast, otherwise needs to be moved to separate thread.
await self.flows_index.build()
def _get_general_instructions(self):
"""Helper to extract the general instruction."""
text = ""
for instruction in self.config.instructions:
if instruction.type == "general":
text = instruction.content
# We stop at the first one for now
break
return text
@lru_cache
def _get_sample_conversation_two_turns(self):
"""Helper to extract only the two turns from the sample conversation.
This is needed to be included to "seed" the conversation so that the model
can follow the format more easily.
"""
lines = self.config.sample_conversation.split("\n")
i = 0
user_count = 0
while i < len(lines):
if lines[i].startswith("user "):
user_count += 1
if user_count == 3:
break
i += 1
sample_conversation = "\n".join(lines[0:i])
# Remove any trailing new lines
sample_conversation = sample_conversation.strip()
return sample_conversation
@action(is_system_action=True)
async def generate_user_intent(
self,
events: List[dict],
context: dict,
config: RailsConfig,
llm: Optional[BaseLLM] = None,
kb: Optional[KnowledgeBase] = None,
):
"""Generate the canonical form for what the user said i.e. user intent."""
# If using a single LLM call, use the specific action defined for this task.
if self.config.rails.dialog.single_call.enabled:
return await self.generate_intent_steps_message(
events=events, llm=llm, kb=kb
)
# The last event should be the "StartInternalSystemAction" and the one before it the "UtteranceUserActionFinished".
event = get_last_user_utterance_event(events)
assert event["type"] == "UserMessage"
# Use action specific llm if registered else fallback to main llm
llm = llm or self.llm
streaming_handler = streaming_handler_var.get()
# TODO: check for an explicit way of enabling the canonical form detection
if self.user_messages:
# TODO: based on the config we can use a specific canonical forms model
# or use the LLM to detect the canonical form. The below implementation
# is for the latter.
log.info("Phase 1 :: Generating user intent")
# We search for the most relevant similar user utterance
examples = ""
potential_user_intents = []
if self.user_message_index is not None:
threshold = None
if config.rails.dialog.user_messages:
threshold = (
config.rails.dialog.user_messages.embeddings_only_similarity_threshold
)
results = await self.user_message_index.search(
text=event["text"], max_results=5, threshold=threshold
)
# If the option to use only the embeddings is activated, we take the first
# canonical form.
if results and config.rails.dialog.user_messages.embeddings_only:
intent = results[0].meta["intent"]
return ActionResult(
events=[new_event_dict("UserIntent", intent=intent)]
)
elif (
config.rails.dialog.user_messages.embeddings_only
and config.rails.dialog.user_messages.embeddings_only_fallback_intent
):
intent = (
config.rails.dialog.user_messages.embeddings_only_fallback_intent
)
return ActionResult(
events=[new_event_dict("UserIntent", intent=intent)]
)
else:
results = await self.user_message_index.search(
text=event["text"], max_results=5
)
# We add these in reverse order so the most relevant is towards the end.
for result in reversed(results):
examples += f"user \"{result.text}\"\n {result.meta['intent']}\n\n"
if result.meta["intent"] not in potential_user_intents:
potential_user_intents.append(result.meta["intent"])
prompt = self.llm_task_manager.render_task_prompt(
task=Task.GENERATE_USER_INTENT,
events=events,
context={
"examples": examples,
"potential_user_intents": ", ".join(potential_user_intents),
},
)
# Initialize the LLMCallInfo object
llm_call_info_var.set(LLMCallInfo(task=Task.GENERATE_USER_INTENT.value))
# We make this call with temperature 0 to have it as deterministic as possible.
with llm_params(llm, temperature=self.config.lowest_temperature):
result = await llm_call(llm, prompt)
# Parse the output using the associated parser
result = self.llm_task_manager.parse_task_output(
Task.GENERATE_USER_INTENT, output=result
)
user_intent = get_first_nonempty_line(result)
if user_intent is None:
user_intent = "unknown message"
if user_intent and user_intent.startswith("user "):
user_intent = user_intent[5:]
log.info(
"Canonical form for user intent: "
+ (user_intent if user_intent else "None")
)
if user_intent is None:
return ActionResult(
events=[new_event_dict("UserIntent", intent="unknown message")]
)
else:
return ActionResult(
events=[new_event_dict("UserIntent", intent=user_intent)]
)
else:
output_events = []
# If we are in passthrough mode, we just use the input for prompting
if self.config.passthrough:
# We check if we have a raw request. If the guardrails API is using
# the `generate_events` API, this will not be set.
raw_prompt = raw_llm_request.get()
if raw_prompt is None:
prompt = event["text"]
else:
if isinstance(raw_prompt, str):
# If we're in completion mode, we use directly the last $user_message
# as it may have been altered by the input rails.
prompt = event["text"]
elif isinstance(raw_prompt, list):
prompt = raw_prompt.copy()
# In this case, if the last message is from the user, we replace the text
# just in case the input rails may have altered it.
if prompt[-1]["role"] == "user":
raw_prompt[-1]["content"] = event["text"]
else:
raise ValueError(
f"Unsupported type for raw prompt: {type(raw_prompt)}"
)
if self.passthrough_fn:
raw_output = await self.passthrough_fn(
context=context, events=events
)
# If the passthrough action returns a single value, we consider that
# to be the text output
if isinstance(raw_output, tuple) or isinstance(raw_output, list):
text, passthrough_output = raw_output[0], raw_output[1]
else:
text = raw_output
passthrough_output = None
# We record the passthrough output in the context
output_events.append(
new_event_dict(
"ContextUpdate",
data={"passthrough_output": passthrough_output},
)
)
else:
# Initialize the LLMCallInfo object
llm_call_info_var.set(LLMCallInfo(task=Task.GENERAL.value))
generation_options: GenerationOptions = generation_options_var.get()
with llm_params(
llm,
**(
(generation_options and generation_options.llm_params) or {}
),
):
text = await llm_call(
llm,
prompt,
custom_callback_handlers=[streaming_handler_var.get()],
)
else:
# Initialize the LLMCallInfo object
llm_call_info_var.set(LLMCallInfo(task=Task.GENERAL.value))
if kb:
chunks = await kb.search_relevant_chunks(event["text"])
relevant_chunks = "\n".join([chunk["body"] for chunk in chunks])
else:
# in case there is no user flow (user message) then we need the context update to work for relevant_chunks
relevant_chunks = get_retrieved_relevant_chunks(
events, skip_user_message=True
)
# Otherwise, we still create an altered prompt.
prompt = self.llm_task_manager.render_task_prompt(
task=Task.GENERAL,
events=events,
context={"relevant_chunks": relevant_chunks},
)
generation_options: GenerationOptions = generation_options_var.get()
with llm_params(
llm,
**((generation_options and generation_options.llm_params) or {}),
):
result = await llm_call(
llm,
prompt,
custom_callback_handlers=[streaming_handler_var.get()],
stop=["User:"],
)
text = result.strip()
if text.startswith('"'):
text = text[1:-1]
# In streaming mode, we also push this.
if streaming_handler:
await streaming_handler.push_chunk(text)
output_events.append(new_event_dict("BotMessage", text=text))
return ActionResult(events=output_events)
async def _search_flows_index(self, text, max_results):
"""Search the index of flows."""
results = await self.flows_index.search(text=text, max_results=10)
# we filter the results to keep only unique flows
flows = set()
final_results = []
for result in results:
if result.meta["flow"] not in flows:
flows.add(result.meta["flow"])
# For backwards compatibility we also replace the text with the full version
result.text = result.meta["flow"]
final_results.append(result)
return final_results[0:max_results]
@action(is_system_action=True)
async def generate_next_step(
self, events: List[dict], llm: Optional[BaseLLM] = None
):
"""Generate the next step in the current conversation flow.
Currently, only generates a next step after a user intent.
"""
log.info("Phase 2 :: Generating next step ...")
# Use action specific llm if registered else fallback to main llm
llm = llm or self.llm
# The last event should be the "StartInternalSystemAction" and the one before it the "UserIntent".
event = get_last_user_intent_event(events)
# Currently, we only predict next step after a user intent using LLM
if event["type"] == "UserIntent":
# If using a single LLM call, use the results computed in the first call.
if self.config.rails.dialog.single_call.enabled:
bot_intent_event = event["additional_info"]["bot_intent_event"]
return ActionResult(events=[bot_intent_event])
user_intent = event["intent"]
# We search for the most relevant similar flows
examples = ""
if self.flows_index:
results = await self._search_flows_index(
text=user_intent, max_results=5
)
# We add these in reverse order so the most relevant is towards the end.
for result in reversed(results):
examples += f"{result.text}\n\n"
prompt = self.llm_task_manager.render_task_prompt(
task=Task.GENERATE_NEXT_STEPS,
events=events,
context={"examples": examples},
)
# Initialize the LLMCallInfo object
llm_call_info_var.set(LLMCallInfo(task=Task.GENERATE_NEXT_STEPS.value))
# We use temperature 0 for next step prediction as well
with llm_params(llm, temperature=self.config.lowest_temperature):
result = await llm_call(llm, prompt)
# Parse the output using the associated parser
result = self.llm_task_manager.parse_task_output(
Task.GENERATE_NEXT_STEPS, output=result
)
# If we don't have multi-step generation enabled, we only look at the first line.
if not self.config.enable_multi_step_generation:
result = get_first_nonempty_line(result)
if result and result.startswith("bot "):
bot_intent = result[4:]
# Sometimes, the LLMs add also the message on the same line.
# We do some cleaning up if that's the case.
if '"' in bot_intent:
bot_intent = bot_intent.split('"')[0].strip()
# Also, sometimes, there's a comma and more content
if "," in bot_intent:
bot_intent = bot_intent.split(",")[0].strip()
next_step = {"bot": bot_intent}
else:
next_step = {"bot": "general response"}
# If we have to execute an action, we return the event to start it
if next_step.get("execute"):
return ActionResult(
events=[
new_event_dict(
"StartInternalSystemAction",
action_name=next_step["execute"],
)
]
)
else:
bot_intent = next_step.get("bot")
return ActionResult(
events=[new_event_dict("BotIntent", intent=bot_intent)]
)
else:
# Otherwise, we parse the output as a single flow.
# If we have a parsing error, we try to reduce size of the flow, potentially
# up to a single step.
lines = result.split("\n")
while True:
try:
parse_colang_file("dynamic.co", content="\n".join(lines))
break
except Exception as e:
# If we could not parse the flow on the last line, we return a general response
if len(lines) == 1:
log.info("Exception while parsing single line: %s", e)
return ActionResult(
events=[
new_event_dict(
"BotIntent", intent="general response"
)
]
)
log.info("Could not parse %s lines, reducing size", len(lines))
lines = lines[:-1]
return ActionResult(
events=[
# We generate a random UUID as the flow_id
new_event_dict(
"start_flow",
flow_id=new_uuid(),
flow_body="\n".join(lines),
)
]
)
return ActionResult(return_value=None)
def _render_string(
self,
template_str: str,
context: Optional[dict] = None,
) -> str:
"""Render a string using the provided context information.
Args:
template_str: The string template to render.
context: The context for rendering.
Returns:
The rendered string.
"""
# First, if we have any direct usage of variables in the string,
# we replace with correct Jinja syntax.
for param in re.findall(r"\$([^ \"'!?\-,;</]*(?:\w|]))", template_str):
template_str = template_str.replace(f"${param}", "{{" + param + "}}")
template = self.env.from_string(template_str)
# First, we extract all the variables from the template.
variables = meta.find_undeclared_variables(self.env.parse(template_str))
# This is the context that will be passed to the template when rendering.
render_context = {}
# Copy the context variables to the render context.
if context:
for variable in variables:
if variable in context:
render_context[variable] = context[variable]
return template.render(render_context)
@action(is_system_action=True)
async def generate_bot_message(
self, events: List[dict], context: dict, llm: Optional[BaseLLM] = None
):
"""Generate a bot message based on the desired bot intent."""
log.info("Phase 3 :: Generating bot message ...")
# Use action specific llm if registered else fallback to main llm
llm = llm or self.llm
# The last event should be the "StartInternalSystemAction" and the one before it the "BotIntent".
event = get_last_bot_intent_event(events)
assert event["type"] == "BotIntent"
bot_intent = event["intent"]
context_updates = {}
streaming_handler = streaming_handler_var.get()
if bot_intent in self.config.bot_messages:
# Choose a message randomly from self.config.bot_messages[bot_message]
# However, in test mode, we always choose the first one, to keep it predictable.
if "pytest" in sys.modules:
bot_utterance = self.bot_messages[bot_intent][0]
else:
bot_utterance = random.choice(self.bot_messages[bot_intent])
log.info("Found existing bot message: " + bot_utterance)
# We also need to render
bot_utterance = self._render_string(bot_utterance, context)
# We skip output rails for predefined messages.
context_updates["skip_output_rails"] = True
# Check if the output is supposed to be the content of a context variable
elif bot_intent[0] == "$" and bot_intent[1:] in context:
bot_utterance = context[bot_intent[1:]]
else:
# Generate the bot message using an LLM call
# If using a single LLM call, use the results computed in the first call.
if self.config.rails.dialog.single_call.enabled:
event = get_last_user_intent_event(events)
if event["type"] == "UserIntent":
bot_message_event = event["additional_info"]["bot_message_event"]
# We only need to use the bot message if it corresponds to the
# generate bot intent as well.
last_bot_intent = get_last_bot_intent_event(events)
if (
last_bot_intent["intent"]
== event["additional_info"]["bot_intent_event"]["intent"]
):
text = bot_message_event["text"]
# If the bot message is being generated in streaming mode
if text.startswith('Bot message: "<<STREAMING['):
# Format: `Bot message: "<<STREAMING[...]>>"`
# Extract the streaming handler uid and get a reference.
streaming_handler_uid = text[26:-4]
_streaming_handler = local_streaming_handlers[
streaming_handler_uid
]
# We pipe the content from this handler to the main one.
_streaming_handler.set_pipe_to(streaming_handler)
await _streaming_handler.disable_buffering()
# And wait for it to finish.
# We stop after the closing double quotes for the bot message.
_streaming_handler.stop = [
'"\n',
]
text = await _streaming_handler.wait()
return ActionResult(
events=[new_event_dict("BotMessage", text=text)]
)
else:
if streaming_handler:
await streaming_handler.push_chunk(
bot_message_event["text"]
)
return ActionResult(events=[bot_message_event])
# If we are in passthrough mode, we just use the input for prompting
if self.config.passthrough:
# If we have a passthrough function, we use that.
if self.passthrough_fn:
prompt = None
raw_output = await self.passthrough_fn(
context=context, events=events
)
# If the passthrough action returns a single value, we consider that
# to be the text output
if isinstance(raw_output, tuple) or isinstance(raw_output, list):
result, passthrough_output = raw_output[0], raw_output[1]
else:
result = raw_output
passthrough_output = None
# We record the passthrough output in the context
context_updates["passthrough_output"] = passthrough_output
else:
# Otherwise, we call the LLM with the prompt coming from the user.
t0 = time()
# Initialize the LLMCallInfo object
llm_call_info_var.set(
LLMCallInfo(task=Task.GENERATE_BOT_MESSAGE.value)
)
# We use the potentially updated $user_message. This means that even
# in passthrough mode, input rails can still alter the input.
prompt = context.get("user_message")
generation_options: GenerationOptions = generation_options_var.get()
with llm_params(
llm,
**(
(generation_options and generation_options.llm_params) or {}
),
):
result = await llm_call(
llm, prompt, custom_callback_handlers=[streaming_handler]
)
log.info(
"--- :: LLM Bot Message Generation passthrough call took %.2f seconds",
time() - t0,
)
else:
# Otherwise, we go through the process of creating the altered prompt,
# which includes examples, relevant chunks, etc.
# We search for the most relevant similar bot utterance
examples = ""
# NOTE: disabling bot message index when there are no user messages
if self.config.user_messages and self.bot_message_index:
results = await self.bot_message_index.search(
text=event["intent"], max_results=5
)
# We add these in reverse order so the most relevant is towards the end.
for result in reversed(results):
examples += (
f"bot {result.text}\n \"{result.meta['text']}\"\n\n"
)
# We compute the relevant chunks to be used as context
relevant_chunks = get_retrieved_relevant_chunks(events)
prompt_config = get_prompt(self.config, Task.GENERATE_BOT_MESSAGE)
prompt = self.llm_task_manager.render_task_prompt(
task=Task.GENERATE_BOT_MESSAGE,
events=events,
context={"examples": examples, "relevant_chunks": relevant_chunks},
)
t0 = time()
if streaming_handler:
# TODO: Figure out a more generic way to deal with this
if prompt_config.output_parser in ["verbose_v1", "bot_message"]:
streaming_handler.set_pattern(
prefix='Bot message: "', suffix='"'
)
else:
streaming_handler.set_pattern(prefix=' "', suffix='"')
# Initialize the LLMCallInfo object
llm_call_info_var.set(LLMCallInfo(task=Task.GENERATE_BOT_MESSAGE.value))
generation_options: GenerationOptions = generation_options_var.get()
with llm_params(
llm,
**((generation_options and generation_options.llm_params) or {}),
):
result = await llm_call(
llm, prompt, custom_callback_handlers=[streaming_handler]
)
log.info(
"--- :: LLM Bot Message Generation call took %.2f seconds",
time() - t0,
)
# Parse the output using the associated parser
result = self.llm_task_manager.parse_task_output(
Task.GENERATE_BOT_MESSAGE, output=result
)
# TODO: catch openai.error.InvalidRequestError from exceeding max token length
result = get_multiline_response(result)
result = strip_quotes(result)
bot_utterance = result
# Context variable starting with "_" are considered private (not used in tests or logging)
context_updates["_last_bot_prompt"] = prompt
log.info(f"Generated bot message: {bot_utterance}")
if bot_utterance:
bot_utterance = clean_utterance_content(bot_utterance)
# In streaming mode, we also push this.
if streaming_handler:
await streaming_handler.push_chunk(bot_utterance)
return ActionResult(
events=[new_event_dict("BotMessage", text=bot_utterance)],
context_updates=context_updates,
)
else:
# In streaming mode, we also push this.
bot_utterance = "I'm not sure what to say."
if streaming_handler:
await streaming_handler.push_chunk(bot_utterance)
return ActionResult(
events=[new_event_dict("BotMessage", text=bot_utterance)],
context_updates=context_updates,
)
@action(is_system_action=True)
async def generate_value(
self,
instructions: str,
events: List[dict],
var_name: Optional[str] = None,
llm: Optional[BaseLLM] = None,
):
"""Generate a value in the context of the conversation.
:param instructions: The instructions to generate the value.
:param events: The full stream of events so far.
:param var_name: The name of the variable to generate. If not specified, it will use
the `action_result_key` as the name of the variable.
:param llm: Custom llm model to generate_value
"""
# Use action specific llm if registered else fallback to main llm
llm = llm or self.llm
last_event = events[-1]
assert last_event["type"] == "StartInternalSystemAction"
if not var_name:
var_name = last_event["action_result_key"]
# We search for the most relevant flows.
examples = ""
if self.flows_index:
results = await self._search_flows_index(
text=f"${var_name} = ", max_results=5