From 7ff9b2fbb9f11771219b462766a502b67669421b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Fri, 5 Jun 2026 10:25:18 +0000 Subject: [PATCH] Update src/cli.py --- src/cli.py | 104 ++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/src/cli.py b/src/cli.py index 9c43b61..6bade2b 100644 --- a/src/cli.py +++ b/src/cli.py @@ -1,67 +1,75 @@ -"""Interactive command‑line client for the RAG agent. +"""Interactive command line client for the RAG agent. Commands: - /add – add a new document to the knowledge base. - /search – perform a semantic search. - /quit – exit the program. + /add <path> – Load a document from a file and add it to the knowledge base. + /search <query> – Search the knowledge base and display results. + /quit – Exit the program. + /help – Show this help message. -Any other input is treated as a user message and is processed by the agent. +The client uses the global agent defined in ``src.agent`` and the knowledge +base instance from ``src.tools``. """ -from typing import List +from __future__ import annotations -from .agent import run_agent -from .tools import search_knowledge_base, add_to_knowledge_base +import sys +from pathlib import Path + +from .agent import run_query +from .tools import kb + +HELP_TEXT = """ +Available commands: + /add <title> <file_path> Add a document to the knowledge base. + /search <query> Search the knowledge base. + /quit Exit the program. + /help Show this help message. +""" def main() -> None: - print("Welcome to the RAG agent CLI. Type /help for commands.") - history: List[dict] = [] + print("RAG Agent CLI. Type /help for commands.") while True: try: - user_input = input(">>> ") - except EOFError: + user_input = input("> ") + except (EOFError, KeyboardInterrupt): + print("\nExiting.") break - if not user_input: + if not user_input.strip(): continue - if user_input.startswith("/"): - cmd, *args = user_input.split(maxsplit=1) - if cmd == "/quit": - print("Goodbye!") - break - elif cmd == "/help": - print("Commands: /add, /search, /quit") + if user_input.startswith("/add"): + parts = user_input.split(maxsplit=2) + if len(parts) != 3: + print("Usage: /add <title> <file_path>") continue - elif cmd == "/add": - title = input("Title: ") - print("Enter content (end with a single line containing only 'END'):\n") - lines = [] - while True: - line = input() - if line.strip() == "END": - break - lines.append(line) - content = "\n".join(lines) - response = add_to_knowledge_base(content, title) - print(response) + title, file_path = parts[1], parts[2] + path = Path(file_path) + if not path.is_file(): + print(f"File not found: {file_path}") continue - elif cmd == "/search": - query = input("Query: ") - results = search_knowledge_base(query, max_results=5) - if not results: - print("No results found.") - else: - for i, res in enumerate(results, 1): - print(f"{i}. Title: {res['title']}, Score: {res['score']:.4f}") - print(f" {res['content'][:200]}...\n") + content = path.read_text(encoding="utf-8") + kb.add_document(title=title, content=content) + print(f"Document '{title}' added.") + elif user_input.startswith("/search"): + query = user_input[len("/search"):].strip() + if not query: + print("Please provide a search query.") continue - else: - print("Unknown command. Type /help for list of commands.") + results = kb.search(query, limit=5) + if not results: + print("No results found.") continue - # Normal user message - history.append({"role": "user", "content": user_input}) - reply = run_agent(history) - print(f"Assistant: {reply}") - history.append({"role": "assistant", "content": reply}) + print("Results:") + for i, r in enumerate(results, 1): + print(f"{i}. [{r['title']} - chunk {r['chunk_index']}] {r['content'][:200]}...") + elif user_input.startswith("/quit"): + print("Goodbye.") + break + elif user_input.startswith("/help"): + print(HELP_TEXT) + else: + # Treat as a normal user query to the agent. + response = run_query(user_input) + print(response) if __name__ == "__main__": main() \ No newline at end of file