package analyzer import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "time" ) type DeepSeekAnalyzer struct { apiKey string baseURL string client *http.Client } func NewDeepSeekAnalyzer(apiKey, baseURL string) *DeepSeekAnalyzer { if baseURL == "" { baseURL = "https://api.deepseek.com" } return &DeepSeekAnalyzer{ apiKey: apiKey, baseURL: strings.TrimRight(baseURL, "/"), client: &http.Client{Timeout: 30 * time.Second}, } } func (d *DeepSeekAnalyzer) AnalyzeJD(ctx context.Context, jobTitle, rawContent string) (*AnalysisOutput, error) { prompt := fmt.Sprintf(`You are an expert technical resume screener. Analyze the following job description for the title "%s". Provide a structured JSON output with 3 fields: - "matched_highlights": list of 3-5 core technical skills/requirements matched (e.g. ["Go (Golang)", "Microservices", "REST APIs"]). - "missing_highlights": list of 1-3 missing skills, experience gaps, or extra requirements (e.g. ["10+ yrs experience", "Kubernetes administration"]). - "tailoring_suggestions": list of 2-3 specific action points for tailoring a resume to this JD. Job Title: %s Job Content: %s`, jobTitle, jobTitle, rawContent) reqBody := map[string]interface{}{ "model": "deepseek-chat", "messages": []map[string]string{ {"role": "system", "content": "You are a professional hiring assistant. You MUST respond ONLY with valid JSON matching {\"matched_highlights\": [...], \"missing_highlights\": [...], \"tailoring_suggestions\": [...]}."}, {"role": "user", "content": prompt}, }, "response_format": map[string]string{ "type": "json_object", }, } jsonBytes, err := json.Marshal(reqBody) if err != nil { return nil, fmt.Errorf("failed to marshal DeepSeek payload: %w", err) } url := fmt.Sprintf("%s/chat/completions", d.baseURL) req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonBytes)) if err != nil { return nil, fmt.Errorf("failed to create DeepSeek request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiKey)) resp, err := d.client.Do(req) if err != nil { return nil, fmt.Errorf("DeepSeek API call failed: %w", err) } defer resp.Body.Close() respBytes, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read DeepSeek response: %w", err) } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("DeepSeek API error (status %d): %s", resp.StatusCode, string(respBytes)) } var dsResp struct { Choices []struct { Message struct { Content string `json:"content"` } `json:"message"` } `json:"choices"` } if err := json.Unmarshal(respBytes, &dsResp); err != nil { return nil, fmt.Errorf("failed to decode DeepSeek envelope: %w", err) } if len(dsResp.Choices) == 0 { return nil, fmt.Errorf("empty choices in DeepSeek response") } rawJSON := cleanJSONResponse(dsResp.Choices[0].Message.Content) var output AnalysisOutput if err := json.Unmarshal([]byte(rawJSON), &output); err != nil { return nil, fmt.Errorf("failed to parse DeepSeek JSON output: %w", err) } return &output, nil } func cleanJSONResponse(raw string) string { cleaned := strings.TrimSpace(raw) if strings.HasPrefix(cleaned, "```json") { cleaned = strings.TrimPrefix(cleaned, "```json") cleaned = strings.TrimSuffix(cleaned, "```") } else if strings.HasPrefix(cleaned, "```") { cleaned = strings.TrimPrefix(cleaned, "```") cleaned = strings.TrimSuffix(cleaned, "```") } return strings.TrimSpace(cleaned) }