Build ChromaDB + Tavily RAG agent with Ollama embeddings, local/web tools, create_agent routing, and CLI ingest flow.: update main.py

This commit is contained in:
2026-06-16 13:12:41 +00:00
parent 845c894ffe
commit 9e132e89c3
+47 -16
View File
@@ -1,20 +1,51 @@
""" import argparse
Simple chat loop for the RAG agent.
""" from dotenv import load_dotenv
import os
from langchain.agents import AgentExecutor
from agent import agent from agent import agent
from vectorstore import create_vectorstore, load_documents
# Create executor with memory
executor = AgentExecutor(agent=agent, verbose=True)
print("RAGAgent ready. Type 'exit' to quit.") def ingest_documents(directory: str) -> None:
while True: vectorstore = create_vectorstore()
user_input = input("You: ") chunk_count = load_documents(directory, vectorstore)
if user_input.lower() in {"exit", "quit"}: print(f"Loaded {chunk_count} chunks from {directory} into ChromaDB.")
break
result = executor.invoke({"input": user_input})
# The agent returns a dict with keys 'output' and possibly tool calls. def run_chat() -> None:
print(f"Assistant: {result.get('output', '')}") print("RAG agent ready. Type 'exit' to quit.")
print("Goodbye!") while True:
user_input = input("You: ").strip()
if user_input.lower() == "exit":
print("Goodbye!")
return
if not user_input:
continue
result = agent.invoke(
{"messages": [{"role": "user", "content": user_input}]}
)
final_message = result["messages"][-1]
print(f"Assistant: {final_message.content}")
def main() -> None:
load_dotenv()
parser = argparse.ArgumentParser()
parser.add_argument(
"--ingest",
metavar="DIRECTORY",
help="Load .txt and .md files from DIRECTORY into ChromaDB before the demo.",
)
args = parser.parse_args()
if args.ingest:
ingest_documents(args.ingest)
return
run_chat()
if __name__ == "__main__":
main()