feat: implement YAML-based configuration, .env support, and customizable prompt templates for AI analysis
This commit is contained in:
@@ -9,6 +9,9 @@ import (
|
||||
)
|
||||
|
||||
type AnalysisOutput struct {
|
||||
AIScore float64 `json:"ai_score"`
|
||||
IsQualified bool `json:"is_qualified"`
|
||||
Summary string `json:"summary"`
|
||||
MatchedHighlights []string `json:"matched_highlights"`
|
||||
MissingHighlights []string `json:"missing_highlights"`
|
||||
TailoringSuggestions []string `json:"tailoring_suggestions"`
|
||||
@@ -25,12 +28,12 @@ func NewAnalyzer(cfg *config.Config) (LLMAnalyzer, error) {
|
||||
if cfg.GeminiAPIKey == "" {
|
||||
return nil, fmt.Errorf("GEMINI_API_KEY is required for provider 'gemini'")
|
||||
}
|
||||
return NewGeminiAnalyzer(cfg.GeminiAPIKey), nil
|
||||
return NewGeminiAnalyzer(cfg.GeminiAPIKey, cfg.SystemPrompt, cfg.UserPrompt), 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
|
||||
return NewDeepSeekAnalyzer(cfg.DeepSeekAPIKey, cfg.DeepSeekBaseURL, cfg.SystemPrompt, cfg.UserPrompt), nil
|
||||
case "mock":
|
||||
return NewMockAnalyzer(), nil
|
||||
default:
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCleanJSONResponse(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
input: "```json\n{\"ai_score\": 90}\n```",
|
||||
expected: "{\"ai_score\": 90}",
|
||||
},
|
||||
{
|
||||
input: "```\n{\"ai_score\": 85}\n```",
|
||||
expected: "{\"ai_score\": 85}",
|
||||
},
|
||||
{
|
||||
input: "{\"ai_score\": 95}",
|
||||
expected: "{\"ai_score\": 95}",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
out := cleanJSONResponse(c.input)
|
||||
if out != c.expected {
|
||||
t.Errorf("cleanJSONResponse(%q) = %q, expected %q", c.input, out, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockAnalyzer(t *testing.T) {
|
||||
mock := NewMockAnalyzer()
|
||||
res, err := mock.AnalyzeJD(context.Background(), "Senior AI Infrastructure Engineer", "Building GKE and K8s runtimes.")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if res.AIScore <= 0 {
|
||||
t.Errorf("expected positive AI score, got %f", res.AIScore)
|
||||
}
|
||||
if !res.IsQualified {
|
||||
t.Errorf("expected IsQualified=true, got false")
|
||||
}
|
||||
if len(res.MatchedHighlights) == 0 {
|
||||
t.Errorf("expected matched highlights to not be empty")
|
||||
}
|
||||
}
|
||||
@@ -12,38 +12,48 @@ import (
|
||||
)
|
||||
|
||||
type DeepSeekAnalyzer struct {
|
||||
apiKey string
|
||||
baseURL string
|
||||
client *http.Client
|
||||
apiKey string
|
||||
baseURL string
|
||||
systemPrompt string
|
||||
userPromptTemplate string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewDeepSeekAnalyzer(apiKey, baseURL string) *DeepSeekAnalyzer {
|
||||
func NewDeepSeekAnalyzer(apiKey, baseURL, systemPrompt, userPromptTemplate string) *DeepSeekAnalyzer {
|
||||
if baseURL == "" {
|
||||
baseURL = "https://api.deepseek.com"
|
||||
}
|
||||
return &DeepSeekAnalyzer{
|
||||
apiKey: apiKey,
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
client: &http.Client{Timeout: 30 * time.Second},
|
||||
apiKey: apiKey,
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
systemPrompt: systemPrompt,
|
||||
userPromptTemplate: userPromptTemplate,
|
||||
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.
|
||||
func (d *DeepSeekAnalyzer) buildUserPrompt(jobTitle, rawContent string) string {
|
||||
if d.userPromptTemplate == "" {
|
||||
return fmt.Sprintf("Analyze the following job description for candidate Pengzhan Hao.\n\nJob Title: %s\n\nJob Description:\n%s\n\nProvide the structured evaluation adhering to the JSON schema.", jobTitle, rawContent)
|
||||
}
|
||||
res := strings.ReplaceAll(d.userPromptTemplate, "{{.JobTitle}}", jobTitle)
|
||||
res = strings.ReplaceAll(res, "{{.JobContent}}", rawContent)
|
||||
return res
|
||||
}
|
||||
|
||||
Job Title: %s
|
||||
Job Content:
|
||||
%s`, jobTitle, jobTitle, rawContent)
|
||||
func (d *DeepSeekAnalyzer) AnalyzeJD(ctx context.Context, jobTitle, rawContent string) (*AnalysisOutput, error) {
|
||||
sysPrompt := d.systemPrompt
|
||||
if sysPrompt == "" {
|
||||
sysPrompt = "You are an expert technical resume screener. You MUST respond with a valid JSON object matching {\"ai_score\": 80.0, \"is_qualified\": true, \"summary\": \"...\", \"matched_highlights\": [...], \"missing_highlights\": [...], \"tailoring_suggestions\": [...]}."
|
||||
}
|
||||
|
||||
userPrompt := d.buildUserPrompt(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},
|
||||
{"role": "system", "content": sysPrompt},
|
||||
{"role": "user", "content": userPrompt},
|
||||
},
|
||||
"response_format": map[string]string{
|
||||
"type": "json_object",
|
||||
@@ -99,7 +109,28 @@ Job 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 nil, fmt.Errorf("failed to parse DeepSeek JSON output: %w (content: %s)", err, rawJSON)
|
||||
}
|
||||
|
||||
// Sanitize output bounds
|
||||
if output.AIScore < 0 {
|
||||
output.AIScore = 0
|
||||
} else if output.AIScore > 100 {
|
||||
output.AIScore = 100
|
||||
}
|
||||
|
||||
if output.AIScore >= 60.0 {
|
||||
output.IsQualified = true
|
||||
}
|
||||
|
||||
if output.MatchedHighlights == nil {
|
||||
output.MatchedHighlights = []string{}
|
||||
}
|
||||
if output.MissingHighlights == nil {
|
||||
output.MissingHighlights = []string{}
|
||||
}
|
||||
if output.TailoringSuggestions == nil {
|
||||
output.TailoringSuggestions = []string{}
|
||||
}
|
||||
|
||||
return &output, nil
|
||||
|
||||
+36
-17
@@ -7,40 +7,49 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GeminiAnalyzer struct {
|
||||
apiKey string
|
||||
client *http.Client
|
||||
apiKey string
|
||||
systemPrompt string
|
||||
userPromptTemplate string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewGeminiAnalyzer(apiKey string) *GeminiAnalyzer {
|
||||
func NewGeminiAnalyzer(apiKey, systemPrompt, userPromptTemplate string) *GeminiAnalyzer {
|
||||
return &GeminiAnalyzer{
|
||||
apiKey: apiKey,
|
||||
client: &http.Client{Timeout: 30 * time.Second},
|
||||
apiKey: apiKey,
|
||||
systemPrompt: systemPrompt,
|
||||
userPromptTemplate: userPromptTemplate,
|
||||
client: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GeminiAnalyzer) buildUserPrompt(jobTitle, rawContent string) string {
|
||||
if g.userPromptTemplate == "" {
|
||||
return fmt.Sprintf("Analyze the following job description for candidate Pengzhan Hao.\n\nJob Title: %s\n\nJob Content:\n%s\n\nRespond with structured JSON.", jobTitle, rawContent)
|
||||
}
|
||||
res := strings.ReplaceAll(g.userPromptTemplate, "{{.JobTitle}}", jobTitle)
|
||||
res = strings.ReplaceAll(res, "{{.JobContent}}", rawContent)
|
||||
return res
|
||||
}
|
||||
|
||||
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.
|
||||
sysPrompt := g.systemPrompt
|
||||
if sysPrompt == "" {
|
||||
sysPrompt = "You are an expert technical resume screener. Respond STRICTLY with valid JSON matching {\"ai_score\": 85.0, \"is_qualified\": true, \"summary\": \"...\", \"matched_highlights\": [...], \"missing_highlights\": [...], \"tailoring_suggestions\": [...]}."
|
||||
}
|
||||
|
||||
Job Title: %s
|
||||
Job Content:
|
||||
%s
|
||||
|
||||
Respond STRICTLY with valid JSON matching the format:
|
||||
{"matched_highlights": [...], "missing_highlights": [...], "tailoring_suggestions": [...]}`, jobTitle, jobTitle, rawContent)
|
||||
userPrompt := g.buildUserPrompt(jobTitle, rawContent)
|
||||
fullPrompt := fmt.Sprintf("%s\n\n%s", sysPrompt, userPrompt)
|
||||
|
||||
reqBody := map[string]interface{}{
|
||||
"contents": []map[string]interface{}{
|
||||
{
|
||||
"parts": []map[string]string{
|
||||
{"text": prompt},
|
||||
{"text": fullPrompt},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -102,5 +111,15 @@ Respond STRICTLY with valid JSON matching the format:
|
||||
return nil, fmt.Errorf("failed to parse Gemini JSON output: %w", err)
|
||||
}
|
||||
|
||||
if output.AIScore < 0 {
|
||||
output.AIScore = 0
|
||||
} else if output.AIScore > 100 {
|
||||
output.AIScore = 100
|
||||
}
|
||||
|
||||
if output.AIScore >= 60.0 {
|
||||
output.IsQualified = true
|
||||
}
|
||||
|
||||
return &output, nil
|
||||
}
|
||||
|
||||
@@ -10,11 +10,13 @@ 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{},
|
||||
AIScore: 75.0,
|
||||
IsQualified: true,
|
||||
Summary: "Mock analysis: relevant systems and distributed infrastructure role.",
|
||||
MatchedHighlights: []string{"Golang", "Distributed Systems", "Kubernetes"},
|
||||
MissingHighlights: []string{},
|
||||
TailoringSuggestions: []string{},
|
||||
TailoringSuggestions: []string{"Highlight Google GKE runtime experience."},
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user