add cli.py
This commit is contained in:
@@ -1,43 +1,19 @@
|
|||||||
"""
|
"""
|
||||||
Command‑line interface for the RAG agent.
|
Simple CLI for interacting with 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
|
import asyncio
|
||||||
from pathlib import Path
|
|
||||||
from langchain_core.messages import HumanMessage
|
from langchain_core.messages import HumanMessage
|
||||||
from agent import agent
|
from agent import run_agent
|
||||||
|
|
||||||
async def run_cli():
|
async def main():
|
||||||
parser = argparse.ArgumentParser(description="RAG Agent CLI")
|
print("RAG Agent CLI. Type /quit to exit.")
|
||||||
subparsers = parser.add_subparsers(dest="cmd", required=True)
|
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")
|
if __name__ == "__main__":
|
||||||
add_parser.add_argument("file", type=Path, help="Path to text file")
|
asyncio.run(main())
|
||||||
|
|
||||||
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