Add cli.py
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
Command‑line interface for the RAG agent.
|
||||
|
||||
Commands:
|
||||
add <file> – add document from file to knowledge base
|
||||
search <q> – search query in knowledge base
|
||||
quit – exit
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from langchain_core.messages import HumanMessage
|
||||
from agent import agent
|
||||
|
||||
async def run_cli():
|
||||
parser = argparse.ArgumentParser(description="RAG Agent CLI")
|
||||
subparsers = parser.add_subparsers(dest="cmd", required=True)
|
||||
|
||||
add_parser = subparsers.add_parser("add", help="Add document from file")
|
||||
add_parser.add_argument("file", type=Path, help="Path to text file")
|
||||
|
||||
search_parser = subparsers.add_parser("search", help="Search query in knowledge base")
|
||||
search_parser.add_argument("query", type=str, help="Search string")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.cmd == "add":
|
||||
content = args.file.read_text(encoding="utf-8")
|
||||
title = args.file.stem
|
||||
await agent.ainvoke(
|
||||
{"messages": [HumanMessage(content=f"Add document: {title}")]},
|
||||
{"configurable": {"thread_id": "cli-add"}},
|
||||
)
|
||||
elif args.cmd == "search":
|
||||
result = await agent.ainvoke(
|
||||
{"messages": [HumanMessage(content=args.query)]},
|
||||
{"configurable": {"thread_id": "cli-search"}},
|
||||
)
|
||||
print(result["messages"][-1].content)
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
asyncio.run(run_cli())
|
||||
Reference in New Issue
Block a user