"""
Pattern 115: Transfer Learning Agent
Description:
Transfer Learning enables agents to leverage knowledge learned from one
task or domain to improve performance on related tasks. This pattern
identifies transferable knowledge, adapts it to new contexts, and combines
it with task-specific learning for faster adaptation and better performance.
Essential for agents that need to work across multiple related domains or
tasks, transfer learning reduces the data and time needed to achieve good
performance in new areas.
Key Components:
1. Source Knowledge Extractor: Identifies transferable knowledge
2. Domain Adapter: Adapts knowledge to new domain
3. Fine-tuner: Specializes for target task
4. Transfer Strategy Selector: Chooses transfer approach
5. Performance Evaluator: Assesses transfer effectiveness
Transfer Strategies:
- Feature Transfer: Use learned representations
- Parameter Transfer: Transfer model parameters
- Instance Transfer: Leverage source examples
- Relational Transfer: Transfer relationships
- Meta-transfer: Transfer learning strategies
Use Cases:
- Multi-domain chatbots
- Cross-lingual applications
- Domain adaptation (e.g., medical to legal)
- Few-shot learning in new domains
- Skill transfer in robotics
LangChain Implementation:
Uses prompt engineering, few-shot examples, and domain adaptation
techniques to transfer knowledge between tasks and domains.
"""
import os
from typing import List, Dict, Any, Optional
from dotenv import load_dotenv
from datetime import datetime
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
from langchain_core.output_parsers import StrOutputParser
load_dotenv()
class DomainKnowledge:
"""Represents knowledge from a source domain"""
def __init__(self, domain_name: str):
self.domain_name = domain_name
self.examples: List[Dict[str, str]] = []
self.patterns: List[str] = []
self.concepts: Dict[str, str] = {}
def add_example(self, input_text: str, output_text: str, pattern: str = ""):
"""Add an example from this domain"""
self.examples.append({
"input": input_text,
"output": output_text,
"pattern": pattern
})
def add_pattern(self, pattern: str):
"""Add a learned pattern"""
if pattern not in self.patterns:
self.patterns.append(pattern)
def add_concept(self, concept: str, description: str):
"""Add a domain concept"""
self.concepts[concept] = description
class TransferLearningAgent:
"""Agent that transfers knowledge between domains"""
def __init__(self, model_name: str = "gpt-4"):
self.llm = ChatOpenAI(model=model_name, temperature=0.7)
self.source_domains: Dict[str, DomainKnowledge] = {}
self.current_domain: Optional[str] = None
def add_source_domain(self, domain: DomainKnowledge):
"""Add a source domain with learned knowledge"""
self.source_domains[domain.domain_name] = domain
print(f"Added source domain: {domain.domain_name}")
print(f" - {len(domain.examples)} examples")
print(f" - {len(domain.patterns)} patterns")
print(f" - {len(domain.concepts)} concepts")
def identify_relevant_source(self, target_task: str) -> Optional[DomainKnowledge]:
"""Identify most relevant source domain for transfer"""
if not self.source_domains:
return None
# Use LLM to identify most relevant source
domains_desc = "\n".join([
f"- {name}: {len(d.examples)} examples, patterns: {', '.join(d.patterns[:3])}"
for name, d in self.source_domains.items()
])
selection_prompt = f"""Given a target task and available source domains, identify which source domain would provide the most useful transferable knowledge.
Target task: {target_task}
Available source domains:
{domains_desc}
Which source domain is most relevant? Answer with just the domain name."""
response = self.llm.invoke(selection_prompt).content.strip()
# Find matching domain
for domain_name in self.source_domains:
if domain_name.lower() in response.lower():
print(f"\nSelected source domain: {domain_name}")
return self.source_domains[domain_name]
# Default to first domain
return list(self.source_domains.values())[0]
def adapt_examples(self, source_domain: DomainKnowledge,
target_domain: str, n: int = 3) -> List[Dict[str, str]]:
"""Adapt source examples to target domain"""
if not source_domain.examples:
return []
# Take first n examples
source_examples = source_domain.examples[:n]
# Create adaptation prompt
examples_str = "\n\n".join([
f"Input: {ex['input']}\nOutput: {ex['output']}"
for ex in source_examples
])
adaptation_prompt = f"""Adapt these examples from {source_domain.domain_name} domain to {target_domain} domain.
Keep the same pattern and structure, but change the content to fit the target domain.
Source examples ({source_domain.domain_name}):
{examples_str}
Generate {n} adapted examples for {target_domain} domain in the same format."""
response = self.llm.invoke(adaptation_prompt).content
# Parse adapted examples (simplified parsing)
adapted = []
lines = response.split('\n')
current_input = None
for line in lines:
if line.startswith('Input:'):
current_input = line.replace('Input:', '').strip()
elif line.startswith('Output:') and current_input:
output = line.replace('Output:', '').strip()
adapted.append({"input": current_input, "output": output})
current_input = None
return adapted[:n] if adapted else source_examples[:n]
def transfer_and_respond(self, target_task: str, target_domain: str,
query: str) -> Dict[str, Any]:
"""Transfer knowledge and respond to query"""
print(f"\n{'='*60}")
print(f"Target Domain: {target_domain}")
print(f"Target Task: {target_task}")
print(f"Query: {query}")
print(f"{'='*60}")
# Identify relevant source domain
source_domain = self.identify_relevant_source(target_task)
if source_domain:
print(f"\nTransferring knowledge from: {source_domain.domain_name}")
# Adapt examples to target domain
adapted_examples = self.adapt_examples(source_domain, target_domain, n=3)
print(f"Adapted {len(adapted_examples)} examples to target domain")
# Extract transferable patterns
patterns_str = "\n".join([f"- {p}" for p in source_domain.patterns])
# Create transfer-enhanced prompt
if adapted_examples:
example_prompt = ChatPromptTemplate.from_messages([
("human", "{input}"),
("ai", "{output}")
])
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=adapted_examples
)
prompt = ChatPromptTemplate.from_messages([
("system", f"""You are an AI assistant for {target_domain}.
You are leveraging transferred knowledge from {source_domain.domain_name}.
Transferable patterns:
{patterns_str}
Apply these learned patterns to the current domain:"""),
few_shot_prompt,
("human", "{input}")
])
else:
prompt = ChatPromptTemplate.from_messages([
("system", f"""You are an AI assistant for {target_domain}.
You are leveraging transferred knowledge from {source_domain.domain_name}.
Transferable patterns:
{patterns_str}
Apply these learned patterns to the current task."""),
("human", "{input}")
])
chain = prompt | self.llm | StrOutputParser()
response = chain.invoke({"input": query})
else:
# No source domain available, use base prompt
print("\nNo source domain available, using base knowledge")
prompt = ChatPromptTemplate.from_messages([
("system", f"You are an AI assistant for {target_domain}."),
("human", "{input}")
])
chain = prompt | self.llm | StrOutputParser()
response = chain.invoke({"input": query})
print(f"\nResponse:")
print(response)
return {
"target_domain": target_domain,
"target_task": target_task,
"query": query,
"source_domain": source_domain.domain_name if source_domain else None,
"response": response,
"timestamp": datetime.now().isoformat()
}
def compare_with_without_transfer(self, target_task: str, target_domain: str,
query: str) -> Dict[str, Any]:
"""Compare performance with and without transfer learning"""
print(f"\n{'='*70}")
print(f"COMPARISON: With vs Without Transfer Learning")
print(f"{'='*70}")
# Response with transfer
print("\n--- WITH TRANSFER LEARNING ---")
with_transfer = self.transfer_and_respond(target_task, target_domain, query)
# Response without transfer (base model)
print("\n--- WITHOUT TRANSFER LEARNING (Baseline) ---")
base_prompt = ChatPromptTemplate.from_messages([
("system", f"You are an AI assistant for {target_domain}."),
("human", "{input}")
])
base_chain = base_prompt | self.llm | StrOutputParser()
without_transfer = base_chain.invoke({"input": query})
print(f"\nBaseline Response:")
print(without_transfer)
return {
"with_transfer": with_transfer,
"without_transfer": without_transfer
}
def demonstrate_transfer_learning():
"""Demonstrate transfer learning pattern"""
print("\n" + "="*70)
print("TRANSFER LEARNING AGENT DEMONSTRATION")
print("="*70)
agent = TransferLearningAgent()
# Create source domain: Customer Support
print("\n" + "="*70)
print("Setting Up Source Domain Knowledge")
print("="*70)
customer_support = DomainKnowledge("customer_support")
# Add examples
customer_support.add_example(
"How do I reset my password?",
"To reset your password: 1) Click 'Forgot Password' on login page, "
"2) Enter your email, 3) Check your email for reset link, 4) Create new password.",
"step_by_step_instructions"
)
customer_support.add_example(
"Where is my order?",
"To track your order: 1) Log into your account, 2) Go to 'Orders', "
"3) Find your order number, 4) Click 'Track' to see current status.",
"step_by_step_instructions"
)
customer_support.add_example(
"How do I cancel my subscription?",
"To cancel subscription: 1) Go to Account Settings, 2) Select 'Subscriptions', "
"3) Choose subscription to cancel, 4) Confirm cancellation.",
"step_by_step_instructions"
)
# Add patterns
customer_support.add_pattern("Provide step-by-step instructions")
customer_support.add_pattern("Be clear and concise")
customer_support.add_pattern("Offer alternative solutions")
customer_support.add_pattern("Include troubleshooting tips")
# Add concepts
customer_support.add_concept("account_management", "User account operations")
customer_support.add_concept("order_processing", "Order lifecycle and tracking")
agent.add_source_domain(customer_support)
# Example 1: Transfer to Technical Support
print("\n" + "="*70)
print("Example 1: Transfer to Technical Support Domain")
print("="*70)
result1 = agent.transfer_and_respond(
target_task="Help users troubleshoot technical issues",
target_domain="technical_support",
query="How do I fix a blue screen error?"
)
# Example 2: Transfer to Medical Assistant
print("\n" + "="*70)
print("Example 2: Transfer to Medical Assistant Domain")
print("="*70)
result2 = agent.transfer_and_respond(
target_task="Provide medical information and guidance",
target_domain="medical_assistant",
query="How do I check my blood pressure at home?"
)
# Example 3: Compare with and without transfer
print("\n" + "="*70)
print("Example 3: Comparing With/Without Transfer")
print("="*70)
comparison = agent.compare_with_without_transfer(
target_task="Help users with cooking recipes",
target_domain="cooking_assistant",
query="How do I make homemade pasta?"
)
# Summary
print("\n" + "="*70)
print("SUMMARY")
print("="*70)
print("""
Transfer Learning Pattern demonstrated:
Key Features:
1. Source Domain Selection - Identify most relevant source knowledge
2. Knowledge Adaptation - Adapt examples and patterns to new domain
3. Pattern Transfer - Transfer learned patterns across domains
4. Few-Shot Enhancement - Use adapted examples for better performance
5. Comparative Evaluation - Compare with/without transfer
Transfer Mechanisms:
- Example adaptation across domains
- Pattern and structure transfer
- Concept mapping between domains
- Meta-learning of general strategies
- Domain-specific fine-tuning
Applications:
- Multi-domain chatbots (customer → technical support)
- Cross-lingual NLP (English → Spanish)
- Domain adaptation (medical → legal documentation)
- Few-shot learning in new domains
- Skill transfer in robotics
Benefits:
- Faster adaptation to new domains
- Better performance with limited data
- Leverages existing knowledge
- Reduces training time and data needs
- Improves generalization
The transfer learning pattern enables agents to leverage knowledge from
related domains, significantly improving performance on new tasks with
limited domain-specific training.
""")
if __name__ == "__main__":
demonstrate_transfer_learning()
Agentic Design Pattern: Transfer Learning
Pattern 115: Transfer Learning – Line-by-Line Explanation
Overview
This document provides a detailed line-by-line explanation of the Transfer Learning pattern implementation.
Pattern Description
Pattern 115: Transfer Learning Agent
Description: Transfer Learning enables agents to leverage knowledge learned from one task or domain to improve performance on related tasks. This pattern identifies transferable knowledge, adapts it to new contexts, and combines it with task-specific learning for faster adaptation and better performance.
Essential for agents that need to work across multiple related domains or
tasks, transfer learning reduces the data and time needed to achieve good
performance in new areas.
Key Components: 1. Source Knowledge Extractor: Identifies transferable knowledge 2. Domain Adapter: Adapts knowledge to new domain 3. Fine-tuner: Specializes for target task 4. Transfer Strategy Selector: Chooses transfer approach 5. Performance Evaluator: Assesses transfer effectiveness
Transfer Strategies: – Feature Transfer: Use learned representations – Parameter Transfer: Transfer model parameters – Instance Transfer: Leverage source examples – Relational Transfer: Transfer relationships – Meta-transfer: Transfer learning strategies
Use Cases: – Multi-domain chatbots – Cross-lingual applications – Domain adaptation (e.g., medical to legal) – Few-shot learning in new domains – Skill transfer in robotics
LangChain Implementation: Uses prompt engineering, few-shot examples, and domain adaptation techniques to transfer knowledge between tasks and domains.
File Statistics
- Total Lines: 403
- Classes: 2
- Functions: 1
- Constants: 0
Module Documentation
"""
Pattern 115: Transfer Learning Agent
Description:
Transfer Learning enables agents to leverage knowledge learned from one
task or domain to improve performance on related tasks. This pattern
identifies transferable knowledge, adapts it to new contexts, and combines
it with task-specific learning for faster adaptation and better performance.
Essential for agents that need to work across multiple related domains or
tasks, transfer learning reduces the data and time needed to achieve good
performance in new areas.
Key Components:
1. Source Knowledge Extractor: Identifies transferable knowledge
2. Domain Adapter: Adapts knowledge to new domain
3. Fine-tuner: Specializes for target task
4. Transfer Strategy Selector: Chooses transfer approach
5. Performance Evaluator: Assesses transfer effectiveness
Transfer Strategies:
- Feature Transfer: Use learned representations
- Parameter Transfer: Transfer model parameters
- Instance Transfer: Leverage source examples
- Relational Transfer: Transfer relationships
- Meta-transfer: Transfer learning strategies
Use Cases:
- Multi-domain chatbots
- Cross-lingual applications
- Domain adaptation (e.g., medical to legal)
- Few-shot learning in new domains
- Skill transfer in robotics
LangChain Implementation:
Uses prompt engineering, few-shot examples, and domain adaptation
techniques to transfer knowledge between tasks and domains.
"""
Purpose: This module implements the Transfer Learning pattern, providing a structured approach to agent design and implementation.
Import Statements
Standard Library Imports
import datetime.datetime
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import langchain_core.prompts.FewShotChatMessagePromptTemplate
import os
import typing.Any
import typing.Dict
import typing.List
import typing.Optional
Purpose: Core Python functionality:
– datetime.datetime: Date and time handling
– langchain_core.output_parsers.StrOutputParser: Regular expression operations
– langchain_core.prompts.ChatPromptTemplate: Regular expression operations
– langchain_core.prompts.FewShotChatMessagePromptTemplate: Regular expression operations
– os: Environment variable access and system operations
– typing.Any: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.List: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Optional: Type hints for better code documentation (List, Dict, Any, Optional)
LangChain Imports
from langchain_openai.ChatOpenAI
Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows
Other Imports
from dotenv.load_dotenv
Purpose: Additional dependencies for specialized functionality.
Classes
Class: DomainKnowledge
Lines 52-76
Purpose:
Represents knowledge from a source domain
Methods:
__init__(self, domain_name)
Lines 55-59
Purpose: Initialize the class instance with required parameters.
Parameters:
– domain_name: Domain name
add_example(self, input_text, output_text, pattern)
Lines 61-67
Purpose:
Add an example from this domain
Parameters:
– input_text: Input data
– output_text: Output text
– pattern: Pattern
add_pattern(self, pattern)
Lines 69-72
Purpose:
Add a learned pattern
Parameters:
– pattern: Pattern
add_concept(self, concept, description)
Lines 74-76
Purpose:
Add a domain concept
Parameters:
– concept: Concept
– description: Description
Class: TransferLearningAgent
Lines 79-277
Purpose:
Agent that transfers knowledge between domains
Methods:
__init__(self, model_name)
Lines 82-85
Purpose: Initialize the class instance with required parameters.
Parameters:
– model_name: Language model instance
add_source_domain(self, domain)
Lines 87-93
Purpose:
Add a source domain with learned knowledge
Parameters:
– domain: Domain
identify_relevant_source(self, target_task)
Lines 95-124
Purpose:
Identify most relevant source domain for transfer
Parameters:
– target_task: Target task
Returns: <ast.Subscript object at 0x000001A06D7FEF50>
adapt_examples(self, source_domain, target_domain, n)
Lines 126-164
Purpose:
Adapt source examples to target domain
Parameters:
– source_domain: Source domain
– target_domain: Target domain
– n: N
Returns: <ast.Subscript object at 0x000001A06D72EDD0>
transfer_and_respond(self, target_task, target_domain, query)
Lines 166-249
Purpose:
Transfer knowledge and respond to query
Parameters:
– target_task: Target task
– target_domain: Target domain
– query: Input query or question
Returns: <ast.Subscript object at 0x000001A06D79B310>
compare_with_without_transfer(self, target_task, target_domain, query)
Lines 251-277
Purpose:
Compare performance with and without transfer learning
Parameters:
– target_task: Target task
– target_domain: Target domain
– query: Input query or question
Returns: <ast.Subscript object at 0x000001A06D7E43D0>
Functions
demonstrate_transfer_learning()
Lines 280-398
Purpose:
Demonstrate transfer learning pattern
Key Concepts Summary
Pattern Overview
The Transfer Learning pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.
Core Components
- Agent Class: Main implementation of the pattern logic
- Prompt Engineering: Carefully crafted prompts to guide LLM behavior
- Tool Integration: Optional tools for extended functionality
- Memory Management: State handling across interactions
- Output Parsing: Structured response processing
When to Use
✅ Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability
❌ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)
Best Practices Demonstrated
1. Environment Configuration
- ✅ Use
.envfiles for API keys - ✅ Never hardcode credentials
- ✅ Validate environment variables on startup
2. Error Handling
- ✅ Graceful degradation on failures
- ✅ Informative error messages
- ✅ Logging for debugging
3. Prompt Engineering
- ✅ Clear, specific instructions
- ✅ Structured output formats
- ✅ Few-shot examples when beneficial
4. Code Organization
- ✅ Separation of concerns (class-based design)
- ✅ Type hints for clarity
- ✅ Docstrings for documentation
5. Performance Optimization
- ✅ Appropriate temperature settings
- ✅ Token budget management
- ✅ Caching where applicable
LangChain Components Used
- ChatOpenAI: OpenAI model interface
- Chains: Sequential component composition
Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development
Execution Flow
1. Initialize agent with configuration
↓
2. Load environment and dependencies
↓
3. Prepare prompt with input data
↓
4. Invoke LLM through LangChain
↓
5. Parse and validate response
↓
6. Return formatted result
Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation
Performance Considerations
Computational Cost
- LLM Calls: Primary cost factor (based on tokens)
- Latency: Depends on model and input size
- Memory: Minimal for most patterns
Optimization Strategies
- Prompt Optimization: Minimize tokens while maintaining clarity
- Caching: Cache repeated queries/responses
- Model Selection: Use smaller models when appropriate
- Batch Processing: Group similar requests
- Streaming: For long responses, stream tokens
Cost Estimates (Approximate)
- GPT-3.5-turbo: ~$0.002 per request
- GPT-4: ~$0.03 per request
- GPT-4-turbo: ~$0.01 per request
Actual costs vary based on input/output token counts
Common Pitfalls to Avoid
1. Prompt Design Issues
❌ Vague or ambiguous instructions
✅ Clear, specific, testable prompts
2. Error Handling
❌ Ignoring API errors or rate limits
✅ Implement retry logic and graceful degradation
3. Token Management
❌ Exceeding model token limits
✅ Truncate inputs, chunk processing
4. Security
❌ Exposing API keys in code
✅ Use environment variables
5. Testing
❌ Only testing happy paths
✅ Test edge cases, failures, malformed inputs
6. Cost Management
❌ Unmonitored API usage
✅ Implement budgets, alerts, and monitoring
Further Improvements
Production Enhancements
- Monitoring: Add logging, metrics, tracing
- Validation: Input/output schema validation
- Rate Limiting: Implement request throttling
- Caching: Redis or in-memory cache for responses
- Async Support: Non-blocking operations for scalability
Feature Extensions
- Multi-Model Support: Switch between providers
- Fallback Models: Graceful degradation strategy
- Custom Tools: Domain-specific function calling
- Advanced Memory: Vector stores for long-term memory
- Evaluation: Automated testing and benchmarking
Code Quality
- Testing: Unit tests, integration tests
- Documentation: API docs, usage examples
- Type Safety: Stricter type hints
- Configuration: Externalized config files
Related Patterns
- Pattern 114
- Pattern 116
Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…
Summary
The Transfer Learning pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:
✅ Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns
🎯 Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability
📚 Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/
File: 115_transfer_learning.py
Total Lines: 403
Pattern Category: AI Agent Design Pattern #115
Generated from actual implementation code
For educational and reference purposes