Cohere Rerank Model for AI-based Resume Shortlisting and Candidates Evaluation.

Cohere Rerank Model: Transforming Recruitment with AI

Building an Advanced Resume Shortlisting and Candidate Selection System with Cohere

In this tutorial, I will guide you through the process of building an advanced system for resume shortlisting and candidate selection using Cohere's Rerank and Generate functionalities. By the end of this guide, you will have a fully functional tool to assist you in the recruitment process, backed by the power of Cohere.

Introduction to Advanced Resume Shortlisting with Cohere

Welcome to the exciting journey of transforming the way we shortlist resumes and select candidates! I'm Sanchay Thalnerkar, and I'll be your guide through this comprehensive tutorial. Today, we're tapping into the capabilities of Cohere, a platform that offers powerful natural language processing models.

What Are We Building?

We are creating a state-of-the-art system that goes beyond traditional keyword matching for resume shortlisting. This tool will understand the context, experience, and skills detailed in the resumes, ensuring that you select the most suitable candidates for your job openings.

  • Streamlit: A framework for creating web applications with ease.
  • Cohere: A platform that provides access to powerful language models.
  • Rerank: To accurately rank resumes based on their relevance to the job description.
  • Generate: To create detailed explanations for our selections.
  • Pinecone: A service for efficient vector search.
  • Pandas: A library for data manipulation and analysis.
  • OpenAI: For additional natural language processing capabilities.

Why Cohere and Not Just Vector Search?

While vector search is a powerful tool for finding similar documents, it sometimes falls short when it comes to understanding the nuances of human language and context. Cohere fills this gap by offering advanced functionalities:

  • Rerank: It provides a deeper understanding of context and relevance, leading to more accurate rankings of resumes.
  • Generate: It enables us to produce detailed explanations for our choices, showcasing a level of understanding and reasoning akin to a human recruiter.

Introduction to Cohere and Streamlit

Cohere is a platform offering access to cutting-edge natural language processing (NLP) models. It enables developers to harness the power of large language models for various applications, including text generation, classification, and more. Cohere's models understand the context and semantics of text, allowing for more accurate and meaningful interactions with textual data.

In this tutorial, we will focus on two specific functionalities of Cohere:

  • Rerank: This function allows us to re-rank a list of items based on their relevance to a particular query. In our case, we will use it to rank resumes based on their fit for a job description.
  • Generate: This function enables us to generate text based on a prompt. We will use it to create explanations for why a particular resume was ranked highly.

Streamlit is an open-source Python library for creating web applications with minimal effort. It's designed for data scientists and engineers who want to turn data scripts into shareable web apps. With Streamlit, you can create interactive dashboards and tools quickly, making it a perfect choice for our resume shortlisting tool.

Step 1: Setting Up the Environment

Before we dive into building our resume shortlisting and candidate selection tool, we need to prepare our development environment. Follow these steps to ensure everything is set up correctly:

Install Python:

Ensure Python is installed on your system. If not, you can download and install it from the official Python website.

Create a Virtual Environment (Optional):

It's good practice to create a virtual environment to manage dependencies more efficiently and avoid potential conflicts. Run the following commands in your terminal:

python -m venv myenv
source myenv/bin/activate

Install Required Packages:

Now, install the necessary Python packages using pip. The packages required for this project include streamlit, pandas, cohere, openai, and pinecone. Run the following command to install all the required packages:

pip install streamlit pandas cohere pinecone openai

Install Additional Dependencies:

Depending on your system and the specifics of your project, you might need to install additional dependencies. Refer to the documentation of each package for guidance.

Now that our environment is ready, we can start diving into the code and building our application!

Step 2: Acquiring API Keys and Setting Up the Environment File

To securely store our API keys, we will create an environment file named .env. This file will store various configurations including the API keys required to interact with Cohere, Pinecone, and OpenAI.

2.1 Cohere API Key

  1. Visit the Cohere Developer Portal and sign up for an account.
  2. Once signed up, navigate to the API keys section.
  3. Create a new API key.
  4. Copy the API key securely as it will not be shown again.

2.2 Pinecone API Key:

  1. Go to the Pinecone website and create an account or log in.
  2. After logging in, go to your dashboard, and create a new API key.
  3. Copy and securely store the API key.

