Skip to content

Commit

Permalink
232js as9 + =23093
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed May 28, 2023
1 parent a88137e commit 30c2168
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
OPENAI_API_KEY="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"
OPENAI_API_KEY="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"
49 changes: 6 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,8 @@ from tree_of_thoughts.treeofthoughts import OpenAILanguageModel, CustomLanguageM

use_v2 = False

api_key=""

api_base= "" # leave it blank if you simply use default openai api url

if not use_v2:
#v1
model = OpenAILanguageModel(api_key=api_key, api_base=api_base # api_model="gpt4" # for higher performance base model is not smart
)
else:
#v2 parallel execution, caching, adaptive temperature
model = OptimizedOpenAILanguageModel(api_key=api_key, api_base=api_base, # api_model="gpt4" # for higher performance base model is not smart
)

#huggingface
# model_name="gpt2"
# model = HuggingLanguageModel(model_name)
model = OptimizedOpenAILanguageModel(api_key=api_key) # api_model="gpt4" # for higher performance base model is not smart


#choose search algorithm('BFS' or 'DFS')
Expand All @@ -77,21 +63,13 @@ strategy="cot"
# value or vote
evaluation_strategy = "value"

# if not use_v2:
# #create an instance of the tree of thoughts class v1


tree_of_thoughts = TreeofThoughts(model, search_algorithm)


# else:
# #or v2 -> dynamic beam width -< adjust the beam width [b] dynamically based on the search depth quality of the generated thoughts # does not work now use regular tree of thoughts
# tree_of_thoughts= OptimizedTreeofThoughts(model, search_algorithm)

input_problem = "use 4 numbers and basic arithmetic operations (+-*/) to obtain 24" #note for super intelligent responses you'll have to be more explicit in your prompt and select a better model
input_problem = "use 4 numbers and basic arithmetic operations (+-*/) to obtain 24" #note for superior intelligent responses you'll have to be more explicit in your prompt and select a better model



input_problem = "What are the best reasoning methods to advance Large Language Models"
k = 5 #number of thoughts to generate
T = 3 # maximum depth of the search tree
Expand All @@ -107,12 +85,6 @@ convergence_count = 5 # number of searchers to be considered converged
#call the solve emthod with the input problem and other params
solution = tree_of_thoughts.solve(input_problem, k, T, b, vth, timeout, confidence, max_iterations, convergence_threshold, convergence_count)




# # Save the tree and metrics to a JSON file
# file_name = "logs/tree_of_thoughts_output.json"
# tree_of_thoughts.save_tree_to_json(file_name)

```

Expand Down Expand Up @@ -205,15 +177,16 @@ class TreeofThoughts:
To use Tree of Thoughts with OpenAI's API, create a custom model class that inherits from `AbstractLanguageModel` and implements the required methods using OpenAI's API. Then, create an instance of the `TreeOfThoughts` class with the custom model and the desired search algorithm ('BFS' or 'DFS').

### Hugging Face Transformers
To run huggingface transformers
To run huggingface transformers with Tree of Thoughts

```
git clone https://github.com/kyegomez/tree-of-thoughts
cd tree-of-thoughts
python3 huggingfaceExample.py
```

```python
from tree_of_thoughts import HuggingLanguageModel
from tree_of_thoughts.tree_of_thoughts import HuggingLanguageModel

model_name="gpt2"
model_tokenizer="your tokenizer"
Expand Down Expand Up @@ -286,16 +259,6 @@ Generate suite of evaluations used in the paper testing AI agents with other rea

Implement a more sophisticated prompt engineering strategy to guide the model's reasoning process more effectively.

Make TreeofThoughts class completely customizable with a config yml file with params like
chatbot:
type: "openai"
max_context_length: 8000
include_chat_history_in_query: false
openai:
model: <model_name>
api_key: <your_open_ai_api_key>


Script that generates an dataset based on a topic input, -> set of questions are asked, then multiple trees of thoughts are run concurrently to generate the decision making rich dataset


Expand Down Expand Up @@ -337,7 +300,7 @@ The next big advancement for the Tree of Thoughts algorithm is to extend it to m

Join us on this exciting journey to advance the Tree of Thoughts algorithm to multi-modality superintelligence! 🚀

# Here's the documentation for the inputs of the optimized Tree of Thoughts model:
# Documentation:

## x (str):
The initial problem statement or prompt for which the Tree of Thoughts algorithm will generate a solution.
Expand Down
18 changes: 6 additions & 12 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import os
from tree_of_thoughts.openaiModels import OpenAILanguageModel, OptimizedOpenAILanguageModel
from tree_of_thoughts.treeofthoughts import TreeofThoughts, OptimizedTreeofThoughts

use_v2 = True
api_key=""
api_base= "" # leave it blank if you simply use default openai api url


api_key = "enter in your api key"
api_model= "gpt-3.5-turbo"


model = OptimizedOpenAILanguageModel(api_key=api_key, api_base=api_base, api_model=api_model)
model = OptimizedOpenAILanguageModel(api_key=api_key, api_model=api_model)
#choose search algorithm('BFS' or 'DFS')
search_algorithm = "BFS"



