Skip to main content

Prompts

Writing good prompts is key to getting the best performance out of your applications. LangSmith provides ways to create, test, and manage prompts.

Prompt types

We support three types of prompt templates:

  1. StringPromptTemplate
  2. ChatPromptTemplate
  3. StructuredPromptTemplate

For detailed information about these templates, please refer to the LangChain documentation. In LangSmith, you can create prompts using the Playground. From the prompt view in the Playground, you can select either "Chat-style prompt" or "Instruct-style prompt" to get started.

Chat-style prompt

Chat prompts are used for chat-style models that accept a list of messages as an input and respond with an assistant message. A chat-style prompt is represented in LangSmith as a ChatPromptTemplate, which can contain multiple messages, each with prompt variables. You can also specify an output schema which is represented in LangSmith as a StructuredPromptTemplate.

Instruct-style prompt

An instruct-style prompt is represented as a StringPromptTemplate that gets formatted to a single string input for your model.

Template formats

We support two types of template formats: f-string and mustache. You can switch between these formats when editing prompts in the Playground.

F-string

F-strings are a Python-specific string formatting method that allows you to embed expressions inside string literals, using curly braces {}. Here's an example of an f-string template:

Hello, {name}!

Mustache

Mustache is a template syntax that allows you to embed variables inside double curly braces {{}}.

Note

Please see the Mustache documentation for more detailed information on how to use Mustache templates.

Here's an example of a mustache template:

Hello, {{name}}!

Mustache is more robust than f-strings since it supports more complex logic. You can use conditionals, loops, and access nested keys in mustache templates.

Conditional

Hello, {{#name}}{{name}}{{/name}}{{^name}}world{{/name}}!

The template will output "Hello, Bob!" if "Bob" is provided as the name variable, and "Hello, world!" if the name variable is not provided.

Loop

{{#names}}{{name}}{{/names}}

input:

{
"names": [{ "name": "Alice" }, { "name": "Bob" }, { "name": "Charlie" }]
}

output: "AliceBobCharlie"

Loop with nesting

{{#people}}{{name}} is {{age}} years old. {{/people}}

input:

{
"people": [
{ "name": "Alice", "age": 30 },
{ "name": "Bob", "age": 25 },
{ "name": "Charlie", "age": 35 }
]
}

output: "Alice is 30 years old. Bob is 25 years old. Charlie is 35 years old."

Dot notation

{{person.name}} is {{person.age}} years old.

input:

{
"person": {
"name": "Alice",
"age": 30
}
}

output: "Alice is 30 years old."


Was this page helpful?


You can leave detailed feedback on GitHub.