feat: initialize project structure with SQLite database, crawler framework, and LLM-based JD analysis integration
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hireme/hireme/internal/config"
|
||||
)
|
||||
|
||||
type AnalysisOutput struct {
|
||||
MatchedHighlights []string `json:"matched_highlights"`
|
||||
MissingHighlights []string `json:"missing_highlights"`
|
||||
TailoringSuggestions []string `json:"tailoring_suggestions"`
|
||||
}
|
||||
|
||||
type LLMAnalyzer interface {
|
||||
AnalyzeJD(ctx context.Context, jobTitle, rawContent string) (*AnalysisOutput, error)
|
||||
}
|
||||
|
||||
func NewAnalyzer(cfg *config.Config) (LLMAnalyzer, error) {
|
||||
provider := strings.ToLower(cfg.LLMProvider)
|
||||
switch provider {
|
||||
case "gemini":
|
||||
if cfg.GeminiAPIKey == "" {
|
||||
return nil, fmt.Errorf("GEMINI_API_KEY is required for provider 'gemini'")
|
||||
}
|
||||
return NewGeminiAnalyzer(cfg.GeminiAPIKey), nil
|
||||
case "deepseek":
|
||||
if cfg.DeepSeekAPIKey == "" {
|
||||
return nil, fmt.Errorf("DEEPSEEK_API_KEY is required for provider 'deepseek'")
|
||||
}
|
||||
return NewDeepSeekAnalyzer(cfg.DeepSeekAPIKey, cfg.DeepSeekBaseURL), nil
|
||||
case "mock":
|
||||
return NewMockAnalyzer(), nil
|
||||
default:
|
||||
return NewMockAnalyzer(), nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GeminiAnalyzer struct {
|
||||
apiKey string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewGeminiAnalyzer(apiKey string) *GeminiAnalyzer {
|
||||
return &GeminiAnalyzer{
|
||||
apiKey: apiKey,
|
||||
client: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GeminiAnalyzer) 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
|
||||
|
||||
Respond STRICTLY with valid JSON matching the format:
|
||||
{"matched_highlights": [...], "missing_highlights": [...], "tailoring_suggestions": [...]}`, jobTitle, jobTitle, rawContent)
|
||||
|
||||
reqBody := map[string]interface{}{
|
||||
"contents": []map[string]interface{}{
|
||||
{
|
||||
"parts": []map[string]string{
|
||||
{"text": prompt},
|
||||
},
|
||||
},
|
||||
},
|
||||
"generationConfig": map[string]interface{}{
|
||||
"response_mime_type": "application/json",
|
||||
},
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal Gemini payload: %w", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=%s", g.apiKey)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonBytes))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create Gemini HTTP request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := g.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Gemini 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 Gemini response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("Gemini API error (status %d): %s", resp.StatusCode, string(respBytes))
|
||||
}
|
||||
|
||||
var geminiResp struct {
|
||||
Candidates []struct {
|
||||
Content struct {
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"parts"`
|
||||
} `json:"content"`
|
||||
} `json:"candidates"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(respBytes, &geminiResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode Gemini envelope: %w", err)
|
||||
}
|
||||
|
||||
if len(geminiResp.Candidates) == 0 || len(geminiResp.Candidates[0].Content.Parts) == 0 {
|
||||
return nil, fmt.Errorf("empty candidates in Gemini response")
|
||||
}
|
||||
|
||||
rawJSON := geminiResp.Candidates[0].Content.Parts[0].Text
|
||||
rawJSON = cleanJSONResponse(rawJSON)
|
||||
|
||||
var output AnalysisOutput
|
||||
if err := json.Unmarshal([]byte(rawJSON), &output); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse Gemini JSON output: %w", err)
|
||||
}
|
||||
|
||||
return &output, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type MockAnalyzer struct{}
|
||||
|
||||
func NewMockAnalyzer() *MockAnalyzer {
|
||||
return &MockAnalyzer{}
|
||||
}
|
||||
|
||||
// AnalyzeJD returns empty slices for mock provider so no fake mock text is injected into side panel during debug
|
||||
func (m *MockAnalyzer) AnalyzeJD(ctx context.Context, jobTitle, rawContent string) (*AnalysisOutput, error) {
|
||||
return &AnalysisOutput{
|
||||
MatchedHighlights: []string{},
|
||||
MissingHighlights: []string{},
|
||||
TailoringSuggestions: []string{},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user