In today’s fast-paced job market, a personalized cover letter can set you apart. ResumeBurger’s mission is to streamline your job application process—and what better way than to leverage AI to generate tailored cover letters in seconds? In this tutorial, we’ll build an API endpoint that takes your résumé details and a job description as input, then uses OpenAI to create a professional, customized cover letter.
What You’ll Need
- Python 3.x installed on your system
- Basic familiarity with Python scripting
- An OpenAI API key (store it securely in a
.env
file) - The following Python packages:
- FastAPI
- uvicorn (for running the app)
- openai
- python-dotenv
Install the dependencies with:
pip install fastapi uvicorn openai python-dotenv
Step 1: Secure Your API Key
Create a .env
file in your project directory with your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here
This keeps your sensitive credentials secure and out of your codebase.
Step 2: Build the Cover Letter Generator Function
We’ll define a Python function that sends a prompt (including your résumé details and the job description) to OpenAI’s API. The AI will return a refined cover letter tailored to the job requirements.
import os
import openai
from dotenv import load_dotenv
# Load your API key from the .env file
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_cover_letter(resume_text: str, job_description: str) -> str:
prompt = (
f"Below is a candidate's résumé information:\n\n{resume_text}\n\n"
f"And here is the job description:\n\n{job_description}\n\n"
"Please generate a tailored, professional cover letter that highlights the candidate's strengths and aligns their skills with the job requirements."
)
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=600,
temperature=0.7,
)
cover_letter = response.choices[0].message.content.strip()
return cover_letter
except Exception as e:
raise Exception(f"OpenAI API error: {e}")
Step 3: Create a FastAPI Endpoint
Next, we’ll create a FastAPI app with an endpoint that accepts a JSON payload containing the résumé text and job description. It then returns the AI-generated cover letter.
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI(title="ResumeBurger Cover Letter Generator")
class CoverLetterRequest(BaseModel):
resume_text: str
job_description: str
class CoverLetterResponse(BaseModel):
cover_letter: str
@app.post("/generate-cover-letter", response_model=CoverLetterResponse)
async def generate_letter(request: CoverLetterRequest):
try:
letter = generate_cover_letter(request.resume_text, request.job_description)
return CoverLetterResponse(cover_letter=letter)
except Exception as error:
raise HTTPException(status_code=500, detail=str(error))
if __name__ == "__main__":
import uvicorn
uvicorn.run("your_script_name:app", host="0.0.0.0", port=8000, reload=True)
Replace "your_script_name"
with the name of your Python file (without the .py
extension).
Step 4: Testing and Deployment
- Local Testing: Run the application with:
uvicorn your_script_name:app --reload
Then send a POST request to http://localhost:8000/generate-cover-letter
with JSON similar to:
{
"resume_text": "Experienced software developer with 5 years in AI-driven projects...",
"job_description": "Looking for a candidate with strong expertise in AI, Python, and cloud services..."
}
You should receive a refined cover letter in response.
- Docker Deployment (Optional): Containerize your app for scalable deployment with a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install --upgrade pip && pip install fastapi uvicorn openai python-dotenv
EXPOSE 8000
CMD ["uvicorn", "your_script_name:app", "--host", "0.0.0.0", "--port", "8000"]
Build and run the container:
docker build -t resumeburger-cover-letter .
docker run -d -p 8000:8000 resumeburger-cover-letter
Conclusion
With just a few lines of code, you’ve created a powerful AI-driven cover letter generator that can help job seekers quickly produce personalized, professional cover letters. This FastAPI-based endpoint leverages OpenAI’s capabilities to refine résumé details in line with job descriptions, ensuring every application stands out.
Customize this tool to fit your workflow, integrate it with ResumeBurger’s suite, and empower users to land that dream interview—faster than ever.
Happy coding and best of luck in your job search!
Top comments (0)