Skip to content

Chat Prompt

The other type of prompt is a chat prompt. The chat prompt can be used with models such as gpt-3.5.turbo and gpt-4(+).

You create a chat prompt using createPrompt("chat") or createChatPrompt().

ts
const prompt = createPrompt("chat")
// or const prompt = createChatPrompt()
{
  "type": "chat",
  "messages": [],
  "partials": [],
  "helpers": [],
  "filters": {
    "pre": [],
    "post": []
  },
  "parseUserTemplates": false
}

When creating a chat prompt, you can optionally set an initial system message.

ts
const message = `You are a customer service agent for Some Company.`
const prompt = createPrompt("chat", message)
{
  "type": "chat",
  "messages": [
    {
      "role": "system",
      "content": "You are a customer service agent for Some Company."
    }
  ],
  "partials": [],
  "helpers": [],
  "filters": {
    "pre": [],
    "post": []
  },
  "parseUserTemplates": false
}

To use the prompt as input to an LLM, you can call the format() method on the prompt. The format method accepts an object, which is used to supply the prompt template with replacement values.

ts
const message = `You are a customer service agent for Some Company.`
const prompt = createPrompt("chat", message)

// The output of format is an array of chat messages
const formatted = prompt.format({});
[
  {
    "role": "system",
    "content": "You are a customer service agent for Some Company."
  }
]

Chat prompts support more than just a basic text-based message. You can also add assistant and user content.

ts
const message = `You are a customer service agent for Some Company.`
const prompt = createChatPrompt(message)
    
// You can add user and assistant messages
prompt.addUserMessage("Hello there")
prompt.addAssistantMessage("Welcome to Some Company, how can I help you?")
[
  {
    "role": "system",
    "content": "You are a customer service agent for Some Company."
  },
  {
    "role": "user",
    "content": "Hello there"
  },
  {
    "role": "assistant",
    "content": "Welcome to Some Company, how can I help you?"
  }
]

See prompt templates for more advanced prompt usage.

Chat Prompt Methods

addUserMessage Appends a user message to the prompt.

addAssistantMessage Appends an assistant message to the prompt.

addSystemMessage Appends a system message to the prompt.

addFromHistory Appends an array of existing chat history messages to the prompt.

format Format the prompt for LLM. This processes the template as a handlebars template.

validate Validate the prompt. Makes sure there are no unresolved tokens.