Few Shot Learning
This example demonstrates how to use additional_input with an Agent.
1"""2Few Shot Learning3=============================45This example demonstrates how to use additional_input with an Agent.6"""78from kern.agent import Agent9from kern.models.message import Message10from kern.models.openai import OpenAIResponses1112# ---------------------------------------------------------------------------13# Create Agent14# ---------------------------------------------------------------------------15# Few-shot examples showing good support patterns16support_examples = [17 # Example 1: Simple issue resolution18 Message(role="user", content="I forgot my password and can't log in"),19 Message(20 role="assistant",21 content="""22 I'll help you reset your password right away.2324 **Steps to Reset Your Password:**25 . Go to the login page and click "Forgot Password"26 . Enter your email address27 . Check your email for the reset link28 . Follow the link to create a new password29 . Use a strong password with uppercase, lowercase, numbers, and symbols3031 f you don't receive the email within 5 minutes, please check your spam folder. Let me know if you need any assistance with these steps!32 """.strip(),33 ),34 # Example 2: Billing issue pattern35 Message(36 role="user",37 content="I've been charged twice for the same order and I'm frustrated!",38 ),39 Message(40 role="assistant",41 content="""42 I sincerely apologize for the billing error and the frustration this has caused you.4344 **Immediate Action Plan:**45 1. I'll investigate your account to confirm the duplicate charge46 2. Process a full refund for the duplicate transaction47 3. Ensure this doesn't happen again by checking your payment method settings48 4. Provide you with a confirmation number once the refund is processed4950 The refund typically takes 3-5 business days to appear on your statement. As an apology for this inconvenience, I'd also like to offer you a 10% discount on your next order.5152 Is there anything else I can help you with regarding this issue?53 """.strip(),54 ),55 # Example 3: Technical support pattern56 Message(role="user", content="Your app keeps crashing when I try to upload photos"),57 Message(58 role="assistant",59 content="""60 I'm sorry you're experiencing crashes with photo uploads. Let me help you troubleshoot this issue.6162 **Troubleshooting Steps:**63 1. **Check App Version:** Ensure you have the latest version from the app store64 2. **Restart the App:** Close completely and reopen the application65 3. **Check Storage:** Make sure you have sufficient device storage (at least 1GB free)66 4. **Photo Size:** Try uploading smaller photos (under 10MB each)67 5. **Network Connection:** Ensure you have a stable internet connection6869 **If the issue persists:**70 - Try uploading one photo at a time instead of multiple71 - Clear the app cache in your device settings72 - Restart your device7374 If none of these steps resolve the issue, please let me know your device type and OS version, and I'll escalate this to our technical team for further investigation.75 """.strip(),76 ),77]787980# ---------------------------------------------------------------------------81# Run Agent82# ---------------------------------------------------------------------------83if __name__ == "__main__":84 # Create agent with few-shot learning85 agent = Agent(86 name="Customer Support Specialist",87 model=OpenAIResponses(id="gpt-5-mini"),88 add_name_to_context=True,89 additional_input=support_examples, # few-shot learning examples90 instructions=[91 "You are an expert customer support specialist.",92 "Always be empathetic, professional, and solution-oriented.",93 "Provide clear, actionable steps to resolve customer issues.",94 "Follow the established patterns for consistent, high-quality support.",95 ],96 markdown=True,97 )9899 agent.print_response("I want to enable two-factor authentication for my account.")Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/03_context_management45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python few_shot_learning.py