-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuct_dataset.py
161 lines (122 loc) · 5.09 KB
/
uct_dataset.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
from datasets import load_dataset
import numpy as np
import tqdm
from anytree import Node as RenderTree
import ollama
prompt_improvement= """this is the current solution, it can be empty: [{current_solution}].
task: improve the current solution.
ALWAYS be short and concise.
"""
prompt_evaluate= """this is the current solution: {current_solution}.
task: evaluate the current solution on a scale of 1 to 10.
DO NOT ADD ANY OTHER ADDITIONAL INFORMATION.
ONLY PROVIDE THE RATING.
"""
class Node:
def __init__(self, parent=None):
self.parent = parent
self.current_solution = ""
self.children = []
self.visits = 0
self.reward = 0
self.reward_indiv = 0
def __repr__(self):
return f"Node(visits={self.visits}, reward={self.reward}, current_solution={self.current_solution})"
def ucb(self, exploration_constant=np.sqrt(2)):
if self.parent is None:
return float('inf')
else:
return self.reward / (self.visits + 1e-9) + exploration_constant * np.sqrt(np.log(self.parent.visits) / (self.visits + 1e-9))
class UCT:
def __init__(self, prompt, max_child=4, exploration_constant=np.sqrt(2)):
self.exploration_constant = exploration_constant
self.root = None
self.prompt = prompt
self.max_child = max_child
def select(self, node):
while len(node.children) == self.max_child:
node = max(node.children, key=lambda child: child.ucb(self.exploration_constant))
return node
def expand(self, node):
child = Node(parent=node)
child.current_solution = self.ollama_chat(model=model_id, prompt_question=self.prompt, current_solution=node.current_solution)
node.children.append(child)
return child
def evaluate_node(self, node):
return float(self.ollama_evaluate(model=model_id, prompt_question=self.prompt, current_solution=node.current_solution))
def backpropagate(self, node, reward):
while node:
node.visits += 1
node.reward += reward
node = node.parent
def uct(self, max_iterations):
self.root = Node()
for _ in tqdm.tqdm(range(max_iterations)):
node = self.select(self.root)
new_node = self.expand(node)
reward = self.evaluate_node(new_node)
new_node.reward_indiv = reward
self.backpropagate(new_node, reward)
return max(self.root.children, key=lambda child: child.visits).current_solution
## Find the node with the best reward value by traversing the tree
def find_best_node(self, node=None):
if node is None:
node = self.root
best_node = node
stack = [node]
while stack:
current_node = stack.pop()
if current_node.reward_indiv > best_node.reward_indiv:
best_node = current_node
stack.extend(current_node.children)
return best_node
def print_anytree(self):
print(RenderTree(self.root)) # Print the tree to the console
@staticmethod
def ollama_chat(model, prompt_question, current_solution="", do_log=False):
if do_log:
print(f"ollama_chat: current_solution: {current_solution}")
response = ollama.chat(model=model, messages=[
{
'role': 'system',
'content': prompt_question,
},
{
'role': 'user',
'content': prompt_improvement.format(current_solution=current_solution),
},
])
if do_log:
print(f"ollama_chat: response: {response['message']['content']}")
return response['message']['content'].strip()
@staticmethod
def ollama_evaluate(model, prompt_question, current_solution="", do_log=False):
if do_log:
print(f"ollama_evaluate: current_solution: {current_solution}")
response = ollama.chat(model=model, messages=[
{
'role': 'system',
'content': prompt_question,
},
{
'role': 'user',
'content': prompt_evaluate.format(current_solution=current_solution),
},
])
if do_log:
print(f"ollama_evaluate: response: {response['message']['content']}")
return float(response['message']['content'].strip())
def main():
#data = load_dataset("lighteval/MATH", split="train", trust_remote_code=True)
data = load_dataset("lighteval/mmlu", "philosophy", split="test", trust_remote_code=True)
for prompt_question in data["question"]:
print(f"Prompt: {prompt_question}")
# Create a UCT agent
uct = UCT(prompt_question)
# Run the UCT algorithm for 1000 iterations
uct.uct(max_iterations=10)
best_node = uct.find_best_node()
print(f"The best solution is {best_node.reward_indiv}: {best_node.current_solution}")
if __name__ == "__main__":
model_id = "gemma2:9b-instruct-q2_K"
main()