← All ivy-nodes
IVYXSTUDIO · IVY NODE
P

Prompt Generator

ivy.node.prompt-generator · v0.1.0

ivyx✓

Builds a prompt from a context and a question with a prompt creator, refusing one longer than max_tokens words.

#Prompt#Generator

Inputs

FieldTypeDescription
creatorrequiredNot declaredA prompt creator, from default-, json- or xml-prompt-creator.
contextrequiredstringThe context the prompt carries.
questionrequiredstringThe question the prompt asks.
max_tokensrequiredintegerRefuse a prompt longer than this many words.

Outputs

FieldTypeDescription
promptrequiredstringNot declared

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.9

  • builds

    The creator's prompt comes back as written.

  • too-long

    A prompt over the word limit is refused.