made jina api key optional

This commit is contained in:
Noam Siegel 2024-09-16 11:44:21 -07:00
parent 6f116ca527
commit 4534ef6544

View File

@ -25,8 +25,8 @@ func NewJinaClient() *JinaClient {
EnvNamePrefix: common.BuildEnvVariablePrefix(label), EnvNamePrefix: common.BuildEnvVariablePrefix(label),
}, },
} }
client.ApiKey = client.AddSetupQuestion("API Key", true) client.ApiKey = client.AddSetupQuestion("API Key", false)
return client return client
} }
// return the main content of a webpage in clean, LLM-friendly text. // return the main content of a webpage in clean, LLM-friendly text.
@ -37,10 +37,10 @@ func (jc *JinaClient) ScrapeURL(url string) (string, error) {
return "", fmt.Errorf("error creating request: %w", err) return "", fmt.Errorf("error creating request: %w", err)
} }
apiKey := jc.ApiKey.Value // if api keys exist, set the header
if apiKey := jc.ApiKey.Value; apiKey != "" {
// Set the Authorization header with the Bearer token req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Authorization", "Bearer " + apiKey) }
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
@ -57,20 +57,29 @@ func (jc *JinaClient) ScrapeURL(url string) (string, error) {
return string(body), nil return string(body), nil
} }
// search engine call that returns top-5 results with their URLs and contents, each in clean, LLM-friendly text.
func (jc *JinaClient) ScrapeQuestion(question string) (string, error) { func (jc *JinaClient) ScrapeQuestion(question string) (string, error) {
url := "https://s.jina.ai/" + question requestURL := "https://s.jina.ai/" + question
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
resp, err := http.Get(url) // if api keys exist, set the header
if err != nil { if apiKey := jc.ApiKey.Value; apiKey != "" {
return "", fmt.Errorf("error making GET request: %w", err) req.Header.Set("Authorization", "Bearer "+apiKey)
} }
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) client := &http.Client{}
if err != nil { resp, err := client.Do(req)
return "", fmt.Errorf("error reading response body: %w", err) if err != nil {
} return "", fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
return string(body), nil body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %w", err)
}
return string(body), nil
} }