|
1 | 1 | """This module contains classes for interacting with various models, including API-based models and HuggingFace models.""" |
2 | 2 |
|
3 | 3 | import json |
| 4 | +import pandas as pd |
4 | 5 | import logging |
5 | 6 | import random |
6 | 7 | import threading |
@@ -87,7 +88,6 @@ def get_api_key(self): |
87 | 88 | self.api_key = get_secret(**self.secret_key_params) |
88 | 89 | return self.api_key |
89 | 90 |
|
90 | | - |
91 | 91 | @dataclass |
92 | 92 | class EndpointModel(Model): |
93 | 93 | """This class is used to interact with API-based models.""" |
@@ -180,6 +180,109 @@ def generate(self, query_text, *args, **kwargs): |
180 | 180 | def handle_request_error(self, e): |
181 | 181 | raise NotImplementedError |
182 | 182 |
|
| 183 | +@dataclass |
| 184 | +class OfflineFileModel(Model): |
| 185 | + """This class is used to read pre-generated model/system results via a local file.""" |
| 186 | + |
| 187 | + file_path: str = None |
| 188 | + model_name: str = None |
| 189 | + df_results: pd.DataFrame = None |
| 190 | + |
| 191 | + def __post_init__(self): |
| 192 | + if not self.file_path: |
| 193 | + raise ValueError("file_path must be provided.") |
| 194 | + if not self.model_name: |
| 195 | + raise ValueError("Model name must be provided as additional information on the model/system that was previous used for generating the file in file_path.") |
| 196 | + |
| 197 | + # Load the results from the file into a DataFrame that can be reused for reading all individual results later. |
| 198 | + try: |
| 199 | + self.df_results = pd.read_json(self.file_path, lines=True) |
| 200 | + except FileNotFoundError: |
| 201 | + raise FileNotFoundError(f"Error: File '{self.file_path}' not found.") |
| 202 | + except ValueError as ve: |
| 203 | + raise ValueError(f"Error reading JSON from '{self.file_path}': {ve}") |
| 204 | + except Exception as e: |
| 205 | + print(f"An unexpected error occurred: {e}") |
| 206 | + |
| 207 | + # Check for required columns in the file |
| 208 | + required_columns = {"prompt", "model_output"} |
| 209 | + missing_columns = required_columns - set(self.df_results.columns) |
| 210 | + if missing_columns: |
| 211 | + raise ValueError(f"Error: Missing required columns in file_path: {missing_columns}") |
| 212 | + return None |
| 213 | + |
| 214 | + def generate(self, query_text, *args, **kwargs): |
| 215 | + """ |
| 216 | + Reads the file from file_path to retrieve the model response. |
| 217 | + args: |
| 218 | + query_text (str): the text prompt to generate the response. |
| 219 | + data_repeat_id (str): the id of the repeat for the same prompt, if the initial file has multiple repeats for the same prompt. |
| 220 | + returns: |
| 221 | + response_dict (dict): a dictionary containing the model_output, is_valid, response_time, and n_output_tokens, |
| 222 | + and any other relevant information returned by the model. |
| 223 | + """ |
| 224 | + response_dict = {} |
| 225 | + if hasattr(self, "system_message") and self.system_message: |
| 226 | + if "system_message" in kwargs: |
| 227 | + logging.warning( |
| 228 | + "Warning: System message is passed via the dataloader but will not be used because the inference results are precomputed offline in file_path." |
| 229 | + ) |
| 230 | + kwargs["system_message"] = self.system_message |
| 231 | + |
| 232 | + if hasattr(self, "query_images") and self.system_message: |
| 233 | + if "query_images" in kwargs: |
| 234 | + logging.warning( |
| 235 | + "Warning: Images are not yet supported for this model class." |
| 236 | + ) |
| 237 | + kwargs["query_images"] = self.query_images |
| 238 | + |
| 239 | + if hasattr(self, "chat_mode") and self.chat_mode: |
| 240 | + if "chat_mode" in kwargs: |
| 241 | + logging.warning( |
| 242 | + "Warning: Chat mode is not supported for this model class." |
| 243 | + ) |
| 244 | + |
| 245 | + model_output = None |
| 246 | + is_valid = False |
| 247 | + response_time = 0 # This is a dummy value, as the response time is not available for offline files. |
| 248 | + n_output_tokens = None |
| 249 | + |
| 250 | + try: |
| 251 | + model_response = self.get_response(query_text, kwargs.get("data_repeat_id", None)) |
| 252 | + model_output = model_response["model_output"] |
| 253 | + is_valid = model_response["is_valid"] |
| 254 | + except Exception as e: |
| 255 | + logging.warning("Warning: ") |
| 256 | + |
| 257 | + response_dict.update( |
| 258 | + { |
| 259 | + "is_valid": is_valid, |
| 260 | + "model_output": model_output, |
| 261 | + "response_time": response_time, |
| 262 | + "n_output_tokens": n_output_tokens or self.count_tokens(model_output, is_valid), |
| 263 | + } |
| 264 | + ) |
| 265 | + return response_dict |
| 266 | + |
| 267 | + def get_response(self, target_prompt, target_repeat_id): |
| 268 | + if target_repeat_id is None: |
| 269 | + filtered_df = self.df_results[(self.df_results['prompt'] == target_prompt)] |
| 270 | + else: |
| 271 | + filtered_df = self.df_results[(self.df_results['data_repeat_id'] == target_repeat_id) & (self.df_results['prompt'] == target_prompt)] |
| 272 | + |
| 273 | + |
| 274 | + # Check if a matching record exists |
| 275 | + if not filtered_df.empty: |
| 276 | + if len(filtered_df) > 1: |
| 277 | + logging.warning(f"Warning: More than one matching record found ({len(filtered_df)} records). Returning the first one.") |
| 278 | + model_output = str(filtered_df.iloc[0]['model_output']) |
| 279 | + # If the model output is empty, return None and is_valid as False |
| 280 | + if len(model_output) == 0: |
| 281 | + return {"model_output": None, "is_valid": False} |
| 282 | + return {"model_output": filtered_df.iloc[0]['model_output'], "is_valid": True} |
| 283 | + else: |
| 284 | + return {"model_output": None, "is_valid": False} |
| 285 | + |
183 | 286 |
|
184 | 287 | @dataclass |
185 | 288 | class RestEndpointModel(EndpointModel, KeyBasedAuthMixIn): |
|
0 commit comments