Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add sexample with text generation web ui
  • Loading branch information
paolorechia committed May 22, 2023
commit 9a68568527017457cbc1691d5dd75a8e1aec96b6
62 changes: 62 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from tree_of_thoughts import OpenAILanguageModel, CustomLanguageModel, TreeofThoughts, OptimizedOpenAILanguageModel, OptimizedTreeofThoughts

search_algorithm = "DFS"
strategy = "cot"
evaluation_strategy="vote"

#create instance
model = OptimizedOpenAILanguageModel('api key')


tree_of_thoughts = OptimizedTreeofThoughts(model, search_algorithm)


input_problem = "What are next generation reasoning methods for Large Language Models"
k = 5
T = 3
b = 5
vth = 0.5

# # Optimal nominal values for the stopping conditions

# confidence = 0.9 #HIGH QUALITY SOLIUTION FOUND

# max_iterations = 5 # MAX ITERATIONS 10

# convergence_threshold = 0.01 #Convergence Check: Monitor the change in evaluation values between consecutive iterations. If the change in evaluation values is below a certain threshold for a specified number of consecutive iterations, the algorithm can stop and return the solution.

# convergence_count = 5

#call the solve method with the input problem and other params
solution = tree_of_thoughts.solve(input_problem, k, T, b, vth)

#use the solution in yes
print(f"solution: {solution}")

"""
should return something like this:

['1. Utilizing reinforcement learning techniques to train large language models can be an effective approach to advancing them.\n2. Developing methods to better incorporate contextual information into large language models can help in their advancement.\n3. Incorpor', '1. Utilizing reinforcement learning techniques to allow for more efficient training of large language models.\n2. Incorporating transfer learning to leverage existing language models for faster and more accurate inference.\n3. Exploring the use of distributed', '1. Identifying and understanding key components of large language models such as natural language processing and machine learning algorithms.\n2. Utilizing methods such as transfer learning to quickly and efficiently train large language models.\n3. Incorporating', '1. Utilizing reinforcement learning techniques to train large language models can be an effective method of advancing them.\n2. Incorporating techniques such as transfer learning and data augmentation can help improve the performance of large language models.', '1. Identifying and understanding the underlying structure of language is essential to advancing large language models.\n2. Developing methods to effectively capture and represent the complexities of language is necessary for the advancement of large language models.\n3. Ut']
0.8
0.8
['4. Analyzing and interpreting large language models to identify areas of improvement.\n5. Utilizing reinforcement learning to enable models to learn from mistakes and further improve accuracy.\n6. Leveraging automated data augmentation techniques to further improve', '4. Experimenting with different architectures and hyperparameters to determine the best model for a given task.\n5. Incorporating techniques such as data augmentation and ensembling to improve the performance of large language models.\n6', '4. Exploring methods to improve the efficiency of large language models such as using distributed computing techniques.\n5. Developing methods to reduce overfitting and improve generalization of large language models.\n6. Incorporating techniques such as', '4. Exploring and utilizing different types of data sets to train large language models.\n5. Developing strategies to optimize the training process and improve the performance of large language models.\n6. Applying advanced techniques such as deep learning', '4. Exploring methods such as reinforcement learning to improve the accuracy and robustness of large language models.\n5. Utilizing data augmentation techniques to increase the amount of training data available to the model.\n6. Incorpor']
0.8
0.8
['7. Developing automated testing frameworks to validate the accuracy of large language models.\n8. Exploring ways to improve the scalability of large language models.\n9. Exploring ways to improve the efficiency of large language models.', '7. Applying methods such as active learning to further refine large language models.\n8. Developing and utilizing techniques such as knowledge distillation to compress large language models.\n9. Incorporating techniques such as semi-supervised', '7. Applying regularization techniques to reduce overfitting and improve generalization of large language models.\n8. Exploring the use of generative adversarial networks to improve the accuracy of large language models.\n9. Applying deep', '7. Developing methods to evaluate the performance of large language models on various tasks.\n8. Applying techniques such as hyperparameter tuning to optimize the performance of large language models.\n9. Utilizing adversarial training to', '7. Developing strategies to ensure large language models are able to generalize to unseen data.\n8. Incorporating methods such as meta-learning to further improve model performance.\n9. Utilizing techniques such as unsuper']
0.7
0.7
['Once the key components of large language models have been identified and understood, the best reasoning methods to advance them include utilizing transfer learning to quickly train them, analyzing and interpreting them to identify areas of improvement, leveraging reinforcement learning to enable them to learn']
0.7
0.7
['Exploring the use of meta-learning to enable models to rapidly adapt to new data and improve accuracy.']
0.7
0.7
['One potential way to further advance large language models is to incorporate automated data augmentation techniques to create more varied datasets to train the models on, as well as leveraging reinforcement learning to enable the models to learn from mistakes and continually improve accuracy.']
0.7
0.7
['By utilizing these methods, we can continue to advance large language models by improving their accuracy and performance. We can also use these methods to identify weaknesses in the models and make modifications to address them. Additionally, these methods can help us to develop']
0.7
0.7


"""
41 changes: 41 additions & 0 deletions example_text_generation_web_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from tree_of_thoughts import TextGenerationWebUILanguageModel, TreeofThoughts, OptimizedTreeofThoughts

