From 5254d064ee02abaad8b773ee07b8d911d2887c98 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=9A=D1=83=D1=82?= =?UTF-8?q?=D0=BB=D0=B0=D1=85=D0=BC=D0=B5=D1=82=D0=BE=D0=B2?= Date: Thu, 28 May 2026 13:12:54 +0000 Subject: [PATCH] add cli.py --- cli.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/cli.py b/cli.py index f1d9bb8..762fda5 100644 --- a/cli.py +++ b/cli.py @@ -1,19 +1,66 @@ """ -Simple CLI for interacting with the RAG agent. +CLI for interacting with the RAG agent. + +This script provides three explicit commands: + +* ``/add `` – add a piece of text to the knowledge base. +* ``/search `` – search the knowledge base and display results. +* ``/quit`` – exit the program. + +Any other input is forwarded to the underlying agent as a normal question. """ import asyncio from langchain_core.messages import HumanMessage from agent import run_agent +from tools import add_to_knowledge_base, search_knowledge_base -async def main(): +async def main() -> None: print("RAG Agent CLI. Type /quit to exit.") while True: user_input = input("You: ") - if user_input.strip() == "/quit": + stripped = user_input.strip() + if not stripped: + continue + # Handle explicit commands + if stripped.startswith("/add "): + text = stripped[5:].strip() + if not text: + print("Error: /add requires a non‑empty text.") + continue + try: + await add_to_knowledge_base(text) + print("✅ Added to knowledge base.") + except Exception as exc: # pragma: no cover - defensive + print(f"❌ Failed to add: {exc}") + elif stripped.startswith("/search "): + query = stripped[8:].strip() + if not query: + print("Error: /search requires a non‑empty query.") + continue + try: + results = await search_knowledge_base(query) + if not results: + print("No results found.") + else: + for idx, res in enumerate(results, start=1): + print(f"{idx}. {res}") + except Exception as exc: # pragma: no cover - defensive + print(f"❌ Search error: {exc}") + elif stripped == "/quit": + print("Goodbye!") break - msg = HumanMessage(content=user_input) - response = await run_agent([msg]) - print(f"Agent: {response}") + else: + # Forward to agent + msg = HumanMessage(content=user_input) + try: + response = await run_agent([msg]) + # ``run_agent`` returns the last assistant message as a string. + if isinstance(response, str): + print(f"Agent: {response}") + else: + print("Agent returned unexpected format.") + except Exception as exc: # pragma: no cover - defensive + print(f"❌ Agent error: {exc}") if __name__ == "__main__": asyncio.run(main())