Skip to content

Commit

Permalink
adding logging tree in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
C0gnitiveSage committed May 28, 2023
1 parent 3d038c7 commit 07b61db
Show file tree
Hide file tree
Showing 9 changed files with 1,069 additions and 49 deletions.
1 change: 0 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#call the solve emthod with the input problem and other params
solution = tree_of_thoughts.solve(x=input_problem, k=k, T=T, b=b, vth=vth, timeout=timeout, confidence_threshold=confidence, max_iterations=max_iterations, convergence_threshold=convergence_threshold, convergence_count=convergence_count)

#use the solution in your production environment
print(f"solution: {solution}")

Expand Down
56 changes: 44 additions & 12 deletions logs/tree_of_thoughts_output.json

Large diffs are not rendered by default.

56 changes: 44 additions & 12 deletions logs/tree_of_thoughts_output_BFS.json

Large diffs are not rendered by default.

962 changes: 962 additions & 0 deletions openai.logs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tree_of_thoughts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tree_of_thoughts.openaiModels import OpenAILanguageModel, OptimizedOpenAILanguageModel
from tree_of_thoughts.treeofthoughts import TreeofThoughts, OptimizedTreeofThoughts
from tree_of_thoughts.treeofthoughts import TreeofThoughts
from tree_of_thoughts.guidanceModels import GuidanceLanguageModel, GuidanceOpenAILanguageModel
from tree_of_thoughts.abstractLanguageModel import AbstractLanguageModel
from tree_of_thoughts.huggingModels import HFPipelineModel, HuggingLanguageModel
Binary file modified tree_of_thoughts/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified tree_of_thoughts/__pycache__/treeofthoughts.cpython-39.pyc
Binary file not shown.
7 changes: 0 additions & 7 deletions tree_of_thoughts/logs/tree_of_thoughts_output_BFS.json

This file was deleted.

34 changes: 18 additions & 16 deletions tree_of_thoughts/treeofthoughts.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,34 @@ def __init__(self, model, search_algorithm):
self.model = model
self.search_algorithm = search_algorithm
self.tree = {
"nodes": {},
"metrics": {
"thoughts": {},
"evaluations": {}
}
"nodes": {}
}

def solve(self, x, k=None, T=None, b=None, vth=None, timeout=None, confidence_threshold=None, max_iterations=None, convergence_threshold=None, convergence_count=None):
start_time = time.time()
file_name = f"logs/tree_of_thoughts_output_{self.search_algorithm}.json"
self.file_name = f"logs/tree_of_thoughts_output_{self.search_algorithm}.json"
try:
best_thoughts = ""
if self.search_algorithm == 'BFS':
while timeout is None or time.time() - start_time < timeout:
result = self.tot_bfs(x, k, T, b, vth)
if result:
self.save_tree_to_json(file_name)
self.save_tree_to_json(self.file_name )
# printed_tree = self.print_tree(result)
# logger.info(f"Tree structure and metrics:\n{printed_tree}")
return result
best_thoughts = result
elif self.search_algorithm == 'DFS':
while timeout is None or time.time() - start_time < timeout:
result = self.tot_dfs(x, k, T, vth)
if result:
self.save_tree_to_json(file_name)
self.save_tree_to_json(self.file_name)
# printed_tree=self.print_tree(result)
# logger.info(f"Tree structure and metrics:\n{printed_tree}")
return result
best_thoughts = result
if(best_thoughts):
solution = self.model.generate_solution(x, best_thoughts)
if solution:
return solution
else:
raise ValueError("Invalid search algorithm. Choose 'BFS' or 'DFS'.")
except KeyboardInterrupt:
Expand All @@ -84,7 +85,7 @@ def solve(self, x, k=None, T=None, b=None, vth=None, timeout=None, confidence_th
logger.error(f"Error: {e}")
finally:
logger.info("Saving the current tree and metrics.")
self.save_tree_to_json(file_name)
self.save_tree_to_json(self.file_name)


def tot_bfs(self, x, k, T, b, pruning_threshold):
Expand All @@ -99,6 +100,12 @@ def tot_bfs(self, x, k, T, b, pruning_threshold):
S0_t.add((*s, z))
Vt = self.model.evaluate_states(S0_t, x)

for s, v in Vt.items():
if not (type(s) == str):
s = " | ".join(s)
self.tree["nodes"][s] = v
print("Saving tree")
self.save_tree_to_json(self.file_name)
# Filter the candidate states based on the pruning threshold
pruned_S0_t = {s: v for s, v in Vt.items() if v >= pruning_threshold}

Expand All @@ -118,9 +125,6 @@ def tot_dfs(self, x, k, T, vth, confidence_threshold=None, max_iterations=None,
iteration_count = 0
consecutive_convergence_count = 0
prev_best_value = None
file_name = f"logs/tree_of_thoughts_output_{self.x}.json"


def dfs(s, t):
nonlocal consecutive_convergence_count, prev_best_value, iteration_count, output
if t > T:
Expand Down Expand Up @@ -165,8 +169,6 @@ def dfs(s, t):

if dfs(child, t + 1):
return True

self.save_tree_to_json(file_name)
return False


Expand Down

0 comments on commit 07b61db

Please sign in to comment.