-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththoughtsculpt.py
229 lines (191 loc) · 7.98 KB
/
thoughtsculpt.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import dspy
from dspy.teleprompt import BootstrapFewShotWithRandomSearch
import os
from dotenv import load_dotenv
import logging
from sentence_transformers import SentenceTransformer, util
from rouge import Rouge
import random
# Load environment variables and setup logging
load_dotenv()
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Configure DSPy with LLM
llm = dspy.OpenAI(
model='gpt-3.5-turbo',
api_key=os.environ['OPENAI_API_KEY'],
max_tokens=1000
)
dspy.settings.configure(lm=llm)
class ThoughtEvaluator(dspy.Signature):
"""Evaluate a thought and provide feedback."""
thought = dspy.InputField()
feedback = dspy.OutputField()
score = dspy.OutputField(desc="A float between 0 and 1")
class ThoughtGenerator(dspy.Signature):
"""Generate a new thought based on instruction, current thought, and feedback."""
instruction = dspy.InputField()
current_thought = dspy.InputField()
feedback = dspy.InputField()
new_thought = dspy.OutputField()
class Evaluator(dspy.Module):
def __init__(self):
super().__init__()
self.evaluate = dspy.Predict(ThoughtEvaluator)
def forward(self, thought):
return self.evaluate(thought=thought)
class Generator(dspy.Module):
def __init__(self):
super().__init__()
self.generate = dspy.Predict(ThoughtGenerator)
def forward(self, instruction, current_thought, feedback):
return self.generate(instruction=instruction, current_thought=current_thought, feedback=feedback)
class DecisionSimulator(dspy.Module):
def forward(self, thoughts):
return self.mcts(thoughts)
def mcts(self, thoughts, iterations=10):
root = MCTSNode(None, thoughts)
for _ in range(iterations):
node = root
while node.children:
node = node.select_child()
if not node.is_terminal():
node.expand()
score = node.simulate()
node.backpropagate(score)
return max(root.children, key=lambda c: c.visits).thought
class MCTSNode:
def __init__(self, parent, thoughts):
self.parent = parent
self.thoughts = thoughts
self.children = []
self.visits = 0
self.score = 0
self.thought = random.choice(thoughts) if thoughts else None
def select_child(self):
return max(self.children, key=lambda c: c.uct())
def expand(self):
for thought in self.thoughts:
self.children.append(MCTSNode(self, self.thoughts))
def simulate(self):
return random.random() # Placeholder for actual simulation
def backpropagate(self, score):
self.visits += 1
self.score += score
if self.parent:
self.parent.backpropagate(score)
def is_terminal(self):
return len(self.thoughts) == 0
def uct(self, c=1.41):
if self.visits == 0:
return float('inf')
return (self.score / self.visits) + c * ((2 * self.parent.visits / self.visits) ** 0.5)
class THOUGHTSCULPT(dspy.Module):
def __init__(self, num_thoughts=3):
super().__init__()
self.evaluate = Evaluator()
self.generate = Generator()
self.simulate = DecisionSimulator()
self.num_thoughts = num_thoughts
def forward(self, instruction, initial_thought, max_iterations=3):
thought = initial_thought
for _ in range(max_iterations):
evaluation = self.evaluate(thought)
new_thoughts = [
self.generate(instruction, thought, evaluation.feedback).new_thought
for _ in range(self.num_thoughts)
]
thought = self.simulate(new_thoughts)
return dspy.Prediction(
instruction=instruction,
initial_thought=initial_thought,
final_thought=thought,
feedback=evaluation.feedback,
score=evaluation.score
)
def generate_trainset(num_examples=20):
instructions = [
"Write a short story about a robot learning to paint.",
"Describe a futuristic city powered entirely by renewable energy.",
"Explain the concept of time travel to a 5-year-old."
]
trainset = []
for _ in range(num_examples):
instruction = random.choice(instructions)
initial_thought = f"Initial thought for: {instruction}"
example = dspy.Example(instruction=instruction, initial_thought=initial_thought)
trainset.append(example.with_inputs('instruction', 'initial_thought'))
return trainset
def improved_thought_evaluation(example, pred, trace=None, frac=0.5):
rouge = Rouge()
model = SentenceTransformer('all-MiniLM-L6-v2')
def normalize_text(text):
return ' '.join(text.lower().split())
def calculate_rouge(prediction, ground_truth):
scores = rouge.get_scores(prediction, ground_truth)
return scores[0]['rouge-l']['f']
def calculate_semantic_similarity(prediction, ground_truth):
embeddings1 = model.encode([prediction], convert_to_tensor=True)
embeddings2 = model.encode([ground_truth], convert_to_tensor=True)
return util.pytorch_cos_sim(embeddings1, embeddings2).item()
prediction = normalize_text(pred.final_thought)
ground_truth = normalize_text(example.initial_thought)
rouge_score = calculate_rouge(prediction, ground_truth)
semantic_similarity = calculate_semantic_similarity(prediction, ground_truth)
combined_score = (rouge_score + semantic_similarity) / 2
return combined_score >= frac
def evaluate(compiled_thoughtsculpt, devset):
results = []
for example in devset:
try:
pred = compiled_thoughtsculpt(example.instruction, example.initial_thought)
score = improved_thought_evaluation(example, pred)
results.append(score)
except Exception as e:
logging.error(f"Error evaluating example: {e}")
return sum(results) / len(results) if results else 0
def main():
try:
# Setup and compilation
dataset = generate_trainset()
trainset = dataset[:-5]
devset = dataset[-5:]
thoughtsculpt_instance = THOUGHTSCULPT()
teleprompter = BootstrapFewShotWithRandomSearch(
metric=improved_thought_evaluation,
num_candidate_programs=10,
max_bootstrapped_demos=4,
max_labeled_demos=16,
max_rounds=2,
num_threads=1,
max_errors=10
)
compiled_thoughtsculpt = teleprompter.compile(thoughtsculpt_instance, trainset=trainset, valset=devset)
# Save the compiled program
compiled_program_json = compiled_thoughtsculpt.save("compiled_thoughtsculpt.json")
print("Program saved to compiled_thoughtsculpt.json")
# Evaluate the compiled program
results = evaluate(compiled_thoughtsculpt, devset)
print("Evaluation Results:")
print(results)
# Interactive loop
while True:
instruction = input("Enter an instruction (or 'quit' to exit): ")
if instruction.lower() == 'quit':
break
initial_thought = input("Enter an initial thought: ")
try:
prediction = compiled_thoughtsculpt(instruction, initial_thought)
print(f"Instruction: {prediction.instruction}")
print(f"Initial Thought: {prediction.initial_thought}")
print(f"Final Thought: {prediction.final_thought}")
print(f"Feedback: {prediction.feedback}")
print(f"Score: {prediction.score}")
except Exception as e:
logging.error(f"Error during prediction: {e}")
print("An error occurred while processing the instruction. Please try again.")
except Exception as e:
logging.error(f"An error occurred in the main execution: {e}")
print("An error occurred. Please check the logs for details.")
if __name__ == "__main__":
main()
print("Thank you for using THOUGHTSCULPT.")