#v1
model = TextGenerationWebUILanguageModel()

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

#cot or propose
strategy="cot"

# value or vote
evaluation_strategy = "value"

#create an instance of the tree of thoughts class v1
tree_of_thoughts = TreeofThoughts(model, search_algorithm)

#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)

input_problem = "What are the next generation reasoning methods for Large Language Models"
k = 5
T = 3
b = 5
vth = 0.5

# # Optimal nominal values for the stopping conditions

# confidence = 0.9 #HIGH QUALITY SOLIUTION FOUND

# max_iterations = 5 # MAX ITERATIONS 10

# convergence_threshold = 0.01 #Convergence Check: Monitor the change in evaluation values between consecutive iterations. If the change in evaluation values is below a certain threshold for a specified number of consecutive iterations, the algorithm can stop and return the solution.

# convergence_count = 5

#call the solve method with the input problem and other params
solution = tree_of_thoughts.solve(input_problem, k, T, b, vth)

#use the solution in env
print(f"solution: {solution}")
2 changes: 1 addition & 1 deletion tree_of_thoughts/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from tree_of_thoughts.treeofthoughts import TreeofThoughts, CustomLanguageModel, OptimizedOpenAILanguageModel, OptimizedTreeofThoughts
from tree_of_thoughts.treeofthoughts import TreeofThoughts, CustomLanguageModel, OptimizedOpenAILanguageModel, OptimizedTreeofThoughts, TextGenerationWebUILanguageModel
130 changes: 130 additions & 0 deletions tree_of_thoughts/text_generation_web_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from typing import List, Mapping, Union, Any, Callable
from typing import Dict
import requests
from copy import deepcopy
from dataclasses import dataclass


def _default_extractor(json_response: Dict[str, Any], stop_parameter_name) -> str:
"""
This function extracts the response from the JSON object using the default parameter name.

Parameters:
json_response (dict): The JSON response to be extracted.
stop_parameter_name (str): The name of the parameter that stops the extraction process.

Returns:
str: The extracted response.
"""
return json_response["response"]


@dataclass
class _HTTPBaseLLM:
prompt_url: str
parameters: Dict[str, Union[float, int, str, bool, List[str]]] = None
response_extractor: Callable[[Dict[str, Any]], str] = _default_extractor
stop_parameter_name: str = "stop"

@property
def _llm_type(self) -> str:
return "custom"

def sample_n(self, prompt, stop, n):
samples = []
for _ in range(n):
samples.append(self._call(prompt, stop))
return samples

def _call(self, prompt: str, stop: List[str]) -> str:
# Merge passed stop list with class parameters
stop_list = list(
set(stop).union(set(self.parameters[self.stop_parameter_name]))
)

params = deepcopy(self.parameters)
params[self.stop_parameter_name] = stop_list

response = requests.post(
self.prompt_url,
json={
"prompt": prompt,
**params,
},
)
response.raise_for_status()
return self.response_extractor(
response.json(), params[self.stop_parameter_name]
)

@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {}

def set_parameter(self, parameter_name, parameter_value):
self.parameters[parameter_name] = parameter_value


def ui_default_parameters():
return {
"max_new_tokens": 1024,
"do_sample": True,
"temperature": 0.001,
"top_p": 0.3,
"typical_p": 1,
"repetition_penalty": 1.2,
"top_k": 30,
"min_length": 0,
"no_repeat_ngram_size": 0,
"num_beams": 1,
"penalty_alpha": 0,
"length_penalty": 1.5,
"early_stopping": False,
"seed": -1,
"add_bos_token": True,
"truncation_length": 2048,
"ban_eos_token": False,
"skip_special_tokens": True,
"stopping_strings": [],
}


def _response_extractor(json_response, stopping_strings):
"""Extract relevant information from the given JSON response."""
result = json_response["results"][0]["text"]
for stop_string in stopping_strings:
# The stop strings from text-generation-webui come back without the last char
ss = stop_string[0:-1]
if ss in result:
cut_result = result.split(ss)[0]
return cut_result
return result


def build_text_generation_web_ui_client_llm(
prompt_url="http://0.0.0.0:5000/api/v1/generate", parameters=None
):
"""
This function builds a text generation web UI client using LLM (Largue Language Machine) API.
It takes a URL for the LLM server and optional parameters as inputs.
If parameters are not provided, default ones will be used. The function returns an HTTP client that can generate text based on user input.

Parameters:
prompt_url (str): URL of the LLM server.
parameters (Optional[dict]): Optional parameters to pass to the LLM API.

Returns:
An HTTP client object that can generate text based on user input.
"""

if parameters is None:
parameters = ui_default_parameters()

return _HTTPBaseLLM(
prompt_url=prompt_url,
parameters=parameters,
stop_parameter_name="stopping_strings",
response_extractor=_response_extractor,
)

Loading