← All ivy-nodes
IVYXSTUDIO · IVY NODE
P

Prompt Generator

ivy.node.prompt-generator · v0.0.0

ivyx

Generates formatted prompts using a prompt creator function and validates token limits. This node takes a creator function (from default-prompt-creator, json-prompt-creator, or xml-prompt-creator), context, and question to produce a formatted prompt string. It enforces maximum token limits to prevent exceeding LLM input constraints. The output prompt is typically used with open-ai-client or hugging-face-llm-client nodes for AI interactions.

#Prompt#Generator

Inputs

FieldTypeDescription
creatorrequiredobjectA function that generates prompts based on context and question (Node reference)
max_tokensrequirednumberThe maximum number of tokens allowed in the prompt
contextrequiredstringThe context or background information
questionrequiredstringThe question to be answered
featuresobjectFeatures
labelsobjectLabels

Outputs

FieldTypeDescription
promptrequiredstringNot declared
featuresobjectFeatures
labelsobjectLabels

Source

python

# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
creator = inp["creator"]
max_tokens = inp["max_tokens"]
context = inp["context"]
question = inp["question"]

# Compute
from typing import Callable, Dict, Any

class PromptGenerator:
    """
    A class to generate prompts using different prompt creation strategies.
    """

    def __init__(self, creator: Callable[[str, str, Dict[str, Any]], str], max_tokens: int):
        """
        Initialize the PromptGenerator with a prompt creation function.

        :param creator: A function that generates prompts based on context and question.
        :param max_tokens: The maximum number of tokens allowed in the prompt.
        """
        self.creator: Callable[[str, str, Dict[str, Any]], str] = creator
        self.max_tokens: int = max_tokens

    def generate_prompt(self, context: str, question: str, **kwargs: Dict[str, Any]) -> str:
        """
        Generate a prompt using the assigned prompt creation function.

        :param context: The context or background information.
        :param question: The question to be answered.
        :param kwargs: Additional parameters for prompt formatting.
        :return: The generated prompt.
        """
        prompt: str = self.creator(context, question, **kwargs)

        # Ensure prompt does not exceed max token limit (if specified)
        if prompt and self.max_tokens is not None and len(prompt.split()) > self.max_tokens:
            raise ValueError(f"Prompt exceeds the maximum token limit of {self.max_tokens} tokens.")

        return prompt

prompt_generator = PromptGenerator(creator=creator, max_tokens=max_tokens)
prompt = prompt_generator.generate_prompt(context, question)

# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["prompt"] = prompt

Tests

Requires: python:3.11

  • basic-prompt-generation

    Generate prompt with valid inputs (should succeed).

    promptgenerationbasic
  • error-exceeds-token-limit

    Prompt exceeding max_tokens should raise an error.

    error-handlingvalidation