@@ -2,6 +2,8 @@ package fetcher
22
33import (
44 "context"
5+ "fmt"
6+ "sync"
57
68 "github.com/tmc/langchaingo/llms"
79)
@@ -14,6 +16,96 @@ type ExecuteLLMSearchFunc func(ctx context.Context, llm llms.Model, prompt strin
1416type ExecuteLLMWebSearchFunc func (ctx context.Context , appCfg * AppConfig , prompt string ) ([]Job , error )
1517type EnrichJobIdentityFunc func (ctx context.Context , llm llms.Model , job Job , page JobIdentityPage ) (* JobIdentityEnrichment , LLMTokenUsage , error )
1618
19+ type LLMService interface {
20+ InitConfiguredLLM (ctx context.Context , appCfg * AppConfig , task string ) (llms.Model , func (), error )
21+ ExecuteSearch (ctx context.Context , llm llms.Model , prompt string ) ([]Job , error )
22+ ExecuteWebSearch (ctx context.Context , appCfg * AppConfig , prompt string ) ([]Job , error )
23+ EnrichJobIdentity (ctx context.Context , llm llms.Model , job Job , page JobIdentityPage ) (* JobIdentityEnrichment , LLMTokenUsage , error )
24+ IsAvailable (ctx context.Context , task string ) bool
25+ }
26+
27+ type llmServiceContextKey struct {}
28+
29+ var contextKey = llmServiceContextKey {}
30+
31+ func WithLLMService (ctx context.Context , service LLMService ) context.Context {
32+ return context .WithValue (ctx , contextKey , service )
33+ }
34+
35+ var (
36+ globalServiceMutex sync.RWMutex
37+ globalLLMService LLMService
38+ )
39+
40+ func RegisterLLMService (service LLMService ) {
41+ globalServiceMutex .Lock ()
42+ defer globalServiceMutex .Unlock ()
43+ globalLLMService = service
44+ }
45+
46+ func getLLMService (ctx context.Context ) LLMService {
47+ if svc , ok := ctx .Value (contextKey ).(LLMService ); ok {
48+ return svc
49+ }
50+ globalServiceMutex .RLock ()
51+ defer globalServiceMutex .RUnlock ()
52+ if globalLLMService != nil {
53+ return globalLLMService
54+ }
55+ return fallbackLLMService {}
56+ }
57+
58+ type fallbackLLMService struct {}
59+
60+ func (fallbackLLMService ) InitConfiguredLLM (ctx context.Context , appCfg * AppConfig , task string ) (llms.Model , func (), error ) {
61+ if fetchAllJobsInitConfiguredLLM != nil {
62+ return fetchAllJobsInitConfiguredLLM (ctx , appCfg , task )
63+ }
64+ return nil , nil , fmt .Errorf ("LLM init hook not configured" )
65+ }
66+
67+ func (fallbackLLMService ) ExecuteSearch (ctx context.Context , llm llms.Model , prompt string ) ([]Job , error ) {
68+ if fetchAllJobsExecuteLLMSearch != nil {
69+ return fetchAllJobsExecuteLLMSearch (ctx , llm , prompt )
70+ }
71+ return nil , fmt .Errorf ("LLM search hook not configured" )
72+ }
73+
74+ func (fallbackLLMService ) ExecuteWebSearch (ctx context.Context , appCfg * AppConfig , prompt string ) ([]Job , error ) {
75+ if fetchAllJobsExecuteLLMWebSearch != nil {
76+ return fetchAllJobsExecuteLLMWebSearch (ctx , appCfg , prompt )
77+ }
78+ if fetchAllJobsInitConfiguredLLM == nil || fetchAllJobsExecuteLLMSearch == nil {
79+ return nil , fmt .Errorf ("LLM web search hook not configured" )
80+ }
81+ llm , restoreAuth , err := fetchAllJobsInitConfiguredLLM (ctx , appCfg , llmTaskJobSearch )
82+ if err != nil {
83+ return nil , fmt .Errorf ("failed to initialize: %w" , err )
84+ }
85+ defer restoreAuth ()
86+ return fetchAllJobsExecuteLLMSearch (ctx , llm , prompt )
87+ }
88+
89+ func (fallbackLLMService ) EnrichJobIdentity (ctx context.Context , llm llms.Model , job Job , page JobIdentityPage ) (* JobIdentityEnrichment , LLMTokenUsage , error ) {
90+ if fetchAllJobsEnrichJobIdentity != nil {
91+ return fetchAllJobsEnrichJobIdentity (ctx , llm , job , page )
92+ }
93+ return nil , LLMTokenUsage {}, fmt .Errorf ("LLM identity enrichment hook not configured" )
94+ }
95+
96+ func (fallbackLLMService ) IsAvailable (ctx context.Context , task string ) bool {
97+ if task == "llm_web_search" {
98+ return fetchAllJobsExecuteLLMWebSearch != nil || (fetchAllJobsInitConfiguredLLM != nil && fetchAllJobsExecuteLLMSearch != nil )
99+ }
100+ if task == "llm_job_search" {
101+ return fetchAllJobsInitConfiguredLLM != nil && fetchAllJobsExecuteLLMSearch != nil
102+ }
103+ if task == "llm_job_enrichment" {
104+ return fetchAllJobsInitConfiguredLLM != nil && fetchAllJobsEnrichJobIdentity != nil
105+ }
106+ return false
107+ }
108+
17109var (
18110 fetchAllJobsInitConfiguredLLM InitLLMFunc
19111 fetchAllJobsExecuteLLMSearch ExecuteLLMSearchFunc
0 commit comments