2.3 OpenAI API Key:

  1. Visit OpenAI's website and sign up for an account or log in.
  2. Navigate to the API keys section in your account settings and generate a new API key.
  3. Securely copy the generated API key.

2.4 Creating the .env File:

Now that you have obtained the API keys, let's create a .env file in the root of your project directory:

YOUR_PINECONE_API_KEY: Your Pinecone API key
YOUR_PINECONE_ENVIRONMENT: Your Pinecone environment (e.g., 'us-west1-gcp')
YOUR_COHERE_API_KEY: Your Cohere API key
YOUR_OPENAI_API_KEY: Your OpenAI API key

Save the .env file after entering the details. Important: Keep your API keys confidential. Never share your .env file or expose your API keys in your code or public repositories.

Step 3: Setting Up the Project Structure

Now that our environment is ready, and we have secured our API keys, it's time to set up the project structure. A clean and organized directory structure is crucial for the maintainability and scalability of your project.

3.1 Directory Structure

Our project will consist of the following files:

  • main.py: This is the main file that will run the Streamlit app.
  • helpers.py: This file contains helper functions and the core logic of our application.
  • .env: This file stores our environment variables, including the API keys.

3.2 Why Two Python Files?

You might wonder why we need to separate our code into two files. Here are some key reasons:

  • Modularity: By keeping the core logic and helper functions in a separate file, we make our code more modular.
  • Maintainability: Changes can be made in helpers.py without affecting the UI code in main.py.
  • Readability: Clear separation between UI code and logic makes the codebase easier to understand.
  • Scalability: A modular structure makes it easier to add features as the application grows.

Step 4: Our helpers.py File

In this helpers.py file, we have a collection of functions that serve various purposes, including initializing connections, generating data, and performing operations related to searching and ranking documents. This modular approach makes our code cleaner, easier to understand, and maintainable.

4.1 Importing Libraries and Initialization

Here, we start by importing the necessary libraries that our helper functions depend on. We use Faker to generate fake data, which is incredibly useful for simulating real-world data without using actual personal information.

4.2 Initializing Pinecone

This function sets up our connection to Pinecone, ensuring that our Pinecone index is ready to be used for inserting and querying data.

4.3 Generating Synthetic Resume

This function generates a synthetic resume with various fields filled with random, but plausible data, crucial for testing our application.

4.4 Creating a Dataset

This function generates a dataset of synthetic resumes to simulate a real-world scenario where you have a collection of resumes to work with.

4.5 Embedding Documents

This function converts our text data into numerical vectors, inputting them into various machine learning models for processing.

4.6 Inserting Data to Pinecone

This function inserts our dataset into the Pinecone index, ensuring that our index is populated with the necessary data for querying.

4.7 Fetching Documents from Pinecone

In this function, we are querying the Pinecone index to fetch the documents that are most relevant to a given query.

4.8 Comparing Search and Rerank Results

This function compares the results of a vector search in Pinecone with the results after applying Cohere's reranking, providing valuable insights into ranking improvements.

4.9 Evaluating Resumes

This function evaluates resumes based on a given job query, employing Cohere's language models to automate the evaluation process.

Step 5: Our main.py

This section focuses on the main application flow, user inputs, and connecting various APIs for processing queries.

Step 6: Running Your Streamlit Application

6.1 Setting Up API Keys

Before the application can search and rerank resumes, input your respective API keys into the designated fields in the left panel.

6.2 Making a Query

Enter your search query and specify how many resumes you would like to fetch and rerank. Press the Search button to execute your query.

6.3 Encountering Errors

If you see a "ForbiddenException" error, it could be due to mismatched API keys or exceeding the allowable number of requests. Simply retry by clicking "Search" again.

6.4 Viewing Results:

Upon executing a search, the application presents a refined list of potential candidates. This list showcases both original and reranked results based on their relevance to your query.

Comparing and contrasting these lists enables better decision-making.

See the Working Prototype! You've successfully navigated through setting up and running your Streamlit application. Thank you for following along with this tutorial. I hope you found it informative and helpful. Happy coding!

Back to blog

Leave a comment