Module: SmartPrompt::SiliconFlow::Rerank

Included in:
SmartPrompt::SiliconFlowAdapter
Defined in:
lib/smart_prompt/adapters/siliconflow/rerank.rb

Overview

Rerank (reorder documents by relevance to a query).

Instance Method Summary collapse

Instance Method Details

#rerank(query, documents, model: nil, **opts) ⇒ Object

Reorder documents by relevance to a query. SiliconFlow returns results[].relevance_score (NOT “score”). Returns an Array of relevance_score: sorted by the provider.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/smart_prompt/adapters/siliconflow/rerank.rb', line 8

def rerank(query, documents, model: nil, **opts)
  model_name = model || @config["rerank_model"] || @config["model"]
  SmartPrompt.logger.info "SiliconFlowAdapter: rerank model=#{model_name}"

  body = { "model" => model_name, "query" => query.to_s, "documents" => documents }
  body["top_n"]                = opts[:top_n]                if opts[:top_n]
  body["return_documents"]     = opts[:return_documents]     unless opts[:return_documents].nil?
  body["max_chunks_per_doc"]   = opts[:max_chunks_per_doc]   if opts[:max_chunks_per_doc]
  body["chunk_overlap_tokens"] = opts[:chunk_overlap_tokens] if opts[:chunk_overlap_tokens]
  body["instruction"]          = opts[:instruction]          if opts[:instruction]

  response =
    begin
      http_post_json("#{@base_url}/rerank", body)
    rescue LLMAPIError, Error
      raise
    rescue => e
      raise LLMAPIError, "Failed to call SiliconFlow rerank: #{e.message}"
    end

  parse_rerank_response(response)
end