-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletions.go
136 lines (124 loc) · 4.08 KB
/
completions.go
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
package main
import (
"context"
_ "embed"
"github.com/newhook/whoishiring/claude"
"github.com/newhook/whoishiring/openai"
"github.com/pkg/errors"
"text/template"
)
//go:embed prompts/job_search.tmpl
var jobSearchPrompt string
//go:embed prompts/search_terms.tmpl
var searchTermsPrompt string
//go:embed prompts/analyze_resume.tmpl
var analyzeResumePrompt string
var (
analyzeResumeTemplate = template.Must(template.New("analyze_resume").Parse(analyzeResumePrompt))
searchTermsTemplate = template.Must(template.New("search_terms").Parse(searchTermsPrompt))
jobSearchTemplate = template.Must(template.New("jobSearch").Parse(jobSearchPrompt))
)
type Completion struct {
Model string
AnalyzeResume func(ctx context.Context, context any) (string, error)
GetTerms func(ctx context.Context, context any) ([]string, error)
GetJobs func(ctx context.Context, context any) ([]string, error)
}
const (
Claude = "claude"
OpenAI = "openai"
)
var completions = map[string]Completion{
Claude: {
Model: Claude,
AnalyzeResume: func(ctx context.Context, context any) (string, error) {
var term string
resp, err := claude.Completions(ctx, "resume", *fake, analyzeResumeTemplate, context)
if err != nil {
return "", errors.WithStack(err)
}
choice := resp.Content[len(resp.Content)-1]
if err := claude.ParseJsonResponse(choice.Text, &term); err != nil {
return "", errors.WithStack(err)
}
return term, nil
},
GetTerms: func(ctx context.Context, context any) ([]string, error) {
var terms []string
resp, err := claude.Completions(ctx, "terms", *fake, searchTermsTemplate, context)
if err != nil {
return nil, errors.WithStack(err)
}
choice := resp.Content[len(resp.Content)-1]
if err := claude.ParseJsonResponse(choice.Text, &terms); err != nil {
return nil, errors.WithStack(err)
}
return terms, nil
},
GetJobs: func(ctx context.Context, context any) ([]string, error) {
var jobIDs []string
r2, err := claude.Completions(ctx, "job_search", *fake, jobSearchTemplate, context)
if err != nil {
return nil, errors.WithStack(err)
}
choice := r2.Content[len(r2.Content)-1]
if err := claude.ParseJsonResponse(choice.Text, &jobIDs); err != nil {
return nil, errors.WithStack(err)
}
return jobIDs, nil
},
},
OpenAI: {
Model: OpenAI,
AnalyzeResume: func(ctx context.Context, context any) (string, error) {
var term string
resp, err := openai.Completions(ctx, "terms", *fake, analyzeResumeTemplate, context)
if err != nil {
return "", errors.WithStack(err)
}
choice := resp.Choices[len(resp.Choices)-1]
if err := openai.ParseJsonResponse(choice, &term); err != nil {
return "", errors.WithStack(err)
}
return term, nil
},
GetTerms: func(ctx context.Context, context any) ([]string, error) {
var terms []string
resp, err := openai.Completions(ctx, "terms", *fake, searchTermsTemplate, context)
if err != nil {
return nil, errors.WithStack(err)
}
choice := resp.Choices[len(resp.Choices)-1]
if err := openai.ParseJsonResponse(choice, &terms); err != nil {
return nil, errors.WithStack(err)
}
return terms, nil
},
GetJobs: func(ctx context.Context, context any) ([]string, error) {
var jobIDs []string
r2, err := openai.Completions(ctx, "job_search", *fake, jobSearchTemplate, context)
if err != nil {
return nil, errors.WithStack(err)
}
if err := openai.ParseJsonResponse(r2.Choices[len(r2.Choices)-1], &jobIDs); err != nil {
return nil, errors.WithStack(err)
}
return jobIDs, nil
},
},
}
func ValidateCompletionModel(s string) error {
if _, ok := completions[s]; ok {
return nil
}
return errors.Errorf("invalid completion model: %s", s)
}
func AnalyzeResume(ctx context.Context, context any) (string, error) {
return completions[*completionModel].AnalyzeResume(ctx, context)
}
func GetTerms(ctx context.Context, context any) ([]string, error) {
return completions[*completionModel].GetTerms(ctx, context)
}
func GetJobs(ctx context.Context, context any) ([]string, error) {
return completions[*completionModel].GetJobs(ctx, context)
}