Getting Started#

This section contains everything related to prompts. A prompt is the value passed into the Language Model. This value can either be a string (for LLMs) or a list of messages (for Chat Models).

The data types of these prompts are rather simple, but their construction is anything but. Value props of LangChain here include:

  • A standard interface for string prompts and message prompts

  • A standard (to get started) interface for string prompt templates and message prompt templates

  • Example Selectors: methods for inserting examples into the prompt for the language model to follow

  • OutputParsers: methods for inserting instructions into the prompt as the format in which the language model should output information, as well as methods for then parsing that string output into a format.

We have in depth documentation for specific types of string prompts, specific types of chat prompts, example selectors, and output parsers.

Here, we cover a quick-start for a standard interface for getting started with simple prompts.

PromptTemplates#

PromptTemplates are responsible for constructing a prompt value. These PromptTemplates can do things like formatting, example selection, and more. At a high level, these are basically objects that expose a format_prompt method for constructing a prompt. Under the hood, ANYTHING can happen.

from langchain.prompts import PromptTemplate, ChatPromptTemplate
string_prompt = PromptTemplate.from_template("tell me a joke about {subject}")
chat_prompt = ChatPromptTemplate.from_template("tell me a joke about {subject}")
string_prompt_value = string_prompt.format_prompt(subject="soccer")
chat_prompt_value = chat_prompt.format_prompt(subject="soccer")

to_string#

This is what is called when passing to an LLM (which expects raw text)

string_prompt_value.to_string()
'tell me a joke about soccer'
chat_prompt_value.to_string()
'Human: tell me a joke about soccer'

to_messages#

This is what is called when passing to ChatModel (which expects a list of messages)

string_prompt_value.to_messages()
[HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)]
chat_prompt_value.to_messages()
[HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)]