add cli.py
This commit is contained in:
@@ -1,43 +1,19 @@
|
||||
"""
|
||||
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
|
||||
Simple CLI for interacting with the RAG agent.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from langchain_core.messages import HumanMessage
|
||||
from agent import agent
|
||||
from agent import run_agent
|
||||
|
||||
async def run_cli():
|
||||
parser = argparse.ArgumentParser(description="RAG Agent CLI")
|
||||
subparsers = parser.add_subparsers(dest="cmd", required=True)
|
||||
async def main():
|
||||
print("RAG Agent CLI. Type /quit to exit.")
|
||||
while True:
|
||||
user_input = input("You: ")
|
||||
if user_input.strip() == "/quit":
|
||||
break
|
||||
msg = HumanMessage(content=user_input)
|
||||
response = await run_agent([msg])
|
||||
print(f"Agent: {response}")
|
||||
|
||||
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())
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user