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.
-
#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.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/vector_amp/datasets.rb', line 239 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, metadata_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.
207 208 209 210 211 212 213 214 215 |
# File 'lib/vector_amp/datasets.rb', line 207 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.
134 135 136 |
# File 'lib/vector_amp/datasets.rb', line 134 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.
222 223 224 225 226 |
# File 'lib/vector_amp/datasets.rb', line 222 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.
198 199 200 |
# File 'lib/vector_amp/datasets.rb', line 198 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.
125 126 127 |
# File 'lib/vector_amp/datasets.rb', line 125 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 |
#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.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/vector_amp/datasets.rb', line 159 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 |