> ## 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.

# Few-Shot & Sequential Prompting

> Guide model behavior with examples and chain prompts sequentially

## 💻 Practice Notebook

Master the concepts from this page with hands-on practice:
[💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/prompt-engg/prompt-engg-fewshot-vscode.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/prompt-engg/prompt-engg-fewshot-colab.ipynb) | <a href="/public/notebooks/prompt-engg/prompt-engg-fewshot-vscode.ipynb" download>📥 Download Notebook</a>

This section covers advanced prompting patterns: guiding model output format using examples (Few-Shot Prompting) and linking prompts in sequence where the output of one step informs the next (Sequential Prompting).

## 1. Few-Shot Prompting

"Shots" refer to the examples provided to the model inside the prompt to show it how to perform a task.

* **Zero-Shot Prompting**: No examples are provided. The model relies entirely on pre-trained instructions.
* **One-Shot Prompting**: One example is provided to illustrate the target structure.
* **Few-Shot Prompting**: Multiple examples are provided. This is highly recommended for complex logic, custom styles, or structural outputs.

### 1.1 Why Few-Shot Prompting is Required & Its Advantages

While modern LLMs are capable of zero-shot completions, they often struggle when:

* **Complex Formatting**: You need the model to return data in a highly specific structure or syntax (e.g., custom JSON format, exact punctuation, or nested schemas) that is hard to explain in instructions alone.
* **Domain Specificity**: The task requires adhering to a specific company tone, shorthand notation, or industry-specific classification schemas.
* **Edge-Case Safety**: You want to train the model's behavior on complex logic boundaries (e.g., math problems or entity relationships) by showing correct resolutions.

#### Key Advantages:

1. **Structural Consistency**: Forces the model to align with the visual and structural formatting of your examples.
2. **Improved Accuracy**: Demonstrating tasks reduces reasoning errors and context hallucination.
3. **No Fine-Tuning Required**: Achieve custom model behaviors inside the context window at runtime, avoiding the cost of fine-tuning the model weights.

### 1.2 Few-Shot Code Examples

#### Example 1: Math Assistant

Create a few-shot prompt to demonstrate basic math calculations and then execute the prompt using a chat model.

```python theme={null}
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain.chat_models import init_chat_model

# 1. Define list of examples
examples = [
    {"question": "2 + 2", "answer": "4"},
    {"question": "3 + 3", "answer": "6"}
]

# 2. Define formatting prompt template for each example
example_prompt = PromptTemplate(
    input_variables=["question", "answer"],
    template="Question: {question}\nAnswer: {answer}"
)

# 3. Create FewShotPromptTemplate
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    suffix="Question: {input}\nAnswer:",
    input_variables=["input"]
)

# 4. Format the final prompt
formatted_prompt = few_shot_prompt.format(input="10 + 10")
print("--- Formatted Prompt ---")
print(formatted_prompt)

# 5. Initialize model and invoke
llm = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")
response = llm.invoke(formatted_prompt)

print("\n--- LLM Response ---")
print(response.content)
```

**Output:**

```text theme={null}
--- Formatted Prompt ---
Question: 2 + 2
Answer: 4

Question: 3 + 3
Answer: 6

Question: 10 + 10
Answer:

--- LLM Response ---
20
```

#### Example 2: Sentiment Classifier

Demonstrate sentiment analysis classification (Positive/Negative) using few-shot templates.

```python theme={null}
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain.chat_models import init_chat_model

# 1. Define few-shot examples
examples = [
    {"text": "I love this product.", "sentiment": "Positive"},
    {"text": "This is terrible.", "sentiment": "Negative"},
    {"text": "This movie is amazing.", "sentiment": "Positive"}
]

# 2. Define example prompt template
example_prompt = PromptTemplate(
    input_variables=["text", "sentiment"],
    template="Text: {text}\nSentiment: {sentiment}"
)

# 3. Create FewShotPromptTemplate
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    suffix="Text: {input}\nSentiment:",
    input_variables=["input"]
)

formatted_prompt = few_shot_prompt.format(input="The customer support was extremely unhelpful and rude.")
print("--- Formatted Prompt ---")
print(formatted_prompt)

# 4. Initialize model and invoke
llm = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")
response = llm.invoke(formatted_prompt)

print("\n--- LLM Response ---")
print(response.content.strip())
```

**Output:**

```text theme={null}
--- Formatted Prompt ---
Text: I love this product.
Sentiment: Positive

Text: This is terrible.
Sentiment: Negative

Text: This movie is amazing.
Sentiment: Positive

Text: The customer support was extremely unhelpful and rude.
Sentiment:

--- LLM Response ---
Negative
```

### 1.3 Few-Shot Practice Exercise

#### Exercise: Few-Shot Entity Extraction

