Create strategy.py
Browse files- strategy.py +86 -0
strategy.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC, abstractmethod
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
class ExecutionStrategy(ABC):
|
| 5 |
+
@abstractmethod
|
| 6 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
@abstractmethod
|
| 10 |
+
def process_response(self, response: str) -> str:
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
class ReactStrategy(ExecutionStrategy):
|
| 14 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 15 |
+
base_prompt = f"""
|
| 16 |
+
Approach this task using the following steps:
|
| 17 |
+
1) Thought: Analyze what needs to be done
|
| 18 |
+
2) Action: Decide on the next action
|
| 19 |
+
3) Observation: Observe the result
|
| 20 |
+
4) Repeat until task is complete
|
| 21 |
+
Final Answer: Provide the recommendation.
|
| 22 |
+
|
| 23 |
+
Task: {task}
|
| 24 |
+
"""
|
| 25 |
+
if instruction:
|
| 26 |
+
base_prompt += f"\nAdditional Instruction: {instruction}"
|
| 27 |
+
return base_prompt
|
| 28 |
+
|
| 29 |
+
def process_response(self, response: str) -> str:
|
| 30 |
+
return response
|
| 31 |
+
|
| 32 |
+
class ChainOfThoughtStrategy(ExecutionStrategy):
|
| 33 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 34 |
+
base_prompt = f"""
|
| 35 |
+
Let's solve this step by step. Provide detailed reasoning at each step.
|
| 36 |
+
|
| 37 |
+
Task: {task}
|
| 38 |
+
|
| 39 |
+
Step 1: Identify the key factors influencing the recommendation.
|
| 40 |
+
Step 2: Analyze employee skill gaps and opportunities.
|
| 41 |
+
Step 3: Propose relevant learning paths.
|
| 42 |
+
Final Answer: Provide the recommendation with reasoning.
|
| 43 |
+
"""
|
| 44 |
+
if instruction:
|
| 45 |
+
base_prompt += f"\nAdditional Instruction: {instruction}"
|
| 46 |
+
return base_prompt
|
| 47 |
+
|
| 48 |
+
def process_response(self, response: str) -> str:
|
| 49 |
+
return response
|
| 50 |
+
|
| 51 |
+
class ReflectionStrategy(ExecutionStrategy):
|
| 52 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 53 |
+
base_prompt = f"""
|
| 54 |
+
Reflect on the task using the following structure:
|
| 55 |
+
|
| 56 |
+
Task: {task}
|
| 57 |
+
|
| 58 |
+
1) Initial Thoughts: Provide your first impression.
|
| 59 |
+
2) Analysis: Identify assumptions and evaluate their validity.
|
| 60 |
+
3) Alternatives: Explore different approaches.
|
| 61 |
+
4) Refined Solution: Based on your reflection, provide the final recommendation.
|
| 62 |
+
"""
|
| 63 |
+
if instruction:
|
| 64 |
+
base_prompt += f"\nAdditional Instruction: {instruction}"
|
| 65 |
+
return base_prompt
|
| 66 |
+
|
| 67 |
+
def process_response(self, response: str) -> str:
|
| 68 |
+
return response
|
| 69 |
+
|
| 70 |
+
class StrategyFactory:
|
| 71 |
+
_strategies = {
|
| 72 |
+
'ReactStrategy': ReactStrategy,
|
| 73 |
+
'ChainOfThoughtStrategy': ChainOfThoughtStrategy,
|
| 74 |
+
'ReflectionStrategy': ReflectionStrategy
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
@classmethod
|
| 78 |
+
def create_strategy(cls, strategy_name: str) -> ExecutionStrategy:
|
| 79 |
+
strategy_class = cls._strategies.get(strategy_name)
|
| 80 |
+
if not strategy_class:
|
| 81 |
+
raise ValueError(f"Unknown strategy: {strategy_name}")
|
| 82 |
+
return strategy_class()
|
| 83 |
+
|
| 84 |
+
@classmethod
|
| 85 |
+
def available_strategies(cls):
|
| 86 |
+
return list(cls._strategies.keys())
|