💻 Practice Notebook
Master the concepts from this page with hands-on practice: 💻 VS Code | 🚀 Colab | 📥 Download Notebook 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:
- Structural Consistency: Forces the model to align with the visual and structural formatting of your examples.
- Improved Accuracy: Demonstrating tasks reduces reasoning errors and context hallucination.
- 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.Example 2: Sentiment Classifier
Demonstrate sentiment analysis classification (Positive/Negative) using few-shot templates.1.3 Few-Shot Practice Exercise
Exercise: Few-Shot Entity Extraction
Create a few-shot prompt usingFewShotPromptTemplate that formats examples for extracting a person and their company from text.
Instructions:
- Import
PromptTemplateandFewShotPromptTemplate. - Define a list containing two example dictionaries matching variables
textandoutput.- Example 1:
"John works at Google."->{"person": "John", "company": "Google"} - Example 2:
"Alice joined Microsoft."->{"person": "Alice", "company": "Microsoft"}
- Example 1:
- Configure the
example_prompttemplate formatting. - Assemble the
FewShotPromptTemplatespecifying a suffix to query for"Bob works at Amazon.". - Invoke a chat model using this formatted template and print the result.
Solution
Solution
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.2.1 Sequential Chains using LCEL
You can construct sequential chains cleanly using LangChain Expression Language: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:
- Import
PromptTemplate,StrOutputParser, andinit_chat_model. - Define
learning_promptusingPromptTemplateto suggest a step-by-step plan for learning{activity}. - Define
time_promptusingPromptTemplateto create a concise 1-week schedule for a{learning_plan}. - Compose the sequential chain using LCEL, mapping the first sub-chain output to the variable
"learning_plan". - Call
.invoke()passing"learn to swim"and print the response.
Solution
Solution