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
28 changes: 26 additions & 2 deletions langchain-crash-course/5_agents_tools/agent_react_chat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# agent_react_chat.py
"""
ReAct Chat Agent Application
(Asynchronous Version)

This module demonstrates a conversational LangChain agent with memory.
It creates a structured chat agent that can use predefined tools to
answer user questions in an interactive conversation loop, maintaining
the context of the conversation.

The agent uses either OpenAI or Ollama as the language model backend and includes
tools for getting the current time and fetching summaries from Wikipedia. It
leverages `RunnableWithMessageHistory` to manage chat history for each session,
allowing for contextual conversations.

Features:
- Configurable LLM backend (OpenAI or Ollama)
- Structured chat agent with ReAct (Reason and Action) logic
- Conversational memory management
- Custom tool integration (Time, Wikipedia)
- Interactive asynchronous conversation loop
- Comprehensive logging
"""

# Import standard libraries
import asyncio
Expand Down Expand Up @@ -28,6 +50,8 @@

# Import custom modules
from utils.logger import RAGLogger

# Import Wikipedia library
from wikipedia import summary

# Load environment variables from .env
Expand Down Expand Up @@ -73,7 +97,7 @@ def get_current_time(*args: Any, **kwargs: Any) -> str:
def get_wikipedia_summary(query: str) -> str:
"""Get a summary from Wikipedia."""
try:
summary_result: str = summary(title=query, sentences=3)
summary_result: str = summary(title=query, sentences=10)
logger.info(msg=f"Getting Wikipedia summary: {summary_result[:100]}.....")
return summary_result
except Exception as e:
Expand Down Expand Up @@ -145,7 +169,7 @@ async def main() -> None:
continue

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

Expand Down
2 changes: 1 addition & 1 deletion langchain-crash-course/5_agents_tools/agent_tools_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from pathlib import Path
from typing import Any

# For async input
# Import async console library
from aioconsole import ainput

# Import necessary libraries
Expand Down