Add src/cli.py
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
|||||||
|
"""Command‑line interface for the FAQ‑bot.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
python cli.py --question "What topics are covered in week 3?"
|
||||||
|
python cli.py --question "What is the course schedule?"
|
||||||
|
|
||||||
|
If no question is supplied, an interactive prompt is started.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from .agent import create_agent_executor
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
parser = argparse.ArgumentParser(description="FAQ‑bot CLI")
|
||||||
|
parser.add_argument("--question", type=str, help="Question to ask the bot")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
executor = await create_agent_executor()
|
||||||
|
|
||||||
|
if args.question:
|
||||||
|
state = {"input": args.question}
|
||||||
|
result = await executor.ainvoke(state)
|
||||||
|
print("\nAnswer:")
|
||||||
|
print(result["answer"])
|
||||||
|
print(f"\nSource: {result['source']}")
|
||||||
|
else:
|
||||||
|
print("Enter a question (Ctrl‑D to exit):")
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
q = input("> ")
|
||||||
|
except EOFError:
|
||||||
|
break
|
||||||
|
if not q.strip():
|
||||||
|
continue
|
||||||
|
state = {"input": q}
|
||||||
|
result = await executor.ainvoke(state)
|
||||||
|
print("\nAnswer:")
|
||||||
|
print(result["answer"])
|
||||||
|
print(f"\nSource: {result['source']}")
|
||||||
|
print("\n---")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
Reference in New Issue
Block a user