]> begriffs open source - ai-unix/blob - ai-class
Simpler prompt for fixer
[ai-unix] / ai-class
1 #!/bin/bash
2
3 # AI-Class: Semantic categorization tool
4 # Beautiful implementation using composable functions
5
6 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7 source "$SCRIPT_DIR/ai-common"
8
9 show_usage() {
10     cat << 'EOF'
11 Usage: ai-class CATEGORIES [FILE...]
12 Categorize input text into predefined buckets based on semantic content.
13
14 Arguments:
15   CATEGORIES   Comma-separated list of category labels
16
17 Options:
18   -h          Show this help message
19 EOF
20 }
21
22 build_categorization_prompt() {
23     local categories="$1"
24     
25     cat << EOF
26 You are a semantic categorization tool. Categorize each line of the input text into one of these categories: $categories
27
28 For each line of input, output the line prefixed with the most appropriate category label followed by a colon and space.
29
30 Example format:
31 category1: original line content
32 category2: another line content
33
34 Categories to choose from: $categories
35 EOF
36 }
37
38 validate_and_setup() {
39     local categories="$1"
40     shift
41     local files=("$@")
42     
43     ensure_dependencies
44     ensure_argument_provided "Categories" "$categories" show_usage
45     
46     [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
47 }
48
49 main() {
50     local categories=""
51     local files=()
52     
53     # Parse options
54     while getopts "h" opt; do
55         case $opt in
56             h) handle_help_option show_usage ;;
57             \?) handle_invalid_option "$OPTARG" ;;
58         esac
59     done
60     
61     shift $((OPTIND-1))
62     categories="$1"
63     shift
64     files=("$@")
65     
66     # Early validation with immediate exit on failure
67     validate_and_setup "$categories" "${files[@]}"
68     
69     # Process input with early exit
70     local input
71     input=$(process_input_sources "${files[@]}") || exit "$EXIT_NO_MATCH"
72     
73     # Execute LLM request with early exit
74     local prompt response
75     prompt=$(build_categorization_prompt "$categories")
76     response=$(execute_llm_request "$prompt" "$input") || handle_llm_error $?
77     
78     # Process response with early exit
79     local result
80     result=$(process_categorization_response "$response") || exit $?
81     
82     echo "$result"
83 }
84
85 main "$@"