Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions langchain-crash-course/5_agents_tools/agent_react_rag_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from dotenv import load_dotenv

# Import langchain modules
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
from langchain.chains import (
create_history_aware_retriever,
create_retrieval_chain,
Expand All @@ -20,6 +22,7 @@
from langchain_core.messages.base import BaseMessage
from langchain_core.prompts.chat import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.base import Runnable
from langchain_core.tools import Tool
from langchain_core.vectorstores.base import VectorStoreRetriever
from langchain_ollama import ChatOllama
from langchain_ollama.embeddings import OllamaEmbeddings
Expand All @@ -41,9 +44,9 @@
logger.info(msg="Starting Agent ReAct RAG Context Application")
logger.info(msg="=" * 50)

# ===== Setup RAG =====
# Define directories and paths
rag_dir: Path = Path(__file__).parents[1] / "4_rag"
books_dir: Path = rag_dir / "books"
db_dir: Path = rag_dir / "db"
store_name: str = "chroma_db_with_metadata"
persistent_directory: Path = db_dir / store_name
Expand Down Expand Up @@ -102,7 +105,7 @@
)

# Create a history-aware retriever
# this users the LLM to help reformulate the question based on chat history
# this helps LLM to reformulate the question based on chat history
history_aware_retriever: VectorStoreRetriever = create_history_aware_retriever(
llm,
retriever,
Expand Down Expand Up @@ -141,19 +144,46 @@
history_aware_retriever, question_answering_chain
)

# ===== Setup ReAct Agent with RAG =====
# load ReAct prompt template from hub
react_prompt_template: Any = hub.pull(owner_repo_commit="hwchase17/react")

# create a tool that uses the RAG chain
tools: list[Tool] = [
Tool(
name="Answer Question",
func=lambda input, **kwargs: rag_chain.invoke(
input={"input": input, "chat_history": kwargs.get("chat_history", [])}
),
description="Useful for answering questions based on the provided context.",
),
]

# Create a ReAct agent with the RAG tool
agent: Runnable[Any, Any] = create_react_agent(
tools=tools,
llm=llm,
prompt=react_prompt_template,
)

# Create agent executor
agent_executor: AgentExecutor = AgentExecutor(
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)

# Run RAG LLM conversation

# ===== Run ReAct RAG conversation =====
def main() -> None:
"""
Runs the main conversational loop for the RAG-based chat application.
Runs the main conversational loop for the RAG-based ReAct chat application.

This function initializes the chat history and enters an infinite loop to
continuously accept user input. It processes the user's query through the
RAG chain, prints the AI's response, and updates the chat history.
The loop can be exited by typing 'exit', or by sending a
KeyboardInterrupt (Ctrl+C) or EOFError (Ctrl+D).
"""
print("\nStart chatting with AI! Type 'exit' to end the conversation.")
print("\nStart RAG-based ReAct chatting! Type 'exit' to end the conversation.")

# Initialize chat history
chat_history: list[BaseMessage] = []
Expand All @@ -164,27 +194,30 @@ def main() -> None:
query: str = input("You: ").strip()

if not query:
print("Please ask a question!.")
continue

if query.lower() == "exit":
logger.info(msg="User exited conversation")
print("Exiting...")
break

# Process user query through RAG chain
logger.info(msg="Processing user query through RAG chain...")
result: Any = rag_chain.invoke(
# Process user query through agent executor
logger.info(
msg="Processing user query through ReAct Agent with RAG chain..."
)
response: Any = agent_executor.invoke(
input={"input": query, "chat_history": chat_history}
)

# Display AI response
if result:
if response:
logger.info(msg="AI response generated successfully")
print(f"AI: {result['answer']}")
print(f"AI: {response['output']}")

# Update chat history
chat_history.append(HumanMessage(content=query))
chat_history.append(AIMessage(content=result["answer"]))
chat_history.append(AIMessage(content=response["output"]))
logger.info(msg="Chat history updated successfully")

except (KeyboardInterrupt, EOFError):
Expand Down
15 changes: 15 additions & 0 deletions langchain-crash-course/5_agents_tools/logs/agent_tools.log
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@
2025-11-04 22:58:41,439 - INFO - agent_react_rag_context - AI response generated successfully
2025-11-04 22:58:41,466 - INFO - agent_react_rag_context - Chat history updated successfully
2025-11-04 22:59:12,397 - INFO - agent_react_rag_context - User exited conversation
2025-11-06 18:12:21,988 - INFO - agent_react_rag_context - ==================================================
2025-11-06 18:12:21,988 - INFO - agent_react_rag_context - Starting Agent ReAct RAG Context Application
2025-11-06 18:12:21,988 - INFO - agent_react_rag_context - ==================================================
2025-11-06 18:12:24,090 - INFO - agent_react_rag_context - Loading vector store 'chroma_db_with_metadata'...
2025-11-06 18:12:24,255 - INFO - agent_react_rag_context - Created retriever from vector store 'chroma_db_with_metadata' successfully.
2025-11-06 18:12:49,546 - INFO - agent_react_rag_context - Processing user query through ReAct Agent with RAG chain...
2025-11-06 18:13:53,367 - INFO - agent_react_rag_context - AI response generated successfully
2025-11-06 18:13:53,369 - INFO - agent_react_rag_context - Chat history updated successfully
2025-11-06 18:15:03,021 - INFO - agent_react_rag_context - Processing user query through ReAct Agent with RAG chain...
2025-11-06 18:16:15,185 - INFO - agent_react_rag_context - AI response generated successfully
2025-11-06 18:16:15,186 - INFO - agent_react_rag_context - Chat history updated successfully
2025-11-06 18:20:35,475 - INFO - agent_react_rag_context - Processing user query through ReAct Agent with RAG chain...
2025-11-06 18:21:10,565 - INFO - agent_react_rag_context - AI response generated successfully
2025-11-06 18:21:10,566 - INFO - agent_react_rag_context - Chat history updated successfully
2025-11-06 18:27:06,733 - INFO - agent_react_rag_context - User exited conversation