3 # AI-Class: Semantic categorization tool
4 # Beautiful implementation using composable functions
6 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7 source "$SCRIPT_DIR/ai-common"
11 Usage: ai-class CATEGORIES [FILE...]
12 Categorize input text into predefined buckets based on semantic content.
15 CATEGORIES Comma-separated list of category labels
18 -h Show this help message
22 build_categorization_prompt() {
26 You are a semantic categorization tool. Categorize each line of the input text into one of these categories: $categories
28 For each line of input, output the line prefixed with the most appropriate category label followed by a colon and space.
31 category1: original line content
32 category2: another line content
34 Categories to choose from: $categories
38 validate_and_setup() {
44 ensure_argument_provided "Categories" "$categories" show_usage
46 [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
54 while getopts "h" opt; do
56 h) handle_help_option show_usage ;;
57 \?) handle_invalid_option "$OPTARG" ;;
66 # Early validation with immediate exit on failure
67 validate_and_setup "$categories" "${files[@]}"
69 # Process input with early exit
71 input=$(process_input_sources "${files[@]}") || exit "$EXIT_NO_MATCH"
73 # Execute LLM request with early exit
75 prompt=$(build_categorization_prompt "$categories")
76 response=$(execute_llm_request "$prompt" "$input") || handle_llm_error $?
78 # Process response with early exit
80 result=$(process_categorization_response "$response") || exit $?