linkedin_job_search_agent / tools /linkedin_job_search.py
preslaff's picture
changed the API base_url = https://serpapi.com/google-jobs-api
227002d unverified
raw
history blame
1.83 kB
from smolagents.tools import Tool
import requests
import json
from typing import List
class LinkedInJobSearchTool(Tool):
name = "linkedin_job_search"
description = "Searches for jobs on LinkedIn based on job title, location, and work mode (remote, hybrid, in-office)."
inputs = {
"position": {"type": "string", "description": "Job title (e.g., Data Scientist)"},
"location": {"type": "string", "description": "City or country (e.g., New York)"},
"work_mode": {"type": "string", "description": "remote, hybrid, in-office"}
}
output_type = "array" # ✅ "array" is an authorized type
def forward(self, position: str, location: str, work_mode: str) -> List[str]:
"""
Searches LinkedIn for job postings using SerpAPI.
"""
SERPAPI_KEY = "YOUR_SERPAPI_KEY"
base_url = "https://serpapi.com/google-jobs-api"
params = {
"engine": "google_jobs",
"q": f"{position} {work_mode} jobs",
"location": location,
"hl": "en",
"api_key": SERPAPI_KEY
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
job_results = data.get("jobs_results", [])
formatted_jobs = [
f"Title: {job['title']}, Company: {job.get('company_name', 'N/A')}, "
f"Location: {job.get('location', 'N/A')}, Posted: {job.get('detected_extensions', {}).get('posted_at', 'N/A')}, "
f"Link: {job.get('job_id', 'N/A')}"
for job in job_results
]
return formatted_jobs # ✅ Returns an array of strings
else:
return [f"Error: {response.status_code} - {response.text}"] # Return error as array