Update main.py
This commit is contained in:
@@ -1,63 +1,80 @@
|
|||||||
"""
|
"""Simple CLI for the RAG agent.
|
||||||
CLI entry point for the RAG agent.
|
|
||||||
|
|
||||||
The script loads / creates the vector store, populates it from the ``documents``
|
Commands:
|
||||||
folder and starts an interactive chat loop.
|
/add <directory> – Load all .txt/.md files from the directory into the local KB.
|
||||||
|
/search <question> – Ask the agent a question.
|
||||||
|
/quit – Exit the program.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from langchain_ollama import ChatOllama
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
# Load environment variables (TAVILY_API_KEY, etc.)
|
from vectorstore import create_vectorstore, load_documents
|
||||||
|
from agent import create_agent, should_use_web
|
||||||
|
|
||||||
|
# Load environment variables (TAVILY_API_KEY)
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# Import our modules
|
# Create or load vector store
|
||||||
from vectorstore import create_vectorstore, load_documents
|
VECTORSTORE_DIR = "./chroma_db"
|
||||||
from agent import create_agent
|
vectorstore = create_vectorstore(persist_directory=VECTORSTORE_DIR)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# Create agent
|
||||||
# Helper: populate vector store
|
agent = create_agent(vectorstore)
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def init_vectorstore(persist_dir: str = "./chroma_db", docs_dir: str = "./documents"):
|
# Helper to print usage
|
||||||
"""Create or load the vector store and load documents if needed."""
|
USAGE = (
|
||||||
vectorstore = create_vectorstore(persist_directory=persist_dir)
|
"Commands:\n"
|
||||||
# Always load documents – Chroma will deduplicate if already present.
|
" /add <directory> – Load documents into the local knowledge base.\n"
|
||||||
print("Loading documents into ChromaDB…")
|
" /search <question> – Ask the agent a question.\n"
|
||||||
load_documents(docs_dir, vectorstore)
|
" /quit – Exit the program.\n"
|
||||||
return vectorstore
|
)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
print("RAG Agent CLI. Type /help for commands.")
|
||||||
# Main chat loop
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def main():
|
while True:
|
||||||
print("Initializing RAG agent…")
|
|
||||||
vectorstore = init_vectorstore()
|
|
||||||
agent = create_agent(vectorstore)
|
|
||||||
|
|
||||||
print("RAG agent ready. Type your question (or 'exit' to quit).")
|
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
user_input = input("\n> ")
|
line = input("> ").strip()
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
print("\nGoodbye!")
|
print("\nExiting.")
|
||||||
break
|
break
|
||||||
if user_input.strip().lower() in {"exit", "quit", "q"}:
|
if not line:
|
||||||
print("Goodbye!")
|
|
||||||
break
|
|
||||||
if not user_input.strip():
|
|
||||||
continue
|
continue
|
||||||
# Run the agent and capture the output
|
if line.lower() == "/help":
|
||||||
result = agent.run(user_input)
|
print(USAGE)
|
||||||
print("\nAnswer:\n", result)
|
continue
|
||||||
|
if line.lower() == "/quit":
|
||||||
if __name__ == "__main__":
|
print("Bye!")
|
||||||
main()
|
break
|
||||||
|
if line.lower().startswith("/add "):
|
||||||
# ---------------------------------------------------------------------------
|
dir_path = line[5:].strip()
|
||||||
# End of script
|
if not dir_path:
|
||||||
# ---------------------------------------------------------------------------
|
print("Please provide a directory path.")
|
||||||
|
continue
|
||||||
|
if not Path(dir_path).exists():
|
||||||
|
print(f"Directory {dir_path} does not exist.")
|
||||||
|
continue
|
||||||
|
load_documents(dir_path, vectorstore)
|
||||||
|
print("Documents loaded.")
|
||||||
|
continue
|
||||||
|
if line.lower().startswith("/search "):
|
||||||
|
query = line[8:].strip()
|
||||||
|
if not query:
|
||||||
|
print("Please provide a question.")
|
||||||
|
continue
|
||||||
|
# Decide tool
|
||||||
|
tool_name = "web_search" if should_use_web(query) else "search_local_kb"
|
||||||
|
# Invoke agent
|
||||||
|
try:
|
||||||
|
result = agent.invoke({"input": query, "tool_choice": tool_name})
|
||||||
|
answer = result.get("output", "")
|
||||||
|
print("Answer:\n", answer)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
continue
|
||||||
|
print("Unknown command. Type /help for usage.")
|
||||||
|
""
|
||||||
Reference in New Issue
Block a user