From 81de3cc497298e92cdb43cd9f7c47e9718abf3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Mon, 16 Nov 2026 15:53:49 +0100 Subject: [PATCH 1/4] :sparkles: add RAG document management --- .github/workflows/_test-integration-lite.yml | 1 + AGENTS.md | 2 + bin/v2/parser.rb | 2 +- lib/mindee/input/local_response.rb | 4 +- lib/mindee/v2/client.rb | 12 +- .../base_annotation_parameters.rb | 30 +++ .../base_rag_document_upload_parameters.rb | 29 +++ lib/mindee/v2/http/.rubocop.yml | 7 - lib/mindee/v2/http/mindee_api_v2.rb | 198 ++++++++++++------ lib/mindee/v2/parsing.rb | 4 +- .../v2/parsing/base_inference_response.rb | 15 ++ .../parsing/base_rag_annotation_response.rb | 40 ++++ lib/mindee/v2/parsing/base_response.rb | 15 +- lib/mindee/v2/parsing/common_response.rb | 20 -- .../v2/parsing/failed_inference_response.rb | 2 +- lib/mindee/v2/parsing/inference_job.rb | 1 - lib/mindee/v2/parsing/job_response.rb | 4 +- .../v2/parsing/search/base_search_response.rb | 2 +- lib/mindee/v2/product/base_product.rb | 25 ++- .../product/classification/classification.rb | 4 +- .../classification/classification_response.rb | 2 +- lib/mindee/v2/product/crop/crop.rb | 4 +- lib/mindee/v2/product/crop/crop_response.rb | 2 +- .../v2/product/extraction/extraction.rb | 8 +- .../product/extraction/extraction_response.rb | 4 +- lib/mindee/v2/product/extraction/params.rb | 6 + .../rag_documents/annotated_base_field.rb | 71 +++++++ .../rag_documents/annotated_fields.rb | 77 +++++++ .../rag_documents/annotated_list_field.rb | 68 ++++++ .../rag_documents/annotated_object_field.rb | 58 +++++ .../rag_documents/annotated_simple_field.rb | 33 +++ .../extraction_rag_annotation_response.rb | 45 ++++ .../rag_document_annotation_parameters.rb | 62 ++++++ .../params/rag_document_upload_parameters.rb | 19 ++ .../rag_documents/rag_annotation.rb | 39 ++++ lib/mindee/v2/product/ocr/ocr.rb | 4 +- lib/mindee/v2/product/ocr/ocr_response.rb | 2 +- lib/mindee/v2/product/split/split.rb | 4 +- lib/mindee/v2/product/split/split_response.rb | 2 +- sig/custom/net_http.rbs | 13 ++ sig/mindee/input/local_response.rbs | 2 +- .../base_annotation_parameters.rbs | 15 ++ .../base_rag_document_upload_parameters.rbs | 15 ++ sig/mindee/v2/http/mindee_api_v2.rbs | 30 ++- .../v2/parsing/base_inference_response.rbs | 11 + .../parsing/base_rag_annotation_response.rbs | 16 ++ sig/mindee/v2/parsing/base_response.rbs | 6 +- sig/mindee/v2/parsing/common_response.rbs | 12 -- .../v2/parsing/failed_inference_response.rbs | 2 +- sig/mindee/v2/parsing/job_response.rbs | 2 +- .../parsing/search/base_search_response.rbs | 2 +- sig/mindee/v2/product/base_product.rbs | 14 +- .../classification_response.rbs | 8 +- sig/mindee/v2/product/crop/crop_response.rbs | 8 +- .../v2/product/extraction/extraction.rbs | 5 +- .../extraction/extraction_inference.rbs | 3 +- .../extraction/extraction_response.rbs | 8 +- .../rag_documents/annotated_base_field.rbs | 21 ++ .../rag_documents/annotated_fields.rbs | 20 ++ .../rag_documents/annotated_list_field.rbs | 21 ++ .../rag_documents/annotated_object_field.rbs | 21 ++ .../rag_documents/annotated_simple_field.rbs | 18 ++ .../extraction_rag_annotation_response.rbs | 20 ++ .../rag_document_annotation_parameters.rbs | 30 +++ .../params/rag_document_upload_parameters.rbs | 16 ++ .../rag_documents/rag_annotation.rbs | 19 ++ sig/mindee/v2/product/ocr/ocr_response.rbs | 8 +- .../v2/product/split/split_response.rbs | 8 +- spec/v2/client_v2_spec.rb | 12 +- ...response_spec.rb => base_response_spec.rb} | 4 +- .../product/extraction/rag_document_spec.rb | 183 ++++++++++++++++ 71 files changed, 1306 insertions(+), 194 deletions(-) create mode 100644 AGENTS.md create mode 100644 lib/mindee/v2/client_options/base_annotation_parameters.rb create mode 100644 lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb delete mode 100644 lib/mindee/v2/http/.rubocop.yml create mode 100644 lib/mindee/v2/parsing/base_inference_response.rb create mode 100644 lib/mindee/v2/parsing/base_rag_annotation_response.rb delete mode 100644 lib/mindee/v2/parsing/common_response.rb create mode 100644 lib/mindee/v2/product/extraction/params.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rb create mode 100644 lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb create mode 100644 sig/mindee/v2/client_options/base_annotation_parameters.rbs create mode 100644 sig/mindee/v2/client_options/base_rag_document_upload_parameters.rbs create mode 100644 sig/mindee/v2/parsing/base_inference_response.rbs create mode 100644 sig/mindee/v2/parsing/base_rag_annotation_response.rbs delete mode 100644 sig/mindee/v2/parsing/common_response.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/annotated_fields.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/annotated_list_field.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/annotated_object_field.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/extraction_rag_annotation_response.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/params/rag_document_upload_parameters.rbs create mode 100644 sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs rename spec/v2/parsing/{common_response_spec.rb => base_response_spec.rb} (81%) create mode 100644 spec/v2/product/extraction/rag_document_spec.rb diff --git a/.github/workflows/_test-integration-lite.yml b/.github/workflows/_test-integration-lite.yml index fe1e3398f..d09a69e35 100644 --- a/.github/workflows/_test-integration-lite.yml +++ b/.github/workflows/_test-integration-lite.yml @@ -33,6 +33,7 @@ jobs: - "ubuntu-24.04" - "macos-latest" ruby: + - "3.2" - "4.0" steps: - uses: actions/checkout@v7 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..68b921ab5 100644 --- a/lib/mindee/v2/http/mindee_api_v2.rb +++ b/lib/mindee/v2/http/mindee_api_v2.rb @@ -19,62 +19,76 @@ 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) @settings.check_api_key - response = enqueue( - input_source, - params - ) + uri = URI("#{@settings.base_url}/v2/products/#{params.slug}/enqueue") + + form_data = {} # : Hash[String, untyped] + if input_source.is_a?(Mindee::Input::Source::URLInputSource) + form_data['uri'] = input_source.url + else + file_data, file_metadata = input_source.read_contents(close: params.close_file) + form_data['file'] = [file_data, file_metadata] + end + form_data.merge!(params.request_parameters) + + response = send_post_request(uri, form_data) 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) @settings.check_api_key - response = poll(url) + response = send_get_request(URI(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)) + response = send_get_request(URI(url)) + 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}") @@ -82,11 +96,57 @@ def req_get_search(params) query_params = params.request_parameters uri.query = URI.encode_www_form(query_params) unless query_params.empty? + response = send_get_request(uri) + params.response_class.new(process_response(response)) + end + + # Get a document's info and annotations from the RAG database. + # + # @param product [Class] The product definition to use. + # @param document_id [String] The document ID to get. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def req_get_rag_annotation(product, document_id) + @settings.check_api_key + uri = URI("#{@settings.base_url}/v2/products/#{product.slug}/rag-documents/#{document_id}") + response = send_get_request(uri) + product.annotation_response_class.new(process_response(response)) + end + + # Add a document to the RAG database. + # + # @param product [Class] The product definition to use. + # @param parameters [ClientOptions::BaseRagDocumentUploadParameters] The parameters to use. + # @param input_source [Input::Source::LocalInputSource] The file to upload. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def req_post_rag_document(product, parameters, input_source) + @settings.check_api_key + uri = URI("#{@settings.base_url}/v2/products/#{product.slug}/rag-documents") + + form_data = {} # : Hash[String, untyped] + file_data, file_metadata = input_source.read_contents + form_data['file'] = [file_data, file_metadata] + form_data.merge!(parameters.request_parameters) + + response = send_post_request(uri, form_data) + product.annotation_response_class.new(process_response(response)) + end + + # Update a document's annotations in the RAG database. + # + # @param product [Class] The product definition to use. + # @param parameters [ClientOptions::BaseAnnotationParameters] The parameters to use. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def req_patch_rag_annotation(product, parameters) + @settings.check_api_key + uri = URI("#{@settings.base_url}/v2/products/#{product.slug}/rag-documents/#{parameters.document_id}") + headers = { 'Authorization' => @settings.api_key, 'User-Agent' => @settings.user_agent, + 'Content-Type' => 'application/json', } - req = Net::HTTP::Get.new(uri, headers) + req = Net::HTTP::Patch.new(uri, headers) + req.body = parameters.request_parameters.to_json response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, @@ -94,7 +154,32 @@ def req_get_search(params) read_timeout: @settings.request_timeout) do |http| http.request(req) end - params.response_class.new(process_response(response)) + product.annotation_response_class.new(process_response(response)) + end + + # Deletes a document from the RAG database. + # + # @param product [Class] The product definition to use. + # @param document_id [String] The document's ID. + # @return [Boolean] true if the document was deleted successfully, false otherwise. + # rubocop:disable-next Naming/PredicateMethod + def req_delete_rag_document(product, document_id) + @settings.check_api_key + uri = URI("#{@settings.base_url}/v2/products/#{product.slug}/rag-documents/#{document_id}") + + headers = { + 'Authorization' => @settings.api_key, + 'User-Agent' => @settings.user_agent, + } + req = Net::HTTP::Delete.new(uri, headers) + + response = Net::HTTP.start(uri.hostname, uri.port, + use_ssl: true, + open_timeout: @settings.request_timeout, + read_timeout: @settings.request_timeout) do |http| + http.request(req) + end + !response.nil? && response.respond_to?(:code) && response.code.to_i >= 200 && response.code.to_i < 400 end private @@ -110,31 +195,10 @@ def validate_https_url!(url) raise Mindee::Error::MindeeError, "Invalid URL: #{url}" end - # Converts an HTTP response to a parsed response object. - # - # @param response [Net::HTTPResponse, nil] - # @return [Hash] - # @raise Throws if the server returned an error. - def process_response(response) - if !response.nil? && response.respond_to?(:body) && - Mindee::HTTP::ResponseValidation.valid_v2_response?(response) - return JSON.parse(response.body, object_class: Hash) - end - - response_body = if response.nil? || !response.respond_to?(:body) - '{ "status": -1, - "detail": "Empty server response." }' - else - response.body - end - raise Mindee::HTTP::ErrorHandler.generate_v2_error(JSON.parse(response_body).transform_keys(&:to_sym)) - end - - # Polls a queue for either a result or a job. - # @param url [String] URL, passed as a string. + # Send a GET request. + # @param uri [URI::Generic] URL, passed as a URI instance or String. # @return [Net::HTTPResponse] - def poll(url) - uri = URI(url) + def send_get_request(uri) headers = { 'Authorization' => @settings.api_key, 'User-Agent' => @settings.user_agent, @@ -150,21 +214,11 @@ def poll(url) raise Mindee::Error::MindeeError, 'Could not resolve server response.' end - # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource] - # @param params [ClientOptions::BaseProductParameters] Inference options. - # @return [Net::HTTPResponse, nil] - def enqueue(input_source, params) - uri = URI("#{@settings.base_url}/v2/products/#{params.slug}/enqueue") - - form_data = {} # : Hash[String, untyped] - if input_source.is_a?(Mindee::Input::Source::URLInputSource) - form_data['url'] = input_source.url - else - file_data, file_metadata = input_source.read_contents(close: params.close_file) - form_data['file'] = [file_data, file_metadata] - end - form_data.merge!(params.request_parameters) - + # Send a POST request. + # @param uri [URI::Generic] URL, passed as a URI instance or String. + # @param form_data [Hash[String, untyped]] Form data to send. + # @return [Net::HTTPResponse] + def send_post_request(uri, form_data) # Convert hash to the array format required by Net::HTTP multipart form_data_list = form_data.map do |k, v| v.is_a?(Array) ? [k.to_s, *v] : [k.to_s, v] @@ -187,6 +241,26 @@ def enqueue(input_source, params) end raise Mindee::Error::MindeeError, 'Could not resolve server response.' end + + # Converts an HTTP response to a parsed response object. + # + # @param response [Net::HTTPResponse, nil] + # @return [Hash] + # @raise Throws if the server returned an error. + def process_response(response) + if !response.nil? && response.respond_to?(:body) && + Mindee::HTTP::ResponseValidation.valid_v2_response?(response) + return JSON.parse(response.body, object_class: Hash) + end + + response_body = if response.nil? || !response.respond_to?(:body) + '{ "status": -1, + "detail": "Empty server response." }' + else + response.body + end + raise Mindee::HTTP::ErrorHandler.generate_v2_error(JSON.parse(response_body).transform_keys(&:to_sym)) + end end end end 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..edaad953a --- /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, nil] 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 = false, guidelines = nil) + @selected = selected.nil? ? false : 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..4c67a870c --- /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.to_json 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/custom/net_http.rbs b/sig/custom/net_http.rbs index 2eef301b4..67260f55a 100644 --- a/sig/custom/net_http.rbs +++ b/sig/custom/net_http.rbs @@ -25,6 +25,19 @@ module Net def basic_auth: (untyped, untyped) -> void def []=: (?untyped, ?untyped) -> bool end + + # Stub for the HTTP PATCH request class. + class Patch + def initialize: (untyped, ?Hash[String, String?]?) -> void + def new: (untyped, untyped) -> void + def body=: (untyped) -> void + end + + # Stub for the HTTP DELETE request class. + class Delete + def initialize: (untyped, ?Hash[String, String?]?) -> void + def new: (untyped, untyped) -> void + end end class HTTPResponse 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..3779b0855 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] @@ -42,17 +44,29 @@ module Mindee def process_response: (Net::HTTPResponse?) -> Hash[String | Symbol, untyped] - def poll: (String) -> Net::HTTPResponse + def req_get_search: [T] (_SearchParameters[T] params) -> T - def enqueue: ( - Input::Source::LocalInputSource | Input::Source::URLInputSource, - ClientOptions::BaseProductParameters - ) -> Net::HTTPResponse? + def req_get_rag_annotation: (_ProductClass[untyped] product, String document_id) -> Parsing::BaseRagAnnotationResponse - def req_get_search: [T] (_SearchParameters[T] params) -> T + def req_post_rag_document: ( + _ProductClass[untyped] product, + ClientOptions::BaseRagDocumentUploadParameters parameters, + Input::Source::LocalInputSource input_source + ) -> Parsing::BaseRagAnnotationResponse + + def req_patch_rag_annotation: ( + _ProductClass[untyped] product, + ClientOptions::BaseAnnotationParameters parameters + ) -> Parsing::BaseRagAnnotationResponse + + def req_delete_rag_document: (_ProductClass[untyped] product, String document_id) -> bool private + def send_get_request: (URI::Generic uri) -> Net::HTTPResponse + + def send_post_request: (URI::Generic uri, Hash[String, untyped] form_data) -> Net::HTTPResponse + def validate_https_url!: (String) -> void end end 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..625de9dea --- /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/client_v2_spec.rb b/spec/v2/client_v2_spec.rb index 6673d0578..b5b230b51 100644 --- a/spec/v2/client_v2_spec.rb +++ b/spec/v2/client_v2_spec.rb @@ -48,7 +48,7 @@ def stub_next_request_with(method, hash:, status_code: 0) it 'enqueue(path) raises MindeeHTTPErrorV2 on 4xx' do expect do - stub_next_request_with(:enqueue, hash: JSON.generate(json400)) + stub_next_request_with(:send_post_request, hash: JSON.generate(json400)) params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( 'dummy-model', raw_text: false, @@ -63,7 +63,7 @@ def stub_next_request_with(method, hash:, status_code: 0) it 'enqueue_and_get_inference(path) raises MindeeHTTPErrorV2 on 4xx' do expect do - stub_next_request_with(:enqueue, hash: JSON.generate(json400)) + stub_next_request_with(:send_post_request, hash: JSON.generate(json400)) params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new('dummy-model') client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, input_doc, params) end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| @@ -76,7 +76,7 @@ def stub_next_request_with(method, hash:, status_code: 0) error_hash = json400.merge({ status: 413, detail: 'File exceeds size limit' }) expect do - stub_next_request_with(:enqueue, hash: JSON.generate(error_hash)) + stub_next_request_with(:send_post_request, hash: JSON.generate(error_hash)) params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new('dummy-model') client.enqueue(Mindee::V2::Product::Extraction::Extraction, input_doc, params) end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| @@ -88,7 +88,7 @@ def stub_next_request_with(method, hash:, status_code: 0) it 'get_job(job_id) returns a fully-formed JobResponse' do json_path = File.join(V2_DATA_DIR, 'job', 'ok_processing.json') parsed = File.read(json_path) - stub_next_request_with(:poll, hash: parsed, status_code: 200) + stub_next_request_with(:send_get_request, hash: parsed, status_code: 200) resp = client.get_job('123e4567-e89b-12d3-a456-426614174000') expect(resp).to be_a(Mindee::V2::Parsing::JobResponse) @@ -103,7 +103,7 @@ def stub_next_request_with(method, hash:, status_code: 0) it 'should deserialize a job properly' do json_path = File.join(V2_DATA_DIR, 'job', 'ok_processed_webhooks_ok.json') parsed = File.read(json_path) - stub_next_request_with(:poll, hash: parsed, status_code: 200) + stub_next_request_with(:send_get_request, hash: parsed, status_code: 200) resp = client.get_job('123e4567-e89b-12d3-a456-426614174000') expect(resp).to be_a(Mindee::V2::Parsing::JobResponse) @@ -135,7 +135,7 @@ def stub_next_request_with(method, hash:, status_code: 0) block.call(http_double) end - api.send(:poll, "#{base_url}/v2/jobs/123e4567-e89b-12d3-a456-426614174000") + api.send(:send_get_request, URI("#{base_url}/v2/jobs/123e4567-e89b-12d3-a456-426614174000")) expect(captured_request).to be_a(Net::HTTP::Get) expect(captured_request['Transfer-Encoding']).to be_nil 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..c07fd0bbc --- /dev/null +++ b/spec/v2/product/extraction/rag_document_spec.rb @@ -0,0 +1,183 @@ +# 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 + let(:expected_annotation_str) do + raw_json = File.read(File.join(V2_PRODUCT_DATA_DIR, 'extraction', 'rag_documents', 'test_annotation.json')) + JSON.generate(JSON.parse(raw_json)) + end + + 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 from an annotation hash' do + annotation = { + 'fields' => { + 'simple' => { 'value' => true }, + 'list' => { 'items' => [] }, + 'object' => { 'fields' => {} }, + }, + } + 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') + req_params = parameters.request_parameters + + expect(req_params['status']).to eq('Active') + expect(req_params['annotation']).to eq(expected_annotation_str) + end + + it 'should init PATCH parameters from an annotation instance' do + annotation = Mindee::V2::Product::Extraction::RagDocuments::RagAnnotation.new( + 'fields' => { + 'simple' => { 'value' => true }, + 'list' => { 'items' => [] }, + 'object' => { 'fields' => {} }, + } + ) + 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') + + req_params = parameters.request_parameters + expect(req_params['status']).to eq('Active') + expect(req_params['annotation']).to eq(expected_annotation_str) + end + + it 'should init PATCH parameters from an annotation string' do + parameters = Mindee::V2::Product::Extraction::RagDocuments::Params::RagDocumentAnnotationParameters.new( + 'invalid-document-id', + status: 'Active', + annotation: '{"fields": {"simple":{"value": true}, "list":{"items": []}, "object":{"fields": {}}}}' + ) + expect(parameters.document_id).to eq('invalid-document-id') + + req_params = parameters.request_parameters + expect(req_params['status']).to eq('Active') + expect(req_params['annotation']).to eq(expected_annotation_str) + 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 From 7f68d8024d35090714ca858136aa09fc3e318334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Mon, 16 Nov 2026 22:33:25 +0100 Subject: [PATCH 2/4] slow down on macos --- .github/workflows/_test-units.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/_test-units.yml b/.github/workflows/_test-units.yml index 4569cdeda..a7b36cb9c 100644 --- a/.github/workflows/_test-units.yml +++ b/.github/workflows/_test-units.yml @@ -21,6 +21,11 @@ jobs: - "3.3" - "3.4" - "4.0" + exclude: + - os: "macos-latest" + ruby: "3.3" + - os: "macos-latest" + ruby: "3.4" steps: - uses: actions/checkout@v7 with: From 767fe82cc208fdc6ca55cf4b407d52644e39af93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Mon, 16 Nov 2026 22:48:16 +0100 Subject: [PATCH 3/4] add client methods --- lib/mindee/v2/client.rb | 257 ++++++++++++++++-- .../base_annotation_parameters.rb | 21 ++ .../base_rag_document_upload_parameters.rb | 21 ++ lib/mindee/v2/http/mindee_api_v2.rb | 2 +- lib/mindee/v2/parsing/base_inference.rb | 3 +- .../parsing/base_rag_annotation_response.rb | 2 +- lib/mindee/v2/product/base_product.rb | 4 +- .../v2/product/extraction/extraction.rb | 2 + .../rag_documents/annotated_base_field.rb | 4 +- .../rag_documents/annotated_list_field.rb | 2 +- .../rag_documents/annotated_object_field.rb | 2 +- .../rag_documents/annotated_simple_field.rb | 2 +- .../rag_document_annotation_parameters.rb | 14 +- sig/mindee/v2/client.rbs | 83 +++++- .../base_annotation_parameters.rbs | 4 + .../base_rag_document_upload_parameters.rbs | 4 + sig/mindee/v2/http/mindee_api_v2.rbs | 36 ++- .../parsing/base_rag_annotation_response.rbs | 2 +- sig/mindee/v2/product/base_product.rbs | 2 + .../v2/product/extraction/extraction.rbs | 2 + .../rag_documents/annotated_base_field.rbs | 4 +- .../rag_documents/annotated_list_field.rbs | 2 +- .../rag_documents/annotated_object_field.rbs | 2 +- .../rag_documents/annotated_simple_field.rbs | 2 +- .../rag_document_annotation_parameters.rbs | 2 + spec/v2/client_v2_spec.rb | 8 +- .../product/extraction/rag_document_spec.rb | 10 +- .../extraction/rag_documents_integration.rb | 95 +++++++ 28 files changed, 521 insertions(+), 73 deletions(-) create mode 100644 spec/v2/product/extraction/rag_documents_integration.rb diff --git a/lib/mindee/v2/client.rb b/lib/mindee/v2/client.rb index f5c6fe596..4059e000d 100644 --- a/lib/mindee/v2/client.rb +++ b/lib/mindee/v2/client.rb @@ -9,6 +9,7 @@ module Mindee module V2 # Mindee V2 API Client. + # rubocop:disable-next Metrics/ClassLength class Client # @return [V2::HTTP::MindeeApiV2] private attr_reader :mindee_api @@ -29,15 +30,7 @@ def enqueue( input_source, params ) - params_instance = case params - when Hash - product.parameters_class.from_hash(params: params) - when Mindee::V2::ClientOptions::BaseProductParameters - params - else - raise ArgumentError, "Expected Hash or BaseProductParameters, got #{params.class}" - end - + params_instance = resolve_product_parameters(product, params) logger.debug("Enqueueing document to model '#{params_instance.model_id}'.") @mindee_api.req_post_product_enqueue(input_source, params_instance) @@ -91,23 +84,8 @@ def enqueue_and_get_result( polling_options = nil, cancellation_token = nil ) - params_instance = case params - when Hash - product.parameters_class.from_hash(params: params) - when Mindee::V2::ClientOptions::BaseProductParameters - params - else - raise ArgumentError, "Expected Hash or BaseProductParameters, got #{params.class}" - end - - polling_options_instance = case polling_options - when Mindee::Input::PollingOptions - polling_options - when Hash - Mindee::Input::PollingOptions.from_hash(polling_options: polling_options) - else - Mindee::Input::PollingOptions.new - end + params_instance = resolve_product_parameters(product, params) + polling_options_instance = resolve_polling_options(polling_options) enqueue_response = enqueue(product, input_source, params_instance) @@ -130,6 +108,127 @@ def enqueue_and_get_result( ) end + # Not recommended for general use, prefer {#upload_and_get_rag_document_poll}. + # You will need to poll until the document is ready for use. + # Add a document to the RAG database. + # @param product [Class] The product class. + # @param input_source [Mindee::Input::Source::LocalInputSource] The file to upload. + # @param params [Hash, ClientOptions::BaseRagDocumentUploadParameters] Upload parameters. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def upload_rag_document( + product, + input_source, + params + ) + params_instance = resolve_rag_upload_parameters(product, params) + logger.info('Adding a document to the RAG database') + @mindee_api.req_post_rag_document(product, params_instance, input_source) + end + + # Add a document to the RAG database and return the initial annotation. + # @param product [Class] The product class. + # @param input_source [Mindee::Input::Source::LocalInputSource] The file to upload. + # @param params [Hash, ClientOptions::BaseRagDocumentUploadParameters] Upload parameters. + # @param polling_options [Hash, Input::PollingOptions, nil] Parameters for polling. + # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def upload_and_get_rag_document( + product, + input_source, + params, + polling_options = nil, + cancellation_token = nil + ) + params_instance = resolve_rag_upload_parameters(product, params) + initial_response = upload_rag_document(product, input_source, params_instance) + + poll_for_rag_document( + product, + initial_response, + resolve_polling_options(polling_options), + cancellation_token + ) + end + + # Not recommended for general use, prefer {#get_ready_rag_document_poll}. + # You will need to poll until the document is ready for use. + # Get a document's info and annotations from the RAG database. + # @param product [Class] The product class. + # @param document_id [String] The document's ID. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def get_rag_document(product, document_id) + logger.info("Getting RAG document ID: #{document_id}") + @mindee_api.req_get_rag_annotation(product, document_id) + end + + # Get a document's info and annotations from the RAG database. + # @param product [Class] The product class. + # @param document_id [String] The document's ID. + # @param polling_options [Hash, Input::PollingOptions, nil] Parameters for polling. + # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def get_ready_rag_document( + product, + document_id, + polling_options = nil, + cancellation_token = nil + ) + initial_response = get_rag_document(product, document_id) + return initial_response unless initial_response.status == 'Processing' + + poll_for_rag_document( + product, + initial_response, + resolve_polling_options(polling_options), + cancellation_token + ) + end + + # Not recommended for general use, prefer {#update_and_get_rag_annotation}. + # You will need to poll until the document is ready for use. + # Update a document's annotations in the RAG database. + # @param product [Class] The product class. + # @param params [Hash, ClientOptions::BaseAnnotationParameters] Annotation parameters. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def update_rag_annotation(product, params) + params_instance = resolve_rag_annotation_parameters(product, params) + logger.info("Updating RAG document ID: #{params_instance.document_id}") + @mindee_api.req_patch_rag_annotation(product, params_instance) + end + + # Update a document's annotations in the RAG database. + # @param product [Class] The product class. + # @param params [Hash, ClientOptions::BaseAnnotationParameters] Annotation parameters. + # @param polling_options [Hash, Input::PollingOptions, nil] Parameters for polling. + # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation. + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + def update_and_get_rag_annotation( + product, + params, + polling_options = nil, + cancellation_token = nil + ) + params_instance = resolve_rag_annotation_parameters(product, params) + initial_response = update_rag_annotation(product, params_instance) + return initial_response unless initial_response.status == 'Processing' + + poll_for_rag_document( + product, + initial_response, + resolve_polling_options(polling_options), + cancellation_token + ) + end + + # Delete a document from the RAG database. + # @param product [Class] The product class. + # @param document_id [String] The document's ID. + # @return [Boolean] + def delete_rag_document(product, document_id) + logger.info("Deleting RAG document ID: #{document_id}") + @mindee_api.req_delete_rag_document(product, document_id) + end + # Search for resources matching the given criteria. # @param params [ClientOptions::BaseSearchParameters] Search parameters. # @return [Mindee::V2::Parsing::Search::BaseSearchResponse] A search response containing the matching resources. @@ -148,6 +247,65 @@ def search_models(model_name, model_type) private + # Normalizes product parameters from an instance or hash. + # @param product [Class] The product class. + # @param parameters [Hash, ClientOptions::BaseProductParameters] Parameters for the inference. + # @return [ClientOptions::BaseProductParameters] + def resolve_product_parameters(product, parameters) + case parameters + when Hash + product.parameters_class.from_hash(params: parameters) + when Mindee::V2::ClientOptions::BaseProductParameters + parameters + else + raise ArgumentError, "Expected Hash or BaseProductParameters, got #{parameters.class}" + end + end + + # Normalizes RAG upload parameters from an instance or hash. + # @param product [Class] The product class. + # @param parameters [Hash, ClientOptions::BaseRagDocumentUploadParameters] Parameters for the upload. + # @return [ClientOptions::BaseRagDocumentUploadParameters] + def resolve_rag_upload_parameters(product, parameters) + case parameters + when Hash + product.rag_document_upload_class.from_hash(params: parameters) + when Mindee::V2::ClientOptions::BaseRagDocumentUploadParameters + parameters + else + raise ArgumentError, "Expected Hash or BaseRagDocumentUploadParameters, got #{parameters.class}" + end + end + + # Normalizes RAG annotation parameters from an instance or hash. + # @param product [Class] The product class. + # @param parameters [Hash, ClientOptions::BaseAnnotationParameters] Parameters for the annotation. + # @return [ClientOptions::BaseAnnotationParameters] + def resolve_rag_annotation_parameters(product, parameters) + case parameters + when Hash + product.annotation_parameters_class.from_hash(params: parameters) + when Mindee::V2::ClientOptions::BaseAnnotationParameters + parameters + else + raise ArgumentError, "Expected Hash or BaseAnnotationParameters, got #{parameters.class}" + end + end + + # Normalizes polling options from an instance, hash, or nil. + # @param polling_options [Hash, Input::PollingOptions, nil] + # @return [Input::PollingOptions] + def resolve_polling_options(polling_options) + case polling_options + when Mindee::Input::PollingOptions + polling_options + when Hash + Mindee::Input::PollingOptions.from_hash(polling_options: polling_options) + else + Mindee::Input::PollingOptions.new + end + end + # Checks if all webhooks associated with a job have finished processing. # @param job_response [Mindee::V2::Parsing::JobResponse] # @return [Boolean] @@ -268,6 +426,53 @@ def poll_for_product_result( get_result_by_url(product, job_response.job.result_url) end + + # Poll until the RAG document is finished processing or the max number of attempts is reached. + # @param product [Class] + # @param initial_response [Mindee::V2::Parsing::BaseRagAnnotationResponse] + # @param polling_options [Input::PollingOptions] + # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] + # @return [Mindee::V2::Parsing::BaseRagAnnotationResponse] + # @raise [Mindee::Error::MindeeError] Thrown when max_retries is reached and the annotation isn't ready. + def poll_for_rag_document( + product, + initial_response, + polling_options, + cancellation_token = nil + ) + logger.info("Polling for RAG document ID: #{initial_response.id}") + + logger.debug( + "Waiting #{polling_options.initial_delay_sec} seconds before attempting to retrieve the result..." + ) + sleep(polling_options.initial_delay_sec) + + try_counter = 0 + max_retries = polling_options.max_retries + document_id = initial_response.id + + while try_counter < max_retries + raise Mindee::Error::MindeeError, 'Polling was canceled.' if cancellation_token&.canceled? + + logger.debug( + "Poll attempt #{try_counter + 1} of #{max_retries}" + ) + + response = @mindee_api.req_get_rag_annotation(product, document_id) + try_counter += 1 + + case response.status + when 'Processing' + throttle_polling_attempt(try_counter, max_retries, polling_options, cancellation_token) + when 'Failed' + raise Mindee::Error::MindeeError, 'RAG failed without an error payload.' + else + return response + end + end + + raise Mindee::Error::MindeeError, "RAG polling not complete after #{try_counter} attempts." + end end end end diff --git a/lib/mindee/v2/client_options/base_annotation_parameters.rb b/lib/mindee/v2/client_options/base_annotation_parameters.rb index b2b74fb67..e5788eae9 100644 --- a/lib/mindee/v2/client_options/base_annotation_parameters.rb +++ b/lib/mindee/v2/client_options/base_annotation_parameters.rb @@ -19,6 +19,27 @@ def initialize(document_id) @document_id = trimmed_id end + # Load from a hash + # @param [Hash] params Parameters to provide as a hash. + # @return [BaseAnnotationParameters] + def self.from_hash(params: {}) + load_from_hash(params: params) + new(params[:document_id]) + end + + # Loads parameters from a Hash. + # @param [Hash] params Parameters to provide as a hash. + # @return [Hash] + def self.load_from_hash(params: {}) + params.transform_keys!(&:to_sym) + + if params.empty? || params[:document_id].nil? || params[:document_id].to_s.strip.empty? + raise Error::MindeeInputError, 'Document ID is required.' + end + + params + end + # Gets the request parameters for the upload request. # @return [Hash[String, String]] def request_parameters 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 index 2188dcb71..a10ceefbb 100644 --- a/lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb +++ b/lib/mindee/v2/client_options/base_rag_document_upload_parameters.rb @@ -18,6 +18,27 @@ def initialize(model_id) @model_id = trimmed_id end + # Load from a hash + # @param [Hash] params Parameters to provide as a hash. + # @return [BaseRagDocumentUploadParameters] + def self.from_hash(params: {}) + load_from_hash(params: params) + new(params[:model_id]) + end + + # Loads parameters from a Hash. + # @param [Hash] params Parameters to provide as a hash. + # @return [Hash] + def self.load_from_hash(params: {}) + params.transform_keys!(&:to_sym) + + if params.empty? || params[:model_id].nil? || params[:model_id].to_s.strip.empty? + raise Error::MindeeInputError, 'Model ID is required.' + end + + params + end + # Gets the request parameters for the upload request. # @return [Hash[String, String]] def request_parameters diff --git a/lib/mindee/v2/http/mindee_api_v2.rb b/lib/mindee/v2/http/mindee_api_v2.rb index 68b921ab5..01b172a7a 100644 --- a/lib/mindee/v2/http/mindee_api_v2.rb +++ b/lib/mindee/v2/http/mindee_api_v2.rb @@ -30,7 +30,7 @@ def req_post_product_enqueue(input_source, params) form_data = {} # : Hash[String, untyped] if input_source.is_a?(Mindee::Input::Source::URLInputSource) - form_data['uri'] = input_source.url + form_data['url'] = input_source.url else file_data, file_metadata = input_source.read_contents(close: params.close_file) form_data['file'] = [file_data, file_metadata] diff --git a/lib/mindee/v2/parsing/base_inference.rb b/lib/mindee/v2/parsing/base_inference.rb index 8e62cf022..c9ea49bc8 100644 --- a/lib/mindee/v2/parsing/base_inference.rb +++ b/lib/mindee/v2/parsing/base_inference.rb @@ -7,7 +7,7 @@ module Mindee module V2 module Parsing # Base class for V2 inference responses. - class BaseInference < Mindee::V2::Product::BaseProduct + class BaseInference # @return [InferenceJob] Metadata about the job. attr_reader :job # @return [V2::Parsing::InferenceModel] Model info for the inference. @@ -20,7 +20,6 @@ class BaseInference < Mindee::V2::Product::BaseProduct def initialize(http_response) raise ArgumentError, 'Server response must be a Hash' unless http_response.is_a?(Hash) - super() @model = Mindee::V2::Parsing::InferenceModel.new(http_response['model']) @file = Mindee::V2::Parsing::InferenceFile.new(http_response['file']) @id = http_response['id'] diff --git a/lib/mindee/v2/parsing/base_rag_annotation_response.rb b/lib/mindee/v2/parsing/base_rag_annotation_response.rb index 289203919..bbbe6db97 100644 --- a/lib/mindee/v2/parsing/base_rag_annotation_response.rb +++ b/lib/mindee/v2/parsing/base_rag_annotation_response.rb @@ -7,7 +7,7 @@ module Mindee module V2 module Parsing # Base class for all RAG document responses from the V2 API. - class BaseRagAnnotationResponse < BaseInferenceResponse + class BaseRagAnnotationResponse < Mindee::V2::Parsing::BaseResponse # Unique identifier of the RAG document. # @return [String] attr_reader :id diff --git a/lib/mindee/v2/product/base_product.rb b/lib/mindee/v2/product/base_product.rb index f0ef1324a..e311ecc03 100644 --- a/lib/mindee/v2/product/base_product.rb +++ b/lib/mindee/v2/product/base_product.rb @@ -33,7 +33,9 @@ class << self attr_reader :parameters_class, :slug, :response_class, - :annotation_response_class + :annotation_response_class, + :annotation_parameters_class, + :rag_document_upload_class end end end diff --git a/lib/mindee/v2/product/extraction/extraction.rb b/lib/mindee/v2/product/extraction/extraction.rb index d30453bb1..42946095d 100644 --- a/lib/mindee/v2/product/extraction/extraction.rb +++ b/lib/mindee/v2/product/extraction/extraction.rb @@ -18,6 +18,8 @@ class Extraction < Product::BaseProduct @parameters_class = Product::Extraction::Params::ExtractionParameters @response_class = Product::Extraction::ExtractionResponse @annotation_response_class = Product::Extraction::RagDocuments::ExtractionRagAnnotationResponse + @annotation_parameters_class = Product::Extraction::RagDocuments::Params::RagDocumentAnnotationParameters + @rag_document_upload_class = Product::Extraction::RagDocuments::Params::RagDocumentUploadParameters end end end 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 index edaad953a..322b70d3c 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb @@ -11,11 +11,11 @@ module RagDocuments class AnnotatedBaseField # When true, use the RAG information for the final result. When false, use the Data Schema information. # @return [Boolean] - attr_reader :selected + attr_accessor :selected # Guidelines or instructions for processing this field. # @return [String, nil] - attr_reader :guidelines + attr_accessor :guidelines # Default constructor. # @param selected [Boolean, nil] When true, use the RAG information for the final result. 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 index 1286eb7ea..3cea1a89c 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_list_field.rb @@ -14,7 +14,7 @@ module RagDocuments class AnnotatedListField < AnnotatedBaseField # Items contained in the list. # @return [Array[AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField]] - attr_reader :items + attr_accessor :items # Default constructor. # @param server_response [Hash[String | Symbol, untyped]] 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 index 72a8c3284..0e329da73 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_object_field.rb @@ -14,7 +14,7 @@ module RagDocuments class AnnotatedObjectField < AnnotatedBaseField # Nested fields carried by this object. # @return [AnnotatedFields] - attr_reader :fields + attr_accessor :fields # Default constructor. # @param server_response [Hash[String | Symbol, untyped]] 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 index 061d5b291..a88835155 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rb @@ -11,7 +11,7 @@ module RagDocuments class AnnotatedSimpleField < AnnotatedBaseField # Field value, one of: string, boolean, number, null. # @return [String, Boolean, Numeric, nil] - attr_reader :value + attr_accessor :value # Default constructor. # @param server_response [Hash[String | Symbol, untyped]] 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 index 4c67a870c..8e7de6ada 100644 --- 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 @@ -32,12 +32,24 @@ def initialize(document_id, status: nil, annotation: nil) @annotation = parse_annotation(annotation) end + # Load from a hash + # @param [Hash] params Parameters to provide as a hash. + # @return [RagDocumentAnnotationParameters] + def self.from_hash(params: {}) + load_from_hash(params: params) + new( + params[:document_id], + status: params[:status], + annotation: params[: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.to_json if @annotation + parameters['annotation'] = @annotation.to_h if @annotation parameters end diff --git a/sig/mindee/v2/client.rbs b/sig/mindee/v2/client.rbs index a82ccacc6..c9a64d7fa 100644 --- a/sig/mindee/v2/client.rbs +++ b/sig/mindee/v2/client.rbs @@ -11,21 +11,21 @@ module Mindee def initialize: (?api_key: String) -> void - def get_result: [T] (HTTP::_ProductClass[T] product, String resource) -> T + def get_result: [T < Parsing::BaseResponse] (HTTP::_ProductClass[T] product, String resource) -> T def get_job: (String job_id) -> V2::Parsing::JobResponse - def get_result_by_url: [T] (HTTP::_ProductClass[T] product, String url) -> T + def get_result_by_url: [T < Parsing::BaseResponse] (HTTP::_ProductClass[T] product, String url) -> T def get_job_by_url: (String url) -> V2::Parsing::JobResponse - def enqueue: [T] ( + def enqueue: [T < Parsing::BaseResponse] ( HTTP::_ProductClass[T] product, Input::Source::LocalInputSource | Input::Source::URLInputSource input_source, Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params ) -> V2::Parsing::JobResponse - def enqueue_and_get_result: [T] ( + def enqueue_and_get_result: [T < Parsing::BaseResponse] ( HTTP::_ProductClass[T] product, Input::Source::LocalInputSource | Input::Source::URLInputSource input_source, Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params, @@ -33,7 +33,50 @@ module Mindee ?Mindee::HTTP::CancellationToken | nil cancellation_token ) -> T - def search: [T] (HTTP::_SearchParameters[T] params) -> T + def upload_rag_document: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + Input::Source::LocalInputSource input_source, + Hash[String | Symbol, untyped] | ClientOptions::BaseRagDocumentUploadParameters params + ) -> T + + def upload_and_get_rag_document: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + Input::Source::LocalInputSource input_source, + Hash[String | Symbol, untyped] | ClientOptions::BaseRagDocumentUploadParameters params, + ?Hash[String | Symbol, untyped] | Input::PollingOptions | nil polling_options, + ?Mindee::HTTP::CancellationToken | nil cancellation_token + ) -> T + + def get_rag_document: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + String document_id + ) -> T + + def get_ready_rag_document: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + String document_id, + ?Hash[String | Symbol, untyped] | Input::PollingOptions | nil polling_options, + ?Mindee::HTTP::CancellationToken | nil cancellation_token + ) -> T + + def update_rag_annotation: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + Hash[String | Symbol, untyped] | ClientOptions::BaseAnnotationParameters params + ) -> T + + def update_and_get_rag_annotation: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + Hash[String | Symbol, untyped] | ClientOptions::BaseAnnotationParameters params, + ?Hash[String | Symbol, untyped] | Input::PollingOptions | nil polling_options, + ?Mindee::HTTP::CancellationToken | nil cancellation_token + ) -> T + + def delete_rag_document: ( + HTTP::_RagProductClass[untyped] | HTTP::_ProductClass[untyped] product, + String document_id + ) -> bool + + def search: [T < Parsing::Search::BaseSearchResponse] (HTTP::_SearchParameters[T] params) -> T # Deprecated: use `search` instead. def search_models: (String?, String?) -> Search::Models::ModelSearchResponse @@ -42,15 +85,41 @@ module Mindee private + def resolve_product_parameters: ( + HTTP::_ProductClass[untyped] product, + Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters parameters, + ) -> ClientOptions::BaseProductParameters + + def resolve_rag_upload_parameters: ( + HTTP::_RagProductClass[untyped] product, + Hash[String | Symbol, untyped] | ClientOptions::BaseRagDocumentUploadParameters parameters + ) -> ClientOptions::BaseRagDocumentUploadParameters + + def resolve_rag_annotation_parameters: ( + HTTP::_RagProductClass[untyped] product, + Hash[String | Symbol, untyped] | ClientOptions::BaseAnnotationParameters parameters + ) -> ClientOptions::BaseAnnotationParameters + + def resolve_polling_options: ( + Hash[String | Symbol, untyped] | Input::PollingOptions | nil polling_options + ) -> Input::PollingOptions + def check_webhooks_done?: (V2::Parsing::JobResponse job_response) -> bool - def poll_for_product_result: [T] ( + def poll_for_product_result: [T < Parsing::BaseResponse] ( HTTP::_ProductClass[T] product, V2::Parsing::JobResponse enqueu_response, Input::PollingOptions polling_options, ?bool wait_for_webhooks, ?(Mindee::HTTP::CancellationToken | nil) cancellation_token - ) -> T + ) -> T + + def poll_for_rag_document: [T < Parsing::BaseRagAnnotationResponse] ( + HTTP::_RagProductClass[T] product, + T initial_response, + Input::PollingOptions polling_options, + ?(Mindee::HTTP::CancellationToken | nil) cancellation_token + ) -> T def poll_on_job: ( V2::Parsing::JobResponse initial_response, diff --git a/sig/mindee/v2/client_options/base_annotation_parameters.rbs b/sig/mindee/v2/client_options/base_annotation_parameters.rbs index bb62e38ff..6c144a2fc 100644 --- a/sig/mindee/v2/client_options/base_annotation_parameters.rbs +++ b/sig/mindee/v2/client_options/base_annotation_parameters.rbs @@ -8,6 +8,10 @@ module Mindee def initialize: (String document_id) -> void + def self.from_hash: (?params: Hash[String | Symbol, untyped]) -> BaseAnnotationParameters + + def self.load_from_hash: (?params: Hash[String | Symbol, untyped]) -> Hash[String | Symbol, untyped] + def request_parameters: -> Hash[String, String] 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 index 3aa69def8..353cd3deb 100644 --- a/sig/mindee/v2/client_options/base_rag_document_upload_parameters.rbs +++ b/sig/mindee/v2/client_options/base_rag_document_upload_parameters.rbs @@ -8,6 +8,10 @@ module Mindee def initialize: (String model_id) -> void + def self.from_hash: (?params: Hash[String | Symbol, untyped]) -> BaseRagDocumentUploadParameters + + def self.load_from_hash: (?params: Hash[String | Symbol, untyped]) -> Hash[String | Symbol, untyped] + def request_parameters: -> Hash[String, String] end end diff --git a/sig/mindee/v2/http/mindee_api_v2.rbs b/sig/mindee/v2/http/mindee_api_v2.rbs index 3779b0855..1f6981c6e 100644 --- a/sig/mindee/v2/http/mindee_api_v2.rbs +++ b/sig/mindee/v2/http/mindee_api_v2.rbs @@ -6,17 +6,25 @@ module Mindee def new: (Hash[String | Symbol, untyped]) -> T end - interface _ProductClass[T] + interface _ProductClass[T < Parsing::BaseResponse] def slug: () -> String def response_class: () -> _ResponseFactory[T] def parameters_class: () -> singleton(ClientOptions::BaseProductParameters) + end + + interface _RagProductClass[T < Parsing::BaseRagAnnotationResponse] + def slug: () -> String + + def annotation_response_class: () -> _ResponseFactory[T] + + def annotation_parameters_class: () -> singleton(ClientOptions::BaseAnnotationParameters) - def annotation_response_class: () -> singleton(Parsing::BaseRagAnnotationResponse) + def rag_document_upload_class: () -> singleton(ClientOptions::BaseRagDocumentUploadParameters) end - interface _SearchParameters[T] + interface _SearchParameters[T < Parsing::Search::BaseSearchResponse] def slug: () -> String def response_class: () -> _ResponseFactory[T] @@ -29,7 +37,7 @@ module Mindee def initialize: (?api_key: String?) -> void - def req_get_result_by_id: [T] (_ProductClass[T] product, String inference_id) -> T + def req_get_result_by_id: [T < Parsing::BaseResponse] (_ProductClass[T] product, String inference_id) -> T def req_post_product_enqueue: ( Input::Source::LocalInputSource | Input::Source::URLInputSource, @@ -40,26 +48,26 @@ module Mindee def req_get_job_by_url: (String url) -> V2::Parsing::JobResponse - def req_get_result_by_url: [T] (_ProductClass[T] product, String url) -> T + def req_get_result_by_url: [T < Parsing::BaseResponse] (_ProductClass[T] product, String url) -> T def process_response: (Net::HTTPResponse?) -> Hash[String | Symbol, untyped] - def req_get_search: [T] (_SearchParameters[T] params) -> T + def req_get_search: [T < Parsing::Search::BaseSearchResponse] (_SearchParameters[T] params) -> T - def req_get_rag_annotation: (_ProductClass[untyped] product, String document_id) -> Parsing::BaseRagAnnotationResponse + def req_get_rag_annotation: [T < Parsing::BaseRagAnnotationResponse] (_RagProductClass[T] product, String document_id) -> T - def req_post_rag_document: ( - _ProductClass[untyped] product, + def req_post_rag_document: [T < Parsing::BaseRagAnnotationResponse] ( + _RagProductClass[T] product, ClientOptions::BaseRagDocumentUploadParameters parameters, Input::Source::LocalInputSource input_source - ) -> Parsing::BaseRagAnnotationResponse + ) -> T - def req_patch_rag_annotation: ( - _ProductClass[untyped] product, + def req_patch_rag_annotation: [T < Parsing::BaseRagAnnotationResponse] ( + _RagProductClass[T] product, ClientOptions::BaseAnnotationParameters parameters - ) -> Parsing::BaseRagAnnotationResponse + ) -> T - def req_delete_rag_document: (_ProductClass[untyped] product, String document_id) -> bool + def req_delete_rag_document: (_RagProductClass[untyped] | _ProductClass[untyped] product, String document_id) -> bool private diff --git a/sig/mindee/v2/parsing/base_rag_annotation_response.rbs b/sig/mindee/v2/parsing/base_rag_annotation_response.rbs index 39c11083f..cd75c76d3 100644 --- a/sig/mindee/v2/parsing/base_rag_annotation_response.rbs +++ b/sig/mindee/v2/parsing/base_rag_annotation_response.rbs @@ -3,7 +3,7 @@ module Mindee module V2 module Parsing - class BaseRagAnnotationResponse < BaseInferenceResponse[untyped] + class BaseRagAnnotationResponse < BaseResponse attr_reader id: String attr_reader filename: String attr_reader created_at: Time diff --git a/sig/mindee/v2/product/base_product.rbs b/sig/mindee/v2/product/base_product.rbs index 9317ea21e..3d3c63ae5 100644 --- a/sig/mindee/v2/product/base_product.rbs +++ b/sig/mindee/v2/product/base_product.rbs @@ -15,6 +15,8 @@ module Mindee 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) + attr_reader self.annotation_parameters_class: singleton(ClientOptions::BaseAnnotationParameters) + attr_reader self.rag_document_upload_class: singleton(ClientOptions::BaseRagDocumentUploadParameters) def initialize: -> void end diff --git a/sig/mindee/v2/product/extraction/extraction.rbs b/sig/mindee/v2/product/extraction/extraction.rbs index 5a5d2dd4f..3463e9846 100644 --- a/sig/mindee/v2/product/extraction/extraction.rbs +++ b/sig/mindee/v2/product/extraction/extraction.rbs @@ -8,6 +8,8 @@ module Mindee self.@parameters_class: singleton(Params::ExtractionParameters) self.@response_class: singleton(ExtractionResponse) self.@annotation_response_class: singleton(RagDocuments::ExtractionRagAnnotationResponse) + self.@annotation_parameters_class: singleton(RagDocuments::Params::RagDocumentAnnotationParameters) + self.@rag_document_upload_class: singleton(RagDocuments::Params::RagDocumentUploadParameters) self.@slug: String end 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 index 625de9dea..57feafc08 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs @@ -6,8 +6,8 @@ module Mindee module Extraction module RagDocuments class AnnotatedBaseField - attr_reader selected: bool - attr_reader guidelines: String? + attr_accessor selected: bool + attr_accessor guidelines: String? def initialize: (?bool?, ?String?) -> void def to_h: () -> Hash[String, untyped] 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 index 57289c5e5..106b701bc 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/annotated_list_field.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_list_field.rbs @@ -6,7 +6,7 @@ module Mindee module Extraction module RagDocuments class AnnotatedListField < AnnotatedBaseField - attr_reader items: Array[AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField] + attr_accessor items: Array[AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField] def initialize: (Hash[String | Symbol, untyped]) -> void def simple_items: () -> Array[AnnotatedSimpleField] 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 index 3bbd2c4b5..c1237ab90 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/annotated_object_field.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_object_field.rbs @@ -6,7 +6,7 @@ module Mindee module Extraction module RagDocuments class AnnotatedObjectField < AnnotatedBaseField - attr_reader fields: AnnotatedFields + attr_accessor fields: AnnotatedFields def initialize: (Hash[String | Symbol, untyped]) -> void def get_simple_field: (String) -> AnnotatedSimpleField 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 index f97e98daf..7a4a08e4e 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_simple_field.rbs @@ -6,7 +6,7 @@ module Mindee module Extraction module RagDocuments class AnnotatedSimpleField < AnnotatedBaseField - attr_reader value: (String | bool | Numeric)? + attr_accessor value: (String | bool | Numeric)? def initialize: (Hash[String | Symbol, untyped]) -> void def to_h: () -> Hash[String, untyped] 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 index 64927e132..3137636b5 100644 --- 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 @@ -16,6 +16,8 @@ module Mindee ?annotation: RagAnnotation | String | Hash[String | Symbol, untyped]? ) -> void + def self.from_hash: (?params: Hash[String | Symbol, untyped]) -> RagDocumentAnnotationParameters + def request_parameters: -> Hash[String, untyped] private diff --git a/spec/v2/client_v2_spec.rb b/spec/v2/client_v2_spec.rb index b5b230b51..0cb5c3e09 100644 --- a/spec/v2/client_v2_spec.rb +++ b/spec/v2/client_v2_spec.rb @@ -49,11 +49,11 @@ def stub_next_request_with(method, hash:, status_code: 0) it 'enqueue(path) raises MindeeHTTPErrorV2 on 4xx' do expect do stub_next_request_with(:send_post_request, hash: JSON.generate(json400)) - params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - 'dummy-model', + params = { + model_id: 'dummy-model', raw_text: false, - text_context: 'Hello my name is mud.' - ) + text_context: 'Hello my name is mud.', + } client.enqueue(Mindee::V2::Product::Extraction::Extraction, input_doc, params) end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| expect(err.status).to eq(400) diff --git a/spec/v2/product/extraction/rag_document_spec.rb b/spec/v2/product/extraction/rag_document_spec.rb index c07fd0bbc..8f178c715 100644 --- a/spec/v2/product/extraction/rag_document_spec.rb +++ b/spec/v2/product/extraction/rag_document_spec.rb @@ -9,9 +9,9 @@ require 'mindee/v2/product/extraction/rag_documents/params/rag_document_annotation_parameters' describe Mindee::V2::Product::Extraction::RagDocuments do - let(:expected_annotation_str) do + let(:expected_annotation) do raw_json = File.read(File.join(V2_PRODUCT_DATA_DIR, 'extraction', 'rag_documents', 'test_annotation.json')) - JSON.generate(JSON.parse(raw_json)) + JSON.parse(raw_json) end def get_response(relative_path) @@ -46,7 +46,7 @@ def get_response(relative_path) req_params = parameters.request_parameters expect(req_params['status']).to eq('Active') - expect(req_params['annotation']).to eq(expected_annotation_str) + expect(req_params['annotation']).to eq(expected_annotation) end it 'should init PATCH parameters from an annotation instance' do @@ -66,7 +66,7 @@ def get_response(relative_path) req_params = parameters.request_parameters expect(req_params['status']).to eq('Active') - expect(req_params['annotation']).to eq(expected_annotation_str) + expect(req_params['annotation']).to eq(expected_annotation) end it 'should init PATCH parameters from an annotation string' do @@ -79,7 +79,7 @@ def get_response(relative_path) req_params = parameters.request_parameters expect(req_params['status']).to eq('Active') - expect(req_params['annotation']).to eq(expected_annotation_str) + expect(req_params['annotation']).to eq(expected_annotation) end it 'should load a POST response from a JSON string' do diff --git a/spec/v2/product/extraction/rag_documents_integration.rb b/spec/v2/product/extraction/rag_documents_integration.rb new file mode 100644 index 000000000..385e67b19 --- /dev/null +++ b/spec/v2/product/extraction/rag_documents_integration.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'mindee' +require 'mindee/v2/product' + +describe Mindee::V2::Product::Extraction::RagDocuments, :integration, :v2 do + let(:extraction_model_id) do + model_id = ENV.fetch('MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID', '') + raise 'Missing MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID environment variable.' if model_id.empty? + + model_id + end + + let(:v2_client) do + Mindee::V2::Client.new + end + + it 'should perform the entire lifecycle of a RAG document' do + input_source = Mindee::Input::Source::PathInputSource.new( + File.join(V2_PRODUCT_DATA_DIR, 'extraction', 'financial_document', 'default_sample.jpg') + ) + + post_response = v2_client.upload_and_get_rag_document( + Mindee::V2::Product::Extraction::Extraction, + input_source, + { model_id: extraction_model_id } + ) + expect(post_response).not_to be_nil + + post_annotation = post_response.annotation + expect(post_annotation).not_to be_nil + expect(post_annotation.fields).not_to be_nil + + document_id = post_response.id + expect(document_id).not_to be_nil + expect(document_id).not_to be_empty + + expect(post_response.status).to eq('Draft') + + post_annotation.fields.get_simple_field('supplier_name').selected = true + post_annotation.fields.get_simple_field('supplier_name').guidelines = 'I am the walrus!' + post_annotation.fields.get_simple_field('invoice_number').selected = true + post_annotation.fields.get_simple_field('invoice_number').guidelines = 'koo koo katchoo!' + + patch_annotation_response = v2_client.update_rag_annotation( + Mindee::V2::Product::Extraction::Extraction, + { document_id: document_id, annotation: post_annotation } + ) + expect(patch_annotation_response).not_to be_nil + patch_annotation = patch_annotation_response.annotation + expect(patch_annotation).not_to be_nil + expect(patch_annotation.fields.get_simple_field('supplier_name').guidelines).to eq('I am the walrus!') + expect(patch_annotation.fields.get_simple_field('supplier_name').selected).to be(true) + expect(patch_annotation.fields.get_simple_field('invoice_number').guidelines).to eq('koo koo katchoo!') + expect(patch_annotation.fields.get_simple_field('invoice_number').selected).to be(true) + + get_response = v2_client.get_ready_rag_document( + Mindee::V2::Product::Extraction::Extraction, + document_id + ) + expect(get_response).not_to be_nil + get_annotation = get_response.annotation + expect(get_annotation).not_to be_nil + expect(get_response.status).to eq('Draft') + expect(get_annotation.fields.get_simple_field('supplier_name').guidelines).to eq('I am the walrus!') + expect(get_annotation.fields.get_simple_field('supplier_name').selected).to be(true) + expect(get_annotation.fields.get_simple_field('invoice_number').guidelines).to eq('koo koo katchoo!') + expect(get_annotation.fields.get_simple_field('invoice_number').selected).to be(true) + + patch_status_params = Mindee::V2::Product::Extraction::RagDocuments::Params::RagDocumentAnnotationParameters.new( + document_id, + status: 'Active' + ) + patch_status_response = v2_client.update_and_get_rag_annotation( + Mindee::V2::Product::Extraction::Extraction, + patch_status_params, + { initial_delay_sec: 1.2 } + ) + expect(patch_status_response).not_to be_nil + expect(patch_status_response.status).to eq('Active') + + delete_response = v2_client.delete_rag_document( + Mindee::V2::Product::Extraction::Extraction, + document_id + ) + expect(delete_response).to be(true) + + expect do + v2_client.get_rag_document( + Mindee::V2::Product::Extraction::Extraction, + document_id + ) + end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) + end +end From a53153a3f7c6631dd4b8d5fa1d8a7bf572539ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Tue, 17 Nov 2026 17:51:24 +0100 Subject: [PATCH 4/4] simplify --- .../rag_documents/annotated_base_field.rb | 6 ------ .../extraction/rag_documents/annotated_fields.rb | 6 ------ .../extraction/rag_documents/rag_annotation.rb | 6 ------ sig/mindee/v2/product/base_product.rbs | 7 ------- .../v2/product/classification/classification.rbs | 4 +++- sig/mindee/v2/product/crop/crop.rbs | 4 +++- sig/mindee/v2/product/extraction/extraction.rbs | 13 ++++++------- .../rag_documents/annotated_base_field.rbs | 5 +++-- .../extraction/rag_documents/annotated_fields.rbs | 1 - .../extraction_rag_annotation_response.rbs | 2 +- .../extraction/rag_documents/rag_annotation.rbs | 1 - sig/mindee/v2/product/ocr/ocr.rbs | 4 +++- sig/mindee/v2/product/split/split.rbs | 4 +++- .../product/extraction/extraction_parameter_spec.rb | 2 +- 14 files changed, 23 insertions(+), 42 deletions(-) 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 index 322b70d3c..70a6e5048 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_base_field.rb @@ -35,12 +35,6 @@ def to_h } 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. diff --git a/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb b/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb index 1642e3cba..55f0abd4b 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/annotated_fields.rb @@ -63,12 +63,6 @@ def get_list_field(field_name) 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 diff --git a/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb b/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb index 71de0dd42..72504657b 100644 --- a/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb +++ b/lib/mindee/v2/product/extraction/rag_documents/rag_annotation.rb @@ -25,12 +25,6 @@ def initialize(server_response) 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 diff --git a/sig/mindee/v2/product/base_product.rbs b/sig/mindee/v2/product/base_product.rbs index 3d3c63ae5..28d8392b9 100644 --- a/sig/mindee/v2/product/base_product.rbs +++ b/sig/mindee/v2/product/base_product.rbs @@ -2,13 +2,6 @@ module Mindee module V2 module Product class BaseProduct - self.@slug: String - 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._parameters_class: () -> singleton(ClientOptions::BaseProductParameters) attr_reader self.slug: String diff --git a/sig/mindee/v2/product/classification/classification.rbs b/sig/mindee/v2/product/classification/classification.rbs index f80975820..e102964af 100644 --- a/sig/mindee/v2/product/classification/classification.rbs +++ b/sig/mindee/v2/product/classification/classification.rbs @@ -2,7 +2,9 @@ module Mindee module V2 module Product module Classification - class Classification < BaseProduct + class Classification < Product::BaseProduct + def self.parameters_class: () -> singleton(Params::ClassificationParameters) + def self.response_class: () -> singleton(ClassificationResponse) end end end diff --git a/sig/mindee/v2/product/crop/crop.rbs b/sig/mindee/v2/product/crop/crop.rbs index 137746859..e49e07be0 100644 --- a/sig/mindee/v2/product/crop/crop.rbs +++ b/sig/mindee/v2/product/crop/crop.rbs @@ -2,7 +2,9 @@ module Mindee module V2 module Product module Crop - class Crop < BaseProduct + class Crop < Product::BaseProduct + def self.parameters_class: () -> singleton(Params::CropParameters) + def self.response_class: () -> singleton(CropResponse) end end end diff --git a/sig/mindee/v2/product/extraction/extraction.rbs b/sig/mindee/v2/product/extraction/extraction.rbs index 3463e9846..65867b924 100644 --- a/sig/mindee/v2/product/extraction/extraction.rbs +++ b/sig/mindee/v2/product/extraction/extraction.rbs @@ -4,13 +4,12 @@ module Mindee module V2 module Product module Extraction - class Extraction - self.@parameters_class: singleton(Params::ExtractionParameters) - self.@response_class: singleton(ExtractionResponse) - self.@annotation_response_class: singleton(RagDocuments::ExtractionRagAnnotationResponse) - self.@annotation_parameters_class: singleton(RagDocuments::Params::RagDocumentAnnotationParameters) - self.@rag_document_upload_class: singleton(RagDocuments::Params::RagDocumentUploadParameters) - self.@slug: String + class Extraction < Product::BaseProduct + def self.parameters_class: () -> singleton(Params::ExtractionParameters) + def self.response_class: () -> singleton(ExtractionResponse) + def self.annotation_response_class: () -> singleton(RagDocuments::ExtractionRagAnnotationResponse) + def self.annotation_parameters_class: () -> singleton(RagDocuments::Params::RagDocumentAnnotationParameters) + def self.rag_document_upload_class: () -> singleton(RagDocuments::Params::RagDocumentUploadParameters) end end 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 index 57feafc08..3b876a116 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_base_field.rbs @@ -11,8 +11,9 @@ module Mindee 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) + def self.create_annotated_field: ( + Hash[String | Symbol, untyped] + ) -> (AnnotatedListField | AnnotatedObjectField | AnnotatedSimpleField) 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 index ac63b74da..cd7f0aa68 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/annotated_fields.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/annotated_fields.rbs @@ -11,7 +11,6 @@ module Mindee 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 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 index 68113d33b..ff2aa14ef 100644 --- 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 @@ -5,7 +5,7 @@ module Mindee module Product module Extraction module RagDocuments - class ExtractionRagAnnotationResponse < Mindee::V2::Parsing::BaseRagAnnotationResponse + class ExtractionRagAnnotationResponse < Parsing::BaseRagAnnotationResponse attr_reader model_id: String attr_reader total_matches: Integer attr_reader last_match_at: Time? diff --git a/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs b/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs index d2df18e6e..0fe9f07ee 100644 --- a/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs +++ b/sig/mindee/v2/product/extraction/rag_documents/rag_annotation.rbs @@ -10,7 +10,6 @@ module Mindee def initialize: (Hash[String | Symbol, untyped]) -> void def to_h: () -> Hash[String, untyped] - def to_json: (?untyped) -> String end end end diff --git a/sig/mindee/v2/product/ocr/ocr.rbs b/sig/mindee/v2/product/ocr/ocr.rbs index 2abff6d91..37d99fc65 100644 --- a/sig/mindee/v2/product/ocr/ocr.rbs +++ b/sig/mindee/v2/product/ocr/ocr.rbs @@ -2,7 +2,9 @@ module Mindee module V2 module Product module OCR - class OCR < BaseProduct + class OCR < Product::BaseProduct + def self.parameters_class: () -> singleton(Params::OCRParameters) + def self.response_class: () -> singleton(OCRResponse) end end end diff --git a/sig/mindee/v2/product/split/split.rbs b/sig/mindee/v2/product/split/split.rbs index 835a2de1c..f17acf71f 100644 --- a/sig/mindee/v2/product/split/split.rbs +++ b/sig/mindee/v2/product/split/split.rbs @@ -2,7 +2,9 @@ module Mindee module V2 module Product module Split - class Split < BaseProduct + class Split < Product::BaseProduct + def self.parameters_class: () -> singleton(Params::SplitParameters) + def self.response_class: () -> singleton(SplitResponse) end end end diff --git a/spec/v2/product/extraction/extraction_parameter_spec.rb b/spec/v2/product/extraction/extraction_parameter_spec.rb index 50cabc2a5..273364b06 100644 --- a/spec/v2/product/extraction/extraction_parameter_spec.rb +++ b/spec/v2/product/extraction/extraction_parameter_spec.rb @@ -9,7 +9,7 @@ describe Mindee::V2::Product::Extraction::Params::ExtractionParameters do let(:replace_path) { File.join(V2_PRODUCT_DATA_DIR, 'extraction', 'data_schema_replace_param.json') } let(:data_schema_dict) { JSON.parse(File.read(replace_path)) } - let(:data_schema_string) { data_schema_dict.to_json } + let(:data_schema_string) { data_schema_dict.to_h.to_json } let(:data_schema_instance) { Mindee::V2::Product::Extraction::Params::DataSchema.new(data_schema_dict) } it 'should init with minimum values' do