Add src/cli.py

This commit is contained in:
2026-06-04 22:57:24 +00:00
parent 4894cc1dcf
commit fdf387ae25
+67
View File
@@ -0,0 +1,67 @@
"""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()