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
|
# File 'lib/rager/search/providers/jina.rb', line 21
def search(query, options)
api_key = options.api_key || ENV["JINA_API_KEY"]
raise Rager::Errors::CredentialsError.new("Jina", env_var: ["JINA_API_KEY"]) if api_key.nil?
params = {"q" => query}
= {
"Accept" => "application/json",
"X-Return-Format" => "markdown"
}
["Authorization"] = "Bearer #{api_key}" if api_key
request = Rager::Http::Request.new(
verb: Rager::Http::Verb::Get,
url: "https://s.jina.ai/?#{URI.encode_www_form(params)}",
headers: ,
timeout: options.timeout || Rager.config.timeout
)
response = Rager.config.http_adapter.make_request(request)
response_body = T.cast(T.must(response.body), String)
raise Rager::Errors::HttpError.new(Rager.config.http_adapter, request.url, response.status, body: response_body) if response.status != 200
parsed_response = JSON.parse(response_body)
search_results = parsed_response["data"] || []
search_results = search_results.first(options.n) if options.n
urls = search_results.map { |r| r["url"] }
titles = search_results.map { |r| r["title"] }
contents = search_results.map { |r|
content = r["content"] || r["description"]
options.max_length ? content&.slice(0, options.max_length) : content
}
Rager::Search::Output.new(
urls: urls,
titles: titles,
contents: contents
)
end
|