Class: DSPy::DeepSearch::Clients::ExaClient

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/deep_search/clients/exa_client.rb

Defined Under Namespace

Classes: ApiError, ConfigurationError, Content, Error, Result

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ ExaClient

Returns a new instance of ExaClient.



29
30
31
# File 'lib/dspy/deep_search/clients/exa_client.rb', line 29

def initialize(client: nil)
  @client = T.let(client || build_client, ::Exa::Client)
end

Instance Method Details

#contents(urls:, **options) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/dspy/deep_search/clients/exa_client.rb', line 69

def contents(urls:, **options)
  raise ArgumentError, "urls must not be empty" if urls.empty?

  defaults = {
    text: true,
    summary: true,
    highlights: true,
    filter_empty_results: true
  }

  payload = Exa::Types::ContentsRequest.new(**defaults.merge(options).merge(urls: urls)).to_payload

  raw_response = with_api_errors do
    client.request(
      method: :post,
      path: "contents",
      body: payload,
      response_model: nil
    )
  end

  symbolized = symbolize_keys(raw_response)

  check_content_statuses!(symbolized)

  Array(symbolized[:results]).each_with_index.filter_map do |result, index|
    result = symbolize_keys(result)
    url = result[:url] || urls[index]
    next if url.nil?

    Content.new(
      url: url,
      text: result[:text],
      summary: result[:summary],
      highlights: normalize_highlights(result[:highlights])
    )
  end
end

#search(query:, num_results: 5, autoprompt: true) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dspy/deep_search/clients/exa_client.rb', line 40

def search(query:, num_results: 5, autoprompt: true)
  response = with_api_errors do
    client.search.search(
      query: query,
      num_results: num_results,
      use_autoprompt: autoprompt,
      summary: true
    )
  end

  response.results.filter_map do |result|
    next if result.url.nil?

    Result.new(
      url: result.url,
      title: result.title,
      summary: result.summary,
      highlights: normalize_highlights(result.highlights),
      score: result.score
    )
  end
end