Represents a modified language model template.

Example

const tpl = new PromptTemplate('alpaca');

Hierarchy

  • PromptTemplate

Constructors

  • Constructs a new PromptTemplate instance.

    Parameters

    • template: string | LmTemplate

      Either the name of the template to load or an instance of LmTemplate.

    Returns PromptTemplate

    Example

    const tpl = new PromptTemplate('alpaca');
    

Properties

_extraAssistant: string = ""
_extraSystem: string = ""
_replacePrompt: string = ""
_replaceSystem: string = ""
afterShot?: string
assistant: string
history: HistoryTurn[] = []
id: string
linebreaks?: SpacingSlots
name: string
prefix?: string
shots?: TurnBlock[]
stop?: string[]
system?: PromptBlock
user: string

Methods

  • Parameters

    • Optional msg: string

    Returns string

  • Parameters

    • skip_empty_system: boolean

    Returns string

  • Parameters

    • Optional msg: string

    Returns string

  • Adds a new shot (a user-assistant interaction) to the template.

    Parameters

    • user: string

      The user's message.

    • assistant: string

      The assistant's response.

    Returns PromptTemplate

    A reference to the current PromptTemplate instance for chaining.

    Example

    tpl.addShot('Is it raining?', 'No, it is sunny.');
    
  • Adds multiple shots (user-assistant interactions) to the template.

    This function allows you to add multiple turns to the conversation. Each turn is represented by an object with a 'user' property (the user's message) and an 'assistant' property (the assistant's response).

    Parameters

    • shots: TurnBlock[]

      An array of objects, where each object represents a user-assistant interaction.

    Returns PromptTemplate

    • A reference to the current PromptTemplate instance for chaining.

    Example

    const tpl = new PromptTemplate('alpaca');
    tpl.addShots([
    { user: 'What is the weather like?', assistant: 'It is sunny today!' },
    { user: 'What is the weather like tomorrow?', assistant: 'I am sorry, but I can\'t predict the future.' }
    ]);
  • Appends a given message after the assistant prompt token.

    Parameters

    • msg: string

      The message to append.

    Returns PromptTemplate

    A reference to the current PromptTemplate instance for chaining.

    Example

    tpl.afterAssistant('( answer in json )');
    
  • Appends a given message after the system message.

    Parameters

    • msg: string

      The message to append.

    Returns PromptTemplate

    A reference to the current PromptTemplate instance for chaining.

    Example

    tpl.afterSystem('You are a javascript expert');
    
  • Clones the current PromptTemplate instance to a new instance of PromptTemplate.

    This function creates a new PromptTemplate instance with the same state as the current instance. It is useful when you want to work with a copy of the current template without modifying the original one.

    Parameters

    • template: string | LmTemplate

      The id or template instance of the new PromptTemplate to make

    • keepShots: boolean = true

      Keep the shots for the template instance: this will also clone the shots

    Returns PromptTemplate

    • A new PromptTemplate instance with the same state as the current one.

    Example

    const tpl = new PromptTemplate('alpaca');
    const clonedTpl = tpl.cloneTo('chatml');
    console.log(clonedTpl);
  • Renders the template with the provided message replacing the {prompt} placeholder.

    Parameters

    • msg: string

      The message to use for replacing the {prompt} placeholder.

    Returns string

    The rendered template with the provided message.

    Example

    const prompted = tpl.prompt("list the planets in the solar system");
    console.log(prompted);
  • Renders the template into a string representation.

    Parameters

    • skip_empty_system: boolean = false

    Returns string

    The rendered template as a string.

    Example

    const rendered = tpl.render();
    console.log(rendered);
  • Render a turn block

    Parameters

    Returns string

    ther rendered text

  • Replaces the {prompt} placeholder in the user message with a given message.

    Parameters

    • msg: string

      The message to replace the placeholder with.

    Returns PromptTemplate

    A reference to the current PromptTemplate instance for chaining.

    Example

    tpl.replacePrompt(fix this invalid json:\n\njson\n{prompt}\n);

  • Replaces the system block with a given message.

    Parameters

    • msg: string

      The message to replace the system block with.

    Returns PromptTemplate

    A reference to the current PromptTemplate instance for chaining.

    Example

    tpl.replaceSystem('You are a javascript expert');
    
  • Converts the current PromptTemplate instance to a JSON object.

    This function serializes the current state of the PromptTemplate instance into a JSON object, which can be used for storing the template or transmitting it over a network.

    Returns LmTemplate

    • A JSON object representing the current state of the PromptTemplate.

    Example

    const tpl = new PromptTemplate('alpaca');
    const json = tpl.toJson();
    console.log(json);

Generated using TypeDoc