preslaff commited on
Commit
c801ad7
·
unverified ·
1 Parent(s): b3d173d

fixed tool, jobs is now a list of dictionaries

Browse files
Files changed (1) hide show
  1. tools/linkedin_job_search.py +18 -16
tools/linkedin_job_search.py CHANGED
@@ -1,11 +1,11 @@
1
  from smolagents.tools import Tool
2
  import requests
3
  import json
4
- from typing import List
5
 
6
  class LinkedInJobSearchTool(Tool):
7
  name = "linkedin_job_search"
8
- description = "Searches for jobs on LinkedIn based on job title, location, and work mode (remote, hybrid, in-office)."
9
 
10
  inputs = {
11
  "position": {"type": "string", "description": "Job title (e.g., Data Scientist)"},
@@ -13,13 +13,13 @@ class LinkedInJobSearchTool(Tool):
13
  "work_mode": {"type": "string", "description": "remote, hybrid, in-office"}
14
  }
15
 
16
- output_type = "array" # ✅ Use "array" to return a list
17
 
18
- def forward(self, position: str, location: str, work_mode: str) -> List[str]:
19
  """
20
- Fetches job listings from LinkedIn using SerpAPI.
21
  """
22
- SERPAPI_KEY = "2f660e5a696e7d1d08662085b95f83a61224476ec19558de3c68218baf346e43" # 🔹 Replace with your actual API key
23
  base_url = "https://serpapi.com/search"
24
 
25
  params = {
@@ -32,20 +32,22 @@ class LinkedInJobSearchTool(Tool):
32
 
33
  try:
34
  response = requests.get(base_url, params=params)
35
- response.raise_for_status() # ✅ Handle HTTP errors
36
 
37
  data = response.json()
38
  job_results = data.get("jobs_results", [])
39
 
40
- # Convert job results to a list of formatted strings
41
- formatted_jobs = [
42
- f"Title: {job['title']}, Company: {job.get('company_name', 'N/A')}, "
43
- f"Location: {job.get('location', 'N/A')}, Posted: {job.get('detected_extensions', {}).get('posted_at', 'N/A')}, "
44
- f"Link: {job.get('job_id', 'N/A')}"
 
 
 
 
45
  for job in job_results
46
- ]
47
-
48
- return formatted_jobs if formatted_jobs else ["No jobs found. Try different keywords."]
49
 
50
  except requests.exceptions.RequestException as e:
51
- return [f"Error fetching job listings: {str(e)}"]
 
1
  from smolagents.tools import Tool
2
  import requests
3
  import json
4
+ from typing import List, Dict
5
 
6
  class LinkedInJobSearchTool(Tool):
7
  name = "linkedin_job_search"
8
+ description = "Searches for job postings on LinkedIn based on job title, location, and work mode (remote, hybrid, in-office)."
9
 
10
  inputs = {
11
  "position": {"type": "string", "description": "Job title (e.g., Data Scientist)"},
 
13
  "work_mode": {"type": "string", "description": "remote, hybrid, in-office"}
14
  }
15
 
16
+ output_type = "array" # ✅ Ensure smolagents handles list output
17
 
18
+ def forward(self, position: str, location: str, work_mode: str) -> List[Dict]:
19
  """
20
+ Fetches job listings from LinkedIn using SerpAPI and returns structured JSON.
21
  """
22
+ SERPAPI_KEY = "2f660e5a696e7d1d08662085b95f83a61224476ec19558de3c68218baf346e43" # Replace with your actual key
23
  base_url = "https://serpapi.com/search"
24
 
25
  params = {
 
32
 
33
  try:
34
  response = requests.get(base_url, params=params)
35
+ response.raise_for_status()
36
 
37
  data = response.json()
38
  job_results = data.get("jobs_results", [])
39
 
40
+ # Convert job listings to a structured list of dictionaries
41
+ return [
42
+ {
43
+ "Title": job["title"],
44
+ "Company": job.get("company_name", "N/A"),
45
+ "Location": job.get("location", "N/A"),
46
+ "Posted": job.get("detected_extensions", {}).get("posted_at", "N/A"),
47
+ "Link": job.get("job_id", "N/A")
48
+ }
49
  for job in job_results
50
+ ] if job_results else [{"Error": "No jobs found. Try different keywords."}]
 
 
51
 
52
  except requests.exceptions.RequestException as e:
53
+ return [{"Error": f"Error fetching job listings: {str(e)}"}]