Files
task-6a02e23da6fe2e4ac16acf65/src/cli.py
T
2026-06-05 10:25:18 +00:00

75 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Interactive command line client for the RAG agent.
Commands:
/add <title> <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.
The client uses the global agent defined in ``src.agent`` and the knowledge
base instance from ``src.tools``.
"""
from __future__ import annotations
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("RAG Agent CLI. Type /help for commands.")
while True:
try:
user_input = input("> ")
except (EOFError, KeyboardInterrupt):
print("\nExiting.")
break
if not user_input.strip():
continue
if user_input.startswith("/add"):
parts = user_input.split(maxsplit=2)
if len(parts) != 3:
print("Usage: /add <title> <file_path>")
continue
title, file_path = parts[1], parts[2]
path = Path(file_path)
if not path.is_file():
print(f"File not found: {file_path}")
continue
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
results = kb.search(query, limit=5)
if not results:
print("No results found.")
continue
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()