# value or vote
evaluation_strategy = "value"

if not use_v2:
#create an instance of the tree of thoughts class v1
tree_of_thoughts = TreeofThoughts(model, search_algorithm)
else:
#or v2 -> dynamic beam width -< adjust the beam width [b] dynamically based on the search depth quality of the generated thoughts
tree_of_thoughts= OptimizedTreeofThoughts(model, search_algorithm)
tree_of_thoughts= OptimizedTreeofThoughts(model, search_algorithm)

input_problem = "use 4 numbers and basic arithmetic operations (+-*/) to obtain 24"
k = 5
Expand Down
23 changes: 1 addition & 22 deletions huggingfaceExample.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
from tree_of_thoughts.treeofthoughts import OpenAILanguageModel, GuidanceOpenAILanguageModel, TreeofThoughts, OptimizedOpenAILanguageModel, OptimizedTreeofThoughts, HuggingLanguageModel
# from transformers import AutoModelForCausalLM, AutoTokenizer

# model_name="gpt2"
# model_tokenizer="gpt2tokenizer"
# model = HuggingLanguageModel(model_name, model_tokenizer)


# class HuggingLanguageModel:
# def __init__(self, model_name):
# self.model = AutoModelForCausalLM.from_pretrained(model_name)
# self.tokenizer = AutoTokenizer.from_pretrained(model_name)

# def generate_text(self, prompt, max_length=50):
# inputs = self.tokenizer(prompt, return_tensors="pt")
# outputs = self.model.generate(inputs["input_ids"], max_length=max_length)
# generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
# return generated_text

# Initialize the HuggingLanguageModel with the GPT-2 model
model_name = "gpt2"
model_name = "input your model"
model = HuggingLanguageModel(model_name,
model_Tokenizer="gpt2",
verbose=True)




#choose search algorithm('BFS' or 'DFS')
search_algorithm = "BFS"

Expand Down
31 changes: 10 additions & 21 deletions pipelinehuggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
tree_of_thoughts = OptimizedTreeofThoughts(gpt2_pipeline_model, search_algorithm="DFS")


# from transformers import AutoModelForCausalLM, AutoTokenizer


# # Initialize the HuggingLanguageModel with the GPT-2 model
# model_name = "gpt2"
# model = HuggingLanguageModel(model_name,
# model_Tokenizer="gpt2",
# verbose=True)




#choose search algorithm('BFS' or 'DFS')
Expand All @@ -32,19 +22,18 @@


input_problem = "use 4 numbers and basic arithmetic operations (+-*/) to obtain 24"
# k = 5
# T = 3
# b = 5
# vth = 0.5
# timeout = 10
# confidence = 0.8 #cmodel is confident on performance
# max_iterations = 40 #tree branh nodes
# convergence_threshold = 0.01
# convergence_count = 5
k = 5
T = 3
b = 5
vth = 0.5
timeout = 10
confidence = 0.8 #cmodel is confident on performance
max_iterations = 40 #tree branh nodes
convergence_threshold = 0.01
convergence_count = 5


solution = tree_of_thoughts.solve(input_problem)
# `k, T, b, vth, timeout, confidence_threshold=confidence, max_iterations=max_iterations, convergence_threshold=convergence_threshold, convergence_count=convergence_count)
solution = tree_of_thoughts.solve(input_problem, k, T, b, vth, 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}")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'tree-of-thoughts',
packages = find_packages(exclude=[]),
version = '0.2.3',
version = '0.2.4',
license='MIT',
description = 'Tree of Thoughts - Pytorch',
author = 'Kye Gomez',
Expand Down
44 changes: 26 additions & 18 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, pruning_threshold=0.5)
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,31 +85,39 @@ 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):
S0 = {x}
for t in range(1, T + 1):
S0_t = set()
for s in S0:
for z in self.model.generate_thoughts(state=s, k=k):
for z in self.model.generate_thoughts(s, k, x):
if (type(s) == str):
S0_t.add((s, z))
else:
S0_t.add((*s, z))
Vt = self.model.evaluate_states(states=S0_t, initial_prompt=x)

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}

St = sorted(pruned_S0_t.keys(), key=lambda s: pruned_S0_t[s], reverse=True)[:b]
S0 = set(St)

logger.info(f'Step: {t}, S0_t: {S0_t}, Vt: {Vt}, St: {St}, S0: {S0}')

best_state = max(St, key=lambda s: Vt[s])

return best_state



def tot_dfs(self, x, k, T, vth, pruning_threshold=0.5, confidence_threshold=None, max_iterations=None, convergence_threshold=None, convergence_count=None):
#vote across across states
Expand Down Expand Up @@ -195,7 +204,6 @@ def print_tree(self, node, depth=0):




#does not output state after each thought --- idk why -- needs work
class OptimizedTreeofThoughts(TreeofThoughts):
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):
Expand Down

0 comments on commit 30c2168

Please sign in to comment.