Few-shot learning is a prompting technique where you provide the AI with one or several small number of examples (typically 1-10) to demonstrate the desired pattern, format, or behavior before asking it to perform a similar task. This technique leverages the AI’s ability to recognize patterns and generalize from limited examples.
Zero-shot prompting involves asking the AI to perform a task without any examples, relying solely on its pre-existing knowledge. Few-shot prompting, on the other hand, provides a few examples to guide the AI’s response. Few-shot learning is generally more effective for complex tasks where context and specific patterns are crucial.
One-shot prompting provides a single example to guide the AI, the idea behind one-shot learning is to show the AI how to perform a task with just one example.
All variants of few-shot prompting (zero-shot, one-shot, and few-shot) can be implemented in Latitude. The choice depends on the complexity of the task and the amount of guidance needed.
Here’s a simple few-shot learning example for email classification:
Email Classification
Copy
Ask AI
---provider: OpenAImodel: gpt-4otemperature: 0.1---# Email Classification TaskYou are an expert email classifier. Based on the examples below, classify the given emailinto one of these categories: URGENT, SPAM, BUSINESS, PERSONAL.## Examples:<user>**Email**: "CONGRATULATIONS! You've won $1,000,000! Click here now!"</user><assistant>**Category**: SPAM</assistant><user>**Email**: "Hi John, can we reschedule tomorrow's meeting? I have a family emergency."</user><assistant>**Category**: PERSONAL</assistant><user>**Email**: "SYSTEM ALERT: Server down. Immediate attention required. Revenue impact critical."</user><assistant>**Category**: URGENT</assistant><user>**Email**: "Please find attached the Q4 financial report for your review."</user><assistant>**Category**: BUSINESS</assistant>## Email to Classify:{{ email_content }}## Category:
---provider: OpenAImodel: gpt-4otemperature: 0.2---# {{ product_category }} Product Review Sentiment AnalysisAnalyze the sentiment of {{ product_category }} reviews and extract {{ detail_level }} insights. Use the examples below as a guide.## Examples:{{ for example in examples }}**Review**: "{{ example.text }}"**Sentiment**: {{ example.sentiment }}**Key Points**: {{ example.key_points }}**Confidence**: {{ example.confidence }}{{ endfor }}## Review to Analyze:{{ review_text }}## Analysis:**Sentiment**:**Key Points**:**Confidence**:
In this advanced example:
Dynamic Content: We use templates ({{ variable }}) to insert parameters into the prompt.
Templating Features: We demonstrate control structures like {{for item in items }} for arrays.
Runtime Examples: The examples array parameter allows users to pass in any number of examples when calling the prompt.
This pattern makes your prompts more flexible and reusable across different use cases without creating separate prompts for each scenario. For more on parameters, see the Latitude Parameter Types documentation.
Latitude’s chain feature allows you to create complex few-shot workflows:
Copy
Ask AI
---provider: OpenAImodel: gpt-4otemperature: 0.1---<step># Information ExtractionExtract structured information from the business proposal. Follow these examples:## Examples:**Input**: "We propose a mobile app for food delivery in NYC with $50K funding needed."**Output**:{ "business_type": "mobile app", "industry": "food delivery", "location": "NYC", "funding_needed": "$50K"}**Input**: "Looking for $2M Series A for our AI-powered healthcare platform serving hospitals nationwide."```json{ "business_type": "AI platform", "industry": "healthcare", "location": "nationwide", "funding_needed": "$2M"}```## Proposal to Extract:{{ proposal_text }}## Extracted Information:</step><step># Business Viability AnalysisBased on the extracted information, provide a viability score following these examples:## Examples:**Business**: AI healthcare platform, $2M Series A, nationwide**Viability Score**: 8.5/10**Reasoning**: Large addressable market, proven demand, appropriate funding level**Business**: Food delivery app, $50K seed, NYC only**Viability Score**: 6.0/10**Reasoning**: Competitive market, limited geographic scope, low initial funding## Analysis:**Viability Score**:**Reasoning**:</step>
Use Latitude’s conditional features to adapt examples based on context:
Copy
Ask AI
---provider: OpenAImodel: gpt-4otemperature: 0.3---# Content Generation with Context-Aware ExamplesGenerate {{ content_type }} following the appropriate examples below:{{ if content_type === "social_media" }} <assistant> ## Social Media Examples: **Topic**: "New restaurant opening" **Content**: "🍕 EXCITING NEWS! Our new pizza place opens tomorrow at 123 Main St! First 50 customers get 50% off! #GrandOpening #Pizza #Foodie" **Topic**: "Product launch" **Content**: "🚀 Introducing our game-changing productivity app! Streamline your workflow like never before. Download now: [link] #ProductLaunch #Productivity #Innovation" </assistant>{{ endif }}{{ if content_type === "email" }} <assistant> ## Email Examples: **Topic**: "Welcome new customer" **Subject**: "Welcome to [Company] - Your journey starts here!" **Content**: "Dear [Name], Thank you for choosing [Company]. We're excited to help you achieve your goals. Here's what to expect next..." **Topic**: "Product announcement" **Subject**: "Introducing [Product] - Transform your [Industry]" **Content**: "Hi [Name], We're thrilled to announce [Product], designed specifically for professionals like you who want to [benefit]..." </assistant>{{ endif }}{{ if content_type === "blog" }} <assistant> ## Blog Examples: **Topic**: "Industry trends" **Content**: "# The Future of [Industry]: 5 Trends to Watch in 2024\n\nThe [industry] landscape is evolving rapidly. Here are the key trends shaping our future:\n\n## 1. [Trend Name]\n[Detailed explanation...]" **Topic**: "How-to guide" **Content**: "# Step-by-Step Guide: How to [Action]\n\nAre you struggling with [problem]? This comprehensive guide will walk you through everything you need to know.\n\n## Prerequisites\n- [Requirement 1]\n- [Requirement 2]" </assistant>{{ endif }}## Content Request:**Topic**: {{ topic }}**Target Audience**: {{ audience }}**Key Message**: {{ key_message }}## Generated {{ content_type }}:
Combine few-shot learning with Latitude’s agent system for complex workflows:
Copy
Ask AI
---provider: OpenAImodel: gpt-4otemperature: 0.2type: agentagents: - agents/data_extractor - agents/validator - agents/formatter---# Document Processing PipelineProcess the document using specialized agents, each with their own few-shot examples.## Document Input:{{ document_content }}## Processing Steps:1. **Data Extraction**: Use the data_extractor agent to pull key information2. **Validation**: Use the validator agent to verify accuracy3. **Formatting**: Use the formatter agent to structure outputProcess the document and provide the final structured result.
Create prompts that can improve their own examples:
Copy
Ask AI
---provider: OpenAImodel: gpt-4otemperature: 0.4---<step># Process the task with current examplesCurrent examples:{{ current_examples }}Task: {{ user_task }}Result:</step><step># Evaluate the result qualityResult: {{ initial_processing }}Was this result satisfactory? If not, what examples would improve future performance?Evaluation:</step><step># Generate improved examples if neededBased on the evaluation. If needs improvement. Generate 2-3 new examples that would help with tasks like: {{ user_task }}New examples should address the identified weaknesses.Improved Examples:</step>
Use few-shot learning to transfer patterns across domains:
Copy
Ask AI
---provider: OpenAImodel: gpt-4otemperature: 0.3---# Cross-Domain Pattern TransferLearn the pattern from {{ source_domain }} examples and apply it to {{ target_domain }}.## Source Domain Examples ({{ source_domain }}):{{ for example in source_examples }}**Input**: {{ example.input }}**Pattern Applied**: {{ example.pattern }}**Output**: {{ example.output }}{{ endfor }}## Pattern Recognition:The consistent pattern across these examples is: {{ identified_pattern }}## Target Domain Application ({{ target_domain }}):Now apply the same pattern to this {{ target_domain }} scenario:**Input**: {{ target_input }}**Pattern to Apply**: {{ identified_pattern }}**Output**: