jobs-monorepo/internal/pkg/openai
Elshimy Ziad Magdy Taha 5060643a3f infrastructure fixes
2025-11-03 00:29:33 +05:00
..
go.mod infrastructure fixes 2025-11-03 00:29:33 +05:00
go.sum infrastructure fixes 2025-11-03 00:29:33 +05:00
interface.go infrastructure fixes 2025-11-03 00:29:33 +05:00
openai.go infrastructure fixes 2025-11-03 00:29:33 +05:00
README.md infrastructure fixes 2025-11-03 00:29:33 +05:00

OpenAI Service

This package provides an OpenAI-powered service for job analysis and CV generation using the official OpenAI Go SDK.

Features

  • Job Analysis: Analyze job descriptions against CVs to determine fit and provide recommendations
  • CV Generation: Create tailored CVs based on job descriptions
  • Cover Letter Generation: Generate personalized cover letters

Setup

  1. Install the OpenAI Go SDK:
go get github.com/sashabaranov/go-openai
  1. Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-api-key-here"
  1. (Optional) Set a custom base URL:
export OPENAI_BASE_URL="https://your-custom-endpoint.com/v1"

Usage

Basic Job Analysis

package main

import (
    "fmt"
    "os"
    
    "github.com/jobs-scraper/internal/pkg/domain"
    "github.com/jobs-scraper/internal/pkg/openai"
)

func main() {
    // Create service
    apiKey := os.Getenv("OPENAI_API_KEY")
    service := openai.NewOpenAIService(apiKey, "gpt-4o-mini")
    
    // Prepare job description
    jobDesc := domain.JobDescription{
        JobID:       1,
        Description: "Looking for a Go developer...",
        Criteria: map[string]string{
            "experience": "5+ years",
            "languages":  "Go, JavaScript",
        },
    }
    
    // Analyze job
    result, err := service.AnalyzeJobDescription(cv, jobDesc)
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Should apply: %t\n", result.ShouldApply())
    fmt.Printf("Confidence: %d%%\n", result.ConfidenceScore)
}

Using Custom Base URL

You can configure a custom base URL in two ways:

Method 1: Environment Variable

export OPENAI_BASE_URL="https://api.openai-proxy.com/v1"
# Service will automatically use this URL
service := openai.NewOpenAIService(apiKey, "gpt-4o-mini")

Method 2: Direct Parameter

// Use a custom base URL directly
service := openai.NewOpenAIServiceWithBaseURL(apiKey, "gpt-4o-mini", "https://api.openai-proxy.com/v1")

// Or use Azure OpenAI
service := openai.NewOpenAIServiceWithBaseURL(apiKey, "gpt-4", "https://your-resource.openai.azure.com/")

Generate Tailored CV

tailoredCV, err := service.GenerateCV(originalCV, jobDesc)
if err != nil {
    panic(err)
}
fmt.Println(tailoredCV)

Generate Cover Letter

coverLetter, err := service.GenerateCoverLetter(cv, jobDesc, "Tech Company Inc")
if err != nil {
    panic(err)
}
fmt.Println(coverLetter)

Models

The service supports all OpenAI models. Common choices:

  • gpt-4o-mini - Cost-effective, good performance (default)
  • gpt-4o - Higher quality, more expensive
  • gpt-3.5-turbo - Fastest, most cost-effective

Environment Variables

  • OPENAI_API_KEY - Your OpenAI API key (required)
  • OPENAI_BASE_URL - Custom base URL for OpenAI API (optional)
  • OPENAI_MODEL - Default model to use (optional)

Response Structure

JobAnalysisResult

type JobAnalysisResult struct {
    Recommendation         string   // "apply" or "do_not_apply"
    ConfidenceScore        int      // 0-100
    MatchingSkills         []string
    MissingSkills          []string
    ExperienceMatch        string   // "excellent", "good", "fair", "poor"
    Summary                string
    ImprovementSuggestions []string
}

Error Handling

The service handles common errors:

  • API authentication issues
  • Rate limiting
  • Invalid JSON responses
  • Network timeouts

Always check for errors when calling service methods.

Testing

Run the example test:

cd internal/pkg/openai
go test -v

Make sure to set OPENAI_API_KEY environment variable before running tests that make actual API calls.