-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
27 lines (20 loc) · 920 Bytes
/
utils.py
File metadata and controls
27 lines (20 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
def get_api_key():
"""
Get API key from a text file. Search for directory in file path, and stop if directory named
"prompt_evaluation" is found. Then, read API key from text file named key.txt located in
this directory.
Returns:
str: the key
"""
# Get the absolute path of current working directory
absolute_path = os.path.abspath(os.getcwd())
# Split the path by separator and stop the loop when 'prompt_evaluation' directory is found
directory = [f for f in absolute_path.split(os.sep) if f != "prompt_evaluation"]
directory.append('prompt_evaluation')
# Join the directory list to form absolute path of required directory
absolute_path = os.sep.join(directory)
# Read the API key from 'key.txt' in the above directory
with open(os.path.join(absolute_path, "key.txt"), "r") as key_file:
key = key_file.read()
return key