Class: ReplicateClient::Model::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/replicate-client/model/version.rb

Constant Summary collapse

INDEX_PATH =
"/versions"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, client: ReplicateClient.client) ⇒ Version

Returns a new instance of Version.



117
118
119
120
121
122
123
# File 'lib/replicate-client/model/version.rb', line 117

def initialize(attributes, client: ReplicateClient.client)
  @client = client
  @id = attributes["id"]
  @created_at = Time.parse(attributes["created_at"])
  @cog_version = attributes["cog_version"]
  @openapi_schema = attributes["openapi_schema"]
end

Instance Attribute Details

#clientReplicateClient::Client

The client used to make API requests for this model version.



115
116
117
# File 'lib/replicate-client/model/version.rb', line 115

def client
  @client
end

#cog_versionString

The cog version of the model version.

Returns:

  • (String)


105
106
107
# File 'lib/replicate-client/model/version.rb', line 105

def cog_version
  @cog_version
end

#created_atTime

The date the model version was created.

Returns:

  • (Time)


100
101
102
# File 'lib/replicate-client/model/version.rb', line 100

def created_at
  @created_at
end

#idString

The ID of the model version.

Returns:

  • (String)


95
96
97
# File 'lib/replicate-client/model/version.rb', line 95

def id
  @id
end

#openapi_schemaHash

The OpenAPI schema of the model version.

Returns:

  • (Hash)


110
111
112
# File 'lib/replicate-client/model/version.rb', line 110

def openapi_schema
  @openapi_schema
end

Class Method Details

.auto_paging_each(owner:, name:, client: ReplicateClient.client) {|ReplicateClient::Model| ... } ⇒ void

This method returns an undefined value.

Paginate through all models.

Parameters:

  • name (String)

    The name of the model.

  • owner (String)

    The owner of the model.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Yields:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/replicate-client/model/version.rb', line 62

def auto_paging_each(owner:, name:, client: ReplicateClient.client, &block)
  cursor = nil
  model_path = Model.build_path(owner: owner, name: name)

  loop do
    url_params = cursor ? "?cursor=#{cursor}" : ""
    attributes = client.get("#{model_path}#{INDEX_PATH}#{url_params}")

    versions = attributes["results"].map { |version| new(version, client: client) }

    versions.each(&block)

    cursor = attributes["next"] ? URI.decode_www_form(URI.parse(attributes["next"]).query).to_h["cursor"] : nil
    break if cursor.nil?
  end
end

.build_path(owner:, name:, version_id:) ⇒ String

Build the path for the model version.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

  • version_id (String)

    The version id of the model.

Returns:

  • (String)


86
87
88
89
# File 'lib/replicate-client/model/version.rb', line 86

def build_path(owner:, name:, version_id:)
  model_path = Model.build_path(owner: owner, name: name)
  "#{model_path}#{INDEX_PATH}/#{version_id}"
end

.find_by(owner:, name:, version_id:, client: ReplicateClient.client) ⇒ ReplicateClient::Model::Version

Find a version of a model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

  • version_id (String)

    The version id of the model.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



31
32
33
34
35
# File 'lib/replicate-client/model/version.rb', line 31

def find_by(owner:, name:, version_id:, client: ReplicateClient.client)
  find_by!(owner: owner, name: name, version_id: version_id, client: client)
rescue ReplicateClient::NotFoundError
  nil
end

.find_by!(owner:, name:, version_id:, client: ReplicateClient.client) ⇒ ReplicateClient::Model::Version

Find a version of a model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

  • version_id (String)

    The version id of the model.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



17
18
19
20
21
# File 'lib/replicate-client/model/version.rb', line 17

def find_by!(owner:, name:, version_id:, client: ReplicateClient.client)
  path = build_path(owner: owner, name: name, version_id: version_id)
  response = client.get(path)
  new(response, client: client)
end

.where(owner:, name:, client: ReplicateClient.client) ⇒ Array<ReplicateClient::Model::Version>

Get all versions of a model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



44
45
46
47
48
49
50
51
52
# File 'lib/replicate-client/model/version.rb', line 44

def where(owner:, name:, client: ReplicateClient.client)
  versions = []

  auto_paging_each(owner: owner, name: name, client: client) do |version|
    versions << version
  end

  versions
end

Instance Method Details

#create_prediction!(input:, webhook_url: nil, webhook_events_filter: nil) ⇒ ReplicateClient::Prediction

Create a new prediction.

Parameters:

  • input (Hash)

    The input data for the prediction.

  • webhook_url (String, nil) (defaults to: nil)

    A URL to receive webhook notifications.

  • webhook_events_filter (Array, nil) (defaults to: nil)

    The events to trigger webhook requests.

Returns:



132
133
134
135
136
137
138
139
140
# File 'lib/replicate-client/model/version.rb', line 132

def create_prediction!(input:, webhook_url: nil, webhook_events_filter: nil)
  Prediction.create!(
    version: self,
    input: input,
    webhook_url: webhook_url,
    webhook_events_filter: webhook_events_filter,
    client: @client
  )
end

#prediction_input_schemaHash

Get the prediction input schema from the openapi schema.

Returns:

  • (Hash)

    The prediction input schema.



145
146
147
# File 'lib/replicate-client/model/version.rb', line 145

def prediction_input_schema
  resolve_ref(openapi_schema.dig("components", "schemas", "PredictionRequest", "properties", "input"))
end

#training_input_schemaHash

Get the training input schema from the openapi schema.

Returns:

  • (Hash)

    The training input schema.



152
153
154
# File 'lib/replicate-client/model/version.rb', line 152

def training_input_schema
  resolve_ref(openapi_schema.dig("components", "schemas", "TrainingRequest", "properties", "input"))
end