diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000..237c96dc5 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,2 @@ +## Typing +When adding or changing a class, make sure to modify the corresponding .rbs file in the `./sig/mindee` folder. diff --git a/bin/v2/parser.rb b/bin/v2/parser.rb index c1ca30e71..5d0703591 100644 --- a/bin/v2/parser.rb +++ b/bin/v2/parser.rb @@ -70,7 +70,7 @@ def execute # Parse the arguments and run the given product command. # @param command [String] - # @return [Array(Mindee::V2::Parsing::BaseResponse, String)] Response and its summary. + # @return [Array(Mindee::V2::Parsing::BaseInferenceResponse, String)] Response and its summary. def run_product(command) @product_parser[command].parse!(@arguments) @options[:file_path] = @arguments.shift diff --git a/lib/mindee/input/local_response.rb b/lib/mindee/input/local_response.rb index 211953a0a..2f1260a5e 100644 --- a/lib/mindee/input/local_response.rb +++ b/lib/mindee/input/local_response.rb @@ -70,8 +70,8 @@ def valid_hmac_signature?(secret_key, signature) end # Deserializes a loaded response - # @param response_class [Class] class to return. - # @return [V2::Parsing::JobResponse, Mindee::V2::Parsing::BaseResponse] + # @param response_class [Class] class to return. + # @return [V2::Parsing::JobResponse, Mindee::V2::Parsing::BaseInferenceResponse] def deserialize_response(response_class) response_class.new(as_hash) # : Mindee::V2::Parsing::JobResponse | Mindee::V2::Parsing::BaseResponse end diff --git a/lib/mindee/v2/client.rb b/lib/mindee/v2/client.rb index 16232fa4e..f5c6fe596 100644 --- a/lib/mindee/v2/client.rb +++ b/lib/mindee/v2/client.rb @@ -31,7 +31,7 @@ def enqueue( ) params_instance = case params when Hash - product.params_type.from_hash(params: params) + product.parameters_class.from_hash(params: params) when Mindee::V2::ClientOptions::BaseProductParameters params else @@ -54,7 +54,7 @@ def get_job_by_url(url) # Get the result of an inference that was previously enqueued. # @param product [Class] The return class. # @param url [String] HTTPS URL to the result. - # @return [Mindee::V2::Parsing::BaseResponse] + # @return [Mindee::V2::Parsing::BaseInferenceResponse] def get_result_by_url(product, url) @mindee_api.req_get_result_by_url(product, url) end @@ -63,7 +63,7 @@ def get_result_by_url(product, url) # Prefer using `get_result_by_url` using the result URL returned by the Job. # @param product [Class] The return class. # @param resource [String] ID of the inference or URL to the result. - # @return [Mindee::V2::Parsing::BaseResponse] + # @return [Mindee::V2::Parsing::BaseInferenceResponse] def get_result(product, resource) @mindee_api.req_get_result_by_id(product, resource) end @@ -83,7 +83,7 @@ def get_job(job_id) # @param params [Hash, ClientOptions::BaseProductParameters] Parameters for the inference. # @param polling_options [Hash, PollingOptions, nil] Parameters for polling. # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation. - # @return [Parsing::BaseResponse] + # @return [Parsing::BaseInferenceResponse] def enqueue_and_get_result( product, input_source, @@ -93,7 +93,7 @@ def enqueue_and_get_result( ) params_instance = case params when Hash - product.params_type.from_hash(params: params) + product.parameters_class.from_hash(params: params) when Mindee::V2::ClientOptions::BaseProductParameters params else @@ -246,7 +246,7 @@ def poll_on_job( # @param polling_options [Input::PollingOptions] # @param wait_for_webhooks [Boolean] # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] - # @return [Mindee::V2::Parsing::BaseResponse] + # @return [Mindee::V2::Parsing::BaseInferenceResponse] def poll_for_product_result( product, enqueue_response, diff --git a/lib/mindee/v2/client_options/base_annotation_parameters.rb b/lib/mindee/v2/client_options/base_annotation_parameters.rb new file mode 100644 index 000000000..b2b74fb67 --- /dev/null +++ b/lib/mindee/v2/client_options/base_annotation_parameters.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module Mindee + module V2 + module ClientOptions + # Base parameters for document annotations. + class BaseAnnotationParameters + # UUID of the annotated document. + # @return [String] + attr_reader :document_id + + # Default constructor. + # @param document_id [String] UUID of the annotated document. + def initialize(document_id) + trimmed_id = document_id&.strip + raise Error::MindeeError, 'Document ID must be provided' if trimmed_id.nil? || trimmed_id.empty? + + # NOTE: document_id is included in the request URL path, it is not a parameter. + @document_id = trimmed_id + end + + # Gets the request parameters for the upload request. + # @return [Hash[String, String]] + def request_parameters + raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'" + end + end + end + end +end diff --git a/lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb b/lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb new file mode 100644 index 000000000..2188dcb71 --- /dev/null +++ b/lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Mindee + module V2 + module ClientOptions + # Base parameters for document upload operations. + class BaseRagDocumentUploadParameters + # UUID of the model that the uploaded RAG document is linked to. + # @return [String] + attr_reader :model_id + + # Default constructor. + # @param model_id [String] UUID of the model that the uploaded RAG document is linked to. + def initialize(model_id) + trimmed_id = model_id&.strip + raise Error::MindeeError, 'Model ID must be provided' if trimmed_id.nil? || trimmed_id.empty? + + @model_id = trimmed_id + end + + # Gets the request parameters for the upload request. + # @return [Hash[String, String]] + def request_parameters + { 'model_id' => @model_id } + end + end + end + end +end diff --git a/lib/mindee/v2/http/.rubocop.yml b/lib/mindee/v2/http/.rubocop.yml deleted file mode 100644 index acea271ec..000000000 --- a/lib/mindee/v2/http/.rubocop.yml +++ /dev/null @@ -1,7 +0,0 @@ -inherit_from: ../../../../.rubocop.yml - -Metrics/CyclomaticComplexity: - Max: 10 - -Metrics/PerceivedComplexity: - Max: 10 diff --git a/lib/mindee/v2/http/mindee_api_v2.rb b/lib/mindee/v2/http/mindee_api_v2.rb index 44e010d06..412bfda99 100644 --- a/lib/mindee/v2/http/mindee_api_v2.rb +++ b/lib/mindee/v2/http/mindee_api_v2.rb @@ -19,8 +19,9 @@ def initialize(api_key: nil) # Send a file to the asynchronous processing queue for inference processing. # - # @param input_source [Input::Source::LocalInputSource, Input::Source::URLInputSource] - # @param params [ClientOptions::BaseProductParameters] + # @param input_source [Input::Source::LocalInputSource, Input::Source::URLInputSource] Local or remote file + # as an input. + # @param params [ClientOptions::BaseProductParameters] Parameters relating to the enqueueing options. # @return [Mindee::V2::Parsing::JobResponse] # @raise [Mindee::Error::MindeeHttpErrorV2] def req_post_product_enqueue(input_source, params) @@ -32,25 +33,29 @@ def req_post_product_enqueue(input_source, params) Mindee::V2::Parsing::JobResponse.new(process_response(response)) end - # Retrieves a result from a given queue. + # Get the result of an inference that was previously enqueued. + # Raises an error if the server's response contains an error. + # # @param product [Class] - # @param inference_id [String] ID of the inference or URL to the result. - # @return [Mindee::V2::Parsing::BaseResponse] + # @param inference_id [String] The inference ID for the result. + # @return [Mindee::V2::Parsing::BaseInferenceResponse] def req_get_result_by_id(product, inference_id) req_get_result_by_url(product, "#{@settings.base_url}/v2/products/#{product.slug}/results/#{inference_id}") end - # Retrieves a queued job. + # Get the status of an inference that was previously enqueued. + # Raises an error if the server's response contains an error. # - # @param job_id [String] ID of the job or URL to the job. + # @param job_id [String] The Job ID as returned by the enqueue request. # @return [Mindee::V2::Parsing::JobResponse] def req_get_job_by_id(job_id) req_get_job_by_url("#{@settings.base_url}/v2/jobs/#{job_id}") end - # Retrieves a queued job from its polling URL. + # Get the status of an inference that was previously enqueued. + # Raises an error if the server's response contains an error. # - # @param url [String] HTTPS URL to the job. + # @param url [String] The polling URL as returned by a Job's polling_url property. # @return [Mindee::V2::Parsing::JobResponse] def req_get_job_by_url(url) validate_https_url!(url) @@ -59,22 +64,23 @@ def req_get_job_by_url(url) Mindee::V2::Parsing::JobResponse.new(process_response(response)) end - # Retrieves a result from its URL. + # Get the result of an inference that was previously enqueued. + # Raises an error if the server's response contains an error. # # @param product [Class] - # @param url [String] HTTPS URL to the result. - # @return [Mindee::V2::Parsing::BaseResponse] + # @param url [String] The URL as returned by a Job's result_url property. + # @return [Mindee::V2::Parsing::BaseInferenceResponse] def req_get_result_by_url(product, url) validate_https_url!(url) @settings.check_api_key response = poll(url) - product.response_type.new(process_response(response)) + product.response_class.new(process_response(response)) end # Retrieves a list of resources with the given criteria. + # # @param params [ClientOptions::BaseSearchParameters] Search parameters. - # @return [Mindee::V2::Parsing::Search::BaseSearchResponse] A search response containing the matching - # resources. + # @return [Mindee::V2::Parsing::Search::BaseSearchResponse] def req_get_search(params) @settings.check_api_key uri = URI("#{@settings.base_url}/v2/search/#{params.slug}") diff --git a/lib/mindee/v2/parsing.rb b/lib/mindee/v2/parsing.rb index eb4770edf..df3605361 100644 --- a/lib/mindee/v2/parsing.rb +++ b/lib/mindee/v2/parsing.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true -require_relative 'parsing/common_response' +require_relative 'parsing/base_response' +require_relative 'parsing/base_inference_response' +require_relative 'parsing/base_rag_annotation_response' require_relative 'parsing/error_response' require_relative 'parsing/field' require_relative 'parsing/inference_file' diff --git a/lib/mindee/v2/parsing/base_inference_response.rb b/lib/mindee/v2/parsing/base_inference_response.rb new file mode 100644 index 000000000..351b84e82 --- /dev/null +++ b/lib/mindee/v2/parsing/base_inference_response.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require_relative 'base_response' + +module Mindee + module V2 + module Parsing + # Base class for V2 inference responses. + class BaseInferenceResponse < Mindee::V2::Parsing::BaseResponse + # @return [BaseInference] The inference result for a split utility request + attr_reader :inference + end + end + end +end diff --git a/lib/mindee/v2/parsing/base_rag_annotation_response.rb b/lib/mindee/v2/parsing/base_rag_annotation_response.rb new file mode 100644 index 000000000..289203919 --- /dev/null +++ b/lib/mindee/v2/parsing/base_rag_annotation_response.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'time' +require_relative 'base_inference_response' + +module Mindee + module V2 + module Parsing + # Base class for all RAG document responses from the V2 API. + class BaseRagAnnotationResponse < BaseInferenceResponse + # Unique identifier of the RAG document. + # @return [String] + attr_reader :id + + # Original filename of the uploaded document. + # @return [String] + attr_reader :filename + + # Date and time of the document creation. + # @return [Time] + attr_reader :created_at + + # Current status of the RAG document. + # @return [String] + attr_reader :status + + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + super + + @id = server_response['id'] + @filename = server_response['filename'] + @created_at = Time.parse(server_response['created_at']) + @status = server_response['status'] + end + end + end + end +end diff --git a/lib/mindee/v2/parsing/base_response.rb b/lib/mindee/v2/parsing/base_response.rb index 126baeec5..4dc7a03b0 100644 --- a/lib/mindee/v2/parsing/base_response.rb +++ b/lib/mindee/v2/parsing/base_response.rb @@ -1,14 +1,19 @@ # frozen_string_literal: true -require_relative 'common_response' +require 'json' module Mindee module V2 module Parsing - # Base class for V2 inference responses. - class BaseResponse < Mindee::V2::Parsing::CommonResponse - # @return [BaseInference] The inference result for a split utility request - attr_reader :inference + # Base class for inference and job responses on the V2 API. + class BaseResponse + # @return [String] + attr_reader :raw_http + + # @param http_response [Hash] + def initialize(http_response) + @raw_http = JSON.generate(http_response) + end end end end diff --git a/lib/mindee/v2/parsing/common_response.rb b/lib/mindee/v2/parsing/common_response.rb deleted file mode 100644 index bcdb7223a..000000000 --- a/lib/mindee/v2/parsing/common_response.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -require 'json' - -module Mindee - module V2 - module Parsing - # Base class for inference and job responses on the V2 API. - class CommonResponse - # @return [String] - attr_reader :raw_http - - # @param http_response [Hash] - def initialize(http_response) - @raw_http = JSON.generate(http_response) - end - end - end - end -end diff --git a/lib/mindee/v2/parsing/failed_inference_response.rb b/lib/mindee/v2/parsing/failed_inference_response.rb index aad15410c..95fc71e3d 100644 --- a/lib/mindee/v2/parsing/failed_inference_response.rb +++ b/lib/mindee/v2/parsing/failed_inference_response.rb @@ -4,7 +4,7 @@ module Mindee module V2 module Parsing # "Webhook payload returned when an inference fails before producing a result. - class FailedInferenceResponse < Mindee::V2::Parsing::CommonResponse + class FailedInferenceResponse < Mindee::V2::Parsing::BaseResponse # @return [String] UUID of the failed inference. attr_reader :inference_id # @return [String] UUID of the model used. diff --git a/lib/mindee/v2/parsing/inference_job.rb b/lib/mindee/v2/parsing/inference_job.rb index c6e3c4764..9fea24ce6 100644 --- a/lib/mindee/v2/parsing/inference_job.rb +++ b/lib/mindee/v2/parsing/inference_job.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require_relative 'common_response' require_relative 'job' module Mindee diff --git a/lib/mindee/v2/parsing/job_response.rb b/lib/mindee/v2/parsing/job_response.rb index 5515a11ba..608f1b06c 100644 --- a/lib/mindee/v2/parsing/job_response.rb +++ b/lib/mindee/v2/parsing/job_response.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -require_relative 'common_response' +require_relative 'base_response' require_relative 'job' module Mindee module V2 module Parsing # HTTP response wrapper that embeds a V2 job. - class JobResponse < CommonResponse + class JobResponse < BaseResponse # @return [Job] Parsed job payload. attr_reader :job diff --git a/lib/mindee/v2/parsing/search/base_search_response.rb b/lib/mindee/v2/parsing/search/base_search_response.rb index 9fa1be459..015fdd345 100644 --- a/lib/mindee/v2/parsing/search/base_search_response.rb +++ b/lib/mindee/v2/parsing/search/base_search_response.rb @@ -5,7 +5,7 @@ module V2 module Parsing module Search # Base class for search responses. - class BaseSearchResponse < CommonResponse + class BaseSearchResponse < BaseResponse # Pagination metadata. # @return [Mindee::V2::Parsing::Search::PaginationMetadata] attr_reader :pagination diff --git a/lib/mindee/v2/product/base_product.rb b/lib/mindee/v2/product/base_product.rb index f22668670..f0ef1324a 100644 --- a/lib/mindee/v2/product/base_product.rb +++ b/lib/mindee/v2/product/base_product.rb @@ -1,26 +1,39 @@ # frozen_string_literal: true require_relative '../parsing/base_response' +require_relative '../parsing/base_inference_response' +require_relative '../parsing/base_rag_annotation_response' require_relative '../client_options/base_product_parameters' +require_relative '../client_options/base_annotation_parameters' +require_relative '../client_options/base_rag_document_upload_parameters' module Mindee module V2 module Product # Base class for all V2 products. class BaseProduct - # @return [String] The slug of the endpoint used for this response + # @return [String] API slug for this product. @slug = '' - # @return [Class] The class of the parameters used for this response - @params_type = Mindee::V2::ClientOptions::BaseProductParameters - # @return [Class] The class of the response used for this product - @response_type = Mindee::V2::Parsing::BaseResponse + # @return [Class] Parameter class accepted by this product. + @parameters_class = Mindee::V2::ClientOptions::BaseProductParameters + # @return [Class] Response class returned by this product. + @response_class = Mindee::V2::Parsing::BaseInferenceResponse + # @return [Class] Annotation Response class returned by this product. + @annotation_response_class = Mindee::V2::Parsing::BaseRagAnnotationResponse + # @return [Class] Annotation Parameters class for this product. + @annotation_parameters_class = Mindee::V2::ClientOptions::BaseAnnotationParameters + # @return [Class] Document Upload class for this product. + @rag_document_upload_class = ClientOptions::BaseRagDocumentUploadParameters def initialize raise StandardError, 'Cannot instantiate abstract class.' if instance_of?(BaseProduct) end class << self - attr_reader :params_type, :slug, :response_type + attr_reader :parameters_class, + :slug, + :response_class, + :annotation_response_class end end end diff --git a/lib/mindee/v2/product/classification/classification.rb b/lib/mindee/v2/product/classification/classification.rb index cfea179aa..51ccfcc5f 100644 --- a/lib/mindee/v2/product/classification/classification.rb +++ b/lib/mindee/v2/product/classification/classification.rb @@ -11,8 +11,8 @@ module Classification # Classification product. class Classification < BaseProduct @slug = 'classification' - @params_type = Mindee::V2::Product::Classification::Params::ClassificationParameters - @response_type = Mindee::V2::Product::Classification::ClassificationResponse + @parameters_class = Mindee::V2::Product::Classification::Params::ClassificationParameters + @response_class = Mindee::V2::Product::Classification::ClassificationResponse end end end diff --git a/lib/mindee/v2/product/classification/classification_response.rb b/lib/mindee/v2/product/classification/classification_response.rb index 0b2f7d3e9..c71f92aa4 100644 --- a/lib/mindee/v2/product/classification/classification_response.rb +++ b/lib/mindee/v2/product/classification/classification_response.rb @@ -9,7 +9,7 @@ module V2 module Product module Classification # HTTP response wrapper that embeds a V2 Inference. - class ClassificationResponse < Mindee::V2::Parsing::BaseResponse + class ClassificationResponse < Mindee::V2::Parsing::BaseInferenceResponse # @return [ClassificationInference] Parsed inference payload. attr_reader :inference diff --git a/lib/mindee/v2/product/crop/crop.rb b/lib/mindee/v2/product/crop/crop.rb index edcec424f..c680a6d8e 100644 --- a/lib/mindee/v2/product/crop/crop.rb +++ b/lib/mindee/v2/product/crop/crop.rb @@ -11,8 +11,8 @@ module Crop # Crop product. class Crop < BaseProduct @slug = 'crop' - @params_type = Mindee::V2::Product::Crop::Params::CropParameters - @response_type = Mindee::V2::Product::Crop::CropResponse + @parameters_class = Mindee::V2::Product::Crop::Params::CropParameters + @response_class = Mindee::V2::Product::Crop::CropResponse end end end diff --git a/lib/mindee/v2/product/crop/crop_response.rb b/lib/mindee/v2/product/crop/crop_response.rb index 90ada7242..ae8670f49 100644 --- a/lib/mindee/v2/product/crop/crop_response.rb +++ b/lib/mindee/v2/product/crop/crop_response.rb @@ -9,7 +9,7 @@ module V2 module Product module Crop # HTTP response wrapper that embeds a V2 Inference. - class CropResponse < Mindee::V2::Parsing::BaseResponse + class CropResponse < Mindee::V2::Parsing::BaseInferenceResponse # @return [CropInference] Parsed inference payload. attr_reader :inference diff --git a/lib/mindee/v2/product/extraction/extraction.rb b/lib/mindee/v2/product/extraction/extraction.rb index e318214fd..d30453bb1 100644 --- a/lib/mindee/v2/product/extraction/extraction.rb +++ b/lib/mindee/v2/product/extraction/extraction.rb @@ -2,6 +2,9 @@ require_relative 'extraction_response' require_relative 'params/extraction_parameters' +require_relative 'rag_documents/extraction_rag_annotation_response' +require_relative 'rag_documents/params/rag_document_annotation_parameters' +require_relative 'rag_documents/params/rag_document_upload_parameters' module Mindee module V2 @@ -12,8 +15,9 @@ module Extraction # Note: currently a placeholder for the `Inference` class. class Extraction < Product::BaseProduct @slug = 'extraction' - @params_type = Product::Extraction::Params::ExtractionParameters - @response_type = Product::Extraction::ExtractionResponse + @parameters_class = Product::Extraction::Params::ExtractionParameters + @response_class = Product::Extraction::ExtractionResponse + @annotation_response_class = Product::Extraction::RagDocuments::ExtractionRagAnnotationResponse end end end diff --git a/lib/mindee/v2/product/extraction/extraction_response.rb b/lib/mindee/v2/product/extraction/extraction_response.rb index af85cad30..22be6b6c0 100644 --- a/lib/mindee/v2/product/extraction/extraction_response.rb +++ b/lib/mindee/v2/product/extraction/extraction_response.rb @@ -7,12 +7,12 @@ module V2 module Product module Extraction # HTTP response wrapper that embeds a V2 Inference. - class ExtractionResponse < Mindee::V2::Parsing::BaseResponse + class ExtractionResponse < Mindee::V2::Parsing::BaseInferenceResponse # @return [ExtractionInference] Parsed inference payload. attr_reader :inference @slug = 'extraction' - @_params_type = Params::ExtractionParameters + @_parameters_class = Params::ExtractionParameters def initialize(server_response) super diff --git a/lib/mindee/v2/product/extraction/params.rb b/lib/mindee/v2/product/extraction/params.rb new file mode 100644 index 000000000..f655f4d9c --- /dev/null +++ b/lib/mindee/v2/product/extraction/params.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +require_relative 'params/data_schema' +require_relative 'params/data_schema_field' +require_relative 'params/data_schema_replace' +require_relative 'params/extraction_parameters' diff --git a/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb b/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb new file mode 100644 index 000000000..1f90a0774 --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'json' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # Base class for annotated fields. + class AnnotatedBaseField + # When true, use the RAG information for the final result. When false, use the Data Schema information. + # @return [Boolean] + attr_reader :selected + + # Guidelines or instructions for processing this field. + # @return [String, nil] + attr_reader :guidelines + + # Default constructor. + # @param selected [Boolean] When true, use the RAG information for the final result. + # When false, use the Data Schema information. + # @param guidelines [String, nil] Guidelines or instructions for processing this field. + def initialize(selected, guidelines = nil) + @selected = selected + @guidelines = guidelines + end + + # Converts the field to a Hash. + # @return [Hash[String, untyped]] + def to_h + { + 'selected' => @selected, + 'guidelines' => @guidelines, + } + end + + # Serializes the field to API JSON format. + # @return [String] + def to_json(state = nil) + to_h.to_json(state) + end + + # Factory method to create appropriate field types. + # @param server_response [Hash] Raw prediction hash. + # @return [AnnotatedBaseField] Appropriate field instance. + # @raise [MindeeError] If the field type isn't recognized. + def self.create_annotated_field(server_response) + if server_response.key?('items') + require_relative 'annotated_list_field' + return AnnotatedListField.new(server_response) + end + + if server_response.key?('fields') + require_relative 'annotated_object_field' + return AnnotatedObjectField.new(server_response) + end + + if server_response.key?('value') + require_relative 'annotated_simple_field' + return AnnotatedSimpleField.new(server_response) + end + + raise Error::MindeeError, "Unrecognized field format in #{server_response.to_json}" + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb b/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb new file mode 100644 index 000000000..1642e3cba --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require_relative 'annotated_base_field' +require_relative 'annotated_list_field' +require_relative 'annotated_object_field' +require_relative 'annotated_simple_field' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # A dictionary of field names and their corresponding configured field types. + class AnnotatedFields < Hash + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + super() + server_response.each do |key, value| + self[key.to_s] = AnnotatedBaseField.create_annotated_field(value) + end + end + + # Returns a field as an `AnnotatedSimpleField`, or raises if the type mismatches. + # @param field_name [String] + # @return [AnnotatedSimpleField] + def get_simple_field(field_name) + field = self[field_name] + raise KeyError, "The field '#{field_name}' was not found." if field.nil? + unless field.is_a?(AnnotatedSimpleField) + raise TypeError, "The field '#{field_name}' is not a AnnotatedSimpleField." + end + + field + end + + # Returns a field as an `AnnotatedObjectField`, or raises if the type mismatches. + # @param field_name [String] + # @return [AnnotatedObjectField] + def get_object_field(field_name) + field = self[field_name] + raise KeyError, "The field '#{field_name}' was not found." if field.nil? + unless field.is_a?(AnnotatedObjectField) + raise TypeError, "The field '#{field_name}' is not an ObjectField." + end + + field + end + + # Returns a field as an `AnnotatedListField`, or raises if the type mismatches. + # @param field_name [String] + # @return [AnnotatedListField] + def get_list_field(field_name) + field = self[field_name] + raise KeyError, "The field '#{field_name}' was not found." if field.nil? + raise TypeError, "The field '#{field_name}' is not a ListField." unless field.is_a?(AnnotatedListField) + + field + end + + # Converts the fields to a Hash. + # @return [Hash[String, untyped]] + def to_h + transform_values(&:to_h) + end + + # Serializes the fields to API format. + # @return [String] + def to_json(state = nil) + to_h.to_json(state) + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb b/lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb new file mode 100644 index 000000000..1286eb7ea --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'json' +require_relative 'annotated_base_field' +require_relative 'annotated_object_field' +require_relative 'annotated_simple_field' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # Field having a list of fields. + class AnnotatedListField < AnnotatedBaseField + # Items contained in the list. + # @return [Array[AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField]] + attr_reader :items + + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + super(server_response['selected'], server_response['guidelines']) + + unless server_response.key?('items') && server_response['items'].is_a?(Array) + raise Error::MindeeError, + "Expected \"items\" to be an array in #{server_response.to_json}." + end + + @items = server_response['items'].map do |item| + AnnotatedBaseField.create_annotated_field(item) + end + end + + # Converts the list field to a Hash. + # @return [Hash[String, untyped]] + def to_h + super.merge('items' => @items.map(&:to_h)) + end + + # AnnotatedSimpleField items from the list. + # @return [Array[AnnotatedSimpleField]] + def simple_items + fields = [] # @type var fields: Array[AnnotatedSimpleField] + @items.each do |item| + raise TypeError, "Invalid field type detected: #{item.class}" unless item.is_a?(AnnotatedSimpleField) + + fields << item + end + fields + end + + # AnnotatedObjectField items from the list. + # @return [Array[AnnotatedObjectField]] + def object_items + fields = [] # @type var fields: Array[AnnotatedObjectField] + @items.each do |item| + raise TypeError, "Invalid field type detected: #{item.class}" unless item.is_a?(AnnotatedObjectField) + + fields << item + end + fields + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb b/lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb new file mode 100644 index 000000000..72a8c3284 --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require_relative 'annotated_base_field' +require_relative 'annotated_fields' +require_relative 'annotated_list_field' +require_relative 'annotated_simple_field' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # Field having a set of sub-fields. + class AnnotatedObjectField < AnnotatedBaseField + # Nested fields carried by this object. + # @return [AnnotatedFields] + attr_reader :fields + + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + super(server_response['selected'], server_response['guidelines']) + + @fields = AnnotatedFields.new(server_response['fields'] || {}) + end + + # Converts the object field to a Hash. + # @return [Hash[String, untyped]] + def to_h + super.merge('fields' => @fields.to_h) + end + + # Retrieves an AnnotatedSimpleField by its name if it exists and is of the correct type. + # @param field_name [String] The name of the field to retrieve. + # @return [AnnotatedSimpleField] + def get_simple_field(field_name) + @fields.get_simple_field(field_name) + end + + # Retrieves an AnnotatedListField by its name if it exists and is of the correct type. + # @param field_name [String] The name of the field to retrieve. + # @return [AnnotatedListField] + def get_list_field(field_name) + @fields.get_list_field(field_name) + end + + # Retrieves an AnnotatedObjectField by its name if it exists and is of the correct type. + # @param field_name [String] The name of the field to retrieve. + # @return [AnnotatedObjectField] + def get_object_field(field_name) + @fields.get_object_field(field_name) + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb b/lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb new file mode 100644 index 000000000..061d5b291 --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative 'annotated_base_field' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # A SimpleField with additional configuration for annotation. + class AnnotatedSimpleField < AnnotatedBaseField + # Field value, one of: string, boolean, number, null. + # @return [String, Boolean, Numeric, nil] + attr_reader :value + + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + super(server_response['selected'], server_response['guidelines']) + @value = server_response['value'] + end + + # Converts the simple field to a Hash. + # @return [Hash[String, untyped]] + def to_h + super.merge('value' => @value) + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rb b/lib/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rb new file mode 100644 index 000000000..f781c5e45 --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'time' +require 'mindee/v2/parsing/base_rag_annotation_response' +require_relative 'rag_annotation' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # Response for a RAG document. + class ExtractionRagAnnotationResponse < Mindee::V2::Parsing::BaseRagAnnotationResponse + # Model identifier linked to the RAG document. + # @return [String] + attr_reader :model_id + + # Number of times this document was used in an inference. + # @return [Integer] + attr_reader :total_matches + + # Date and time of the latest matching inference, if any. + # @return [Time, nil] + attr_reader :last_match_at + + # Annotation metadata associated with the document. + # @return [RagAnnotation, nil] + attr_reader :annotation + + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + super + + @model_id = server_response['model_id'] + @total_matches = server_response['total_matches'] + @last_match_at = server_response['last_match_at'] ? Time.parse(server_response['last_match_at']) : nil + @annotation = server_response['annotation'] ? RagAnnotation.new(server_response['annotation']) : nil + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rb b/lib/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rb new file mode 100644 index 000000000..40779968c --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'json' +require 'mindee/v2/client_options/base_annotation_parameters' +require_relative '../rag_annotation' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + module Params + # Annotation parameters for RAG documents. + class RagDocumentAnnotationParameters < Mindee::V2::ClientOptions::BaseAnnotationParameters + # New public status to apply to the document (for example, to deactivate it). + # @return [String, nil] + attr_reader :status + + # Field-level RAG annotation and guidelines configuration for the document. + # @return [RagAnnotation, nil] + attr_reader :annotation + + # Default constructor. + # @param document_id [String] UUID of the annotated document. + # @param status [String, nil] New public status to apply to the document. + # @param annotation [RagAnnotation, String, Hash[String | Symbol, untyped], nil] Field-level + # RAG annotation and guidelines configuration. + def initialize(document_id, status: nil, annotation: nil) + super(document_id) + + @status = status + @annotation = parse_annotation(annotation) + end + + # Gets the request parameters for the annotation request. + # @return [Hash[String, untyped]] + def request_parameters + parameters = {} # : Hash[String, untyped] + parameters['status'] = @status if @status + parameters['annotation'] = @annotation if @annotation + parameters + end + + private + + # @param annotation [RagAnnotation, String, Hash[String | Symbol, untyped], nil] + # @return [RagAnnotation, nil] + def parse_annotation(annotation) + return nil if annotation.nil? + return annotation if annotation.is_a?(RagAnnotation) + return RagAnnotation.new(JSON.parse(annotation)) if annotation.is_a?(String) + return RagAnnotation.new(JSON.parse(annotation.to_json)) if annotation.is_a?(Hash) + + raise TypeError, 'Invalid RAG Annotation format.' + end + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rb b/lib/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rb new file mode 100644 index 000000000..9d8045c62 --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'mindee/v2/client_options/base_rag_document_upload_parameters' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + module Params + # Parameters for uploading a file to an extraction RAG database. + class RagDocumentUploadParameters < Mindee::V2::ClientOptions::BaseRagDocumentUploadParameters + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb b/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb new file mode 100644 index 000000000..71de0dd42 --- /dev/null +++ b/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'json' +require_relative 'annotated_fields' + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + # A RAG annotation enriched with field-level configuration. + class RagAnnotation + # A dictionary of field names and their corresponding configured field types. + # @return [AnnotatedFields] + attr_reader :fields + + # Default constructor. + # @param server_response [Hash[String | Symbol, untyped]] + def initialize(server_response) + @fields = AnnotatedFields.new(server_response['fields'] || {}) + end + + # Converts the annotation to a Hash. + # @return [Hash[String, untyped]] + def to_h + { 'fields' => @fields.to_h } + end + + # Serializes the annotation to API JSON format. + # @return [String] + def to_json(state = nil) + to_h.to_json(state) + end + end + end + end + end + end +end diff --git a/lib/mindee/v2/product/ocr/ocr.rb b/lib/mindee/v2/product/ocr/ocr.rb index 9f81147cb..814f7e0c7 100644 --- a/lib/mindee/v2/product/ocr/ocr.rb +++ b/lib/mindee/v2/product/ocr/ocr.rb @@ -11,8 +11,8 @@ module OCR # OCR product. class OCR < BaseProduct @slug = 'ocr' - @params_type = Mindee::V2::Product::OCR::Params::OCRParameters - @response_type = Mindee::V2::Product::OCR::OCRResponse + @parameters_class = Mindee::V2::Product::OCR::Params::OCRParameters + @response_class = Mindee::V2::Product::OCR::OCRResponse end end end diff --git a/lib/mindee/v2/product/ocr/ocr_response.rb b/lib/mindee/v2/product/ocr/ocr_response.rb index 0516e5356..e85c702b8 100644 --- a/lib/mindee/v2/product/ocr/ocr_response.rb +++ b/lib/mindee/v2/product/ocr/ocr_response.rb @@ -9,7 +9,7 @@ module V2 module Product module OCR # HTTP response wrapper that embeds a V2 Inference. - class OCRResponse < Mindee::V2::Parsing::BaseResponse + class OCRResponse < Mindee::V2::Parsing::BaseInferenceResponse # @return [OCRInference] Parsed inference payload. attr_reader :inference diff --git a/lib/mindee/v2/product/split/split.rb b/lib/mindee/v2/product/split/split.rb index 40318cead..e59cb3d01 100644 --- a/lib/mindee/v2/product/split/split.rb +++ b/lib/mindee/v2/product/split/split.rb @@ -10,8 +10,8 @@ module Split # Split product. class Split < BaseProduct @slug = 'split' - @params_type = Mindee::V2::Product::Split::Params::SplitParameters - @response_type = Mindee::V2::Product::Split::SplitResponse + @parameters_class = Mindee::V2::Product::Split::Params::SplitParameters + @response_class = Mindee::V2::Product::Split::SplitResponse end end end diff --git a/lib/mindee/v2/product/split/split_response.rb b/lib/mindee/v2/product/split/split_response.rb index 9fdafae18..dc746444b 100644 --- a/lib/mindee/v2/product/split/split_response.rb +++ b/lib/mindee/v2/product/split/split_response.rb @@ -9,7 +9,7 @@ module V2 module Product module Split # HTTP response wrapper that embeds a V2 Inference. - class SplitResponse < Mindee::V2::Parsing::BaseResponse + class SplitResponse < Mindee::V2::Parsing::BaseInferenceResponse # @return [SplitInference] Parsed inference payload. attr_reader :inference diff --git a/sig/mindee/input/local_response.rbs b/sig/mindee/input/local_response.rbs index 6b384f018..984968ca1 100644 --- a/sig/mindee/input/local_response.rbs +++ b/sig/mindee/input/local_response.rbs @@ -8,7 +8,7 @@ module Mindee def self.process_secret_key: (String) -> String def get_hmac_signature: (String) -> String def valid_hmac_signature?: (String, String) -> bool - def deserialize_response: (singleton(V2::Parsing::CommonResponse))-> (V2::Parsing::CommonResponse) + def deserialize_response: (singleton(V2::Parsing::BaseResponse))-> (V2::Parsing::BaseResponse) end end end diff --git a/sig/mindee/v2/client_options/base_annotation_parameters.rbs b/sig/mindee/v2/client_options/base_annotation_parameters.rbs new file mode 100644 index 000000000..bb62e38ff --- /dev/null +++ b/sig/mindee/v2/client_options/base_annotation_parameters.rbs @@ -0,0 +1,15 @@ +# lib/mindee/v2/client_options/base_annotation_parameters.rb + +module Mindee + module V2 + module ClientOptions + class BaseAnnotationParameters + attr_reader document_id: String + + def initialize: (String document_id) -> void + + def request_parameters: -> Hash[String, String] + end + end + end +end diff --git a/sig/mindee/v2/client_options/base_rag_document_upload_parameters.rbs b/sig/mindee/v2/client_options/base_rag_document_upload_parameters.rbs new file mode 100644 index 000000000..3aa69def8 --- /dev/null +++ b/sig/mindee/v2/client_options/base_rag_document_upload_parameters.rbs @@ -0,0 +1,15 @@ +# lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb + +module Mindee + module V2 + module ClientOptions + class BaseRagDocumentUploadParameters + attr_reader model_id: String + + def initialize: (String model_id) -> void + + def request_parameters: -> Hash[String, String] + end + end + end +end diff --git a/sig/mindee/v2/http/mindee_api_v2.rbs b/sig/mindee/v2/http/mindee_api_v2.rbs index d9fe14c9f..7771922fd 100644 --- a/sig/mindee/v2/http/mindee_api_v2.rbs +++ b/sig/mindee/v2/http/mindee_api_v2.rbs @@ -9,9 +9,11 @@ module Mindee interface _ProductClass[T] def slug: () -> String - def response_type: () -> _ResponseFactory[T] + def response_class: () -> _ResponseFactory[T] + + def parameters_class: () -> singleton(ClientOptions::BaseProductParameters) - def params_type: () -> singleton(ClientOptions::BaseProductParameters) + def annotation_response_class: () -> singleton(Parsing::BaseRagAnnotationResponse) end interface _SearchParameters[T] diff --git a/sig/mindee/v2/parsing/base_inference_response.rbs b/sig/mindee/v2/parsing/base_inference_response.rbs new file mode 100644 index 000000000..fcc561891 --- /dev/null +++ b/sig/mindee/v2/parsing/base_inference_response.rbs @@ -0,0 +1,11 @@ +# lib/mindee/v2/parsing/base_inference_response.rb + +module Mindee + module V2 + module Parsing + class BaseInferenceResponse[T] < BaseResponse + attr_reader inference: T + end + end + end +end diff --git a/sig/mindee/v2/parsing/base_rag_annotation_response.rbs b/sig/mindee/v2/parsing/base_rag_annotation_response.rbs new file mode 100644 index 000000000..39c11083f --- /dev/null +++ b/sig/mindee/v2/parsing/base_rag_annotation_response.rbs @@ -0,0 +1,16 @@ +# lib/mindee/v2/parsing/base_rag_annotation_response.rb + +module Mindee + module V2 + module Parsing + class BaseRagAnnotationResponse < BaseInferenceResponse[untyped] + attr_reader id: String + attr_reader filename: String + attr_reader created_at: Time + attr_reader status: String + + def initialize: (Hash[String | Symbol, untyped]) -> void + end + end + end +end diff --git a/sig/mindee/v2/parsing/base_response.rbs b/sig/mindee/v2/parsing/base_response.rbs index 1b49aeaf4..77ecca70e 100644 --- a/sig/mindee/v2/parsing/base_response.rbs +++ b/sig/mindee/v2/parsing/base_response.rbs @@ -3,8 +3,10 @@ module Mindee module V2 module Parsing - class BaseResponse[T] < CommonResponse - attr_reader inference: T + class BaseResponse + attr_reader raw_http: String + + def initialize: (Hash[String | Symbol, untyped]) -> void end end end diff --git a/sig/mindee/v2/parsing/common_response.rbs b/sig/mindee/v2/parsing/common_response.rbs deleted file mode 100644 index abf94f0b5..000000000 --- a/sig/mindee/v2/parsing/common_response.rbs +++ /dev/null @@ -1,12 +0,0 @@ -# lib/mindee/v2/parsing/common_response.rb -module Mindee - module V2 - module Parsing - class CommonResponse - attr_reader raw_http: String - - def initialize: (Hash[String | Symbol, untyped]) -> void - end - end - end -end diff --git a/sig/mindee/v2/parsing/failed_inference_response.rbs b/sig/mindee/v2/parsing/failed_inference_response.rbs index cc7f90a8a..c3e7d2611 100644 --- a/sig/mindee/v2/parsing/failed_inference_response.rbs +++ b/sig/mindee/v2/parsing/failed_inference_response.rbs @@ -1,7 +1,7 @@ module Mindee module V2 module Parsing - class FailedInferenceResponse + class FailedInferenceResponse < BaseResponse def initialize: (Hash[String | Symbol, untyped]) -> void attr_reader created_at: Time attr_reader error: ::Mindee::V2::Parsing::ErrorResponse diff --git a/sig/mindee/v2/parsing/job_response.rbs b/sig/mindee/v2/parsing/job_response.rbs index eceb631b3..f044098c3 100644 --- a/sig/mindee/v2/parsing/job_response.rbs +++ b/sig/mindee/v2/parsing/job_response.rbs @@ -2,7 +2,7 @@ module Mindee module V2 module Parsing - class JobResponse < CommonResponse + class JobResponse < BaseResponse attr_reader job: Job def initialize: (Hash[String | Symbol, untyped]) -> void diff --git a/sig/mindee/v2/parsing/search/base_search_response.rbs b/sig/mindee/v2/parsing/search/base_search_response.rbs index b144c2d5b..0e932ea91 100644 --- a/sig/mindee/v2/parsing/search/base_search_response.rbs +++ b/sig/mindee/v2/parsing/search/base_search_response.rbs @@ -2,7 +2,7 @@ module Mindee module V2 module Parsing module Search - class BaseSearchResponse < CommonResponse + class BaseSearchResponse < BaseResponse attr_reader pagination: PaginationMetadata def initialize: (Hash[String|Symbol, untyped]) -> void diff --git a/sig/mindee/v2/product/base_product.rbs b/sig/mindee/v2/product/base_product.rbs index 43ca94c2b..9317ea21e 100644 --- a/sig/mindee/v2/product/base_product.rbs +++ b/sig/mindee/v2/product/base_product.rbs @@ -3,14 +3,18 @@ module Mindee module Product class BaseProduct self.@slug: String - self.@params_type: singleton(ClientOptions::BaseProductParameters) - self.@response_type: singleton(Parsing::BaseResponse) + self.@parameters_class: singleton(ClientOptions::BaseProductParameters) + self.@response_class: singleton(Parsing::BaseInferenceResponse) + self.@annotation_response_class: singleton(Parsing::BaseRagAnnotationResponse) + self.@annotation_parameters_class: singleton(ClientOptions::BaseAnnotationParameters) + self.@rag_document_upload_class: singleton(ClientOptions::BaseRagDocumentUploadParameters) - def self._params_type: () -> singleton(ClientOptions::BaseProductParameters) + def self._parameters_class: () -> singleton(ClientOptions::BaseProductParameters) attr_reader self.slug: String - attr_reader self.params_type: singleton(ClientOptions::BaseProductParameters) - attr_reader self.response_type: singleton(Parsing::BaseResponse) + attr_reader self.parameters_class: singleton(ClientOptions::BaseProductParameters) + attr_reader self.response_class: singleton(Parsing::BaseInferenceResponse) + attr_reader self.annotation_response_class: singleton(Parsing::BaseRagAnnotationResponse) def initialize: -> void end diff --git a/sig/mindee/v2/product/classification/classification_response.rbs b/sig/mindee/v2/product/classification/classification_response.rbs index b1ea8595b..34bb4b94b 100644 --- a/sig/mindee/v2/product/classification/classification_response.rbs +++ b/sig/mindee/v2/product/classification/classification_response.rbs @@ -4,17 +4,17 @@ module Mindee module V2 module Product module Classification - class ClassificationResponse < Parsing::BaseResponse[ClassificationInference] + class ClassificationResponse < Parsing::BaseInferenceResponse[ClassificationInference] self.@slug: String - self.@_params_type: singleton(Params::ClassificationParameters) + self.@_parameters_class: singleton(Params::ClassificationParameters) attr_reader inference: ClassificationInference def initialize: (Hash[String | Symbol, untyped]) -> void - def _params_type: -> singleton(Params::ClassificationParameters) + def _parameters_class: -> singleton(Params::ClassificationParameters) def to_s: -> String - def self._params_type: () -> singleton(Params::ClassificationParameters) + def self._parameters_class: () -> singleton(Params::ClassificationParameters) def self.slug: () -> String end end diff --git a/sig/mindee/v2/product/crop/crop_response.rbs b/sig/mindee/v2/product/crop/crop_response.rbs index 5530ef1d4..f12763876 100644 --- a/sig/mindee/v2/product/crop/crop_response.rbs +++ b/sig/mindee/v2/product/crop/crop_response.rbs @@ -4,17 +4,17 @@ module Mindee module V2 module Product module Crop - class CropResponse < Parsing::BaseResponse[CropInference] + class CropResponse < Parsing::BaseInferenceResponse[CropInference] self.@slug: String - self.@_params_type: singleton(Params::CropParameters) + self.@_parameters_class: singleton(Params::CropParameters) attr_reader inference: CropInference def initialize: (Hash[String | Symbol, untyped]) -> void - def _params_type: -> singleton(Params::CropParameters) + def _parameters_class: -> singleton(Params::CropParameters) def to_s: -> String - def self._params_type: () -> singleton(Params::CropParameters) + def self._parameters_class: () -> singleton(Params::CropParameters) def self.slug: () -> String end end diff --git a/sig/mindee/v2/product/extraction/extraction.rbs b/sig/mindee/v2/product/extraction/extraction.rbs index 262db4b57..5a5d2dd4f 100644 --- a/sig/mindee/v2/product/extraction/extraction.rbs +++ b/sig/mindee/v2/product/extraction/extraction.rbs @@ -5,8 +5,9 @@ module Mindee module Product module Extraction class Extraction - self.@params_type: singleton(Params::ExtractionParameters) - self.@response_type: singleton(ExtractionResponse) + self.@parameters_class: singleton(Params::ExtractionParameters) + self.@response_class: singleton(ExtractionResponse) + self.@annotation_response_class: singleton(RagDocuments::ExtractionRagAnnotationResponse) self.@slug: String end end diff --git a/sig/mindee/v2/product/extraction/extraction_inference.rbs b/sig/mindee/v2/product/extraction/extraction_inference.rbs index 0ec8a3f14..a18775377 100644 --- a/sig/mindee/v2/product/extraction/extraction_inference.rbs +++ b/sig/mindee/v2/product/extraction/extraction_inference.rbs @@ -3,8 +3,7 @@ module Mindee module Product module Extraction class ExtractionInference - attr_reader self.params_type: singleton(Params::ExtractionParameters) - attr_reader self.response_type: singleton(ExtractionResponse) + attr_reader self.parameters_class: singleton(Params::ExtractionParameters) attr_reader self.slug: String attr_reader active_options: Parsing::InferenceActiveOptions diff --git a/sig/mindee/v2/product/extraction/extraction_response.rbs b/sig/mindee/v2/product/extraction/extraction_response.rbs index 0b5cec064..978deaed9 100644 --- a/sig/mindee/v2/product/extraction/extraction_response.rbs +++ b/sig/mindee/v2/product/extraction/extraction_response.rbs @@ -4,17 +4,17 @@ module Mindee module V2 module Product module Extraction - class ExtractionResponse < Parsing::BaseResponse[ExtractionInference] - self.@_params_type: singleton(Params::ExtractionParameters) + class ExtractionResponse < Parsing::BaseInferenceResponse[ExtractionInference] + self.@_parameters_class: singleton(Params::ExtractionParameters) self.@slug: String attr_reader inference: ExtractionInference def initialize: (Hash[String | Symbol, untyped]) -> void - def _params_type: -> singleton(Params::ExtractionParameters) + def _parameters_class: -> singleton(Params::ExtractionParameters) - def self._params_type: () -> singleton(Params::ExtractionParameters) + def self._parameters_class: () -> singleton(Params::ExtractionParameters) def to_s: -> String end diff --git a/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs b/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs new file mode 100644 index 000000000..59ad8eabb --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs @@ -0,0 +1,21 @@ +# lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class AnnotatedBaseField + attr_reader selected: bool + attr_reader guidelines: String? + + def initialize: (bool, ?String?) -> void + def to_h: () -> Hash[String, untyped] + def to_json: (?untyped) -> String + def self.create_annotated_field: (Hash[String | Symbol, untyped]) -> (AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField) + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/annotated_fields.rbs b/sig/mindee/v2/product/extraction/rag_documents/annotated_fields.rbs new file mode 100644 index 000000000..ac63b74da --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_fields.rbs @@ -0,0 +1,20 @@ +# lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class AnnotatedFields < Hash[String, AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField] + def initialize: (Hash[String | Symbol, untyped]) -> void + def get_simple_field: (String) -> AnnotatedSimpleField + def get_object_field: (String) -> AnnotatedObjectField + def get_list_field: (String) -> AnnotatedListField + def to_h: () -> Hash[String, untyped] + def to_json: (?untyped) -> String + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/annotated_list_field.rbs b/sig/mindee/v2/product/extraction/rag_documents/annotated_list_field.rbs new file mode 100644 index 000000000..57289c5e5 --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_list_field.rbs @@ -0,0 +1,21 @@ +# lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class AnnotatedListField < AnnotatedBaseField + attr_reader items: Array[AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField] + + def initialize: (Hash[String | Symbol, untyped]) -> void + def simple_items: () -> Array[AnnotatedSimpleField] + def object_items: () -> Array[AnnotatedObjectField] + def to_s: () -> String + def to_h: () -> Hash[String, untyped] + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/annotated_object_field.rbs b/sig/mindee/v2/product/extraction/rag_documents/annotated_object_field.rbs new file mode 100644 index 000000000..3bbd2c4b5 --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_object_field.rbs @@ -0,0 +1,21 @@ +# lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class AnnotatedObjectField < AnnotatedBaseField + attr_reader fields: AnnotatedFields + + def initialize: (Hash[String | Symbol, untyped]) -> void + def get_simple_field: (String) -> AnnotatedSimpleField + def get_list_field: (String) -> AnnotatedListField + def get_object_field: (String) -> AnnotatedObjectField + def to_h: () -> Hash[String, untyped] + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rbs b/sig/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rbs new file mode 100644 index 000000000..f97e98daf --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rbs @@ -0,0 +1,18 @@ +# lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class AnnotatedSimpleField < AnnotatedBaseField + attr_reader value: (String | bool | Numeric)? + + def initialize: (Hash[String | Symbol, untyped]) -> void + def to_h: () -> Hash[String, untyped] + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rbs b/sig/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rbs new file mode 100644 index 000000000..68113d33b --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rbs @@ -0,0 +1,20 @@ +# lib/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class ExtractionRagAnnotationResponse < Mindee::V2::Parsing::BaseRagAnnotationResponse + attr_reader model_id: String + attr_reader total_matches: Integer + attr_reader last_match_at: Time? + attr_reader annotation: RagAnnotation? + + def initialize: (Hash[String | Symbol, untyped]) -> void + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rbs b/sig/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rbs new file mode 100644 index 000000000..64927e132 --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rbs @@ -0,0 +1,30 @@ +# lib/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + module Params + class RagDocumentAnnotationParameters < Mindee::V2::ClientOptions::BaseAnnotationParameters + attr_reader status: String? + attr_reader annotation: RagAnnotation? + + def initialize: ( + String document_id, + ?status: String?, + ?annotation: RagAnnotation | String | Hash[String | Symbol, untyped]? + ) -> void + + def request_parameters: -> Hash[String, untyped] + + private + + def parse_annotation: (RagAnnotation | String | Hash[String | Symbol, untyped]?) -> RagAnnotation? + end + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rbs b/sig/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rbs new file mode 100644 index 000000000..eb7dcd741 --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rbs @@ -0,0 +1,16 @@ +# lib/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + module Params + class RagDocumentUploadParameters < Mindee::V2::ClientOptions::BaseRagDocumentUploadParameters + end + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs b/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs new file mode 100644 index 000000000..d2df18e6e --- /dev/null +++ b/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs @@ -0,0 +1,19 @@ +# lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb + +module Mindee + module V2 + module Product + module Extraction + module RagDocuments + class RagAnnotation + attr_reader fields: AnnotatedFields + + def initialize: (Hash[String | Symbol, untyped]) -> void + def to_h: () -> Hash[String, untyped] + def to_json: (?untyped) -> String + end + end + end + end + end +end diff --git a/sig/mindee/v2/product/ocr/ocr_response.rbs b/sig/mindee/v2/product/ocr/ocr_response.rbs index 8f713b5b2..767f700c9 100644 --- a/sig/mindee/v2/product/ocr/ocr_response.rbs +++ b/sig/mindee/v2/product/ocr/ocr_response.rbs @@ -4,17 +4,17 @@ module Mindee module V2 module Product module OCR - class OCRResponse < Parsing::BaseResponse[OCRInference] + class OCRResponse < Parsing::BaseInferenceResponse[OCRInference] self.@slug: String - self.@_params_type: singleton(Params::OCRParameters) + self.@_parameters_class: singleton(Params::OCRParameters) attr_reader inference: Mindee::V2::Product::OCR::OCRInference def initialize: (Hash[String | Symbol, untyped]) -> void - def _params_type: -> singleton(Params::OCRParameters) + def _parameters_class: -> singleton(Params::OCRParameters) def to_s: -> String - def self._params_type: () -> singleton(Params::OCRParameters) + def self._parameters_class: () -> singleton(Params::OCRParameters) def self.slug: () -> String end end diff --git a/sig/mindee/v2/product/split/split_response.rbs b/sig/mindee/v2/product/split/split_response.rbs index 9a0232517..f873508b8 100644 --- a/sig/mindee/v2/product/split/split_response.rbs +++ b/sig/mindee/v2/product/split/split_response.rbs @@ -4,17 +4,17 @@ module Mindee module V2 module Product module Split - class SplitResponse < Parsing::BaseResponse[SplitInference] + class SplitResponse < Parsing::BaseInferenceResponse[SplitInference] self.@slug: String - self.@_params_type: singleton(Params::SplitParameters) + self.@_parameters_class: singleton(Params::SplitParameters) attr_reader inference: Mindee::V2::Product::Split::SplitInference def initialize: (Hash[String | Symbol, untyped]) -> void - def _params_type: -> singleton(Params::SplitParameters) + def _parameters_class: -> singleton(Params::SplitParameters) def to_s: -> String - def self._params_type: () -> singleton(Params::SplitParameters) + def self._parameters_class: () -> singleton(Params::SplitParameters) def self.slug: () -> String end end diff --git a/spec/v2/parsing/common_response_spec.rb b/spec/v2/parsing/base_response_spec.rb similarity index 81% rename from spec/v2/parsing/common_response_spec.rb rename to spec/v2/parsing/base_response_spec.rb index 530c4dc23..d3688593f 100644 --- a/spec/v2/parsing/common_response_spec.rb +++ b/spec/v2/parsing/base_response_spec.rb @@ -2,9 +2,9 @@ require 'json' require 'mindee' -require 'mindee/v2/parsing/common_response' +require 'mindee/v2/parsing/base_response' -describe Mindee::V2::Parsing::CommonResponse do +describe Mindee::V2::Parsing::BaseResponse do it 'stores raw_http as a JSON string' do server_response = { 'api_request' => { 'status' => 'success' }, diff --git a/spec/v2/product/extraction/rag_document_spec.rb b/spec/v2/product/extraction/rag_document_spec.rb new file mode 100644 index 000000000..95c2aa290 --- /dev/null +++ b/spec/v2/product/extraction/rag_document_spec.rb @@ -0,0 +1,148 @@ +# frozen_string_literal: true + +require 'json' +require_relative '../../../data' + +require 'mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response' +require 'mindee/v2/product/extraction/rag_documents/rag_annotation' +require 'mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters' +require 'mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters' + +describe Mindee::V2::Product::Extraction::RagDocuments do + def get_response(relative_path) + file_path = File.join(V2_PRODUCT_DATA_DIR, relative_path) + dict = JSON.parse(File.read(file_path)) + Mindee::V2::Product::Extraction::RagDocuments::ExtractionRagAnnotationResponse.new(dict) + end + + it 'should init POST parameters' do + parameters = Mindee::V2::Product::Extraction::RagDocuments::Params::RagDocumentUploadParameters.new( + 'invalid-model-id' + ) + req_params = parameters.request_parameters + + expect(req_params['model_id']).to eq('invalid-model-id') + end + + it 'should init PATCH parameters' do + annotation = Mindee::V2::Product::Extraction::RagDocuments::RagAnnotation.new( + 'fields' => { + 'hello' => { + 'selected' => true, + 'guidelines' => nil, + 'value' => nil, + }, + } + ) + parameters = Mindee::V2::Product::Extraction::RagDocuments::Params::RagDocumentAnnotationParameters.new( + 'invalid-document-id', + status: 'Active', + annotation: annotation + ) + + expect(parameters.document_id).to eq('invalid-document-id') + # server expects JSON string + expect(parameters.request_parameters.to_json).to eq( + '{"status":"Active","annotation":{"fields":{"hello":{"selected":true,"guidelines":null,"value":null}}}}' + ) + end + + it 'should load a POST response from a JSON string' do + response = get_response('extraction/rag_documents/post_response.json') + + expect(response).not_to be_nil + expect(response.id).to eq('cc831599-c545-48b7-aa27-6d7ccd5b8d32') + expect(response.status).to eq('Processing') + expect(response.annotation).to be_nil + end + + it 'should load a GET response from a JSON string' do + response = get_response('extraction/rag_documents/get_response_draft.json') + + expect(response).not_to be_nil + expect(response.id).to eq('cc831599-c545-48b7-aa27-6d7ccd5b8d32') + expect(response.status).to eq('Draft') + expect(response.annotation).not_to be_nil + + fields = response.annotation.fields + expect(fields).not_to be_nil + + # null simple field + tip_field = fields.get_simple_field('tip') + expect(tip_field).not_to be_nil + expect(tip_field.selected).to be(false) + expect(tip_field.guidelines).to be_nil + expect(tip_field.value).to be_nil + + # filled simple field + date_field = fields.get_simple_field('date') + expect(date_field).not_to be_nil + expect(date_field.selected).to be(false) + expect(date_field.guidelines).to be_nil + expect(date_field.value).to eq('2019-11-02') + + # filled object field + locale_field = fields.get_object_field('locale') + expect(locale_field).not_to be_nil + expect(locale_field.selected).to be(false) + expect(locale_field.guidelines).to be_nil + expect(locale_field.fields).not_to be_nil + expect(locale_field.fields.size).to eq(3) + expect(locale_field.get_simple_field('country').value).to eq('US') + expect(locale_field.get_simple_field('currency').value).to eq('USD') + expect(locale_field.get_simple_field('language').value).to be_nil + + # list of simple fields + reference_numbers_field = fields.get_list_field('reference_numbers') + expect(reference_numbers_field).not_to be_nil + expect(reference_numbers_field.selected).to be(false) + expect(reference_numbers_field.guidelines).to be_nil + expect(reference_numbers_field.simple_items).not_to be_nil + expect(reference_numbers_field.simple_items.length).to eq(1) + expect(reference_numbers_field.simple_items[0].value).to eq('2412/2019') + + # list of object fields + line_items_field = fields.get_list_field('line_items') + expect(line_items_field).not_to be_nil + expect(line_items_field.selected).to be(false) + expect(line_items_field.guidelines).to be_nil + expect(line_items_field.object_items).not_to be_nil + expect(line_items_field.object_items.length).to eq(3) + + line_item0 = line_items_field.object_items[0] + expect(line_item0.fields).not_to be_nil + expect(line_item0.fields.size).to eq(8) + expect(line_item0.get_simple_field('description').value).to eq('Front and rear brake cables') + expect(line_item0.get_simple_field('quantity').value).to eq(1) + expect(line_item0.get_simple_field('unit_price').value).to eq(100) + expect(line_item0.get_simple_field('total_price').value).to eq(100) + expect(line_item0.get_simple_field('tax_rate').value).to be_nil + expect(line_item0.get_simple_field('tax_amount').value).to be_nil + expect(line_item0.get_simple_field('product_code').value).to be_nil + expect(line_item0.get_simple_field('unit_measure').value).to be_nil + + line_item1 = line_items_field.object_items[1] + expect(line_item1.fields).not_to be_nil + expect(line_item1.fields.size).to eq(8) + expect(line_item1.get_simple_field('description').value).to eq('New set of pedal arms') + expect(line_item1.get_simple_field('quantity').value).to eq(2) + expect(line_item1.get_simple_field('unit_price').value).to eq(25) + expect(line_item1.get_simple_field('total_price').value).to eq(50) + expect(line_item1.get_simple_field('tax_rate').value).to be_nil + expect(line_item1.get_simple_field('tax_amount').value).to be_nil + expect(line_item1.get_simple_field('product_code').value).to be_nil + expect(line_item1.get_simple_field('unit_measure').value).to be_nil + + line_item2 = line_items_field.object_items[2] + expect(line_item2.fields).not_to be_nil + expect(line_item2.fields.size).to eq(8) + expect(line_item2.get_simple_field('description').value).to eq('Labor 3hrs') + expect(line_item2.get_simple_field('quantity').value).to eq(3) + expect(line_item2.get_simple_field('unit_price').value).to eq(15) + expect(line_item2.get_simple_field('total_price').value).to eq(45) + expect(line_item2.get_simple_field('tax_rate').value).to be_nil + expect(line_item2.get_simple_field('tax_amount').value).to be_nil + expect(line_item2.get_simple_field('product_code').value).to be_nil + expect(line_item2.get_simple_field('unit_measure').value).to be_nil + end +end