|
|
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()) |
|
|
|