Node Details

  • Name: chatPromptTemplate
  • Type: ChatPromptTemplate
  • Version: 2.0
  • Category: Prompts

Base Classes

  • ChatPromptTemplate
  • BasePromptTemplate

Parameters

Inputs

  1. System Message (Required)
    • Type: string
    • Description: Initial system message that sets the context or role for the AI
    • Example: "You are a helpful assistant that translates {input_language} to {output_language}."
  2. Human Message (Required)
    • Type: string
    • Description: Human message prompt added at the end of the message sequence
    • Example: “{text}
  3. Format Prompt Values (Optional)
    • Type: JSON
    • Description: Variables specification for use in prompts
    • Example:
      {
        "input_language": "English",
        "output_language": "Spanish"
      }
      
  4. Messages History (Optional)
    • Type: Tabs
    • Default: messageHistoryCode
    • Description: Additional messages after System Message for few-shot examples
    • Tabs:
      • Add Messages (Code)
        • Type: code
        • Description: Custom message history using JavaScript code

Functionality

  1. Message Management
    • System message handling
    • Human message formatting
    • History integration
    • Variable substitution
  2. Template Processing
    • Dynamic execution
    • Code sandboxing
    • Context building
    • Error handling

Use Cases

  1. Conversational AI
    • Customer service
    • Language translation
    • Task assistance
    • Knowledge base queries
  2. Context Management
    • Role definition
    • Few-shot learning
    • Conversation history
    • Dynamic responses

Integration Notes

  • Uses @langchain/core/prompts
  • Supports JavaScript execution
  • Handles JSON parsing
  • Implements sandboxed environments

Best Practices

  1. System Message Design
    • Clear instructions
    • Specific role definition
    • Consistent context
    • Appropriate constraints
  2. Message History
    • Relevant examples
    • Progressive complexity
    • Clear structure
    • Safe code execution
  3. Variable Management
    • Clear naming
    • Type consistency
    • Error handling
    • Default values

Input/Output

Input

  • The node takes in the defined parameters (system message, human message, prompt values, and optional message history).
  • If message history code is provided, it is executed in a sandboxed environment.

Output

  • The node outputs a ChatPromptTemplate object that includes:
    • A system message
    • Optional message history (if provided and valid)
    • A human message

Usage

This node is used to create structured chat prompts for language models. It’s particularly useful for:
  1. Setting up consistent system instructions across multiple interactions.
  2. Defining a standard format for human inputs.
  3. Incorporating few-shot examples or specific conversation context through the message history feature.
  4. Allowing for dynamic prompt creation by using variables in the system and human messages.

Implementation Details

  • The node uses the @langchain/core/prompts library to create the chat prompt template.
  • It supports dynamic execution of JavaScript code for creating custom message histories.
  • The node handles JSON parsing for prompt values, allowing for variable substitution in the prompts.
  • Error handling is implemented for invalid JSON or message history code execution.

Notes

  • The message history feature uses a sandboxed VM for executing user-provided code, ensuring safety and isolation.
  • The node is designed to be flexible, allowing for both simple prompt creation and more complex setups with custom message histories.
  • It integrates with a database (likely for storing or retrieving additional context), as indicated by the use of DataSource and IDatabaseEntity.