Class: EmbeddingUtil::Providers::Endpoint

Inherits:
EmbeddingUtil::Provider show all
Defined in:
lib/embedding_util/providers/endpoint.rb

Constant Summary collapse

NETWORK_ERRORS =
[
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::ENETUNREACH,
  EOFError,
  IOError,
  Net::OpenTimeout,
  Net::ReadTimeout,
  SocketError,
  Timeout::Error
].freeze

Instance Attribute Summary

Attributes inherited from EmbeddingUtil::Provider

#config

Instance Method Summary collapse

Methods inherited from EmbeddingUtil::Provider

#initialize, provider_name, #provider_name, supported?

Constructor Details

This class inherits a constructor from EmbeddingUtil::Provider

Instance Method Details

#embed(texts, profile: config.resolved_profile) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/embedding_util/providers/endpoint.rb', line 39

def embed(texts, profile: config.resolved_profile)
  endpoint = require_endpoint(config.embedding_endpoint_url, "embedding")
  response = post_json(endpoint, "/v1/embeddings", {
                         input: texts,
                         model: profile.embedding.fetch(:model)
                       })

  data = Array(response.fetch("data"))
  embeddings = data.sort_by { |item| item.fetch("index", data.index(item) || 0) }.map { |item| item.fetch("embedding") }
  EmbeddingResult.new(
    embedding: embeddings,
    model: response["model"],
    profile: profile.name,
    provider: provider_name,
    metadata: { usage: response["usage"] }.compact
  )
end

#rerank(query, documents, profile: config.resolved_profile) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/embedding_util/providers/endpoint.rb', line 57

def rerank(query, documents, profile: config.resolved_profile)
  endpoint = require_endpoint(config.reranker_endpoint_url, "reranker")
  response = begin
    post_json(endpoint, "/v1/rerank", rerank_payload(query, documents, profile))
  rescue EndpointNotFoundError => e
    raise unless fallback_rerank_not_found?(e)

    post_json(endpoint, "/rerank", rerank_payload(query, documents, profile))
  end

  RerankResult.new(
    results: ranked_documents(response, documents),
    model: response["model"],
    profile: profile.name,
    provider: provider_name,
    metadata: { usage: response["usage"] }.compact
  )
end

#supportObject



30
31
32
33
34
35
36
37
# File 'lib/embedding_util/providers/endpoint.rb', line 30

def support
  {
    provider: provider_name,
    supported: supported?,
    embedding_endpoint: config.embedding_endpoint_url,
    reranker_endpoint: config.reranker_endpoint_url
  }
end

#supported?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/embedding_util/providers/endpoint.rb', line 26

def supported?
  !!(config.embedding_endpoint_url || config.reranker_endpoint_url)
end