{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LLAMA Models:\n",
    "\n",
    "[Titan Text: Optimized for natural language tasks like summarization, ranking, and text generation.](https://docs.aws.amazon.com/it_it/bedrock/latest/userguide/model-parameters-meta.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import boto3\n",
    "import yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load credentials from YAML file\n",
    "with open(\"credentials.yml\", \"r\") as keyholder:\n",
    "    try:\n",
    "        credentials=yaml.safe_load(keyholder)\n",
    "    except yaml.YAMLError as exc:\n",
    "        print(exc)\n",
    "\n",
    "AWS_SERVER_PUBLIC_KEY=credentials['bedrock']['AWS_SERVER_PUBLIC_KEY']\n",
    "AWS_SERVER_SECRET_KEY=credentials['bedrock']['AWS_SERVER_SECRET_KEY']\n",
    "REGION_NAME=credentials['bedrock']['REGION_NAME']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_input_files():\n",
    "    # Load candidate resumes\n",
    "    with open(\"candidate_resumes.json\", \"r\") as candidates_file:\n",
    "        candidates = json.load(candidates_file)\n",
    "\n",
    "    # Load job description\n",
    "    with open(\"job_description.json\", \"r\") as job_desc_file:\n",
    "        job_description = json.load(job_desc_file)\n",
    "\n",
    "    return candidates, job_description"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the Bedrock client\n",
    "client = boto3.client(\n",
    "    \"bedrock-runtime\",\n",
    "    region_name=REGION_NAME,\n",
    "    aws_access_key_id=AWS_SERVER_PUBLIC_KEY,\n",
    "    aws_secret_access_key=AWS_SERVER_SECRET_KEY,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rank_candidates_with_llama(candidates, job_description):\n",
    "    job_desc_text = job_description[\"job_description\"]\n",
    "\n",
    "    prompt_template = (\n",
    "        \"You are an AI model skilled in evaluating resumes for job positions. \"\n",
    "        \"The job description is as follows:\\n{job_description}\\n\\n\"\n",
    "        \"Here are the candidate resumes:\\n\"\n",
    "        \"{candidate_resumes}\\n\\n\"\n",
    "        \"Please rank the candidates based on how well they match the job description, \"\n",
    "        \"providing a brief explanation for each ranking. Please explicitly give a number from 0 to 100 on how the candidate fits the position.\\n\"\n",
    "    )\n",
    "\n",
    "    # Prepare the candidates' details as text\n",
    "    candidate_texts = []\n",
    "    for candidate in candidates:\n",
    "        candidate_texts.append(\n",
    "            f\"Name: {candidate['name']}\\n\"\n",
    "            f\"Skills: {candidate['skills']}\\n\"\n",
    "            f\"Experience: {candidate['experience']}\\n\"\n",
    "            f\"Education: {candidate['education']}\\n\"\n",
    "        )\n",
    "    candidates_combined = \"\\n\\n\".join(candidate_texts)\n",
    "\n",
    "    # Construct the input text\n",
    "    input_text = prompt_template.format(\n",
    "        job_description=job_desc_text,\n",
    "        candidate_resumes=candidates_combined\n",
    "    )\n",
    "\n",
    "    try:\n",
    "        # Prepare the payload\n",
    "        body = json.dumps({\n",
    "            \"prompt\": input_text,\n",
    "            \"max_gen_len\": 2000,\n",
    "            \"temperature\": 0.1,\n",
    "            \"top_p\": 0.9\n",
    "        })\n",
    "\n",
    "        # Invoke the model using Llama 3 8B Instruct\n",
    "        response = client.invoke_model(\n",
    "            modelId=\"meta.llama3-8b-instruct-v1:0\",  # Use Llama 3 8B Instruct\n",
    "            body=body,\n",
    "            contentType=\"application/json\"\n",
    "        )\n",
    "        \n",
    "        # Access token counts from response headers\n",
    "        input_tokens = response[\"ResponseMetadata\"][\"HTTPHeaders\"].get(\"x-amzn-bedrock-input-token-count\", \"Unknown\")\n",
    "        output_tokens = response[\"ResponseMetadata\"][\"HTTPHeaders\"].get(\"x-amzn-bedrock-output-token-count\", \"Unknown\")\n",
    "\n",
    "        print(f\"Input Tokens: {input_tokens}\")\n",
    "        print(f\"Output Tokens: {output_tokens}\")\n",
    "\n",
    "        # Parse the model's response\n",
    "        response_body = response[\"body\"].read().decode('utf-8')\n",
    "        print(\"Decoded response body:\")\n",
    "        print(response_body)  # Debug the decoded response body\n",
    "        model_output = json.loads(response_body)\n",
    "\n",
    "        # Extract the generated text directly from the `generation` key\n",
    "        if \"generation\" in model_output:\n",
    "            return model_output[\"generation\"]\n",
    "        else:\n",
    "            print(\"Error: Missing 'generation' in response.\")\n",
    "            return None\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"Error while calling the model: {e}\")\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input Tokens: 512\n",
      "Output Tokens: 382\n",
      "Decoded response body:\n",
      "{\"generation\":\"```\\n\\nRanking:\\n\\n1. Edward Turner (90)\\n\\t* Edward has the most relevant experience in Python and Django, which is a requirement for the position. He also has experience with AWS services like S3 and EC2, which is a plus. His experience as a DevOps Engineer is also valuable, as it shows he has hands-on experience with infrastructure provisioning and automation.\\n2. Alice Smith (85)\\n\\t* Alice has experience with Python and Django, which is a requirement for the position. She also has experience with AWS services like Lambda and S3, which is a plus. Her experience with Docker is also valuable, as it shows she has experience with containerization.\\n3. Charlie Lee (75)\\n\\t* Charlie has experience with Python and Flask, which is a plus. He also has experience with AWS DynamoDB, which is a requirement for the position. However, his experience is limited to 2 years, and he lacks experience with Django, which is a requirement for the position.\\n4. Diana Morales (70)\\n\\t* Diana has experience with Python and FastAPI, which is a plus. She also has experience with Azure, which is not a requirement for the position, but shows she has experience with cloud technologies. However, her experience is limited to 2 years, and she lacks experience with Django, which is a requirement for the position.\\n5. Bob Johnson (0)\\n\\t* Bob does not have any relevant experience with Python or Django, which are requirements for the position. His experience with Java and Spring is not relevant to the position, and his lack of experience with cloud technologies is a major drawback.\\n\\nNote: The ranking is subjective and based on the information provided in the resumes. The actual ranking may vary based on the specific requirements of the position and the company.```\\n\\n\\n\\nI hope this helps! Let me know if you have any further questions.\",\"prompt_token_count\":512,\"generation_token_count\":382,\"stop_reason\":\"stop\"}\n",
      "Ranking and feedback from Llama:\n",
      "```\n",
      "\n",
      "Ranking:\n",
      "\n",
      "1. Edward Turner (90)\n",
      "\t* Edward has the most relevant experience in Python and Django, which is a requirement for the position. He also has experience with AWS services like S3 and EC2, which is a plus. His experience as a DevOps Engineer is also valuable, as it shows he has hands-on experience with infrastructure provisioning and automation.\n",
      "2. Alice Smith (85)\n",
      "\t* Alice has experience with Python and Django, which is a requirement for the position. She also has experience with AWS services like Lambda and S3, which is a plus. Her experience with Docker is also valuable, as it shows she has experience with containerization.\n",
      "3. Charlie Lee (75)\n",
      "\t* Charlie has experience with Python and Flask, which is a plus. He also has experience with AWS DynamoDB, which is a requirement for the position. However, his experience is limited to 2 years, and he lacks experience with Django, which is a requirement for the position.\n",
      "4. Diana Morales (70)\n",
      "\t* Diana has experience with Python and FastAPI, which is a plus. She also has experience with Azure, which is not a requirement for the position, but shows she has experience with cloud technologies. However, her experience is limited to 2 years, and she lacks experience with Django, which is a requirement for the position.\n",
      "5. Bob Johnson (0)\n",
      "\t* Bob does not have any relevant experience with Python or Django, which are requirements for the position. His experience with Java and Spring is not relevant to the position, and his lack of experience with cloud technologies is a major drawback.\n",
      "\n",
      "Note: The ranking is subjective and based on the information provided in the resumes. The actual ranking may vary based on the specific requirements of the position and the company.```\n",
      "\n",
      "\n",
      "\n",
      "I hope this helps! Let me know if you have any further questions.\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    candidates, job_description = read_input_files()\n",
    "    result = rank_candidates_with_llama(candidates, job_description)\n",
    "\n",
    "    if result:\n",
    "        print(\"Ranking and feedback from Llama:\")\n",
    "        print(result)\n",
    "    else:\n",
    "        print(\"Failed to get results from the model.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dinoVenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
