Files
task-6a02e23da6fe2e4ac16acf65/src/cli.py
T
2026-06-04 22:57:24 +00:00

67 lines
2.3 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 commandline client for the RAG agent.
Commands:
/add add a new document to the knowledge base.
/search perform a semantic search.
/quit exit the program.
Any other input is treated as a user message and is processed by the agent.
"""
from typing import List
from .agent import run_agent
from .tools import search_knowledge_base, add_to_knowledge_base
def main() -> None:
print("Welcome to the RAG agent CLI. Type /help for commands.")
history: List[dict] = []
while True:
try:
user_input = input(">>> ")
except EOFError:
break
if not user_input:
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")
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)
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")
continue
else:
print("Unknown command. Type /help for list of commands.")
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})
if __name__ == "__main__":
main()