Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bin/mindee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
def root_help
help = "Usage: mindee command [options]\n\nAvailable commands:\n"
help += " #{'v1'.ljust(50)}Use Version 1 of the Mindee API\n"
help += " #{'search-models'.ljust(50)}Search for available models for this API key\n"

MindeeCLI::V2SearchCommands::V2_SEARCH_COMMANDS.each do |search_command, search_values|
help += " #{search_command.ljust(50)}#{search_values[:description]}\n"
end

V2_PRODUCTS.each do |product_key, product_values|
help += " #{product_key.ljust(50)}#{product_values[:description]}\n"
Expand Down
85 changes: 40 additions & 45 deletions bin/v2/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

require 'mindee'
require_relative 'products'
require_relative 'search_commands'

module MindeeCLI
# Mindee Command Line Interface
# V2 CLI class.
class V2Parser
include V2SearchCommands

# @return [Array<String>]
attr_reader :arguments

Expand All @@ -17,7 +20,10 @@ class V2Parser
attr_reader :product_parser

# @return [Parser]
attr_reader :search_parser
attr_reader :search_models_parser

# @return [Parser]
attr_reader :search_rag_docs_parser

def initialize(arguments, command_prefix: 'mindee v2')
@arguments = arguments
Expand All @@ -26,26 +32,18 @@ def initialize(arguments, command_prefix: 'mindee v2')
opts.banner = "Usage: #{@command_prefix} command [options]"
end
@product_parser = init_product_parser
@search_parser = init_search_parser
@search_models_parser = init_search_models_parser
@search_rag_docs_parser = init_search_rag_docs_parser
end

# Summarize and print the result of the command.
# @param command [String]
def print_result(command)
if command == 'search-models'
@search_parser.parse!(@arguments)
result = search(@options)
summarized_result = output_format == :full ? result.to_s : result.models.to_s
else
@product_parser[command].parse!(@arguments)
@options[:file_path] = @arguments.shift
if @options[:file_path].nil?
warn 'file missing'
abort(@product_parser[command].help)
end
result = send(command, @options)
summarized_result = output_format == :full ? result.inference.to_s : result.inference.result.to_s
end
result, summarized_result = if V2_SEARCH_COMMANDS.key?(command)
run_search(command)
else
run_product(command)
end

if output_format == :raw
puts JSON.pretty_generate(raw_payload(result.raw_http))
Expand All @@ -63,22 +61,41 @@ def execute
validate_command!(command)
print_result(command)
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
if command == 'search-models'
abort("#{e.message}\n\n#{@search_parser.help}")
else
abort("#{e.message}\n\n#{@product_parser[command].help}")
end
abort("#{e.message}\n\n#{command_parser(command).help}")
rescue Mindee::Error::MindeeError => e
abort(format_cli_error(e))
end

private

# Parse the arguments and run the given product command.
# @param command [String]
# @return [Array(Mindee::V2::Parsing::BaseResponse, String)] Response and its summary.
def run_product(command)
@product_parser[command].parse!(@arguments)
@options[:file_path] = @arguments.shift
if @options[:file_path].nil?
warn 'file missing'
abort(@product_parser[command].help)
end
result = send(command, @options)
summarized_result = output_format == :full ? result.inference.to_s : result.inference.result.to_s
[result, summarized_result]
end

# @param command [String]
# @return [OptionParser]
def command_parser(command)
V2_SEARCH_COMMANDS.key?(command) ? search_parser_for(command) : @product_parser[command]
end

def validate_command!(command)
return if V2_PRODUCTS.include?(command) || command == 'search-models'
return if V2_PRODUCTS.include?(command) || V2_SEARCH_COMMANDS.key?(command)

error_msg = "#{@options_parser.help}\nAvailable commands:\n"
error_msg += " #{'search-models'.ljust(50)}Search for available models for this API key\n"
V2_SEARCH_COMMANDS.each do |search_command, search_values|
error_msg += " #{search_command.ljust(50)}#{search_values[:description]}\n"
end

V2_PRODUCTS.each do |product_key, product_values|
error_msg += " #{product_key.to_s.ljust(50)}#{product_values[:description]}\n"
Expand All @@ -98,21 +115,6 @@ def format_cli_error(error)
end
end

