|
| 1 | +import configparser |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import yaml |
| 5 | + |
| 6 | +config = configparser.ConfigParser() |
| 7 | + |
| 8 | +def is_root_dir(): |
| 9 | + """ |
| 10 | + Checks if the current working directory is the root directory of a project |
| 11 | + by looking for either the "/notebooks" or "/agents" folders. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + bool: True if either directory exists in the current directory, False otherwise. |
| 15 | + """ |
| 16 | + |
| 17 | + current_dir = os.getcwd() |
| 18 | + print("current dir: ", current_dir) |
| 19 | + notebooks_path = os.path.join(current_dir, "notebooks") |
| 20 | + agents_path = os.path.join(current_dir, "agents") |
| 21 | + |
| 22 | + return os.path.exists(notebooks_path) or os.path.exists(agents_path) |
| 23 | + |
| 24 | +def load_yaml(file_path: str) -> dict: |
| 25 | + with open(file_path, "r", encoding="utf-8") as f: |
| 26 | + return yaml.safe_load(f) |
| 27 | + |
| 28 | +if is_root_dir(): |
| 29 | + current_dir = os.getcwd() |
| 30 | + config.read(current_dir + '/config.ini') |
| 31 | + root_dir = current_dir |
| 32 | +else: |
| 33 | + root_dir = os.path.abspath(os.path.join(os.getcwd(), '..')) |
| 34 | + config.read(root_dir+'/config.ini') |
| 35 | + |
| 36 | + |
| 37 | +def save_config(): |
| 38 | + with open(root_dir + '/config.ini', 'w') as configfile: |
| 39 | + config.write(configfile) |
| 40 | + |
| 41 | + |
| 42 | +if not 'root_dir' in locals(): # If not found in any parent dir |
| 43 | + raise FileNotFoundError("config.ini not found in current or parent directories.") |
| 44 | + |
| 45 | +print(f'root_dir set to: {root_dir}') |
| 46 | + |
| 47 | +def format_prompt(context_prompt, **kwargs): |
| 48 | + """ |
| 49 | + Formats a context prompt by replacing placeholders with values from keyword arguments. |
| 50 | + Args: |
| 51 | + context_prompt (str): The prompt string containing placeholders (e.g., {var1}). |
| 52 | + **kwargs: Keyword arguments representing placeholder names and their values. |
| 53 | + Returns: |
| 54 | + str: The formatted prompt with placeholders replaced. |
| 55 | + """ |
| 56 | + return context_prompt.format(**kwargs) |
| 57 | + |
| 58 | + |
| 59 | +# Ensure that the config dictionary is properly initialized and contains the 'CONFIG' key before this line executes |
| 60 | +# Ensure that the config dictionary is properly initialized and contains the 'CONFIG' key before this line executes |
| 61 | + |
| 62 | + |
| 63 | +# [CONFIG] |
| 64 | +EMBEDDING_MODEL = config['CONFIG']['EMBEDDING_MODEL'] |
| 65 | +DESCRIPTION_MODEL = config['CONFIG']['DESCRIPTION_MODEL'] |
| 66 | +# DATA_SOURCE = config['CONFIG']['DATA_SOURCE'] |
| 67 | +VECTOR_STORE = config['CONFIG']['VECTOR_STORE'] |
| 68 | + |
| 69 | +#CACHING = config.getboolean('CONFIG','CACHING') |
| 70 | +#DEBUGGING = config.getboolean('CONFIG','DEBUGGING') |
| 71 | +LOGGING = config.getboolean('CONFIG','LOGGING') |
| 72 | +EXAMPLES = config.getboolean('CONFIG', 'KGQ_EXAMPLES') |
| 73 | +USE_SESSION_HISTORY = config.getboolean('CONFIG', 'USE_SESSION_HISTORY') |
| 74 | +USE_COLUMN_SAMPLES = config.getboolean('CONFIG','USE_COLUMN_SAMPLES') |
| 75 | + |
| 76 | +#[GCP] |
| 77 | +PROJECT_ID = config['GCP']['PROJECT_ID'] |
| 78 | + |
| 79 | +#[PGCLOUDSQL] |
| 80 | +PG_REGION = config['PGCLOUDSQL']['PG_REGION'] |
| 81 | +# PG_SCHEMA = config['PGCLOUDSQL']['PG_SCHEMA'] |
| 82 | +PG_INSTANCE = config['PGCLOUDSQL']['PG_INSTANCE'] |
| 83 | +PG_DATABASE = config['PGCLOUDSQL']['PG_DATABASE'] |
| 84 | +PG_USER = config['PGCLOUDSQL']['PG_USER'] |
| 85 | +PG_PASSWORD = config['PGCLOUDSQL']['PG_PASSWORD'] |
| 86 | + |
| 87 | +#[BIGQUERY] |
| 88 | +BQ_REGION = config['BIGQUERY']['BQ_DATASET_REGION'] |
| 89 | +# BQ_DATASET_NAME = config['BIGQUERY']['BQ_DATASET_NAME'] |
| 90 | +BQ_OPENDATAQNA_DATASET_NAME = config['BIGQUERY']['BQ_OPENDATAQNA_DATASET_NAME'] |
| 91 | +BQ_LOG_TABLE_NAME = config['BIGQUERY']['BQ_LOG_TABLE_NAME'] |
| 92 | +# BQ_TABLE_LIST = config['BIGQUERY']['BQ_TABLE_LIST'] |
| 93 | + |
| 94 | +#[FIRESTORE] |
| 95 | +FIRESTORE_REGION = config['CONFIG']['FIRESTORE_REGION'] |
| 96 | + |
| 97 | +#[PROMPTS] |
| 98 | +PROMPTS = load_yaml(root_dir + '/prompts.yaml') |
| 99 | + |
| 100 | +__all__ = ["EMBEDDING_MODEL", |
| 101 | + "DESCRIPTION_MODEL", |
| 102 | + #"DATA_SOURCE", |
| 103 | + "VECTOR_STORE", |
| 104 | + #"CACHING", |
| 105 | + #"DEBUGGING", |
| 106 | + "LOGGING", |
| 107 | + "EXAMPLES", |
| 108 | + "PROJECT_ID", |
| 109 | + "PG_REGION", |
| 110 | + # "PG_SCHEMA", |
| 111 | + "PG_INSTANCE", |
| 112 | + "PG_DATABASE", |
| 113 | + "PG_USER", |
| 114 | + "PG_PASSWORD", |
| 115 | + "BQ_REGION", |
| 116 | + # "BQ_DATASET_NAME", |
| 117 | + "BQ_OPENDATAQNA_DATASET_NAME", |
| 118 | + "BQ_LOG_TABLE_NAME", |
| 119 | + # "BQ_TABLE_LIST", |
| 120 | + "FIRESTORE_REGION", |
| 121 | + "PROMPTS" |
| 122 | + "root_dir", |
| 123 | + "save_config"] |
0 commit comments