-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (193 loc) · 5.96 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"context"
"database/sql"
_ "embed"
"flag"
"fmt"
"github.com/MatusOllah/slogcolor"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/mattn/go-sqlite3"
"github.com/newhook/whoishiring/queries"
"github.com/pkg/errors"
slogecho "github.com/samber/slog-echo"
"golang.org/x/sync/errgroup"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
var (
whoIsHiring = "Ask HN: Who is hiring?%"
whoWantsToBeHired = "Ask HN: Who wants to be hired?%"
)
const (
MaxWindow = 6
)
//go:embed banner
var banner string
//go:embed schema.sql
var ddl string
var (
fake = flag.Bool("fake", false, "use fake data")
completionModel = flag.String("completion", Claude, "completion model")
embeddingModel = flag.String("embedding", OpenAI3Small, "embedding model")
)
func main() {
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
//l := slog.New(slog.NewJSONHandler(os.Stdout, nil))
l := slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions))
if err := run(ctx, l); err != nil {
l.Error("failed", slog.String("error", err.Error()))
os.Exit(1)
}
}
func run(ctx context.Context, l *slog.Logger) error {
fmt.Println(banner)
if err := ValidateEmbeddingModel(*embeddingModel); err != nil {
return err
}
if err := ValidateCompletionModel(*completionModel); err != nil {
return err
}
var err error
dbPath := "./whoishiring.db"
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return errors.WithStack(err)
}
defer db.Close()
l.Info("database opened", slog.String("path", dbPath))
if _, err := db.ExecContext(ctx, ddl); err != nil {
return errors.WithStack(err)
}
q := queries.New(db)
if err := FetchPosts(ctx, l, q); err != nil {
return err
}
if err := CreateEmbeddings(ctx, l, q, *embeddingModel); err != nil {
return err
}
//if err := PrintTokens(ctx); err != nil {
// return err
//}
e := echo.New()
e.Use(slogecho.New(l))
// For debugging this one is a pain.
//e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
// Skipper: middleware.DefaultSkipper,
// ErrorMessage: "custom timeout error message returns to client",
// OnTimeoutRouteErrorHandler: func(err error, c echo.Context) {
// l.Error("timeout", slog.String("path", c.Path()), slog.String("error", err.Error()))
// },
// Timeout: 30 * time.Second,
//}))
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"}, // Allow all origins
AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE}, // Specify allowed methods
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
e.POST("/jobs", func(c echo.Context) error {
if err := c.Request().ParseMultipartForm(32 << 20); err != nil { // 32 MB max memory
return err
}
monthsParam := c.FormValue("months")
prompt := c.FormValue("prompt")
searchType := c.FormValue("type")
linkedin := c.FormValue("linkedin")
form, err := c.MultipartForm()
if err != nil {
log.Println(err.Error())
return err
}
var terms SearchTerms
for _, files := range form.File {
if len(files) != 1 {
return c.String(http.StatusBadRequest, "Invalid number of files")
}
file := files[0]
f, err := file.Open()
if err != nil {
return err
}
defer f.Close()
terms.ResumeName = file.Filename
terms.Resume = f
terms.Size = file.Size
}
terms.Months, err = strconv.Atoi(monthsParam)
if err != nil {
return c.String(http.StatusBadRequest, "Invalid months parameter")
}
terms.LinkedIn = linkedin
terms.JobPrompt = prompt
if searchType == "hiring" {
terms.SearchType = SearchType_WhoIsHiring
} else if searchType == "seekers" {
terms.SearchType = SearchType_WhoWantToBeHired
}
resp, err := JobSearch(c.Request().Context(), l, q, terms)
if err != nil {
l.Error("job search failed", slog.String("error", err.Error()))
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
var links []string
for _, id := range resp.Comments {
links = append(links, fmt.Sprintf("https://news.ycombinator.com/item?id=%d", id))
}
var originalLinks []string
for _, id := range resp.OriginalComments {
originalLinks = append(originalLinks, fmt.Sprintf("https://news.ycombinator.com/item?id=%d", id))
}
return c.JSON(http.StatusOK, map[string]any{
"comments": resp.Comments,
"parents": resp.Parents,
"items": resp.Items,
"original_comments": resp.OriginalComments,
"original_parents": resp.OriginalParents,
"hacker_news_links": links,
"original_hacker_news_links": originalLinks,
"resume_summary": resp.ResumeSummary,
"search_terms": resp.SearchTerms,
"total_posts": resp.TotalPosts,
"total_items": resp.TotalItems,
"posts": resp.Posts,
"items_searched": resp.ItemsSearched,
"latencies": resp.Latencies,
})
})
ln, err := net.Listen("tcp", ":8080")
if err != nil {
return errors.WithStack(err)
}
defer ln.Close()
s := http.Server{
Handler: e,
//ReadTimeout: 30 * time.Second, // customize http.Server timeouts
}
l.Info("using configuration", slog.String("embedding", *embeddingModel), slog.String("completion", *completionModel))
l.Info("starting http server", slog.String("addr", ln.Addr().String()))
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
if err := s.Serve(ln); !errors.Is(err, http.ErrServerClosed) {
return errors.WithStack(err)
}
return nil
})
<-ctx.Done()
// start gracefully shutdown with a timeout of 10 seconds.
ctx, cancelGC := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelGC()
if err := s.Shutdown(ctx); err != nil {
return errors.WithStack(err)
}
return g.Wait()
}