Update cli.py

This commit is contained in:
2026-06-04 20:02:26 +00:00
parent 79dac766c7
commit 6f3f64a4b4
+38 -57
View File
@@ -1,68 +1,49 @@
"""Simple CLI for interacting with the RAG agent. """Simple interactive CLI for the RAG agent.
Commands: Commands:
/add <title> <file_path> - Add a document to the knowledge base. /add title content add a document to the knowledge base.
/search <query> [max] - Search the knowledge base. /search query perform a semantic search.
/quit - Exit the CLI. /quit exit.
any other text will be sent to the agent for normal answering.
""" """
import sys import sys
from pathlib import Path
# Import the tools directly. They are decorated with @tool but can be called like normal functions. from .agent import create_agent_executor
from rag_tools import add_to_knowledge_base, search_knowledge_base
agent = create_agent_executor()
def main(): print("RAG Agent CLI. Type /quit to exit.")
print("Welcome to the RAG Agent CLI. Type /help for commands.") while True:
while True: try:
try: user_input = input(">>> ")
user_input = input(">> ") except EOFError:
except (EOFError, KeyboardInterrupt): break
print("\nExiting.") if not user_input:
break continue
if not user_input: if user_input.lower() == "/quit":
print("Goodbye!")
break
if user_input.startswith("/add "):
# Expected format: /add title content
parts = user_input.split(" ", 2)
if len(parts) < 3:
print("Usage: /add title content")
continue continue
if user_input.startswith("/help"): title, content = parts[1], parts[2]
print("Commands:\n /add <title> <file_path> - Add a document to the knowledge base.\n /search <query> [max] - Search the knowledge base.\n /quit - Exit the CLI.") # Directly call the tool via the agent
continue result = agent.run({"input": f"Add document: {title} {content}"})
if user_input.startswith("/quit"): print(result)
print("Goodbye!") continue
break if user_input.startswith("/search "):
if user_input.startswith("/add"): query = user_input[8:].strip()
parts = user_input.split(maxsplit=2) result = agent.run({"input": f"Search for: {query}"})
if len(parts) < 3: print(result)
print("Usage: /add <title> <file_path>") continue
continue # Normal conversation
title, file_path = parts[1], parts[2] result = agent.run({"input": user_input})
try: print(result)
content = Path(file_path).read_text(encoding="utf-8")
except Exception as e:
print(f"Error reading file: {e}")
continue
print("Adding document...", end=" ")
result = add_to_knowledge_base(content=content, title=title)
print(result)
continue
if user_input.startswith("/search"):
parts = user_input.split(maxsplit=2)
if len(parts) < 2:
print("Usage: /search <query> [max_results]")
continue
query = parts[1]
max_results = 5
if len(parts) == 3:
try:
max_results = int(parts[2])
except ValueError:
print("max_results must be an integer.")
continue
print("Searching...", end=" ")
result = search_knowledge_base(query=query, max_results=max_results)
print(result)
continue
print("Unknown command. Type /help for a list of commands.")
if __name__ == "__main__": if __name__ == "__main__":
main() # The CLI is already running in the main thread
pass