Author: admin

  • Agentic Architecture Pattern: Experimentation

    
        """
    Pattern 219: Experimentation Pattern
    
    Runs controlled experiments to optimize behavior
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class ExperimentationPattern:
        """
        Experimentation Pattern - Runs controlled experiments to optimize behavior
        
        Provides experimentation capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Experimentation. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Experimentation pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Experimentation",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Experimentation Pattern Demo ===\n")
        
        pattern = ExperimentationPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the experimentation functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Experimentation

    Pattern 219: 219 Experimentation – Line-by-Line Explanation

    Overview

    Pattern 219: Experimentation Pattern

    Runs controlled experiments to optimize behavior Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 219
    • Pattern Name: 219 Experimentation
    • File Path: patterns\feedback_improvement\219_experimentation.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 219: Experimentation Pattern
    
    Runs controlled experiments to optimize behavior
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 219 Experimentation pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    ExperimentationPattern (Line 45)

    Description: Experimentation Pattern – Runs controlled experiments to optimize behavior

    Provides experimentation capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Ab Testing

    
        """
    Pattern 218: A/B Testing Pattern
    
    Tests multiple approaches and selects best performer
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class A/btestingPattern:
        """
        A/B Testing Pattern - Tests multiple approaches and selects best performer
        
        Provides a/b_testing capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for A/B Testing. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using A/B Testing pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "A/B Testing",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== A/B Testing Pattern Demo ===\n")
        
        pattern = A/btestingPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the a/b_testing functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Ab Testing

    Pattern 218: 218 Ab Testing – Line-by-Line Explanation

    Overview

    Pattern 218: A/B Testing Pattern

    Tests multiple approaches and selects best performer Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 218
    • Pattern Name: 218 Ab Testing
    • File Path: patterns\feedback_improvement\218_ab_testing.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 218: A/B Testing Pattern
    
    Tests multiple approaches and selects best performer
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 218 Ab Testing pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Usage example not available
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Continuous Improvement

    
        """
    Pattern 217: Continuous Improvement Pattern
    
    Iterative improvement based on feedback loops
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class ContinuousimprovementPattern:
        """
        Continuous Improvement Pattern - Iterative improvement based on feedback loops
        
        Provides continuous_improvement capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Continuous Improvement. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Continuous Improvement pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Continuous Improvement",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Continuous Improvement Pattern Demo ===\n")
        
        pattern = ContinuousimprovementPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the continuous_improvement functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Continuous Improvement

    Pattern 217: 217 Continuous Improvement – Line-by-Line Explanation

    Overview

    Pattern 217: Continuous Improvement Pattern

    Iterative improvement based on feedback loops Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 217
    • Pattern Name: 217 Continuous Improvement
    • File Path: patterns\feedback_improvement\217_continuous_improvement.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 217: Continuous Improvement Pattern
    
    Iterative improvement based on feedback loops
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 217 Continuous Improvement pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    ContinuousimprovementPattern (Line 45)

    Description: Continuous Improvement Pattern – Iterative improvement based on feedback loops

    Provides continuous_improvement capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Quality Assurance

    
        """
    Pattern 216: Quality Assurance Pattern
    
    Ensures quality standards are met
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class QualityassurancePattern:
        """
        Quality Assurance Pattern - Ensures quality standards are met
        
        Provides quality_assurance capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Quality Assurance. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Quality Assurance pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Quality Assurance",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Quality Assurance Pattern Demo ===\n")
        
        pattern = QualityassurancePattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the quality_assurance functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Quality Assurance

    Pattern 216: 216 Quality Assurance – Line-by-Line Explanation

    Overview

    Pattern 216: Quality Assurance Pattern

    Ensures quality standards are met Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 216
    • Pattern Name: 216 Quality Assurance
    • File Path: patterns\feedback_improvement\216_quality_assurance.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 216: Quality Assurance Pattern
    
    Ensures quality standards are met
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 216 Quality Assurance pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    QualityassurancePattern (Line 45)

    Description: Quality Assurance Pattern – Ensures quality standards are met

    Provides quality_assurance capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Validator

    
        """
    Pattern 215: Validator Pattern
    
    Validates outputs against requirements and constraints
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class ValidatorPattern:
        """
        Validator Pattern - Validates outputs against requirements and constraints
        
        Provides validator capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Validator. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Validator pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Validator",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Validator Pattern Demo ===\n")
        
        pattern = ValidatorPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the validator functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Validator

    Pattern 215: 215 Validator – Line-by-Line Explanation

    Overview

    Pattern 215: Validator Pattern

    Validates outputs against requirements and constraints Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 215
    • Pattern Name: 215 Validator
    • File Path: patterns\feedback_improvement\215_validator.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 215: Validator Pattern
    
    Validates outputs against requirements and constraints
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 215 Validator pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    ValidatorPattern (Line 45)

    Description: Validator Pattern – Validates outputs against requirements and constraints

    Provides validator capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Critic Agent

    
        """
    Pattern 214: Critic Agent Pattern
    
    Specialized agent that provides constructive criticism
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class CriticagentPattern:
        """
        Critic Agent Pattern - Specialized agent that provides constructive criticism
        
        Provides critic_agent capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Critic Agent. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Critic Agent pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Critic Agent",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Critic Agent Pattern Demo ===\n")
        
        pattern = CriticagentPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the critic_agent functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Critic Agent

    Pattern 214: 214 Critic Agent – Line-by-Line Explanation

    Overview

    Pattern 214: Critic Agent Pattern

    Specialized agent that provides constructive criticism Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 214
    • Pattern Name: 214 Critic Agent
    • File Path: patterns\feedback_improvement\214_critic_agent.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 214: Critic Agent Pattern
    
    Specialized agent that provides constructive criticism
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 214 Critic Agent pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    CriticagentPattern (Line 45)

    Description: Critic Agent Pattern – Specialized agent that provides constructive criticism

    Provides critic_agent capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Peer Review

    
        """
    Pattern 213: Peer Review Pattern
    
    Agents review each other's work collaboratively
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class PeerreviewPattern:
        """
        Peer Review Pattern - Agents review each other's work collaboratively
        
        Provides peer_review capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Peer Review. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Peer Review pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Peer Review",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Peer Review Pattern Demo ===\n")
        
        pattern = PeerreviewPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the peer_review functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Peer Review

    Pattern 213: 213 Peer Review – Line-by-Line Explanation

    Overview

    Pattern 213: Peer Review Pattern

    Agents review each other’s work collaboratively Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 213
    • Pattern Name: 213 Peer Review
    • File Path: patterns\feedback_improvement\213_peer_review.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 213: Peer Review Pattern
    
    Agents review each other's work collaboratively
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 213 Peer Review pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    PeerreviewPattern (Line 45)

    Description: Peer Review Pattern – Agents review each other’s work collaboratively

    Provides peer_review capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Self Evaluation

    
        """
    Pattern 212: Self-Evaluation Pattern
    
    Agent evaluates its own performance and outputs
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        CONSTRUCTIVE = "constructive"
        NEUTRAL = "neutral"
    
    
    @dataclass
    class FeedbackItem:
        """Represents a feedback item."""
        source: str
        content: str
        feedback_type: FeedbackType
        score: float = 0.0  # 0.0 to 1.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class EvaluationResult:
        """Result of evaluation."""
        score: float
        feedback: List[FeedbackItem]
        improvements: List[str]
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class SelfEvaluationPattern:
        """
        Self-Evaluation Pattern - Agent evaluates its own performance and outputs
        
        Provides self_evaluation capabilities for continuous improvement
        and quality enhancement in agentic systems.
        """
        
        def __init__(self, model_name: str = "gpt-4"):
            self.llm = ChatOpenAI(model=model_name, temperature=0.3)
            self.history: List[EvaluationResult] = []
            self.feedback_log: List[FeedbackItem] = []
        
        def evaluate(self, output: str, criteria: List[str]) -> EvaluationResult:
            """Evaluate output against criteria."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", """You are an expert evaluator for Self-Evaluation. 
                Evaluate the output against the criteria and provide:
                1. Overall score (0.0 to 1.0)
                2. Specific feedback points
                3. Improvement suggestions"""),
                ("human", """Output to evaluate:
    {output}
    
    Criteria:
    {criteria}
    
    Provide evaluation.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "output": output,
                "criteria": "\n".join(f"- {c}" for c in criteria)
            })
            
            content = str(response.content)
            
            # Parse response
            score = 0.75  # Default score
            if "score:" in content.lower():
                try:
                    score_text = content.lower().split("score:")[1].split("\n")[0].strip()
                    score = float(score_text.split()[0].replace("/1.0", "").replace(":", ""))
                except:
                    pass
            
            feedback_items = [
                FeedbackItem(
                    source=self.__class__.__name__,
                    content=content,
                    feedback_type=FeedbackType.CONSTRUCTIVE,
                    score=score
                )
            ]
            
            improvements = [
                line.strip("- ").strip() 
                for line in content.split("\n") 
                if line.strip().startswith("-") or "improve" in line.lower()
            ][:3]
            
            result = EvaluationResult(
                score=score,
                feedback=feedback_items,
                improvements=improvements
            )
            
            self.history.append(result)
            self.feedback_log.extend(feedback_items)
            
            return result
        
        def provide_feedback(self, output: str) -> Dict[str, Any]:
            """Provide feedback on output."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are providing constructive feedback using Self-Evaluation pattern."),
                ("human", "{output}")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": output})
            
            feedback = FeedbackItem(
                source="feedback_provider",
                content=str(response.content),
                feedback_type=FeedbackType.CONSTRUCTIVE,
                score=0.8
            )
            
            self.feedback_log.append(feedback)
            
            return {
                "feedback": feedback.content,
                "score": feedback.score,
                "type": feedback.feedback_type.value
            }
        
        def run(self, output: str, criteria: Optional[List[str]] = None) -> Dict[str, Any]:
            """Main execution method."""
            if criteria is None:
                criteria = ["clarity", "accuracy", "completeness"]
            
            result = self.evaluate(output, criteria)
            
            return {
                "pattern": "Self-Evaluation",
                "score": result.score,
                "feedback_count": len(result.feedback),
                "improvements": result.improvements,
                "total_evaluations": len(self.history)
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Self-Evaluation Pattern Demo ===\n")
        
        pattern = SelfEvaluationPattern()
        
        # Example evaluation
        test_output = "This is a sample output that demonstrates the self_evaluation functionality."
        result = pattern.run(test_output, ["clarity", "usefulness", "accuracy"])
        
        print(f"Pattern: {result['pattern']}")
        print(f"Score: {result['score']:.2f}")
        print(f"Improvements suggested: {len(result['improvements'])}")
        if result['improvements']:
            print("\nSuggestions:")
            for i, imp in enumerate(result['improvements'][:3], 1):
                print(f"  {i}. {imp}")
        
        print(f"\nTotal evaluations: {result['total_evaluations']}")
        print(f"Total feedback items: {len(pattern.feedback_log)}")
    
        

    Agentic Architecture Pattern: Self Evaluation

    Pattern 212: 212 Self Evaluation – Line-by-Line Explanation

    Overview

    Pattern 212: Self-Evaluation Pattern

    Agent evaluates its own performance and outputs Implements feedback and improvement mechanisms using LangChain/LangGraph.


    Code Structure Analysis

    File Information

    • Pattern Number: 212
    • Pattern Name: 212 Self Evaluation
    • File Path: patterns\feedback_improvement\212_self_evaluation.py
    • Total Lines: 179

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 212: Self-Evaluation Pattern
    
    Agent evaluates its own performance and outputs
    Implements feedback and improvement mechanisms using LangChain/LangGraph.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 212 Self Evaluation pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration – from langgraph.graph import StateGraph, END: LangGraph for workflow orchestration – from typing_extensions import TypedDict: Type hints for better code documentation


    Enumerations

    FeedbackType (Line 18)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        CONSTRUCTIVE
        NEUTRAL
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – CONSTRUCTIVE: Represents constructive – NEUTRAL: Represents neutral


    Classes

    FeedbackType (Line 18)

    Description: Types of feedback.

    FeedbackItem (Line 27)

    Description: Represents a feedback item.

    EvaluationResult (Line 37)

    Description: Result of evaluation.

    SelfEvaluationPattern (Line 45)

    Description: Self-Evaluation Pattern – Agent evaluates its own performance and outputs

    Provides self_evaluation capabilities for continuous improvement and quality enhancement in agentic systems.

    Methods:

    __init__ (Line 53)
    def __init__(self, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – model_name: LLM model to use (e.g., “gpt-4”)

    evaluate (Line 58)
    def evaluate(self, output, criteria):
        """Evaluate output against criteria."""
    

    Purpose: Evaluate output against criteria.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria

    provide_feedback (Line 118)
    def provide_feedback(self, output):
        """Provide feedback on output."""
    

    Purpose: Provide feedback on output.

    Parameters: – self: Instance reference – output: Parameter for output

    run (Line 143)
    def run(self, output, criteria):
        """Main execution method."""
    

    Purpose: Main execution method.

    Parameters: – self: Instance reference – output: Parameter for output – criteria: Parameter for criteria


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Feedback Loop

    
        """
    Pattern 211: Feedback Loop Pattern
    
    Implements continuous improvement through iterative feedback cycles.
    Features feedback collection, analysis, and action application.
    """
    
    from typing import Dict, Any, List, Optional, Callable
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    
    
    class FeedbackType(Enum):
        """Types of feedback."""
        POSITIVE = "positive"
        NEGATIVE = "negative"
        NEUTRAL = "neutral"
        SUGGESTION = "suggestion"
    
    
    @dataclass
    class Feedback:
        """Represents feedback from evaluation."""
        feedback_id: str
        content: str
        feedback_type: FeedbackType
        score: float  # -1.0 to 1.0
        source: str
        iteration: int
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    @dataclass
    class Iteration:
        """Represents one iteration in the feedback loop."""
        iteration_number: int
        input_data: Any
        output: Any
        feedbacks: List[Feedback] = field(default_factory=list)
        improvements_applied: List[str] = field(default_factory=list)
        quality_score: float = 0.0
        timestamp: datetime = field(default_factory=datetime.now)
    
    
    class FeedbackCollector:
        """Collects and analyzes feedback."""
        
        def __init__(self, llm: ChatOpenAI):
            self.llm = llm
            self.all_feedback: List[Feedback] = []
        
        def collect_feedback(self, output: Any, iteration: int) -> List[Feedback]:
            """Collect feedback on output using LLM."""
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are a critic providing constructive feedback. "
                          "Analyze the output and provide specific, actionable feedback."),
                ("human", """Output to evaluate:
    {output}
    
    Provide feedback in the following format:
    1. List 2-3 specific strengths
    2. List 2-3 specific areas for improvement  
    3. Overall quality score (0.0 to 1.0)
    
    Be specific and actionable.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"output": str(output)})
            
            # Parse feedback (simplified)
            feedback_items = []
            
            # Positive feedback
            feedback_items.append(Feedback(
                feedback_id=f"fb_{iteration}_pos",
                content=f"Positive aspects from iteration {iteration}",
                feedback_type=FeedbackType.POSITIVE,
                score=0.7,
                source="llm_critic",
                iteration=iteration
            ))
            
            # Improvement suggestions
            feedback_items.append(Feedback(
                feedback_id=f"fb_{iteration}_improve",
                content=response.content,
                feedback_type=FeedbackType.SUGGESTION,
                score=0.0,
                source="llm_critic",
                iteration=iteration
            ))
            
            self.all_feedback.extend(feedback_items)
            return feedback_items
    
    
    class FeedbackLoopAgent:
        """Agent that improves through feedback loops."""
        
        def __init__(self, name: str, model_name: str = "gpt-4"):
            self.name = name
            self.llm = ChatOpenAI(model=model_name, temperature=0.7)
            self.feedback_collector = FeedbackCollector(self.llm)
            self.iterations: List[Iteration] = []
            self.current_best_output: Optional[Any] = None
            self.best_score: float = 0.0
        
        def execute_with_feedback(self, task: str, max_iterations: int = 3) -> Dict[str, Any]:
            """Execute task with iterative feedback and improvement."""
            print(f"\n   {self.name}: Starting feedback loop for task")
            print(f"   Max iterations: {max_iterations}")
            
            current_input = task
            
            for iteration_num in range(1, max_iterations + 1):
                print(f"\n   === Iteration {iteration_num}/{max_iterations} ===")
                
                # Generate output
                output = self._generate_output(current_input, iteration_num)
                print(f"   Output generated: {str(output)[:80]}...")
                
                # Collect feedback
                feedbacks = self.feedback_collector.collect_feedback(output, iteration_num)
                print(f"   Collected {len(feedbacks)} feedback items")
                
                # Calculate quality score
                quality_score = self._calculate_quality(feedbacks)
                print(f"   Quality score: {quality_score:.3f}")
                
                # Record iteration
                improvements = self._identify_improvements(feedbacks)
                iteration = Iteration(
                    iteration_number=iteration_num,
                    input_data=current_input,
                    output=output,
                    feedbacks=feedbacks,
                    improvements_applied=improvements,
                    quality_score=quality_score
                )
                self.iterations.append(iteration)
                
                # Update best output
                if quality_score > self.best_score:
                    self.best_score = quality_score
                    self.current_best_output = output
                    print(f"   ✓ New best output!")
                
                # Check convergence
                if quality_score >= 0.9:
                    print(f"   ✓ Quality threshold reached!")
                    break
                
                # Apply improvements for next iteration
                if iteration_num  str:
            """Generate output for current iteration."""
            context = ""
            if iteration > 1:
                prev_feedback = self.iterations[-1].feedbacks
                context = f"\nPrevious feedback: {prev_feedback[0].content[:200] if prev_feedback else 'None'}"
            
            prompt = ChatPromptTemplate.from_messages([
                ("system", f"You are {self.name}. Generate high-quality output. "
                          f"This is iteration {iteration}. Apply previous feedback if available."),
                ("human", "Task: {task}{context}\n\nGenerate output:")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({"task": str(input_data), "context": context})
            return response.content
        
        def _calculate_quality(self, feedbacks: List[Feedback]) -> float:
            """Calculate overall quality score from feedback."""
            if not feedbacks:
                return 0.5
            
            # Simple average of feedback scores (normalized to 0-1)
            positive_count = sum(1 for f in feedbacks if f.feedback_type == FeedbackType.POSITIVE)
            total = len(feedbacks)
            return min(1.0, 0.3 + (positive_count / total) * 0.7)
        
        def _identify_improvements(self, feedbacks: List[Feedback]) -> List[str]:
            """Identify improvements from feedback."""
            improvements = []
            for feedback in feedbacks:
                if feedback.feedback_type in [FeedbackType.NEGATIVE, FeedbackType.SUGGESTION]:
                    improvements.append(f"Address: {feedback.content[:50]}...")
            return improvements
        
        def _apply_improvements(self, original_task: str, feedbacks: List[Feedback], 
                               previous_output: Any) -> str:
            """Apply improvements for next iteration."""
            suggestions = [f.content for f in feedbacks 
                          if f.feedback_type == FeedbackType.SUGGESTION]
            
            improved_task = f"{original_task}\n\nPrevious attempt had these issues:\n"
            improved_task += "\n".join(suggestions[:2])  # Top 2 suggestions
            
            return improved_task
    
    
    class FeedbackLoopPattern:
        """
        Feedback Loop Pattern - Continuous improvement through feedback.
        
        Implements iterative refinement where agents improve their outputs
        based on collected feedback.
        """
        
        def __init__(self):
            self.agents: Dict[str, FeedbackLoopAgent] = {}
        
        def register_agent(self, agent: FeedbackLoopAgent):
            """Register feedback loop agent."""
            self.agents[agent.name] = agent
        
        def run(self, tasks: List[Dict[str, Any]]) -> Dict[str, Any]:
            """Execute feedback loop on tasks."""
            print("=== Feedback Loop Pattern Demo ===\n")
            
            results = []
            for task_info in tasks:
                print(f"\nTask: {task_info.get('description', 'Unknown')}")
                
                agent_name = task_info.get("agent")
                if agent_name not in self.agents:
                    continue
                
                agent = self.agents[agent_name]
                task = task_info.get("task")
                max_iterations = task_info.get("max_iterations", 3)
                
                result = agent.execute_with_feedback(task, max_iterations)
                results.append(result)
            
            # Statistics
            total_iterations = sum(r["total_iterations"] for r in results)
            avg_score = sum(r["best_score"] for r in results) / len(results) if results else 0
            
            return {
                "pattern": "FeedbackLoop",
                "total_agents": len(self.agents),
                "tasks_completed": len(results),
                "total_iterations": total_iterations,
                "average_best_score": avg_score
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Feedback Loop Pattern Demo ===\n")
        
        # Create pattern
        pattern = FeedbackLoopPattern()
        
        # Create agent
        print("1. Creating feedback loop agent...")
        agent = FeedbackLoopAgent("WriterAgent")
        pattern.register_agent(agent)
        
        # Define tasks
        print("\n2. Defining tasks for iterative improvement...")
        
        tasks = [
            {
                "description": "Write product description",
                "agent": "WriterAgent",
                "task": "Write a compelling product description for a smart watch",
                "max_iterations": 3
            },
            {
                "description": "Create email subject line",
                "agent": "WriterAgent",
                "task": "Create an engaging email subject line for a summer sale",
                "max_iterations": 2
            }
        ]
        
        # Execute
        print("\n3. Executing with feedback loops...")
        summary = pattern.run(tasks)
        
        print(f"\n4. Summary:")
        print(f"   Tasks Completed: {summary['tasks_completed']}")
        print(f"   Total Iterations: {summary['total_iterations']}")
        print(f"   Average Best Score: {summary['average_best_score']:.3f}")
        
        print("\n5. Iteration History:")
        for i, iteration in enumerate(agent.iterations, 1):
            print(f"   Iteration {iteration.iteration_number}:")
            print(f"     Quality: {iteration.quality_score:.3f}")
            print(f"     Improvements: {len(iteration.improvements_applied)}")
            print(f"     Feedback items: {len(iteration.feedbacks)}")
        
        print(f"\n6. Best Output:")
        if agent.current_best_output:
            print(f"   {str(agent.current_best_output)[:200]}...")
            print(f"   Score: {agent.best_score:.3f}")
        
        print("\n✓ Feedback loop enabled continuous improvement!")
    
        

    Agentic Architecture Pattern: Feedback Loop

    Pattern 211: 211 Feedback Loop – Line-by-Line Explanation

    Overview

    Pattern 211: Feedback Loop Pattern

    Implements continuous improvement through iterative feedback cycles. Features feedback collection, analysis, and action application.


    Code Structure Analysis

    File Information

    • Pattern Number: 211
    • Pattern Name: 211 Feedback Loop
    • File Path: patterns\feedback_improvement\211_feedback_loop.py
    • Total Lines: 314

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 211: Feedback Loop Pattern
    
    Implements continuous improvement through iterative feedback cycles.
    Features feedback collection, analysis, and action application.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 211 Feedback Loop pattern.


    Imports Section

    from typing import Dict, Any, List, Optional, Callable
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional, Callable: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration


    Enumerations

    FeedbackType (Line 16)

    class FeedbackType(Enum):
        POSITIVE
        NEGATIVE
        NEUTRAL
        SUGGESTION
    

    Purpose: Types of feedback.

    Values: – POSITIVE: Represents positive – NEGATIVE: Represents negative – NEUTRAL: Represents neutral – SUGGESTION: Represents suggestion


    Classes

    FeedbackType (Line 16)

    Description: Types of feedback.

    Feedback (Line 25)

    Description: Represents feedback from evaluation.

    Iteration (Line 37)

    Description: Represents one iteration in the feedback loop.

    FeedbackCollector (Line 48)

    Description: Collects and analyzes feedback.

    Methods:

    __init__ (Line 51)
    def __init__(self, llm):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – llm: Parameter for llm

    collect_feedback (Line 55)
    def collect_feedback(self, output, iteration):
        """Collect feedback on output using LLM."""
    

    Purpose: Collect feedback on output using LLM.

    Parameters: – self: Instance reference – output: Parameter for output – iteration: Parameter for iteration

    FeedbackLoopAgent (Line 101)

    Description: Agent that improves through feedback loops.

    Methods:

    __init__ (Line 104)
    def __init__(self, name, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – name: Identifier or label – model_name: LLM model to use (e.g., “gpt-4”)

    execute_with_feedback (Line 112)
    def execute_with_feedback(self, task, max_iterations):
        """Execute task with iterative feedback and improvement."""
    

    Purpose: Execute task with iterative feedback and improvement.

    Parameters: – self: Instance reference – task: Parameter for task – max_iterations: Parameter for max_iterations

    _generate_output (Line 168)
    def _generate_output(self, input_data, iteration):
        """Generate output for current iteration."""
    

    Purpose: Generate output for current iteration.

    Parameters: – self: Instance reference – input_data: Parameter for input_data – iteration: Parameter for iteration

    _calculate_quality (Line 185)
    def _calculate_quality(self, feedbacks):
        """Calculate overall quality score from feedback."""
    

    Purpose: Calculate overall quality score from feedback.

    Parameters: – self: Instance reference – feedbacks: Parameter for feedbacks

    _identify_improvements (Line 195)
    def _identify_improvements(self, feedbacks):
        """Identify improvements from feedback."""
    

    Purpose: Identify improvements from feedback.

    Parameters: – self: Instance reference – feedbacks: Parameter for feedbacks

    _apply_improvements (Line 203)
    def _apply_improvements(self, original_task, feedbacks, previous_output):
        """Apply improvements for next iteration."""
    

    Purpose: Apply improvements for next iteration.

    Parameters: – self: Instance reference – original_task: Parameter for original_task – feedbacks: Parameter for feedbacks – previous_output: Parameter for previous_output

    FeedbackLoopPattern (Line 215)

    Description: Feedback Loop Pattern – Continuous improvement through feedback.

    Implements iterative refinement where agents improve their outputs based on collected feedback.

    Methods:

    __init__ (Line 223)
    def __init__(self):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference

    register_agent (Line 226)
    def register_agent(self, agent):
        """Register feedback loop agent."""
    

    Purpose: Register feedback loop agent.

    Parameters: – self: Instance reference – agent: Parameter for agent

    run (Line 230)
    def run(self, tasks):
        """Execute feedback loop on tasks."""
    

    Purpose: Execute feedback loop on tasks.

    Parameters: – self: Instance reference – tasks: Parameter for tasks


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = FeedbackType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities

  • Agentic Architecture Pattern: Counterfactual Reasoning

    
        """
    Pattern 210: Counterfactual Reasoning Pattern
    
    Reasons about "what if" scenarios and alternative possibilities.
    Explores hypothetical situations contrary to actual facts.
    """
    
    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    
    
    class CounterfactualType(Enum):
        """Types of counterfactual reasoning."""
        WHAT_IF = "what_if"  # What if X had happened?
        IF_ONLY = "if_only"  # If only X had been different
        ALTERNATIVE = "alternative"  # Alternative possible outcomes
        MINIMAL_CHANGE = "minimal_change"  # Minimal change for different outcome
    
    
    @dataclass
    class ActualWorld:
        """The actual state of affairs."""
        facts: List[str]
        outcome: str
        context: Dict[str, Any] = field(default_factory=dict)
    
    
    @dataclass
    class CounterfactualWorld:
        """A hypothetical alternative world."""
        change: str  # What's different from actual
        predicted_facts: List[str]
        predicted_outcome: str
        plausibility: float = 0.5  # 0.0 to 1.0
        reasoning: str = ""
    
    
    @dataclass
    class CounterfactualQuery:
        """Query about counterfactual scenario."""
        actual_world: ActualWorld
        counterfactual_change: str
        question: str
    
    
    class CounterfactualReasoningAgent:
        """Agent that performs counterfactual reasoning."""
        
        def __init__(self, name: str, model_name: str = "gpt-4"):
            self.name = name
            self.llm = ChatOpenAI(model=model_name, temperature=0.7)
            self.scenarios: List[CounterfactualWorld] = []
        
        def generate_counterfactual(self, actual: ActualWorld, 
                                   change: str) -> CounterfactualWorld:
            """Generate counterfactual scenario with change."""
            print(f"\n  {self.name}: Generating counterfactual: '{change}'...")
            
            actual_facts = "\n".join([f"- {fact}" for fact in actual.facts])
            
            prompt = ChatPromptTemplate.from_messages([
                ("system", "You are a counterfactual reasoning expert. Imagine alternative "
                          "scenarios and predict consequences."),
                ("human", """Actual World:
    Facts: 
    {facts}
    Outcome: {outcome}
    
    Counterfactual Change: {change}
    
    If this change had occurred, what would be different?
    Provide:
    1. New facts (what changes)
    2. Alternative outcome
    3. Plausibility (0.0-1.0)
    4. Reasoning chain
    
    Format:
    FACTS: 
    OUTCOME: 
    PLAUSIBILITY: 
    REASONING: """)
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "facts": actual_facts,
                "outcome": actual.outcome,
                "change": change
            })
            
            # Parse response
            new_facts = []
            new_outcome = ""
            plausibility = 0.5
            reasoning = ""
            
            current_section = None
            for line in response.content.split("\n"):
                line = line.strip()
                if line.startswith("FACTS:"):
                    current_section = "facts"
                    continue
                elif line.startswith("OUTCOME:"):
                    new_outcome = line.replace("OUTCOME:", "").strip()
                    current_section = "outcome"
                    continue
                elif line.startswith("PLAUSIBILITY:"):
                    try:
                        plausibility = float(line.replace("PLAUSIBILITY:", "").strip())
                    except:
                        plausibility = 0.5
                    current_section = "plausibility"
                    continue
                elif line.startswith("REASONING:"):
                    current_section = "reasoning"
                    reasoning = line.replace("REASONING:", "").strip()
                    continue
                
                if current_section == "facts" and line.startswith("-"):
                    new_facts.append(line.lstrip("- "))
                elif current_section == "reasoning" and line:
                    reasoning += " " + line
            
            counterfactual = CounterfactualWorld(
                change=change,
                predicted_facts=new_facts if new_facts else ["Unknown changes"],
                predicted_outcome=new_outcome or "Unknown outcome",
                plausibility=plausibility,
                reasoning=reasoning.strip() or "Hypothetical alternative"
            )
            
            self.scenarios.append(counterfactual)
            
            print(f"  Predicted outcome: {counterfactual.predicted_outcome[:80]}...")
            print(f"  Plausibility: {counterfactual.plausibility}")
            
            return counterfactual
        
        def compare_worlds(self, actual: ActualWorld, 
                          counterfactual: CounterfactualWorld) -> Dict[str, Any]:
            """Compare actual and counterfactual worlds."""
            print(f"\n  {self.name}: Comparing worlds...")
            
            prompt = ChatPromptTemplate.from_messages([
                ("system", "Compare actual and counterfactual scenarios."),
                ("human", """Actual Outcome: {actual_outcome}
    Counterfactual Outcome: {counterfactual_outcome}
    
    What are the key differences? What insights does this comparison reveal?""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "actual_outcome": actual.outcome,
                "counterfactual_outcome": counterfactual.predicted_outcome
            })
            
            comparison = {
                "actual": actual.outcome,
                "counterfactual": counterfactual.predicted_outcome,
                "analysis": response.content,
                "change": counterfactual.change
            }
            
            return comparison
        
        def identify_critical_factors(self, actual: ActualWorld) -> List[str]:
            """Identify factors critical to the actual outcome."""
            print(f"\n  {self.name}: Identifying critical factors...")
            
            facts_text = "\n".join(actual.facts)
            
            prompt = ChatPromptTemplate.from_messages([
                ("system", "Identify which factors were most critical to the outcome."),
                ("human", """Facts:
    {facts}
    
    Outcome: {outcome}
    
    Which factors were most critical? List 2-3 key factors.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "facts": facts_text,
                "outcome": actual.outcome
            })
            
            factors = [line.strip().lstrip("0123456789.-) ")
                      for line in response.content.split("\n")
                      if line.strip()]
            
            print(f"  Identified {len(factors)} critical factors")
            return factors[:3]
        
        def explore_minimal_changes(self, actual: ActualWorld, 
                                   desired_outcome: str) -> CounterfactualWorld:
            """Find minimal change needed for desired outcome."""
            print(f"\n  {self.name}: Finding minimal change for desired outcome...")
            
            facts_text = "\n".join(actual.facts)
            
            prompt = ChatPromptTemplate.from_messages([
                ("system", "Find the minimal change that would lead to desired outcome."),
                ("human", """Current Facts:
    {facts}
    
    Current Outcome: {current_outcome}
    Desired Outcome: {desired_outcome}
    
    What is the SMALLEST change that would lead to the desired outcome?
    Explain the change and why it would work.""")
            ])
            
            chain = prompt | self.llm
            response = chain.invoke({
                "facts": facts_text,
                "current_outcome": actual.outcome,
                "desired_outcome": desired_outcome
            })
            
            minimal_change = CounterfactualWorld(
                change=response.content.split("\n")[0],
                predicted_facts=[],
                predicted_outcome=desired_outcome,
                plausibility=0.7,
                reasoning=response.content
            )
            
            print(f"  Minimal change: {minimal_change.change[:80]}...")
            
            return minimal_change
    
    
    class CounterfactualReasoningPattern:
        """
        Counterfactual Reasoning Pattern - Explore "what if" scenarios.
        
        Generates alternative scenarios, compares outcomes, identifies
        critical factors, and explores minimal changes.
        """
        
        def __init__(self):
            self.agents: Dict[str, CounterfactualReasoningAgent] = {}
        
        def register_agent(self, agent: CounterfactualReasoningAgent):
            """Register counterfactual reasoning agent."""
            self.agents[agent.name] = agent
        
        def run(self, queries: List[CounterfactualQuery]) -> Dict[str, Any]:
            """Execute counterfactual reasoning queries."""
            print("=== Counterfactual Reasoning Pattern ===\n")
            
            comparisons = []
            for agent in self.agents.values():
                for query in queries:
                    # Generate counterfactual
                    counterfactual = agent.generate_counterfactual(
                        query.actual_world,
                        query.counterfactual_change
                    )
                    
                    # Compare worlds
                    comparison = agent.compare_worlds(
                        query.actual_world,
                        counterfactual
                    )
                    comparisons.append(comparison)
            
            return {
                "pattern": "CounterfactualReasoning",
                "agents": len(self.agents),
                "queries_processed": len(queries),
                "scenarios_explored": sum(len(a.scenarios) for a in self.agents.values()),
                "comparisons": comparisons
            }
    
    
    # Example usage
    if __name__ == "__main__":
        print("=== Counterfactual Reasoning Pattern Demo ===\n")
        
        # Create pattern and agent
        pattern = CounterfactualReasoningPattern()
        agent = CounterfactualReasoningAgent("WhatIfAnalyst")
        pattern.register_agent(agent)
        
        # Actual world scenarios
        print("1. Defining actual world scenarios...\n")
        
        # Scenario 1: Historical event
        actual1 = ActualWorld(
            facts=[
                "Columbus sailed west in 1492",
                "He had support from Spanish monarchy",
                "Navigation technology was available"
            ],
            outcome="European discovery of the Americas in 1492"
        )
        
        # Scenario 2: Business decision
        actual2 = ActualWorld(
            facts=[
                "Company invested heavily in mobile apps",
                "Market shifted to mobile-first",
                "Competitors were slow to adapt"
            ],
            outcome="Company became market leader in mobile space"
        )
        
        # Counterfactual queries
        print("2. Exploring counterfactual scenarios...\n")
        queries = [
            CounterfactualQuery(
                actual_world=actual1,
                counterfactual_change="What if the Spanish monarchy had refused funding?",
                question="Would the Americas still be discovered in 1492?"
            ),
            CounterfactualQuery(
                actual_world=actual2,
                counterfactual_change="What if the company had focused on desktop instead?",
                question="Would they still be market leader?"
            )
        ]
        
        # Run counterfactual reasoning
        summary = pattern.run(queries)
        
        print(f"\n=== Summary ===")
        print(f"Queries processed: {summary['queries_processed']}")
        print(f"Scenarios explored: {summary['scenarios_explored']}")
        
        print(f"\n=== Counterfactual Analysis ===")
        for i, comp in enumerate(summary['comparisons'], 1):
            print(f"\nScenario {i}:")
            print(f"  Change: {comp['change'][:60]}...")
            print(f"  Actual: {comp['actual'][:60]}...")
            print(f"  Counterfactual: {comp['counterfactual'][:60]}...")
        
        # Additional: Identify critical factors
        print(f"\n=== Critical Factors ===")
        critical = agent.identify_critical_factors(actual1)
        for factor in critical:
            print(f"  - {factor}")
        
        print("\n✓ Counterfactual reasoning: exploring alternative realities!")
    
        

    Agentic Architecture Pattern: Counterfactual Reasoning

    Pattern 210: 210 Counterfactual Reasoning Pattern – Line-by-Line Explanation

    Overview

    Pattern 210: Counterfactual Reasoning Pattern

    Reasons about “what if” scenarios and alternative possibilities. Explores hypothetical situations contrary to actual facts.


    Code Structure Analysis

    File Information

    • Pattern Number: 210
    • Pattern Name: 210 Counterfactual Reasoning Pattern
    • File Path: patterns\advanced_reasoning\210_counterfactual_reasoning_pattern.py
    • Total Lines: 352

    Detailed Code Explanation

    Module Docstring

    """
    Pattern 210: Counterfactual Reasoning Pattern
    
    Reasons about "what if" scenarios and alternative possibilities.
    Explores hypothetical situations contrary to actual facts.
    """
    

    Explanation: This module docstring describes the purpose and key features of the 210 Counterfactual Reasoning Pattern pattern.


    Imports Section

    from typing import Dict, Any, List, Optional
    from dataclasses import dataclass, field
    from datetime import datetime
    from enum import Enum
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI  # First 20 imports shown
    

    Explanation: The module imports necessary dependencies: – from typing import Dict, Any, List, Optional: Type hints for better code documentation – from dataclasses import dataclass, field: Data class decorators for clean data structures – from datetime import datetime: Date and time utilities – from enum import Enum: Enumeration support for fixed sets of values – from langchain_core.prompts import ChatPromptTemplate: LangChain framework integration – from langchain_openai import ChatOpenAI: LangChain framework integration


    Enumerations

    CounterfactualType (Line 16)

    class CounterfactualType(Enum):
        WHAT_IF
        IF_ONLY
        ALTERNATIVE
        MINIMAL_CHANGE
    

    Purpose: Types of counterfactual reasoning.

    Values: – WHAT_IF: Represents what if – IF_ONLY: Represents if only – ALTERNATIVE: Represents alternative – MINIMAL_CHANGE: Represents minimal change


    Classes

    CounterfactualType (Line 16)

    Description: Types of counterfactual reasoning.

    ActualWorld (Line 25)

    Description: The actual state of affairs.

    CounterfactualWorld (Line 33)

    Description: A hypothetical alternative world.

    CounterfactualQuery (Line 43)

    Description: Query about counterfactual scenario.

    CounterfactualReasoningAgent (Line 50)

    Description: Agent that performs counterfactual reasoning.

    Methods:

    __init__ (Line 53)
    def __init__(self, name, model_name):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference – name: Identifier or label – model_name: LLM model to use (e.g., “gpt-4”)

    generate_counterfactual (Line 58)
    def generate_counterfactual(self, actual, change):
        """Generate counterfactual scenario with change."""
    

    Purpose: Generate counterfactual scenario with change.

    Parameters: – self: Instance reference – actual: Parameter for actual – change: Parameter for change

    compare_worlds (Line 144)
    def compare_worlds(self, actual, counterfactual):
        """Compare actual and counterfactual worlds."""
    

    Purpose: Compare actual and counterfactual worlds.

    Parameters: – self: Instance reference – actual: Parameter for actual – counterfactual: Parameter for counterfactual

    identify_critical_factors (Line 172)
    def identify_critical_factors(self, actual):
        """Identify factors critical to the actual outcome."""
    

    Purpose: Identify factors critical to the actual outcome.

    Parameters: – self: Instance reference – actual: Parameter for actual

    explore_minimal_changes (Line 201)
    def explore_minimal_changes(self, actual, desired_outcome):
        """Find minimal change needed for desired outcome."""
    

    Purpose: Find minimal change needed for desired outcome.

    Parameters: – self: Instance reference – actual: Parameter for actual – desired_outcome: Parameter for desired_outcome

    CounterfactualReasoningPattern (Line 240)

    Description: Counterfactual Reasoning Pattern – Explore “what if” scenarios.

    Generates alternative scenarios, compares outcomes, identifies critical factors, and explores minimal changes.

    Methods:

    __init__ (Line 248)
    def __init__(self):
        """No method docstring."""
    

    Purpose: No method docstring.

    Parameters: – self: Instance reference

    register_agent (Line 251)
    def register_agent(self, agent):
        """Register counterfactual reasoning agent."""
    

    Purpose: Register counterfactual reasoning agent.

    Parameters: – self: Instance reference – agent: Parameter for agent

    run (Line 255)
    def run(self, queries):
        """Execute counterfactual reasoning queries."""
    

    Purpose: Execute counterfactual reasoning queries.

    Parameters: – self: Instance reference – queries: Parameter for queries


    Design Patterns Used

    • Strategy Pattern: Different execution strategies for various scenarios
    • State Pattern: Managing different states in the workflow
    • Chain of Responsibility: Sequential processing through workflow nodes
    • Observer Pattern: Tracking and monitoring pattern execution
    • Factory Pattern: Creating instances with proper configuration

    Usage Example

    # Initialize the pattern
    pattern = CounterfactualType(name="example", model_name="gpt-4")
    
    # Configure and execute
    result = pattern.execute(data={"key": "value"})
    
    # Check results
    print(result)
    

    Key Concepts

    • LLM Integration: Uses LangChain/OpenAI for intelligent decision-making
    • Workflow Orchestration: LangGraph manages execution flow through nodes
    • State Management: TypedDict defines workflow state structure
    • Error Handling: Comprehensive error handling and fallback mechanisms
    • Extensibility: Designed for easy customization and extension

    Extension Points

    This pattern can be extended by: 1. Adding custom workflow nodes 2. Implementing additional validation logic 3. Integrating with external services 4. Customizing LLM prompts and responses 5. Adding monitoring and logging capabilities