Create a few-shot prompt using `FewShotPromptTemplate` that formats examples for extracting a person and their company from text.

**Instructions:**

1. Import `PromptTemplate` and `FewShotPromptTemplate`.
2. Define a list containing two example dictionaries matching variables `text` and `output`.
   * Example 1: `"John works at Google."` -> `{"person": "John", "company": "Google"}`
   * Example 2: `"Alice joined Microsoft."` -> `{"person": "Alice", "company": "Microsoft"}`
3. Configure the `example_prompt` template formatting.
4. Assemble the `FewShotPromptTemplate` specifying a suffix to query for `"Bob works at Amazon."`.
5. Invoke a chat model using this formatted template and print the result.

<Accordion title="Solution">
  ```python theme={null}
  from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
  from langchain.chat_models import init_chat_model

  # 1. Define few-shot examples
  examples = [
      {"text": "John works at Google.", "output": '{"person": "John", "company": "Google"}'},
      {"text": "Alice joined Microsoft.", "output": '{"person": "Alice", "company": "Microsoft"}'}
  ]

  # 2. Define example prompt template
  example_prompt = PromptTemplate(
      input_variables=["text", "output"],
      template="Text: {text}\nOutput: {output}"
  )

  # 3. Define FewShotPromptTemplate
  few_shot_template = FewShotPromptTemplate(
      examples=examples,
      example_prompt=example_prompt,
      suffix="Text: {input}\nOutput:",
      input_variables=["input"]
  )

  # 4. Format prompt
  formatted_prompt = few_shot_template.format(input="Bob works at Amazon.")

  # 5. Initialize model and invoke
  llm = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")
  response = llm.invoke(formatted_prompt)

  print(response.content)
  ```
</Accordion>

## 2. Sequential Prompting

Sequential prompting chains multiple prompts together so the output of one LLM call is automatically passed as an input variable into the next.

```text theme={null}
User Input -> [Prompt 1] -> LLM -> [Response 1] -> [Prompt 2 (utilizes Response 1)] -> LLM -> Final Response
```

### 2.1 Sequential Chains using LCEL

You can construct sequential chains cleanly using LangChain Expression Language:

```python theme={null}
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain.chat_models import init_chat_model

llm = init_chat_model("groq:llama-3.3-70b-versatile")

# First Prompt: Generates detailed information
info_prompt = PromptTemplate(
    input_variables=["topic"],
    template="Provide a detailed overview of {topic}."
)

# Second Prompt: Summarizes information generated by Prompt 1
summary_prompt = PromptTemplate(
    input_variables=["details"],
    template="Summarize the following details in 2 bullet points:\n{details}"
)

# Compose the sequential chain
# We map the output of the first chain to the input variable 'details' for the second prompt
sequential_chain = (
    {"details": info_prompt | llm | StrOutputParser()}
    | summary_prompt
    | llm
    | StrOutputParser()
)

response = sequential_chain.invoke({"topic": "Docker"})
print(response)
```

### 2.2 Sequential Practice Exercise

#### Exercise: Sequential Learning Planner

Write a sequential chain that takes a goal activity (e.g., `"learn to swim"`), asks the LLM to write a comprehensive learning guide, and then passes that guide to a second prompt that formats it as a 1-week crash course schedule.

**Instructions:**

1. Import `PromptTemplate`, `StrOutputParser`, and `init_chat_model`.
2. Define `learning_prompt` using `PromptTemplate` to suggest a step-by-step plan for learning `{activity}`.
3. Define `time_prompt` using `PromptTemplate` to create a concise 1-week schedule for a `{learning_plan}`.
4. Compose the sequential chain using LCEL, mapping the first sub-chain output to the variable `"learning_plan"`.
5. Call `.invoke()` passing `"learn to swim"` and print the response.

<Accordion title="Solution">
  ```python theme={null}
  from langchain_core.prompts import PromptTemplate
  from langchain_core.output_parsers import StrOutputParser
  from langchain.chat_models import init_chat_model

  llm = init_chat_model("groq:llama-3.3-70b-versatile")

  learning_prompt = PromptTemplate(
      input_variables=["activity"],
      template="I want to learn how to {activity}. Can you suggest how I can learn this step-by-step?"
  )

  time_prompt = PromptTemplate(
      input_variables=["learning_plan"],
      template="I only have one week. Can you create a concise plan to help me hit this goal: {learning_plan}."
  )

  # Complete the sequential chain with LCEL
  seq_chain = (
      {"learning_plan": learning_prompt | llm | StrOutputParser()}
      | time_prompt
      | llm
      | StrOutputParser()
  )

  # Call the chain
  result = seq_chain.invoke({"activity": "learn to swim"})
  print(result)
  ```
</Accordion>
