Fix relative import issue in examples

This commit is contained in:
Nils Blach 2023-12-01 11:12:32 +01:00 committed by Nils Blach
parent a6aee6bb76
commit b5a8c960d6
9 changed files with 172 additions and 134 deletions

View File

@ -121,7 +121,13 @@ We took extra care to fully document the code, so that you can easily understand
The [examples](examples) directory contains several examples of problems that can be solved using the framework, including the ones presented in the paper. The [examples](examples) directory contains several examples of problems that can be solved using the framework, including the ones presented in the paper.
It is a great starting point for learning how to use the framework to solve real problems. It is a great starting point for learning how to use the framework to solve real problems.
Each example contains a `README.md` file with instructions on how to run it and play with it. The code is fully documented and should be easy to follow. Each example contains a `README.md` file with instructions on how to run it and play with it. The code is fully documented and should be easy to follow.
You can also run the examples straight from the main directory. Note that the results will be stored in the repsective examples sub-directory.
Try for instance:
```bash
python -m examples.sorting.sorting_032
python -m examples.keyword_counting.keyword_counting
```
## Paper Results ## Paper Results
You can run the experiments from the paper by following the instructions in the [examples](examples) directory. You can run the experiments from the paper by following the instructions in the [examples](examples) directory.

View File

@ -656,9 +656,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "documents.csv") data_path = os.path.join(os.path.dirname(__file__), "documents.csv")
data = [] data = []
with open(path, "r", encoding="utf8") as f: with open(data_path, "r", encoding="utf8") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -669,12 +669,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -682,22 +685,18 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
) )
for method in methods: for method in methods:
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]}") logging.info(f"Running data {data[0]}: {data[1]}")
@ -715,7 +714,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -737,8 +739,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -1341,9 +1341,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "countries.csv") data_path = os.path.join(os.path.dirname(__file__), "countries.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -1357,12 +1357,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -1370,13 +1373,11 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
@ -1384,9 +1385,7 @@ def run(
for method in methods: for method in methods:
# create a results directory for the method # create a results directory for the method
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]}") logging.info(f"Running data {data[0]}: {data[1]}")
@ -1404,7 +1403,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -1427,8 +1429,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -17,7 +17,11 @@ import csv
from typing import Dict, List, Callable, Union from typing import Dict, List, Callable, Union
from graph_of_thoughts import controller, language_models, operations, prompter, parser from graph_of_thoughts import controller, language_models, operations, prompter, parser
from . import utils # This is a hack to also allow execution of this file from the examples directory
try:
from . import utils
except ImportError:
import utils
class SetIntersectionPrompter(prompter.Prompter): class SetIntersectionPrompter(prompter.Prompter):
@ -604,9 +608,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "set_intersection_032.csv") data_path = os.path.join(os.path.dirname(__file__), "set_intersection_032.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -616,12 +620,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -629,13 +636,11 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
@ -643,9 +648,7 @@ def run(
for method in methods: for method in methods:
# create a results directory for the method # create a results directory for the method
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]} {data[2]}") logging.info(f"Running data {data[0]}: {data[1]} {data[2]}")
@ -663,7 +666,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -687,8 +693,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -17,7 +17,11 @@ import csv
from typing import Dict, List, Callable, Union from typing import Dict, List, Callable, Union
from graph_of_thoughts import controller, language_models, operations, prompter, parser from graph_of_thoughts import controller, language_models, operations, prompter, parser
from . import utils # This is a hack to also allow execution of this file from the examples directory
try:
from . import utils
except ImportError:
import utils
class SetIntersectionPrompter(prompter.Prompter): class SetIntersectionPrompter(prompter.Prompter):
@ -634,9 +638,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "set_intersection_064.csv") data_path = os.path.join(os.path.dirname(__file__), "set_intersection_064.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -646,12 +650,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -659,13 +666,11 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
@ -673,9 +678,7 @@ def run(
for method in methods: for method in methods:
# create a results directory for the method # create a results directory for the method
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]} {data[2]}") logging.info(f"Running data {data[0]}: {data[1]} {data[2]}")
@ -693,7 +696,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -717,8 +723,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -17,7 +17,11 @@ import csv
from typing import Dict, List, Callable, Union from typing import Dict, List, Callable, Union
from graph_of_thoughts import controller, language_models, operations, prompter, parser from graph_of_thoughts import controller, language_models, operations, prompter, parser
from . import utils # This is a hack to also allow execution of this file from the examples directory
try:
from . import utils
except ImportError:
import utils
class SetIntersectionPrompter(prompter.Prompter): class SetIntersectionPrompter(prompter.Prompter):
@ -684,9 +688,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "set_intersection_128.csv") data_path = os.path.join(os.path.dirname(__file__), "set_intersection_128.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -696,12 +700,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -709,13 +716,11 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
@ -723,9 +728,7 @@ def run(
for method in methods: for method in methods:
# create a results directory for the method # create a results directory for the method
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]} {data[2]}") logging.info(f"Running data {data[0]}: {data[1]} {data[2]}")
@ -743,7 +746,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -767,8 +773,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -14,7 +14,11 @@ import csv
from typing import Dict, List, Callable, Union from typing import Dict, List, Callable, Union
from graph_of_thoughts import controller, language_models, operations, prompter, parser from graph_of_thoughts import controller, language_models, operations, prompter, parser
from . import utils # This is a hack to also allow execution of this file from the examples directory
try:
from . import utils
except ImportError:
import utils
class SortingPrompter(prompter.Prompter): class SortingPrompter(prompter.Prompter):
@ -617,9 +621,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "sorting_032.csv") data_path = os.path.join(os.path.dirname(__file__), "sorting_032.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -629,12 +633,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -642,22 +649,18 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
) )
for method in methods: for method in methods:
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]}") logging.info(f"Running data {data[0]}: {data[1]}")
@ -675,7 +678,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -697,8 +703,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -14,7 +14,11 @@ import csv
from typing import Dict, List, Callable, Union from typing import Dict, List, Callable, Union
from graph_of_thoughts import controller, language_models, operations, prompter, parser from graph_of_thoughts import controller, language_models, operations, prompter, parser
from . import utils # This is a hack to also allow execution of this file from the examples directory
try:
from . import utils
except ImportError:
import utils
class SortingPrompter(prompter.Prompter): class SortingPrompter(prompter.Prompter):
@ -674,9 +678,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "sorting_064.csv") data_path = os.path.join(os.path.dirname(__file__), "sorting_064.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -686,12 +690,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -699,13 +706,11 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
@ -713,9 +718,7 @@ def run(
for method in methods: for method in methods:
# create a results directory for the method # create a results directory for the method
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]}") logging.info(f"Running data {data[0]}: {data[1]}")
@ -733,7 +736,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -755,8 +761,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )

