Skip to content

Commit c3a42b7

Browse files
committed
feat: add raw_llm_request handling in LLMGenerationActions
1 parent 25c61bc commit c3a42b7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

nemoguardrails/actions/v2_x/generation.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from nemoguardrails.context import (
5252
generation_options_var,
5353
llm_call_info_var,
54+
raw_llm_request,
5455
streaming_handler_var,
5556
)
5657
from nemoguardrails.embeddings.index import EmbeddingsIndex, IndexItem
@@ -407,6 +408,27 @@ async def passthrough_llm_action(
407408
):
408409
event = get_last_user_utterance_event_v2_x(events)
409410

411+
# We check if we have a raw request. If the guardrails API is using
412+
# the `generate_events` API, this will not be set.
413+
raw_prompt = raw_llm_request.get()
414+
415+
if raw_prompt is None:
416+
prompt = event["text"]
417+
else:
418+
if isinstance(raw_prompt, str):
419+
# If we're in completion mode, we use directly the last $user_message
420+
# as it may have been altered by the input rails.
421+
prompt = event["text"]
422+
elif isinstance(raw_prompt, list):
423+
prompt = raw_prompt.copy()
424+
425+
# In this case, if the last message is from the user, we replace the text
426+
# just in case the input rails may have altered it.
427+
if prompt[-1]["role"] == "user":
428+
raw_prompt[-1]["content"] = event["text"]
429+
else:
430+
raise ValueError(f"Unsupported type for raw prompt: {type(raw_prompt)}")
431+
410432
if not state.rails_config.passthrough:
411433
raise Exception("Passthrough is not enabled.")
412434

0 commit comments

Comments
 (0)