Spaces:
Sleeping
Sleeping
Create strategy.py
Browse files- strategy.py +112 -0
strategy.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC, abstractmethod
|
| 2 |
+
from typing import List, Dict, Optional
|
| 3 |
+
|
| 4 |
+
class ExecutionStrategy(ABC):
|
| 5 |
+
@abstractmethod
|
| 6 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 7 |
+
"""Build the prompt according to the strategy."""
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
@abstractmethod
|
| 11 |
+
def process_response(self, response: str) -> str:
|
| 12 |
+
"""Process the LLM response according to the strategy."""
|
| 13 |
+
pass
|
| 14 |
+
|
| 15 |
+
class ReactStrategy(ExecutionStrategy):
|
| 16 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 17 |
+
base_prompt = """Approach this task using the following steps:
|
| 18 |
+
1) Thought: Analyze what needs to be done
|
| 19 |
+
2) Action: Decide on the next action
|
| 20 |
+
3) Observation: Observe the result
|
| 21 |
+
4) Repeat until task is complete
|
| 22 |
+
|
| 23 |
+
Follow this format for your response:
|
| 24 |
+
Thought: [Your reasoning about the current situation]
|
| 25 |
+
Action: [The action you decide to take]
|
| 26 |
+
Observation: [What you observe after the action]
|
| 27 |
+
... (continue steps as needed)
|
| 28 |
+
Final Answer: [Your final response to the task]
|
| 29 |
+
|
| 30 |
+
Task: {task}"""
|
| 31 |
+
|
| 32 |
+
if instruction:
|
| 33 |
+
base_prompt += f"\nAdditional Instruction: {instruction}"
|
| 34 |
+
|
| 35 |
+
return base_prompt.format(task=task)
|
| 36 |
+
|
| 37 |
+
def process_response(self, response: str) -> str:
|
| 38 |
+
# Could add additional processing here to extract final answer
|
| 39 |
+
return response
|
| 40 |
+
|
| 41 |
+
class ChainOfThoughtStrategy(ExecutionStrategy):
|
| 42 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 43 |
+
base_prompt = """Let's solve this step by step:
|
| 44 |
+
|
| 45 |
+
Task: {task}
|
| 46 |
+
|
| 47 |
+
Please break down your thinking into clear steps:
|
| 48 |
+
1) First, ...
|
| 49 |
+
2) Then, ...
|
| 50 |
+
(continue with your step-by-step reasoning)
|
| 51 |
+
|
| 52 |
+
Final Answer: [Your conclusion based on the above reasoning]"""
|
| 53 |
+
|
| 54 |
+
if instruction:
|
| 55 |
+
base_prompt += f"\nAdditional Instruction: {instruction}"
|
| 56 |
+
|
| 57 |
+
return base_prompt.format(task=task)
|
| 58 |
+
|
| 59 |
+
def process_response(self, response: str) -> str:
|
| 60 |
+
return response
|
| 61 |
+
|
| 62 |
+
class ReflectionStrategy(ExecutionStrategy):
|
| 63 |
+
def build_prompt(self, task: str, instruction: Optional[str] = None) -> str:
|
| 64 |
+
base_prompt = """Complete this task using reflection:
|
| 65 |
+
|
| 66 |
+
Task: {task}
|
| 67 |
+
|
| 68 |
+
1) Initial Approach:
|
| 69 |
+
- What is your first impression of how to solve this?
|
| 70 |
+
- What assumptions are you making?
|
| 71 |
+
|
| 72 |
+
2) Analysis:
|
| 73 |
+
- What could go wrong with your initial approach?
|
| 74 |
+
- What alternative approaches could you consider?
|
| 75 |
+
|
| 76 |
+
3) Refined Solution:
|
| 77 |
+
- Based on your reflection, what is the best approach?
|
| 78 |
+
- Why is this approach better than the alternatives?
|
| 79 |
+
|
| 80 |
+
4) Final Answer:
|
| 81 |
+
- Provide your solution
|
| 82 |
+
- Briefly explain why this is the optimal approach"""
|
| 83 |
+
|
| 84 |
+
if instruction:
|
| 85 |
+
base_prompt += f"\nAdditional Instruction: {instruction}"
|
| 86 |
+
|
| 87 |
+
return base_prompt.format(task=task)
|
| 88 |
+
|
| 89 |
+
def process_response(self, response: str) -> str:
|
| 90 |
+
return response
|
| 91 |
+
|
| 92 |
+
class StrategyFactory:
|
| 93 |
+
"""Factory class for creating execution strategies."""
|
| 94 |
+
|
| 95 |
+
_strategies = {
|
| 96 |
+
'ReactStrategy': ReactStrategy,
|
| 97 |
+
'ChainOfThoughtStrategy': ChainOfThoughtStrategy,
|
| 98 |
+
'ReflectionStrategy': ReflectionStrategy
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
@classmethod
|
| 102 |
+
def create_strategy(cls, strategy_name: str) -> ExecutionStrategy:
|
| 103 |
+
"""Create a strategy instance based on the strategy name."""
|
| 104 |
+
strategy_class = cls._strategies.get(strategy_name)
|
| 105 |
+
if not strategy_class:
|
| 106 |
+
raise ValueError(f"Unknown strategy: {strategy_name}")
|
| 107 |
+
return strategy_class()
|
| 108 |
+
|
| 109 |
+
@classmethod
|
| 110 |
+
def available_strategies(cls) -> List[str]:
|
| 111 |
+
"""Return a list of available strategy names."""
|
| 112 |
+
return list(cls._strategies.keys())
|