> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Prompt Templates

> Learn how to define, manage, and format reusable prompts in LangChain

In this section, you will learn how to define and structure reusable prompt templates using LangChain.

## Objectives

1. Understand the difference between `PromptTemplate` and `ChatPromptTemplate`.
2. Construct templates using `.from_template()`, `.from_messages()`, and direct class constructors.
3. Format templates with inputs and feed them to Chat Models.

***

## Why Use Prompt Templates?

In real-world LLM applications, you rarely send hardcoded, static strings to a model. Instead, you construct dynamic prompts that combine instructions, context, and user inputs. Prompt templates abstract this formatting, allowing you to define reusable templates with placeholders that are populated at runtime.

LangChain provides two core classes for building prompts:

1. **`PromptTemplate`**: Used for plain string prompts.
2. **`ChatPromptTemplate`**: Used for structured chat-based messages (containing system, human, or assistant roles).

***

## Three Ways to Construct Prompts

You can access and build these templates in three primary ways:

### 1. Using `.from_template()`

Create a prompt template from a single string containing placeholders.

```python theme={null}
from langchain.prompts import PromptTemplate

# Creates a PromptTemplate directly from a string template
template = PromptTemplate.from_template("Tell me a joke about {topic}.")
```

### 2. Using `.from_messages()` (ChatPromptTemplate)

Create a structured chat prompt from a list of messages (such as system and human roles represented as tuples or message objects).

```python theme={null}
from langchain.prompts import ChatPromptTemplate

# Creates a ChatPromptTemplate from a sequence of system/human roles
chat_template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("human", "Explain the concept of {concept}.")
])
```

### 3. Direct Class Instantiation

Construct the templates directly using class constructors, manually specifying input variables and template strings.

```python theme={null}
from langchain.prompts import PromptTemplate

# Create a PromptTemplate using the class constructor directly
template = PromptTemplate(
    input_variables=["topic"],
    template="Tell me a joke about {topic}."
)
```

***

## Next Steps: What We Will Build

We will implement and explore prompt templates and output parsers in the following sections:

1. **Prompt Template**: Focuses on string-based prompts using `PromptTemplate`.
2. **Chat Prompt Template**: Focuses on structured message prompts using `ChatPromptTemplate`.
3. **Parsers Basics**: Introduces output parsers, the need for them, and basic parsers (`StrOutputParser`, `CommaSeparatedListOutputParser`).
4. **Pydantic Output Parser**: Focuses on deserializing outputs directly into strongly-typed Pydantic schemas.
5. **JSON Output Parser**: Explores generating structured JSON outputs in dict formats, with or without validation schemas.

## Practice & Exercises

To reinforce what you've learned in this section, practice with the interactive notebooks:

<CardGroup cols={2}>
  <Card title="PromptTemplate Practice" icon="laptop-code">
    Practice string templates.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langchain/1_prompt_template.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langchain/1_prompt_template.ipynb)
  </Card>

  <Card title="ChatPromptTemplate Practice" icon="laptop-code">
    Practice chat templates.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langchain/2_chat_prompt_template.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langchain/2_chat_prompt_template.ipynb)
  </Card>
</CardGroup>
