|
3 | 3 | """ |
4 | 4 | import json |
5 | 5 | import logging |
| 6 | +import time |
6 | 7 | from pathlib import Path |
7 | 8 |
|
| 9 | +import requests |
| 10 | +from django.conf import settings |
8 | 11 | from openedx_ai_extensions.processors import LLMProcessor |
9 | 12 |
|
10 | 13 | logger = logging.getLogger(__name__) |
@@ -97,3 +100,105 @@ def generate_skills(self): |
97 | 100 | prompt = self.fill_prompt(prompt) |
98 | 101 | result = self._call_completion_wrapper(system_role=prompt) |
99 | 102 | return result |
| 103 | + |
| 104 | + def generate_skills_laiser_api(self): |
| 105 | + """ |
| 106 | + Submit course context to the LAiSER API and poll for extracted skills. |
| 107 | +
|
| 108 | + Resolves base_url and api_key from processor config or Django settings. |
| 109 | + POSTs context to /laiser, polls /result until a terminal state, then |
| 110 | + normalizes the result array into the internal skills alignment format. |
| 111 | + """ |
| 112 | + base_url = (self.config.get("base_url") or getattr(settings, "LAISER_API_BASE_URL", "")).rstrip("/") |
| 113 | + api_key = self.config.get("api_key") or getattr(settings, "LAISER_API_KEY", "") |
| 114 | + |
| 115 | + if not base_url or not api_key: |
| 116 | + logger.error("LAiSER API is not configured. Check LAISER_API_BASE_URL or LAISER_API_KEY") |
| 117 | + return {"error": "LAiSER API incorrectly configured"} |
| 118 | + |
| 119 | + try: |
| 120 | + submit_response = requests.post( |
| 121 | + f"{base_url}/laiser", |
| 122 | + json={"inputText": self.context}, |
| 123 | + headers={"x-api-key": api_key, "Content-Type": "application/json"}, |
| 124 | + timeout=30, |
| 125 | + ) |
| 126 | + submit_response.raise_for_status() |
| 127 | + submit_data = submit_response.json() |
| 128 | + except requests.exceptions.RequestException as exc: |
| 129 | + logger.error("LAiSER API submit failed: %s", exc) |
| 130 | + return {"error": str(exc)} |
| 131 | + except ValueError as exc: |
| 132 | + logger.error("LAiSER API submit returned non-JSON: %s", exc) |
| 133 | + return {"error": f"Invalid JSON from LAiSER submit: {exc}"} |
| 134 | + |
| 135 | + job_id = submit_data.get("jobId") |
| 136 | + if not job_id: |
| 137 | + logger.error("LAiSER API submit response missing jobId: %s", submit_data) |
| 138 | + return {"error": "No jobId in LAiSER submit response"} |
| 139 | + |
| 140 | + result = self._poll_laiser_job(base_url, api_key, job_id) |
| 141 | + if "error" in result: |
| 142 | + return result |
| 143 | + |
| 144 | + skills = [self._normalize_laiser_skill(s) for s in result.get("result", [])] |
| 145 | + return {"response": json.dumps({"skills": skills}), "status": "success"} |
| 146 | + |
| 147 | + def _poll_laiser_job(self, base_url, api_key, job_id): |
| 148 | + """Poll GET /result until the job reaches a terminal state or timeout.""" |
| 149 | + timeout = getattr(settings, "LAISER_API_TIMEOUT_SECONDS", 90) |
| 150 | + poll_interval = getattr(settings, "LAISER_API_POLL_INTERVAL_SECONDS", 2) |
| 151 | + elapsed = 0 |
| 152 | + while elapsed < timeout: |
| 153 | + time.sleep(poll_interval) |
| 154 | + elapsed += poll_interval |
| 155 | + |
| 156 | + try: |
| 157 | + response = requests.get( |
| 158 | + f"{base_url}/result", |
| 159 | + params={"jobId": job_id}, |
| 160 | + headers={"x-api-key": api_key}, |
| 161 | + timeout=30, |
| 162 | + ) |
| 163 | + response.raise_for_status() |
| 164 | + data = response.json() |
| 165 | + except requests.exceptions.RequestException as exc: |
| 166 | + logger.error("LAiSER API poll failed (jobId=%s): %s", job_id, exc) |
| 167 | + return {"error": str(exc)} |
| 168 | + except ValueError as exc: |
| 169 | + logger.error("LAiSER API poll returned non-JSON (jobId=%s): %s", job_id, exc) |
| 170 | + return {"error": f"Invalid JSON from LAiSER poll: {exc}"} |
| 171 | + |
| 172 | + status = data.get("status") |
| 173 | + if status in ("QUEUED", "RUNNING"): |
| 174 | + continue |
| 175 | + |
| 176 | + if status != "SUCCEEDED": |
| 177 | + logger.error("LAiSER API job terminal failure (jobId=%s): status=%s", job_id, status) |
| 178 | + return {"error": f"LAiSER job failed with status: {status}"} |
| 179 | + |
| 180 | + return data |
| 181 | + |
| 182 | + logger.error("LAiSER API timed out after %ds, jobId=%s", timeout, job_id) |
| 183 | + return {"error": f"LAiSER API timed out after {timeout} seconds"} |
| 184 | + |
| 185 | + @staticmethod |
| 186 | + def _normalize_laiser_skill(skill: dict) -> dict: |
| 187 | + """Map a LAiSER API result entry to the internal skills alignment format.""" |
| 188 | + source = (skill.get("Taxonomy Source") or "").lower() |
| 189 | + target_type_map = { |
| 190 | + "esco": "ESCO:Skill", |
| 191 | + "ukos": "UKOS:Skill", |
| 192 | + "onet_tech": "ONET:Skill", |
| 193 | + } |
| 194 | + return { |
| 195 | + "type": "Alignment", |
| 196 | + "skill_tag": skill.get("Raw Skill", ""), |
| 197 | + "target_name": skill.get("Taxonomy Skill", ""), |
| 198 | + "target_description": skill.get("Taxonomy Description", ""), |
| 199 | + "target_url": skill.get("Source URL", ""), |
| 200 | + "target_type": target_type_map.get(source, source), |
| 201 | + "correlation_coefficient": skill.get("Correlation Coefficient", 0), |
| 202 | + "task_abilities": [], |
| 203 | + "knowledge_required": [], |
| 204 | + } |
0 commit comments