Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BFCL] Overhaul apply_function_credential_config.py for Enhanced Usability #508

Merged
merged 13 commits into from
Jul 17, 2024
4 changes: 2 additions & 2 deletions berkeley-function-call-leaderboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ To run the executable test categories, there are 4 API keys to include:
3. OMDB API: http://www.omdbapi.com/apikey.aspx
4. Geocode API: https://geocode.maps.co/

The `apply_function_credential_config.py` takes an input and optionally an output file. If the output file is not given as an argument, it will overwrite your original file with the data reset.
The `apply_function_credential_config.py` will automatically search for dataset files in the default `./data/` directory and replace the placeholder values with the actual API keys.

```bash
python apply_function_credential_config.py --input-file data/gorilla_openfunctions_v1_test_rest.json
python apply_function_credential_config.py
```

Then, use `eval_data_compilation.py` to compile all files by
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import glob
import json
import argparse

import os
from eval_checker import custom_exception

parser = argparse.ArgumentParser(description="Replace placeholders in the function credential config file.")
parser.add_argument("--input-file", help="Path to the function credential config file.", required=True)
parser.add_argument("--output-file", help="Path to the output file.", default="")
parser.add_argument("--input-path", help="Path to the function credential config file. Can be a file or a directory.")
parser.add_argument("--output-path", help="Path to the output file.")
args = parser.parse_args()

# Load the configuration with actual API keys
Expand Down Expand Up @@ -41,16 +43,10 @@ def replace_placeholders(data):
data[idx] = item.replace(placeholder, actual_value)
return data

def main():
# Verify all values are provided
for key, value in PLACEHOLDERS.items():
if value == "":
print(f"Please provide a value for the placeholder {key}.")
return
print("All API keys are present.")


def process_file(input_file_path, output_file_path):
modified_data = []
with open(f"{args.input_file}", 'r') as f:
with open(input_file_path, "r") as f:
lines = f.readlines()
for line in lines:
try:
Expand All @@ -61,17 +57,48 @@ def main():
# Handle the case where a line is not a valid JSON object
print("Invalid JSON line skipped.")
continue

# Write each modified JSON object overwrite the output file
HuanzhiMao marked this conversation as resolved.
Show resolved Hide resolved
with open(output_file_path, "w") as f:
for i, modified_line in enumerate(modified_data):
f.write(modified_line)
if i < len(data) - 1:
f.write("\n")

print(f"All placeholders have been replaced for {input_file_path} 🦍.")


def process_dir(input_dir, output_dir):
# This function does not support nested directories
# To support nested directories, refer to this commit:
# https://github.com/ShishirPatil/gorilla/pull/508/commits/8b1e35590e5bce3bd52a7c6405775b1ce4a64945
print(f"Input directory: {input_dir}")
# Get a list of all entries in the folder
entries = os.scandir(input_dir)

json_files_pattern = os.path.join(input_dir, "*.json")
for input_file_path in glob.glob(json_files_pattern):
file_name = os.path.basename(input_file_path)
output_file_path = os.path.join(output_dir, file_name)
process_file(input_file_path, output_file_path)


if args.output_file == "":
with open(f"{args.input_file}", 'w') as f:
for modified_line in modified_data:
f.write(modified_line + '\n') # Write each modified JSON object back to the input file
print(f"All placeholders have been replaced in {args.input_file} 🦍.")
else:
with open(f"{args.output_file}", 'w') as f:
for modified_line in modified_data:
f.write(modified_line + '\n') # Write each modified JSON object overwrite the output file
print(f"All placeholders have been replaced in {args.output_file} 🦍.")

if __name__ == "__main__":
main()
# Verify all values are provided
for key, value in PLACEHOLDERS.items():
if value == "":
raise custom_exception.NoAPIKeyError()
print("All API keys are present.")

input_path = args.input_path
if input_path is None:
input_path = "./data/"

output_path = args.output_path
if output_path is None:
output_path = input_path

if os.path.isdir(input_path):
process_dir(input_path, output_path)
else:
process_file(input_path, output_path)