Files
langchain/docs/modules/prompts/getting_started.ipynb
T

5.3 KiB

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.

In [13]:
from langchain.prompts import PromptTemplate, ChatPromptTemplate
In [14]:
string_prompt = PromptTemplate.from_template("tell me a joke about {subject}")
In [15]:
chat_prompt = ChatPromptTemplate.from_template("tell me a joke about {subject}")
In [16]:
string_prompt_value = string_prompt.format_prompt(subject="soccer")
In [17]:
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)

In [18]:
string_prompt_value.to_string()
Out [18]:
'tell me a joke about soccer'
In [19]:
chat_prompt_value.to_string()
Out [19]:
'Human: tell me a joke about soccer'

to_messages

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

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