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
|
# 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?
base_url = "https://s.jina.ai"
url = "#{base_url}/?#{URI.encode_www_form({"q" => query})}"
= options.
url = "#{url}&page=#{pagination}" if && > 1
= {
"Accept" => "application/json",
"X-Return-Format" => "markdown"
}.tap do |h|
h["Authorization"] = "Bearer #{api_key}" if api_key
end
request = Rager::Http::Request.new(
verb: Rager::Http::Verb::Get,
url: url,
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
search_results.map do |r|
content = r["content"] || r["description"]
content = content&.slice(0, options.max_length) if options.max_length
Rager::Search::Result.new(
url: r["url"],
title: r["title"],
content: content
)
end
end
|