Class: VectorAmp::DatasetsResource
- Inherits:
-
Object
- Object
- VectorAmp::DatasetsResource
- Defined in:
- lib/vector_amp/datasets.rb
Overview
Dataset API resource for create/list/get/search/insert operations.
Instance Method Summary collapse
-
#add_texts(dataset_id, texts_arg = nil, texts: nil, ids: nil, metadata: nil) ⇒ Hash
Embed and insert texts into a dataset.
-
#create(name:, dim: nil, embedding: nil, metric: "cosine", hybrid: nil, filters: nil, metadata_schema: nil, tuning: nil, metadata: nil, **unknown) ⇒ Dataset
Create a SABLE dataset.
-
#create_with_openai_api_key(name:, api_key:, size: "small", secret_ref: "emb:openai:api_key", validate: false, **options) ⇒ Dataset
Store/update an OpenAI API key in org secrets, then create an OpenAI-backed dataset whose embedding config references that secret.
-
#delete(dataset_id) ⇒ Hash
Delete a dataset by id.
-
#delete_vectors(dataset_id, ids:, write_concern: nil) ⇒ Hash
Delete one or more vectors from a dataset by id.
-
#download_document(dataset_id, document_id) ⇒ String
Download the retained original bytes for a dataset document.
-
#embed(dataset_id, text: nil, texts: nil) ⇒ Hash
Generate embeddings using the dataset embedding configuration.
-
#get(dataset_id) ⇒ Dataset
Fetch a dataset by id.
- #initialize(transport, client: nil) ⇒ DatasetsResource constructor
-
#insert(dataset_id, vectors:) ⇒ Hash
Insert vectors into a dataset.
-
#list(limit: 50, offset: 0) ⇒ Hash
List datasets.
-
#list_documents(dataset_id, limit: 50, cursor: nil, status: nil) ⇒ Hash
List retained source documents for a dataset using cursor pagination.
-
#patch_metadata_schema(dataset_id, schema) ⇒ Dataset
Add or update typed metadata fields, retaining omitted fields.
-
#replace_metadata_schema(dataset_id, schema) ⇒ Dataset
Replace the complete typed metadata schema.
-
#search(dataset_id, query_text_or_options = nil, query: nil, query_text: nil, search_text: nil, top_k: 10, filters: nil, advanced_filters: nil, embedding_model: nil, embedding_provider: nil, nprobe_override: nil, rerank_depth_override: nil, hybrid: nil, sparse_query: nil, alpha: nil, include_embeddings: nil, include_documents: nil, include_metadata: nil, rerank: nil, **unknown) ⇒ Hash
Search a dataset by text or vector query.
-
#stats(dataset_id) ⇒ Hash
Fetch dataset statistics.
Constructor Details
#initialize(transport, client: nil) ⇒ DatasetsResource
14 15 16 17 |
# File 'lib/vector_amp/datasets.rb', line 14 def initialize(transport, client: nil) @transport = transport @client = client end |
Instance Method Details
#add_texts(dataset_id, texts_arg = nil, texts: nil, ids: nil, metadata: nil) ⇒ Hash
Embed and insert texts into a dataset.
Accepts a single string or a list of strings. Ids are auto-generated when
omitted; supplied ids may be strings or integers, and integer ids are
preserved as JSON numbers. The source text is copied into metadata.text.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/vector_amp/datasets.rb', line 254 def add_texts(dataset_id, texts_arg = nil, texts: nil, ids: nil, metadata: nil) texts = Array(texts || texts_arg) raise ArgumentError, "texts must not be empty" if texts.empty? raise ArgumentError, "ids length must match texts length" if ids && ids.length != texts.length if .is_a?(Array) && .length != texts.length raise ArgumentError, "metadata length must match texts length" end response = (dataset_id, texts: texts) = response.fetch("embeddings") vectors = texts.each_with_index.map do |text, index| = .is_a?(Array) ? [index] : { id: ids ? Utils.coerce_vector_id(ids[index]) : SecureRandom.uuid, values: [index], metadata: Utils.compact_hash(( || {}).merge(text: text)) } end insert(dataset_id, vectors: vectors) end |
#create(name:, dim: nil, embedding: nil, metric: "cosine", hybrid: nil, filters: nil, metadata_schema: nil, tuning: nil, metadata: nil, **unknown) ⇒ Dataset
Create a SABLE dataset. index_type is managed by the SDK and always sent as sable.
Only name is required. The embedding defaults to the managed
vectoramp/VectorAmp-Embedding-4B model and the dimension is inferred
(2560 for the default model). Pass embedding: (a Hash or model String, or
the result of Embedding.openai) to use a different model; if
the model's dimension is not known to the SDK you must also pass dim:.
67 68 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 |
# File 'lib/vector_amp/datasets.rb', line 67 def create(name:, dim: nil, embedding: nil, metric: "cosine", hybrid: nil, filters: nil, metadata_schema: nil, tuning: nil, metadata: nil, **unknown) if unknown.key?(:index_type) || unknown.key?("index_type") raise ArgumentError, "index_type is managed by VectorAmp Ruby SDK and is always 'sable'" end Utils.ensure_no_unknown!(unknown, "create") = Embedding.normalize() resolved_dim = dim || Embedding.infer_dim() if resolved_dim.nil? model = [:model] || ["model"] raise ArgumentError, "dim is required for embedding model #{model.inspect}; pass dim: explicitly" end body = Utils.compact_hash( name: name, dim: resolved_dim, metric: metric, embedding: , index_type: "sable", hybrid: hybrid, filters: filters, schema: (), tuning: tuning, metadata: ) wrap_dataset(@transport.request(:post, "/datasets", body: body)) end |
#create_with_openai_api_key(name:, api_key:, size: "small", secret_ref: "emb:openai:api_key", validate: false, **options) ⇒ Dataset
Store/update an OpenAI API key in org secrets, then create an OpenAI-backed dataset whose embedding config references that secret.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vector_amp/datasets.rb', line 104 def create_with_openai_api_key(name:, api_key:, size: "small", secret_ref: "emb:openai:api_key", validate: false, **) secrets = @client&.org_secrets || begin require_relative "org_secrets" OrgSecretsResource.new(@transport) end secrets.put_openai_api_key( api_key: api_key, secret_ref: secret_ref, validate: validate, model: openai_model(size) ) create(name: name, embedding: Embedding.openai(size, secret_ref: secret_ref), **) end |
#delete(dataset_id) ⇒ Hash
Delete a dataset by id.
37 38 39 |
# File 'lib/vector_amp/datasets.rb', line 37 def delete(dataset_id) @transport.request(:delete, "/datasets/#{dataset_id}") end |
#delete_vectors(dataset_id, ids:, write_concern: nil) ⇒ Hash
Delete one or more vectors from a dataset by id.
222 223 224 225 226 227 228 229 230 |
# File 'lib/vector_amp/datasets.rb', line 222 def delete_vectors(dataset_id, ids:, write_concern: nil) raise ArgumentError, "ids must not be empty" if ids.nil? || ids.empty? @transport.request( :delete, "/datasets/#{dataset_id}/vectors", body: Utils.compact_hash(ids: ids, write_concern: write_concern) ) end |
#download_document(dataset_id, document_id) ⇒ String
Download the retained original bytes for a dataset document. The HTTP transport follows redirects so this returns the final raw object bytes.
149 150 151 |
# File 'lib/vector_amp/datasets.rb', line 149 def download_document(dataset_id, document_id) @transport.request(:get, "/datasets/#{dataset_id}/documents/#{document_id}/download", raw: true, headers: { Accept: "*/*" }) end |
#embed(dataset_id, text: nil, texts: nil) ⇒ Hash
Generate embeddings using the dataset embedding configuration.
237 238 239 240 241 |
# File 'lib/vector_amp/datasets.rb', line 237 def (dataset_id, text: nil, texts: nil) raise ArgumentError, "provide text or texts" if text.nil? && texts.nil? @transport.request(:post, "/datasets/#{dataset_id}/embed", body: Utils.compact_hash(text: text, texts: texts)) end |
#get(dataset_id) ⇒ Dataset
Fetch a dataset by id.
30 31 32 |
# File 'lib/vector_amp/datasets.rb', line 30 def get(dataset_id) wrap_dataset(@transport.request(:get, "/datasets/#{dataset_id}")) end |
#insert(dataset_id, vectors:) ⇒ Hash
Insert vectors into a dataset.
Vector record ids may be strings or integers. Integer ids are preserved as JSON numbers (not coerced to strings) so the API does not rewrite them.
213 214 215 |
# File 'lib/vector_amp/datasets.rb', line 213 def insert(dataset_id, vectors:) @transport.request(:post, "/datasets/#{dataset_id}/insert", body: { vectors: Utils.normalize_vectors(vectors) }) end |
#list(limit: 50, offset: 0) ⇒ Hash
List datasets.
23 24 25 |
# File 'lib/vector_amp/datasets.rb', line 23 def list(limit: 50, offset: 0) wrap_list(@transport.request(:get, "/datasets", query: { limit: limit, offset: offset })) end |
#list_documents(dataset_id, limit: 50, cursor: nil, status: nil) ⇒ Hash
List retained source documents for a dataset using cursor pagination.
140 141 142 |
# File 'lib/vector_amp/datasets.rb', line 140 def list_documents(dataset_id, limit: 50, cursor: nil, status: nil) @transport.request(:get, "/datasets/#{dataset_id}/documents", query: Utils.compact_hash(limit: limit, cursor: cursor, status: status)) end |
#patch_metadata_schema(dataset_id, schema) ⇒ Dataset
Add or update typed metadata fields, retaining omitted fields.
122 123 124 |
# File 'lib/vector_amp/datasets.rb', line 122 def (dataset_id, schema) (dataset_id, schema, "merge") end |
#replace_metadata_schema(dataset_id, schema) ⇒ Dataset
Replace the complete typed metadata schema.
130 131 132 |
# File 'lib/vector_amp/datasets.rb', line 130 def (dataset_id, schema) (dataset_id, schema, "replace") end |
#search(dataset_id, query_text_or_options = nil, query: nil, query_text: nil, search_text: nil, top_k: 10, filters: nil, advanced_filters: nil, embedding_model: nil, embedding_provider: nil, nprobe_override: nil, rerank_depth_override: nil, hybrid: nil, sparse_query: nil, alpha: nil, include_embeddings: nil, include_documents: nil, include_metadata: nil, rerank: nil, **unknown) ⇒ Hash
Search a dataset by text or vector query.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/vector_amp/datasets.rb', line 174 def search(dataset_id, = nil, query: nil, query_text: nil, search_text: nil, top_k: 10, filters: nil, advanced_filters: nil, embedding_model: nil, embedding_provider: nil, nprobe_override: nil, rerank_depth_override: nil, hybrid: nil, sparse_query: nil, alpha: nil, include_embeddings: nil, include_documents: nil, include_metadata: nil, rerank: nil, **unknown) if .is_a?(Hash) unknown = .merge(unknown) = nil end Utils.ensure_no_unknown!(unknown, "search") raise ArgumentError, "provide query_text or search_text, not both" if query_text && search_text resolved_query_text = query_text || search_text || body = Utils.compact_hash( query: query, query_text: resolved_query_text, top_k: top_k, filters: filters, advanced_filters: advanced_filters, embedding_model: , embedding_provider: , nprobe_override: nprobe_override, rerank_depth_override: rerank_depth_override, hybrid: hybrid, sparse_query: sparse_query, alpha: alpha, include_embeddings: , include_documents: include_documents, include_metadata: , rerank: rerank ) @transport.request(:post, "/datasets/#{dataset_id}/search", body: body) end |
#stats(dataset_id) ⇒ Hash
Fetch dataset statistics.
44 45 46 |
# File 'lib/vector_amp/datasets.rb', line 44 def stats(dataset_id) @transport.request(:get, "/datasets/#{dataset_id}/stats") end |