-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuct_cot.py
152 lines (112 loc) · 4.57 KB
/
uct_cot.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
import numpy as np
import tqdm
from anytree import Node as RenderTree
from problems import prompt_question_1, prompt_question_2, problem_remi
prompt_improvement= """this is the current solution, it can be empty: [{current_solution}].
task: add a new thought to the list of thoughts to improve the current solution.
if enough thoughts are added, the solution will be improved.
if enough thoughts are present, you MUST provide the solution to the problem.
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.
"""
model_id = "gemma2:9b-instruct-q2_K"
import ollama
def ollama_chat(model, current_solution, do_print=False):
current_solution = "\n".join(current_solution)
if do_print:
print(f"ollama_chat: current_solution: {current_solution}")
response = ollama.chat(model=model, messages=[
{
'role': 'system',
'content': prompt_question_2,
},
{
'role': 'user',
'content': prompt_improvement.format(current_solution=current_solution),
},
])
if do_print:
print(f"ollama_chat: response: {response['message']['content']}")
return response['message']['content']
def ollama_evaluate(model, current_solution, do_print=True):
current_solution = "\n *** ".join(current_solution)
if do_print:
print(f"ollama_evaluate: current_solution: {current_solution}")
response = ollama.chat(model=model, messages=[
{
'role': 'system',
'content': prompt_question_2,
},
{
'role': 'user',
'content': prompt_evaluate.format(current_solution=current_solution),
},
])
if do_print:
print(f"ollama_evaluate: response: {response['message']['content']}")
return response['message']['content']
class Node:
def __init__(self, parent=None):
self.parent = parent
self.current_solution = ""
self.children = []
self.visits = 0
self.reward = 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, exploration_constant=np.sqrt(2)):
self.exploration_constant = exploration_constant
self.root = None
def select(self, node):
while node.children:
node = max(node.children, key=lambda child: child.ucb(self.exploration_constant))
return node
def get_current_solution_path(self, node):
path = []
while node:
path.append(node.current_solution)
node = node.parent
return path[::-1]
def expand(self, node):
child = Node(parent=node)
child.current_solution = ollama_chat(model=model_id, current_solution=self.get_current_solution_path(node))
node.children.append(child)
return child
def evaluate_node(self, node):
return float(ollama_evaluate(model=model_id, current_solution=self.get_current_solution_path(node)))
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)
self.backpropagate(new_node, reward)
return max(self.root.children, key=lambda child: child.visits)
def print_anytree(self):
print(RenderTree(self.root)) # Print the tree to the console
def main():
# Create a UCT agent
uct = UCT()
# Run the UCT algorithm for 1000 iterations
current_solution = uct.uct(max_iterations=10)
# Print the best action
best_path = "\n".join(uct.get_current_solution_path(current_solution))
print(f"The best solution is: {best_path}")
uct.print_anytree()
if __name__ == "__main__":
main()