diff --git a/agent.py b/agent.py index d3cef2dd..2c166ca1 100644 --- a/agent.py +++ b/agent.py @@ -682,7 +682,7 @@ def _handle_zep_agent(self, message, memorizing, query_id, context_id): # save the context save_dir = f"./outputs/rag_retrieved/{self.agent_name}/k_{self.retrieve_num}/{self.sub_dataset}/chunksize_{self.chunk_size}/query_{query_id}_context_{context_id}.json" os.makedirs(os.path.dirname(save_dir), exist_ok=True) - with open(save_dir, "w") as f: + with open(save_dir, "w", encoding="utf-8") as f: paragraphs = [p for p in retrieved_context.replace("\r\n", "\n").split("\n") if p.strip()] json.dump({"retrieved_context_paragraphs": paragraphs, "response": response}, f, ensure_ascii=False, indent=2) @@ -746,8 +746,8 @@ def _process_rag_query(self, message, query_id, context_id): if output.get("retrieval_context"): save_dir = f"./outputs/rag_retrieved/{self.agent_name}/k_{self.retrieve_num}/{self.sub_dataset}/chunksize_{self.chunk_size}/query_{query_id}_context_{context_id}.json" os.makedirs(os.path.dirname(save_dir), exist_ok=True) - with open(save_dir, "w") as f: - json.dump(output["retrieval_context"], f) + with open(save_dir, "w", encoding="utf-8") as f: + json.dump(output["retrieval_context"], f, ensure_ascii=False) # drop the retrieval_context output.pop("retrieval_context") @@ -1091,13 +1091,13 @@ def save_agent(self): shutil.copyfile(source_db_path, target_db_path) # Save the agent ID for future loading - with open(f"{agent_save_folder}/agent_id.txt", "w") as f: + with open(f"{agent_save_folder}/agent_id.txt", "w", encoding="utf-8") as f: f.write(self.agent_state.id) elif self._is_agent_type("zep"): # save the message that agent has processed messages = "agent finished memorization" os.makedirs(self.agent_save_to_folder, exist_ok=True) - with open(f"{self.agent_save_to_folder}/messages.txt", "w") as f: + with open(f"{self.agent_save_to_folder}/messages.txt", "w", encoding="utf-8") as f: f.write(messages) print("\n\n Agent saved...\n\n") diff --git a/initialization.py b/initialization.py index b94ba63e..78213552 100644 --- a/initialization.py +++ b/initialization.py @@ -60,23 +60,30 @@ def create_agent_and_fetch_data(agent_config, dataset_config): return start_time, conversation_creator.get_chunks(), conversation_creator.get_query_and_answers() -def load_existing_results(output_file_path, dataset_config, all_query_answer_pairs): +def load_existing_results(output_file_path, dataset_config, all_query_answer_pairs, + force_rerun=False): """ Load existing results from output file and initialize variables. - + Args: output_file_path: Path to the output results file dataset_config: Configuration dictionary for the dataset all_query_answer_pairs: List of query-answer pairs for all contexts - + force_rerun: When True, ignore any saved results and start from scratch. + Returns: tuple: (metrics, results, last_completed_context_id, last_completed_query_id) """ - if not os.path.exists(output_file_path): + # --force promises "re-run even if results already exist". Returning saved rows here breaks + # that promise twice: their metrics are averaged into the new score, and their high-water + # mark makes should_skip_query skip the very queries the flag asked to re-run. A run after a + # code change then reports figures partly produced by the old code, with nothing in the + # output to show it happened. + if force_rerun or not os.path.exists(output_file_path): return defaultdict(list), [], 0, 0 - + # Load existing results from file - with open(output_file_path, "r") as file: + with open(output_file_path, "r", encoding="utf-8") as file: saved_output = json.load(file) # Initialize data structures diff --git a/main.py b/main.py index 7247c34f..1978ae0c 100644 --- a/main.py +++ b/main.py @@ -86,8 +86,8 @@ def save_results_to_file(output_path, agent_config, dataset_config, results, met } # Write to file - with open(output_path, "w") as file: - json.dump(output_data, file, indent=4) + with open(output_path, "w", encoding="utf-8") as file: + json.dump(output_data, file, indent=4, ensure_ascii=False) logger.info(f"Results saved at {output_path}") @@ -180,7 +180,7 @@ def main(): # Load existing results and initialize tracking variables time_cost_list = [] metrics, results, last_processed_context_id, last_processed_query_id = load_existing_results( - output_path, dataset_config, all_query_answer_pairs + output_path, dataset_config, all_query_answer_pairs, args.force ) # Start evaluation loop - process each context and its associated queries