Files
task-6a02e23d-agent-s-rag/client.py
T

80 lines
2.3 KiB
Python

"""
client.py
Interactive CLI client for the RAG agent.
Commands:
/add <title> | <content> — Add a document to the knowledge base
/search <query> — Semantic search in the knowledge base
/quit — Exit the client
<any other input> — Send query to the agent
"""
from tools import search_knowledge_base, add_to_knowledge_base
from agent import run_agent
HELP_TEXT = """
Commands:
/add <title> | <content> Add a document to the knowledge base
/search <query> Search the knowledge base directly
/quit Exit
<any text> Ask the agent a question
"""
def handle_add(args: str) -> None:
if "|" not in args:
print("[Error] Usage: /add <title> | <content>")
return
title, _, content = args.partition("|")
title = title.strip()
content = content.strip()
if not title or not content:
print("[Error] Both title and content are required.")
return
result = add_to_knowledge_base.invoke({"content": content, "title": title})
print(result)
def handle_search(query: str) -> None:
if not query.strip():
print("[Error] Please provide a search query.")
return
result = search_knowledge_base.invoke({"query": query.strip(), "max_results": 5})
print(result)
def main():
print("=== RAG Agent Interactive Client ===")
print(HELP_TEXT)
while True:
try:
user_input = input("You: ").strip()
except (EOFError, KeyboardInterrupt):
print("\nGoodbye!")
break
if not user_input:
continue
if user_input.lower() == "/quit":
print("Goodbye!")
break
elif user_input.lower().startswith("/add "):
handle_add(user_input[5:])
elif user_input.lower().startswith("/search "):
handle_search(user_input[8:])
elif user_input.lower() == "/help":
print(HELP_TEXT)
else:
print("Agent: thinking...\n")
try:
response = run_agent(user_input)
print(f"Agent: {response}\n")
except Exception as e:
print(f"[Error] Agent failed: {e}\n")
if __name__ == "__main__":
main()