File size: 14,911 Bytes
88da18c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
LLM integration module for analyzing job descriptions and tailoring CV/cover letter.
Supports OpenAI, Grok, Groq, and local Ollama via the OpenAI-compatible API format.
"""

import os
from openai import OpenAI
from typing import Dict, List, Tuple, Any, Optional
import json
import re
import requests
from bs4 import BeautifulSoup
import time


class OpenAIIntegration:
    """Class for handling LLM API interactions."""
    
    def __init__(self, api_key: Optional[str] = None):
        """Initialize LLM integration.
        
        Args:
            api_key: API key for the configured provider. Optional for local Ollama.
        """
        self.provider = (os.environ.get("LLM_PROVIDER", "ollama") or "ollama").lower()

        if self.provider == "openai":
            self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
            self.base_url = os.environ.get("OPENAI_BASE_URL")
            self.model = os.environ.get("OPENAI_MODEL", "gpt-4o-mini")
            if self.api_key:
                if self.base_url:
                    self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
                else:
                    self.client = OpenAI(api_key=self.api_key)
            else:
                self.client = None
        elif self.provider == "grok":
            # Grok uses xAI API with OpenAI-compatible endpoint
            self.api_key = api_key or os.environ.get("GROK_API_KEY")
            self.base_url = os.environ.get("GROK_BASE_URL", "https://api.x.ai/v1")
            self.model = os.environ.get("GROK_MODEL", "grok-2")
            if self.api_key:
                self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
            else:
                self.client = None
        elif self.provider == "groq":
            # Groq provides OpenAI-compatible chat completions.
            self.api_key = api_key or os.environ.get("GROQ_API_KEY")
            self.base_url = os.environ.get("GROQ_BASE_URL", "https://api.groq.com/openai/v1")
            self.model = os.environ.get("GROQ_MODEL", "llama-3.3-70b-versatile")
            if self.api_key:
                self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
            else:
                self.client = None
        else:
            # Default to free/local Ollama (OpenAI-compatible endpoint).
            self.api_key = api_key or os.environ.get("OLLAMA_API_KEY", "ollama")
            self.base_url = os.environ.get("OLLAMA_BASE_URL", "http://127.0.0.1:11434/v1")
            self.model = os.environ.get("OLLAMA_MODEL", "llama3.1:8b")
            self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
    
    def is_api_key_set(self) -> bool:
        """Check if API client is available.
        
        Returns:
            Boolean indicating if API client is ready
        """
        if self.provider in ("openai", "grok", "groq"):
            return bool(self.api_key) and bool(self.client)
        return bool(self.client)
    
    def set_api_key(self, api_key: str) -> None:
        """Set API key for the current provider.
        
        Args:
            api_key: Provider API key
        """
        self.api_key = api_key
        if self.base_url:
            self.client = OpenAI(api_key=api_key, base_url=self.base_url)
        else:
            self.client = OpenAI(api_key=api_key)

    def _build_model_candidates(self) -> List[str]:
        """Build an ordered model candidate list for provider fallback."""
        candidates: List[str] = [self.model]

        # Optional manual fallback list from environment (comma-separated).
        extra = os.environ.get("LLM_FALLBACK_MODELS", "")
        if extra:
            candidates.extend([m.strip() for m in extra.split(",") if m.strip()])

        if self.provider == "grok":
            candidates.extend([
                "grok-3-mini",
                "grok-3",
                "grok-3-fast",
                "grok-2-latest",
                "grok-2",
                "grok-beta",
            ])
        elif self.provider == "groq":
            candidates.extend([
                "llama-3.3-70b-versatile",
                "llama-3.1-8b-instant",
                "mixtral-8x7b-32768",
            ])
        elif self.provider == "openai":
            candidates.extend([
                "gpt-4o-mini",
                "gpt-4.1-mini",
            ])

        # Deduplicate while preserving order.
        deduped: List[str] = []
        seen = set()
        for model in candidates:
            if model and model not in seen:
                deduped.append(model)
                seen.add(model)
        return deduped

    def _chat_completion_with_fallback(self, messages: List[Dict[str, str]], temperature: float, max_tokens: int):
        """Run chat completion with model fallback on model-not-found errors."""
        model_candidates = self._build_model_candidates()
        last_error: Optional[Exception] = None

        for model_name in model_candidates:
            try:
                response = self.client.chat.completions.create(
                    model=model_name,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                )
                # Persist successful model so later requests are faster/stable.
                self.model = model_name
                return response
            except Exception as e:
                last_error = e
                error_text = str(e).lower()
                is_model_error = (
                    "model not found" in error_text
                    or "invalid model" in error_text
                    or "does not exist" in error_text
                )
                if is_model_error:
                    continue
                raise

        if last_error is not None:
            raise ValueError(
                f"All model candidates failed for provider '{self.provider}': {model_candidates}. "
                f"Last error: {last_error}"
            )
        raise ValueError("No model candidates available for completion.")
        
    def extract_job_description_from_url(self, url: str) -> str:
        """Extract job description from LinkedIn URL.
        
        Args:
            url: LinkedIn job posting URL
            
        Returns:
            Extracted job description text
        """
        if not url.startswith(('http://', 'https://')):
            raise ValueError("Invalid URL format")

        if 'linkedin.com' not in url:
            raise ValueError("URL must be from LinkedIn")

        try:
            # Add headers to mimic a browser request
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            
            # Make the request
            response = requests.get(url, headers=headers)
            response.raise_for_status()
            
            # Parse the HTML
            soup = BeautifulSoup(response.text, 'html.parser')
            
            # Extract job title
            job_title = ""
            title_element = soup.find('h1', class_='top-card-layout__title')
            if title_element:
                job_title = title_element.get_text(strip=True)
            
            # Extract company name
            company = ""
            company_element = soup.find('a', class_='topcard__org-name-link')
            if company_element:
                company = company_element.get_text(strip=True)
            
            # Extract job description
            description = ""
            desc_element = soup.find('div', class_='show-more-less-html__markup')
            if desc_element:
                description = desc_element.get_text(strip=True)
            
            # If we couldn't find the description in the expected place, try alternative selectors
            if not description:
                desc_element = soup.find('div', class_='description__text')
                if desc_element:
                    description = desc_element.get_text(strip=True)
            
            # Combine all information
            full_description = f"Job Title: {job_title}\nCompany: {company}\n\nJob Description:\n{description}"
            
            return full_description
            
        except requests.RequestException as e:
            raise ValueError(f"Error fetching job description: {str(e)}")
        except Exception as e:
            raise ValueError(f"Error parsing job description: {str(e)}")

    def analyze_job_description(self, job_description_or_url: str, cv_content: str) -> Dict[str, Any]:
        """Analyze job description and suggest CV modifications.
        
        Args:
            job_description_or_url: Text of the job description or LinkedIn URL
            cv_content: Current content of the CV
            
        Returns:
            Dictionary with suggested modifications for different CV sections
        """
        if not self.is_api_key_set():
            if self.provider == "openai":
                raise ValueError("OpenAI API key is not set. Please set OPENAI_API_KEY or call set_api_key().")
            elif self.provider == "grok":
                raise ValueError("Grok API key is not set. Please set GROK_API_KEY or call set_api_key().")
            elif self.provider == "groq":
                raise ValueError("Groq API key is not set. Please set GROQ_API_KEY or call set_api_key().")
            raise ValueError("LLM client is not initialized. Check local Ollama settings.")
        
        # Check if input is a LinkedIn URL
        if job_description_or_url.startswith(('http://', 'https://')) and 'linkedin.com' in job_description_or_url:
            try:
                job_description = self.extract_job_description_from_url(job_description_or_url)
            except Exception as e:
                raise ValueError(f"Error extracting job description from URL: {str(e)}")
        else:
            job_description = job_description_or_url

        # Prepare the prompt for GPT
        prompt = f"""
        You are an expert CV and resume tailoring assistant. Your task is to analyze a job description 
        and suggest modifications to a CV to better match the job requirements.
        
        JOB DESCRIPTION:
        {job_description}
        
        CURRENT CV CONTENT:
        {cv_content}
        
        Please analyze the job description and suggest specific modifications to the following sections of the CV:
        1. Profile/Summary: Suggest a tailored professional summary that highlights relevant skills and experience.
        2. Skills: Identify key skills from the job description that should be emphasized or added.
        3. Experience: Suggest how to reframe or emphasize certain experiences to better match the job requirements.
        
        Format your response as a JSON object with the following structure:
        {{
            "profile_summary": "Suggested profile summary text",
            "skills": ["skill1", "skill2", "skill3"],
            "experience_highlights": ["point1", "point2", "point3"],
            "keywords_to_emphasize": ["keyword1", "keyword2", "keyword3"]
        }}
        """
        
        try:
            # Call LLM API with model fallback.
            response = self._chat_completion_with_fallback(
                messages=[
                    {"role": "system", "content": "You are an expert CV tailoring assistant that provides structured JSON responses."},
                    {"role": "user", "content": prompt}
                ],
                temperature=0.5,
                max_tokens=1000,
            )
            
            # Extract and parse the response
            result = response.choices[0].message.content
            
            try:
                # Try to parse as JSON
                return json.loads(result)
            except json.JSONDecodeError:
                # If parsing fails, return raw response
                return {"raw_response": result}
            
        except Exception as e:
            return {"error": str(e)}
    
    def tailor_cover_letter(self, job_description: str, current_cover_letter: str, cv_content: str) -> str:
        """Generate a tailored cover letter based on job description and CV.
        
        Args:
            job_description: Text of the job description
            current_cover_letter: Current content of the cover letter
            cv_content: Content of the CV for reference
            
        Returns:
            Tailored cover letter text
        """
        if not self.is_api_key_set():
            if self.provider == "openai":
                raise ValueError("OpenAI API key is not set. Please set OPENAI_API_KEY or call set_api_key().")
            elif self.provider == "grok":
                raise ValueError("Grok API key is not set. Please set GROK_API_KEY or call set_api_key().")
            elif self.provider == "groq":
                raise ValueError("Groq API key is not set. Please set GROQ_API_KEY or call set_api_key().")
            raise ValueError("LLM client is not initialized. Check local Ollama settings.")
        
        # Prepare the prompt for LLM
        prompt = f"""
        You are an expert cover letter writing assistant. Your task is to tailor a cover letter 
        to better match a specific job description, while maintaining the original structure and tone.
        
        JOB DESCRIPTION:
        {job_description}
        
        CURRENT COVER LETTER:
        {current_cover_letter}
        
        CV CONTENT (for reference):
        {cv_content}
        
        Please rewrite the body of the cover letter to:
        1. Address specific requirements mentioned in the job description
        2. Highlight relevant skills and experiences from the CV
        3. Demonstrate enthusiasm for the specific role and company
        4. Maintain a professional tone similar to the original
        5. Keep approximately the same length as the original
        
        Return only the tailored body text of the cover letter, without greeting or closing.
        """
        
        try:
            # Call LLM API with model fallback.
            response = self._chat_completion_with_fallback(
                messages=[
                    {"role": "system", "content": "You are an expert cover letter writing assistant."},
                    {"role": "user", "content": prompt}
                ],
                temperature=0.7,
                max_tokens=1000,
            )
            
            # Extract the response
            result = response.choices[0].message.content
            return result
            
        except Exception as e:
            return f"Error generating cover letter: {str(e)}"