def init_search_parser
OptionParser.new do |options_parser|
options_parser.banner = "Usage: #{@command_prefix} search-models [options]"
init_common_options(options_parser)
options_parser.on('-n [NAME]', '--name [NAME]',
'Search for partial matches in model name. Note: case insensitive') do |v|
@options[:model_name] = v
end
options_parser.on('-t [NAME]', '--type [NAME]',
'Search for EXACT matches in model type. Note: case sensitive') do |v|
@options[:model_type] = v
end
end
end

def setup_specific_options(options_parser, doc_value)
options_parser.on('-r', '--rag', 'Enable RAG') { @options[:rag] = true } if doc_value.key?(:rag)
if doc_value.key?(:raw_text)
Expand Down Expand Up @@ -218,13 +220,6 @@ def send(product_command, options)
)
end

# @param options [Hash]
# @return [Mindee::V2::Parsing::Search::SearchResponse]
def search(options)
mindee_client = Mindee::V2::Client.new(api_key: options[:api_key])
mindee_client.search_models(options[:model_name], options[:model_type])
end

# @param options [Hash]
# @return [Mindee::Input::InputSource]
def setup_input_source(options)
Expand Down
100 changes: 100 additions & 0 deletions bin/v2/search_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# frozen_string_literal: true

require 'mindee'
require_relative 'products'

module MindeeCLI
# Search-related commands for the V2 CLI.
module V2SearchCommands
# NOTE: keep command names as string instead of symbols due to kebab-case.
V2_SEARCH_COMMANDS = {
'search-models' => {
description: 'Search available models.',
parser: :search_models_parser,
runner: :search_models,
summary: :models,
},
'search-rag-docs' => {
description: 'Search available RAG documents for a given model.',
parser: :search_rag_docs_parser,
runner: :search_rag_documents,
summary: :rag_documents,
},
}.freeze

private

# Parse the arguments and run the given search command.
# @param command [String]
# @return [Array(Mindee::V2::Parsing::Search::BaseSearchResponse, String)] Response and its summary.
def run_search(command)
search_command = V2_SEARCH_COMMANDS[command]
search_parser_for(command).parse!(@arguments)
result = __send__(search_command[:runner], @options)
summarized_result = output_format == :full ? result.to_s : result.public_send(search_command[:summary]).to_s
[result, summarized_result]
end

# @param command [String]
# @return [OptionParser]
def search_parser_for(command)
__send__(V2_SEARCH_COMMANDS[command][:parser])
end

# @return [OptionParser]
def init_search_models_parser
OptionParser.new do |options_parser|
options_parser.banner = "Usage: #{@command_prefix} search-models [options]"
init_common_options(options_parser)
options_parser.on('-n [NAME]', '--name [NAME]',
'Filter by model name partial match (case insensitive).') do |v|
@options[:model_name] = v
end
options_parser.on('-m [MODEL_TYPE]', '--model-type [MODEL_TYPE]',
'Filter by exact model type (case sensitive).',
"Available options: #{V2_PRODUCTS.keys.join(', ')}") do |v|
@options[:model_type] = v
end
end
end

# @return [OptionParser]
def init_search_rag_docs_parser
OptionParser.new do |options_parser|
options_parser.banner = "Usage: #{@command_prefix} search-rag-docs [options]"
init_common_options(options_parser)
options_parser.on('-m MODEL_ID', '--model-id MODEL_ID', 'Filter by model ID') do |v|
@options[:model_id] = v
end
options_parser.on('-f [FILENAME]', '--filename [FILENAME]',
'Filter by file name partial match (case insensitive).') do |v|
@options[:filename] = v
end
end
end

# @param options [Hash]
# @return [Mindee::V2::Search::Models::ModelSearchResponse]
def search_models(options)
mindee_client = Mindee::V2::Client.new(api_key: options[:api_key])
mindee_client.search(
Mindee::V2::Search::Models::ModelSearchParameters.new(
name: options[:model_name],
model_type: options[:model_type]
)
)
end

# @param options [Hash]
# @return [Mindee::V2::Search::RAGDocuments::RAGDocumentSearchResponse]
def search_rag_documents(options)
mindee_client = Mindee::V2::Client.new(api_key: options[:api_key])
mindee_client.search(
Mindee::V2::Search::RAGDocuments::RAGDocumentSearchParameters.new(
model_id: options[:model_id],
filename: options[:filename]
)
)
end
end
end
Loading
Loading