Custom log function for swarm operations. Includes dynamic global variables. Args: sender (str): The name of the sender. text (str): The text message to log. cost (float): The cost associated with the operation. result_file (Path, optional): Path to the resu
(sender: str, text: str, cost: float, prompt_tokens: int, complete_tokens: int, log_file_path: str)
| 47 | return log_file_path |
| 48 | |
| 49 | def swarmlog(sender: str, text: str, cost: float, prompt_tokens: int, complete_tokens: int, log_file_path: str) -> None: |
| 50 | """ |
| 51 | Custom log function for swarm operations. Includes dynamic global variables. |
| 52 | |
| 53 | Args: |
| 54 | sender (str): The name of the sender. |
| 55 | text (str): The text message to log. |
| 56 | cost (float): The cost associated with the operation. |
| 57 | result_file (Path, optional): Path to the result file. Default is None. |
| 58 | solution (list, optional): Solution data to be logged. Default is an empty list. |
| 59 | """ |
| 60 | |
| 61 | formatted_message = ( |
| 62 | f"{sender} | 💵Total Cost: ${cost:.5f} | " |
| 63 | f"Prompt Tokens: {prompt_tokens} | " |
| 64 | f"Completion Tokens: {complete_tokens} | \n {text}" |
| 65 | ) |
| 66 | logger.info(formatted_message) |
| 67 | |
| 68 | try: |
| 69 | os.makedirs(log_file_path.parent, exist_ok=True) |
| 70 | with open(log_file_path, 'a') as file: |
| 71 | file.write(f"{formatted_message}\n") |
| 72 | except OSError as error: |
| 73 | logger.error(f"Error initializing log file: {error}") |
| 74 | raise |
| 75 | |
| 76 | def main(): |
| 77 | configure_logging() |