View File

@ -14,7 +14,11 @@ import csv
from typing import Dict, List, Callable, Union from typing import Dict, List, Callable, Union
from graph_of_thoughts import controller, language_models, operations, prompter, parser from graph_of_thoughts import controller, language_models, operations, prompter, parser
from . import utils # This is a hack to also allow execution of this file from the examples directory
try:
from . import utils
except ImportError:
import utils
class SortingPrompter(prompter.Prompter): class SortingPrompter(prompter.Prompter):
@ -773,9 +777,9 @@ def run(
""" """
orig_budget = budget orig_budget = budget
path = os.path.join(os.path.dirname(__file__), "sorting_128.csv") data_path = os.path.join(os.path.dirname(__file__), "sorting_128.csv")
data = [] data = []
with open(path, "r") as f: with open(data_path, "r") as f:
reader = csv.reader(f) reader = csv.reader(f)
next(reader) next(reader)
for row in reader: for row in reader:
@ -785,12 +789,15 @@ def run(
data_ids = list(range(len(data))) data_ids = list(range(len(data)))
selected_data = [data[i] for i in data_ids] selected_data = [data[i] for i in data_ids]
if not os.path.exists(os.path.join(os.path.dirname(__file__), "results")): results_dir = os.path.join(os.path.dirname(__file__), "results")
os.makedirs(os.path.join(os.path.dirname(__file__), "results"))
if not os.path.exists(results_dir):
os.makedirs(results_dir)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}" extra_info = f"{lm_name}_{'-'.join([method.__name__ for method in methods])}"
folder_name = f"results/{extra_info}_{timestamp}" folder_name = f"{extra_info}_{timestamp}"
os.makedirs(os.path.join(os.path.dirname(__file__), folder_name)) results_folder = os.path.join(results_dir, folder_name)
os.makedirs(results_folder)
config = { config = {
"data": selected_data, "data": selected_data,
@ -798,22 +805,18 @@ def run(
"lm": lm_name, "lm": lm_name,
"budget": budget, "budget": budget,
} }
with open( with open(os.path.join(results_folder, "config.json"), "w") as f:
os.path.join(os.path.dirname(__file__), folder_name, "config.json"), "w"
) as f:
json.dump(config, f) json.dump(config, f)
logging.basicConfig( logging.basicConfig(
filename=f"{folder_name}/log.log", filename=os.path.join(results_folder, "log.log"),
filemode="w", filemode="w",
format="%(name)s - %(levelname)s - %(message)s", format="%(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG, level=logging.DEBUG,
) )
for method in methods: for method in methods:
os.makedirs( os.makedirs(os.path.join(results_folder, method.__name__))
os.path.join(os.path.dirname(__file__), folder_name, method.__name__)
)
for data in selected_data: for data in selected_data:
logging.info(f"Running data {data[0]}: {data[1]}") logging.info(f"Running data {data[0]}: {data[1]}")
@ -831,7 +834,10 @@ def run(
) )
break break
lm = language_models.ChatGPT( lm = language_models.ChatGPT(
"../../graph_of_thoughts/language_models/config.json", os.path.join(
os.path.dirname(__file__),
"../../graph_of_thoughts/language_models/config.json",
),
model_name=lm_name, model_name=lm_name,
cache=True, cache=True,
) )
@ -853,8 +859,7 @@ def run(
except Exception as e: except Exception as e:
logging.error(f"Exception: {e}") logging.error(f"Exception: {e}")
path = os.path.join( path = os.path.join(
os.path.dirname(__file__), results_folder,
folder_name,
method.__name__, method.__name__,
f"{data[0]}.json", f"{data[0]}.json",
) )