DEV Community

Cover image for Build a Recruitment Chatbot with Python and OpenAI
Sangram Sundaray
Sangram Sundaray

Posted on

Build a Recruitment Chatbot with Python and OpenAI

Introduction:

In today's fast-paced recruitment world, automating initial interactions with potential candidates can be a valuable time-saver. Chatbots offer a convenient and efficient way to screen candidates, answer frequently asked questions, and provide basic information about your company and open positions.

This article guides you through building a basic recruitment chatbot using Python and OpenAI's API. We'll cover the necessary steps, code implementation, and explanations to get you started.

Prerequisites:

Steps:

1. Obtain OpenAI API Key:

  • Log in to your OpenAI account.
  • Navigate to "API Keys" and create a new secret key.
  • Store this key securely (we'll use it in the code).

2. Install Libraries:
Open your terminal or command prompt and run the following command:

pip install openai
Enter fullscreen mode Exit fullscreen mode

3. Write the Python code:
Create a new Python file (e.g., recruitment_chatbot.py) and paste the following code :

import os
import openai

# Replace with your OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")

# Define conversation flow functions (replace with your desired responses)
def greet():
    return "Hi! I'm your recruitment assistant. How can I help you today?"

def ask_position():
    return "What position are you interested in?"

def unavailable_position(position):
    return f"Sorry, we're not currently hiring for the {position} position. However, we have openings for {/* List other positions */}. Would you like to know more?"

def available_position(position):
    return f"Great! We're hiring for the {position} position. Can you tell me a bit about your experience?"

def any_questions():
    return "Do you have any questions for me about the position?"

def goodbye():
    return "Thank you for your interest! We look forward to hearing from you soon."

# Main conversation loop
while True:
    user_message = input("You: ")

    # Send user message and model response to OpenAI for processing
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=f"Assistant: {greet()}\nUser: {user_message}",
        max_tokens=1000,
        n=1,
        stop=None,
        temperature=0.7,
    )

    # Parse OpenAI response and continue conversation flow
    assistant_response = response.choices[0].text.strip()
    print(f"Assistant: {assistant_response}")

    # Handle different conversation stages based on user input and assistant response
    if "position" in user_message.lower():
        position = user_message.split()[1]
        if position in ["available_positions"]:  # Replace with actual list
            print(available_position(position))
        else:
            print(unavailable_position(position))
    elif assistant_response == any_questions():
        user_question = input("You: ")
        # Add logic to handle user questions based on position
    elif assistant_response == goodbye():
        break


Enter fullscreen mode Exit fullscreen mode

Explanation:

The code is divided into sections:

Importing libraries and setting up the OpenAI API key.
Defining functions for various conversation stages, including greetings, position inquiry, and handling user questions. Replace the placeholder responses with your specific recruitment information and desired conversation flow.

The main conversation loop that continuously interacts with the user, sends prompts and responses to OpenAI for processing, and progresses the conversation based on predefined logic.

4. Run the Code:

  • Save the Python file.
  • Open your terminal/command prompt and navigate to the directory containing the file.
  • Set your OpenAI API key as an environment variable using export OPENAI_API_KEY=your_key (replace with your actual key).
  • Run the script using python recruitment_chatbot.py.

Note:
This is a basic example and can be further customized.
Remember to replace the placeholder responses with your specific needs.
OpenAI's API has costs associated with usage. Refer to their pricing plans for details: https://openai.com/pricing

Conclusion:

By following these steps, you can create a basic recruitment chatbot using Python and OpenAI. This can streamline initial interactions with potential candidates and improve your recruitment process efficiency. Remember to customize the conversation flow and responses to align with your specific company and open positions.

Top comments (1)

Collapse
 
nicksinghdata profile image
Nick Singh

This is super cool!