From 3a46dfdff94fc3dcbfb9aaffcc5dffd5e69be15d Mon Sep 17 00:00:00 2001 From: Nova AI Date: Wed, 11 Feb 2026 04:09:28 +0000 Subject: [PATCH] Add automatic context checker for RAG integration --- rag_context.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 rag_context.py diff --git a/rag_context.py b/rag_context.py new file mode 100644 index 0000000..d0b9229 --- /dev/null +++ b/rag_context.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +""" +RAG Context Checker - Simple wrapper for automatic knowledge base lookup + +Usage from within Python/OpenClaw sessions: + from rag_context import check_context + check_context("your question here") + +This prints relevant context if found, otherwise silent. +""" + +import sys +sys.path.insert(0, '/home/william/.openclaw/workspace/rag') + +from rag_query_wrapper import search_knowledge, format_for_ai + + +def check_context(query: str, max_results: int = 5, min_score: float = 0.3) -> None: + """ + Check knowledge base for relevant context and print it. + + This is designed as a simple call-and-forget function. If context exists, + it prints it. If not, it's silent. + + Args: + query: The question or topic to search for + max_results: Maximum results to retrieve + min_score: Minimum similarity score (not used in current implementation) + + Example: + >>> check_context("how to send SMS") + 📚 Found 3 relevant items... + [prints context] + """ + if not query or len(query) < 3: + return + + try: + results = search_knowledge(query, n_results=max_results) + + if results and results.get('count', 0) > 0: + print("\n" + "="*80) + print("📚 RELEVANT CONTEXT FROM KNOWLEDGE BASE\n") + formatted = format_for_ai(results) + print(formatted) + print("="*80 + "\n") + + except Exception as e: + # Fail silently - RAG errors shouldn't break conversations + pass + + +def get_context(query: str, max_results: int = 5) -> str: + """ + Get context as a string (for automated use). + + Returns formatted context string, or empty string if no results. + + Args: + query: Search query + max_results: Maximum results + + Returns: + Formatted context string, or "" if no results + + Example: + >>> context = get_context("Reddit automation") + >>> if context: + >>> print("Found relevant past work:") + >>> print(context) + """ + try: + results = search_knowledge(query, n_results=max_results) + + if results and results.get('count', 0) > 0: + return format_for_ai(results) + + except Exception as e: + pass + + return "" + + +# Quick test when run directly +if __name__ == "__main__": + if len(sys.argv) > 1: + query = ' '.join(sys.argv[1:]) + check_context(query) + else: + print("Usage: python3 rag_context.py ") + print("Or import: from rag_context import check_context") \ No newline at end of file