diff --git a/my-app/utils/resume_analysis.py b/my-app/utils/resume_analysis.py index 73db7ed..73bea1b 100755 --- a/my-app/utils/resume_analysis.py +++ b/my-app/utils/resume_analysis.py @@ -21,11 +21,15 @@ load_dotenv() # Configuration OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY") if not OPENROUTER_API_KEY: - raise ValueError("OPENROUTER_API_KEY environment variable is required") + # Use logger here if possible, but it might not be configured yet. + # Consider raising the error later or logging after basicConfig. + print("ERROR: OPENROUTER_API_KEY environment variable is required", file=sys.stderr) + sys.exit(1) OPENROUTER_MODEL_NAME = os.getenv("OPENROUTER_MODEL_NAME") if not OPENROUTER_MODEL_NAME: - raise ValueError("OPENROUTER_MODEL_NAME environment variable is required") + print("ERROR: OPENROUTER_MODEL_NAME environment variable is required", file=sys.stderr) + sys.exit(1) MAX_TOKENS = int(os.getenv("MAX_TOKENS", 500)) USE_MOCKUP = os.getenv("USE_MOCKUP", "false").lower() == "true" @@ -43,37 +47,48 @@ logging.basicConfig( format="[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s", datefmt="%Y-%m-%dT%H:%M:%S%z", ) +logger = logging.getLogger(__name__) # Define logger earlier -logger = logging.getLogger(__name__) +# Global variable to hold the client instance +_opernrouter_client_instance = None -# Initialize OpenRouter client -logger.info("Initializing OpenRouter client...") -logger.debug(f"Using model: {OPENROUTER_MODEL_NAME}") -logger.debug("API Key present and valid format: %s", bool(OPENROUTER_API_KEY and OPENROUTER_API_KEY.startswith("sk-or-v1-"))) +def get_opernrouter_client(): + """ + Initializes and returns the OpenRouter client instance (lazy initialization). + Ensures the client is initialized only once. + """ + global _opernrouter_client_instance + if _opernrouter_client_instance is None: + logger.info("Initializing OpenRouter client for the first time...") + logger.debug(f"Using model: {OPENROUTER_MODEL_NAME}") + logger.debug("API Key present and valid format: %s", bool(OPENROUTER_API_KEY and OPENROUTER_API_KEY.startswith("sk-or-v1-"))) + try: + _opernrouter_client_instance = initialize_openrouter_client( + api_key=OPENROUTER_API_KEY, + model_name=OPENROUTER_MODEL_NAME + ) + logger.info(f"Successfully initialized OpenRouter client with model: {OPENROUTER_MODEL_NAME}") + except ValueError as e: + logger.error(f"Configuration error during client initialization: {e}") + # Re-raise or handle appropriately, maybe return None or raise specific error + raise # Re-raise the ValueError to be caught higher up if needed + except Exception as e: + logger.error(f"Failed to initialize OpenRouter client: {e}", exc_info=True) + # Re-raise or handle appropriately + raise # Re-raise the exception + else: + logger.debug("Returning existing OpenRouter client instance.") + return _opernrouter_client_instance -try: - llm_client = initialize_openrouter_client( - api_key=OPENROUTER_API_KEY, - model_name=OPENROUTER_MODEL_NAME - ) - logger.info(f"Successfully initialized OpenRouter client with model: {OPENROUTER_MODEL_NAME}") -except ValueError as e: - logger.error(f"Configuration error: {e}") - sys.exit(1) -except Exception as e: - logger.error(f"Failed to initialize OpenRouter client: {e}", exc_info=True) - sys.exit(1) def get_mongo_collection(): """Initialize and return MongoDB collection.""" + # Consider lazy initialization for MongoDB as well if beneficial mongo_client = pymongo.MongoClient(MONGODB_URI) db = mongo_client[MONGODB_DATABASE] return db[MONGO_COLLECTION_NAME] -logger = logging.getLogger(__name__) - - def parse_arguments(): """Parses command line arguments.""" parser = argparse.ArgumentParser( @@ -158,7 +173,7 @@ def save_processing_data(resume_text, summary, response, args, processing_id, us def get_cv_summary_from_response(response): """Extracts CV summary from LLM response.""" if response and hasattr(response, "choices"): - message_content = response.choices[0].message.content + message_content = response.choices[0]['message']['content'] try: summary = json.loads(message_content) except json.JSONDecodeError as e: @@ -242,12 +257,19 @@ def call_llm_api(text: str, use_mockup: bool) -> Optional[OpenRouterResponse]: # Call OpenRouter API logger.info(f"Calling OpenRouter API with model: {OPENROUTER_MODEL_NAME}") logger.debug(f"Max tokens set to: {MAX_TOKENS}") - - response = llm_client.create_chat_completion( + + # Get the client instance (initializes on first call) + try: + client = get_opernrouter_client() + except Exception as e: + logger.error(f"Failed to get OpenRouter client: {e}") + return None # Cannot proceed without a client + + response = client.create_chat_completion( messages=messages, max_tokens=MAX_TOKENS ) - + # Validate response if not response.choices: logger.warning("API response contains no choices") @@ -302,8 +324,10 @@ def write_llm_response( # Generate unique file path processing_id = str(uuid.uuid4()) + now = datetime.now() + timestamp_str = now.strftime("%Y%m%d_%H%M%S") file_path = os.path.join( - output_dir, f"{base_filename}_llm_response_{processing_id}" + output_dir, f"{base_filename}_llm_response_{timestamp_str}_{processing_id}" ) + ".json" # Prepare serializable response