nemoguardrails.actions.llm.generation

View as Markdown

A set of actions for generating various types of completions using an LLMs.

Module Contents

Classes

NameDescription
LLMGenerationActionsA container objects for multiple related actions.

Functions

NameDescription
clean_utterance_contentClean an utterance by performing the following operations:

Data

local_streaming_handlers

log

API

class nemoguardrails.actions.llm.generation.LLMGenerationActions(
config: nemoguardrails.rails.llm.config.RailsConfig,
llm: typing.Optional[nemoguardrails.types.LLMModel],
llm_task_manager: nemoguardrails.llm.taskmanager.LLMTaskManager,
get_embedding_search_provider_instance: typing.Callable[[Optional[EmbeddingSearchProvider]], nemoguardrails.embeddings.index.EmbeddingsIndex],
verbose: bool = False
)

A container objects for multiple related actions.

_init_lock
= asyncio.Lock()
_passthrough_fn
Optional[Callable[..., Awaitable[str]]] = None
bot_messages
= config.bot_messages.copy()
env
= SandboxedEnvironment()
user_messages
= config.user_messages.copy()
nemoguardrails.actions.llm.generation.LLMGenerationActions._ensure_bot_message_index()
async
nemoguardrails.actions.llm.generation.LLMGenerationActions._ensure_flows_index()
async
nemoguardrails.actions.llm.generation.LLMGenerationActions._ensure_user_message_index()
async
nemoguardrails.actions.llm.generation.LLMGenerationActions._extract_bot_message_example(
flow: nemoguardrails.colang.v2_x.lang.colang_ast.Flow
)
nemoguardrails.actions.llm.generation.LLMGenerationActions._extract_user_message_example(
flow: nemoguardrails.colang.v2_x.lang.colang_ast.Flow
) -> None

Heuristic to extract user message examples from a flow.

nemoguardrails.actions.llm.generation.LLMGenerationActions._get_general_instructions()

Helper to extract the general instruction.

nemoguardrails.actions.llm.generation.LLMGenerationActions._get_sample_conversation_two_turns()

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.

nemoguardrails.actions.llm.generation.LLMGenerationActions._init_bot_message_index()
async

Initializes the index of bot messages.

nemoguardrails.actions.llm.generation.LLMGenerationActions._init_flows_index()
async

Initializes the index of flows.

nemoguardrails.actions.llm.generation.LLMGenerationActions._init_user_message_index()
async

Initializes the index of user messages.

nemoguardrails.actions.llm.generation.LLMGenerationActions._process_flows()

Process the provided flows to extract the user utterance examples.

nemoguardrails.actions.llm.generation.LLMGenerationActions._render_string(
template_str: str,
context: typing.Optional[dict] = None
) -> str

Render a string using the provided context information.

Parameters:

template_str
str

The string template to render.

context
Optional[dict]Defaults to None

The context for rendering.

Returns: str

The rendered string.

nemoguardrails.actions.llm.generation.LLMGenerationActions._search_flows_index(
text,
max_results
)
async

Search the index of flows.

nemoguardrails.actions.llm.generation.LLMGenerationActions.generate_bot_message(
events: typing.List[dict],
context: dict,
llm: typing.Optional[nemoguardrails.types.LLMModel] = None
)
async

Generate a bot message based on the desired bot intent.

nemoguardrails.actions.llm.generation.LLMGenerationActions.generate_intent_steps_message(
events: typing.List[dict],
llm: typing.Optional[nemoguardrails.types.LLMModel] = None,
kb: typing.Optional[nemoguardrails.kb.kb.KnowledgeBase] = None
)
async

Generate all three main Guardrails phases with a single LLM call. The three phases are: user canonical from (user intent), next flow steps (i.e. bot canonical form) and bot message.

nemoguardrails.actions.llm.generation.LLMGenerationActions.generate_next_steps(
events: typing.List[dict],
llm: typing.Optional[nemoguardrails.types.LLMModel] = None
)
async

Generate the next step in the current conversation flow.

Currently, only generates a next step after a user intent.

nemoguardrails.actions.llm.generation.LLMGenerationActions.generate_user_intent(
events: typing.List[dict],
context: dict,
config: nemoguardrails.rails.llm.config.RailsConfig,
llm: typing.Optional[nemoguardrails.types.LLMModel] = None,
kb: typing.Optional[nemoguardrails.kb.kb.KnowledgeBase] = None
)
async

Generate the canonical form for what the user said i.e. user intent.

nemoguardrails.actions.llm.generation.LLMGenerationActions.generate_value(
instructions: str,
events: typing.List[dict],
var_name: typing.Optional[str] = None,
llm: typing.Optional[nemoguardrails.types.LLMModel] = None
)
async

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

nemoguardrails.actions.llm.generation.clean_utterance_content(
utterance: str
) -> str

Clean an utterance by performing the following operations:

  • replacing “\n” with ” ”.

Args: utterance (str): The utterance to clean.

Returns: str: The cleaned utterance.

nemoguardrails.actions.llm.generation.local_streaming_handlers = {}
nemoguardrails.actions.llm.generation.log = logging.getLogger(__name__)