File size: 3,678 Bytes
429e7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from abc import ABC, abstractmethod
from typing import List, Dict, Optional

class ExecutionStrategy(ABC):
    @abstractmethod
    def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
        """Build the prompt according to the strategy."""
        pass

    @abstractmethod
    def process_response(self, response: str) -> str:
        """Process the LLM response according to the strategy."""
        pass

class ReactStrategy(ExecutionStrategy):
    def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
        base_prompt = """Approach this task using the following steps:
1) Thought: Analyze what needs to be done
2) Action: Decide on the next action
3) Observation: Observe the result
4) Repeat until task is complete

Follow this format for your response:
Thought: [Your reasoning about the current situation]
Action: [The action you decide to take]
Observation: [What you observe after the action]
... (continue steps as needed)
Final Answer: [Your final response to the task]

Task: {task}"""
        
        if instruction:
            base_prompt += f"\nAdditional Instruction: {instruction}"
            
        return base_prompt.format(task=task)

    def process_response(self, response: str) -> str:
        # Could add additional processing here to extract final answer
        return response

class ChainOfThoughtStrategy(ExecutionStrategy):
    def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
        base_prompt = """Let's solve this step by step:

Task: {task}

Please break down your thinking into clear steps:
1) First, ...
2) Then, ...
(continue with your step-by-step reasoning)

Final Answer: [Your conclusion based on the above reasoning]"""

        if instruction:
            base_prompt += f"\nAdditional Instruction: {instruction}"
            
        return base_prompt.format(task=task)

    def process_response(self, response: str) -> str:
        return response

class ReflectionStrategy(ExecutionStrategy):
    def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
        base_prompt = """Complete this task using reflection:

Task: {task}

1) Initial Approach:
   - What is your first impression of how to solve this?
   - What assumptions are you making?

2) Analysis:
   - What could go wrong with your initial approach?
   - What alternative approaches could you consider?

3) Refined Solution:
   - Based on your reflection, what is the best approach?
   - Why is this approach better than the alternatives?

4) Final Answer:
   - Provide your solution
   - Briefly explain why this is the optimal approach"""

        if instruction:
            base_prompt += f"\nAdditional Instruction: {instruction}"
            
        return base_prompt.format(task=task)

    def process_response(self, response: str) -> str:
        return response

class StrategyFactory:
    """Factory class for creating execution strategies."""
    
    _strategies = {
        'ReactStrategy': ReactStrategy,
        'ChainOfThoughtStrategy': ChainOfThoughtStrategy,
        'ReflectionStrategy': ReflectionStrategy
    }   
    
    @classmethod
    def create_strategy(cls, strategy_name: str) -> ExecutionStrategy:
        """Create a strategy instance based on the strategy name."""
        strategy_class = cls._strategies.get(strategy_name)
        if not strategy_class:
            raise ValueError(f"Unknown strategy: {strategy_name}")
        return strategy_class()
    
    @classmethod
    def available_strategies(cls) -> List[str]:
        """Return a list of available strategy names."""
        return list(cls._strategies.keys())