#!/usr/bin/env awk -f # Step 1: Initialize stop words to filter out common noise BEGIN { split("the and of to a in is that it on for as was with at by an be this are from or can your layout", words) for (i in words) stopwords[tolower(words[i])] = 1 # Store the user's question from the external environment variable query = tolower(ENVIRON["QUERY"]) patsplit(query, query_tokens, /[a-zA-Z0-9]+/) for (i in query_tokens) { if (!stopwords[query_tokens[i]]) { query_weights[query_tokens[i]]++ } } } # Step 2: Chunk documents by empty lines (Paragraph mode) BEGIN { RS = "" } { # Clean up formatting whitespace gsub(/[ \t\r\n]+/, " ") paragraph = $0 score = 0 # Tokenize the paragraph text patsplit(tolower(paragraph), p_tokens, /[a-zA-Z0-9]+/) # Count term frequencies in this paragraph delete tf for (i in p_tokens) { if (!stopwords[p_tokens[i]]) tf[p_tokens[i]]++ } # Calculate text match score using a basic vector dot product for (token in query_weights) { if (token in tf) { score += query_weights[token] * tf[token] } } # If this paragraph scores higher than previous ones, save it if (score > max_score) { max_score = score best_paragraph = paragraph } } # Step 3: Format the context directly for llama.cpp END { if (max_score == 0) { print "Context: (No highly relevant context found in documents.)" } else { print "Context: " best_paragraph } print "Question: " ENVIRON["QUERY"] print "Answer: " }