Spaces:
Sleeping
Sleeping
Rishi commited on
Commit ·
63fd1bd
1
Parent(s): 0eb3bff
refactor: migrate linkedin tool from proxycurl to rapidapi 'linkedin-data-api'
Browse filesFiles changed (2)
- .env.example+3-3
- tools/linkedin_tool.py+31-30
10 unmodified lines11121314151610 unmodified linesADZUNA_APP_ID=your_adzuna_app_idADZUNA_API_KEY=your_adzuna_api_key# Proxycurl LinkedIn API (https://nubela.co/proxycurl/)# Used by the autonomous agent to extract and summarize LinkedIn profilesPROXYCURL_API_KEY=your_proxycurl_api_key10 unmodified lines11121314151610 unmodified linesADZUNA_APP_ID=your_adzuna_app_idADZUNA_API_KEY=your_adzuna_api_key# RapidAPI Key (https://rapidapi.com/hub)# Used for extracting LinkedIn data via linkedin-data-api.p.rapidapi.comRAPIDAPI_KEY=your_rapidapi_key
123456789101112131415161718192021222324252627282930313233343536371 unmodified line39404142434445464748495051525354555657585960616263646566676869707172737475767778798081"""LangChain tool that retrieves and parses LinkedIn profile data using the Proxycurl API."""import osfrom typing import Optionalimport jsonfrom langchain.tools import toolimport requests@tooldef linkedin_profile_tool(linkedin_url: str) -> str:"""Given a LinkedIn Profile URL, this tool returns the person's professional history,skills, education, and summary as a JSON string. Uses the Proxycurl API.Example input: "https://www.linkedin.com/in/williamhgates""""api_key = os.getenv("PROXYCURL_API_KEY", "")if not api_key:return json.dumps({"error": "PROXYCURL_API_KEY is not configured in the environment variables. ""Please add it to the Hugging Face Space Secrets or local .env file."})api_endpoint = 'https://nubela.co/proxycurl/api/v2/linkedin'headers = {'Authorization': f'Bearer {api_key}'}params = {'url': linkedin_url,'fallback_to_cache': 'on-error','use_cache': 'if-present','skills': 'include',}try:1 unmodified lineif response.status_code == 200:data = response.json()# Extract and clean up only the most relevant fields to avoid overwhelming the LLMprofile_summary = {"full_name": data.get("full_name"),"headline": data.get("headline"),"summary": data.get("summary"),"city": data.get("city"),"country": data.get("country"),"experiences": [],"education": [],"skills": data.get("skills", []),}# Limit experiences and education to last 3 entries to save token spacefor exp in data.get("experiences", [])[:3]:profile_summary["experiences"].append({"company": exp.get("company"),"title": exp.get("title"),"starts_at": exp.get("starts_at"),"ends_at": exp.get("ends_at"),"description": exp.get("description"),})for edu in data.get("education", [])[:3]:profile_summary["education"].append({"school": edu.get("school"),"degree_name": edu.get("degree_name"),"field_of_study": edu.get("field_of_study"),})return json.dumps(profile_summary, indent=2)elif response.status_code == 404:return json.dumps({"error": "Profile not found or is private."})elif response.status_code == 401 or response.status_code == 403:return json.dumps({"error": "Proxycurl API Key is invalid or unauthorized."})else:return json.dumps({"error": f"API returned status code: {response.status_code}"})except requests.exceptions.RequestException as e:return json.dumps({"error": f"Failed to connect to LinkedIn extraction service: {str(e)}"})123456789101112131415161718192021222324252627282930313233343536371 unmodified line3940414243444546474849505152535455565758596061626364656667686970717273747576777879808182"""LangChain tool that retrieves and parses LinkedIn profile data using RapidAPI's LinkedIn Data API."""import osimport jsonimport requestsfrom langchain.tools import tool@tooldef linkedin_profile_tool(linkedin_url: str) -> str:"""Given a LinkedIn Profile URL, this tool returns the person's professional history,skills, education, and summary as a JSON string.Example input: "https://www.linkedin.com/in/williamhgates""""api_key = os.getenv("RAPIDAPI_KEY", "")if not api_key:return json.dumps({"error": "RAPIDAPI_KEY is not configured in the environment variables. ""Please add it to the Hugging Face Space Secrets or local .env file."})# The RapidAPI endpoint for fetching a person's profileapi_endpoint = 'https://linkedin-data-api.p.rapidapi.com/get-profile-data-by-url'headers = {'x-rapidapi-host': 'linkedin-data-api.p.rapidapi.com','x-rapidapi-key': api_key}params = {'url': linkedin_url}try:1 unmodified lineif response.status_code == 200:data = response.json()# The API might encapsulate the data under a distinct key. Try to flatten.# Assuming the standard structure for this specific rapidapi provider.profile_summary = {"full_name": data.get("firstName", "") + " " + data.get("lastName", ""),"headline": data.get("headline"),"summary": data.get("summary"),"location": data.get("locationName"),"experiences": [],"education": [],}# Extract up to top 3 experiencesfor exp in data.get("experience", [])[:3]:profile_summary["experiences"].append({"companyName": exp.get("companyName"),"title": exp.get("title"),"duration": exp.get("timePeriod"),"description": exp.get("description"),})# Extract up to top 3 educationfor edu in data.get("education", [])[:3]:profile_summary["education"].append({"schoolName": edu.get("schoolName"),"degreeName": edu.get("degreeName"),"fieldOfStudy": edu.get("fieldOfStudy"),})return json.dumps(profile_summary, indent=2)elif response.status_code == 404:return json.dumps({"error": "Profile not found or is private."})elif response.status_code in [401, 403]:return json.dumps({"error": "RapidAPI Key is invalid, unauthorized, or quota exceeded."})else:return json.dumps({"error": f"API returned status code: {response.status_code}", "message": response.text[:200]})except requests.exceptions.RequestException as e:return json.dumps({"error": f"Failed to connect to LinkedIn extraction service: {str(e)}"})
.env.example+3-3
tools/linkedin_tool.py+31-30