File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
nemoguardrails/actions/v2_x Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 5151from nemoguardrails .context import (
5252 generation_options_var ,
5353 llm_call_info_var ,
54+ raw_llm_request ,
5455 streaming_handler_var ,
5556)
5657from 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
You can’t perform that action at this time.
0 commit comments