3 # AI-Cut: Structured field extraction 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-cut -f FIELDS [FILE...]
12 Extract structured fields from unstructured text using natural language field descriptions.
15 -f FIELDS Comma-separated field descriptions (required)
16 -j Output as JSON objects (default: TSV)
17 -h Show this help message
21 build_extraction_prompt() {
23 local json_output="$2"
26 local format_instructions
27 if [[ "$json_output" == "true" ]]; then
28 format_instructions='For each line or record in the input, extract the specified fields and output as a JSON object with the field names as keys. Output one JSON object per line. Output ONLY the raw JSON - do not wrap in markdown code blocks or add any other formatting.
31 {"field1": "value1", "field2": "value2", "field3": "value3"}'
33 format_instructions='For each line or record in the input, extract the specified fields and output as tab-separated values. Output the fields in the same order as specified.
40 You are a structured data extraction tool. Extract the following fields from the unstructured text: $fields
49 process_extraction_response() {
51 local json_output="$2"
53 if [[ "$json_output" == "true" ]]; then
54 process_json_response "$response"
63 local json_output="false"
65 local input response result
68 while getopts "f:jh" opt; do
70 f) fields="$OPTARG" ;;
71 j) json_output="true" ;;
72 h) handle_help_option show_usage ;;
73 \?) handle_invalid_option "$OPTARG" ;;
80 # Validate environment and arguments
82 ensure_argument_provided "Field specification (-f)" "$fields" show_usage
84 # Validate files if provided
85 [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
88 if ! input=$(process_input_sources "${files[@]}"); then
94 prompt=$(build_extraction_prompt "$fields" "$json_output" "$input")
96 if ! response=$(execute_llm_request "$prompt"); then
98 "$EXIT_NO_MATCH") print_error "$(error_llm_no_response)" ;;
99 "$EXIT_API_ERROR") print_error "$(error_llm_api_error)" ;;
100 *) print_error "$(error_llm_command_failed)" ;;
105 # Process and validate response
106 if ! result=$(process_extraction_response "$response" "$json_output"); then