File size: 2,982 Bytes
5d15ae6 |
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 |
from abc import ABC, abstractmethod
from typing import Optional
class ExecutionStrategy(ABC):
@abstractmethod
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
pass
@abstractmethod
def process_response(self, response: str) -> str:
pass
class ReactStrategy(ExecutionStrategy):
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
base_prompt = f"""
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
Final Answer: Provide the recommendation.
Task: {task}
"""
if instruction:
base_prompt += f"\nAdditional Instruction: {instruction}"
return base_prompt
def process_response(self, response: str) -> str:
return response
class ChainOfThoughtStrategy(ExecutionStrategy):
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
base_prompt = f"""
Let's solve this step by step. Provide detailed reasoning at each step.
Task: {task}
Step 1: Identify the key factors influencing the recommendation.
Step 2: Analyze employee skill gaps and opportunities.
Step 3: Propose relevant learning paths.
Final Answer: Provide the recommendation with reasoning.
"""
if instruction:
base_prompt += f"\nAdditional Instruction: {instruction}"
return base_prompt
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 = f"""
Reflect on the task using the following structure:
Task: {task}
1) Initial Thoughts: Provide your first impression.
2) Analysis: Identify assumptions and evaluate their validity.
3) Alternatives: Explore different approaches.
4) Refined Solution: Based on your reflection, provide the final recommendation.
"""
if instruction:
base_prompt += f"\nAdditional Instruction: {instruction}"
return base_prompt
def process_response(self, response: str) -> str:
return response
class StrategyFactory:
_strategies = {
'ReactStrategy': ReactStrategy,
'ChainOfThoughtStrategy': ChainOfThoughtStrategy,
'ReflectionStrategy': ReflectionStrategy
}
@classmethod
def create_strategy(cls, strategy_name: str) -> ExecutionStrategy:
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):
return list(cls._strategies.keys())
|