Few Shot Learning

This example demonstrates how to use additional_input with an Agent.

1"""
2Few Shot Learning
3=============================
4
5This example demonstrates how to use additional_input with an Agent.
6"""
7
8from kern.agent import Agent
9from kern.models.message import Message
10from kern.models.openai import OpenAIResponses
11
12# ---------------------------------------------------------------------------
13# Create Agent
14# ---------------------------------------------------------------------------
15# Few-shot examples showing good support patterns
16support_examples = [
17 # Example 1: Simple issue resolution
18 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.
23
24 **Steps to Reset Your Password:**
25 . Go to the login page and click "Forgot Password"
26 . Enter your email address
27 . Check your email for the reset link
28 . Follow the link to create a new password
29 . Use a strong password with uppercase, lowercase, numbers, and symbols
30
31 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 pattern
35 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.
43
44 **Immediate Action Plan:**
45 1. I'll investigate your account to confirm the duplicate charge
46 2. Process a full refund for the duplicate transaction
47 3. Ensure this doesn't happen again by checking your payment method settings
48 4. Provide you with a confirmation number once the refund is processed
49
50 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.
51
52 Is there anything else I can help you with regarding this issue?
53 """.strip(),
54 ),
55 # Example 3: Technical support pattern
56 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.
61
62 **Troubleshooting Steps:**
63 1. **Check App Version:** Ensure you have the latest version from the app store
64 2. **Restart the App:** Close completely and reopen the application
65 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 connection
68
69 **If the issue persists:**
70 - Try uploading one photo at a time instead of multiple
71 - Clear the app cache in your device settings
72 - Restart your device
73
74 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]
78
79
80# ---------------------------------------------------------------------------
81# Run Agent
82# ---------------------------------------------------------------------------
83if __name__ == "__main__":
84 # Create agent with few-shot learning
85 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 examples
90 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 )
98
99 agent.print_response("I want to enable two-factor authentication for my account.")

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/03_context_management
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python few_shot_learning.py