Skip to content

Commit 253610f

Browse files
modified: langchain-crash-course/5_agents_tools/agent_react_chat.py
modified: langchain-crash-course/5_agents_tools/agent_react_rag_context.py modified: langchain-crash-course/5_agents_tools/agent_tools_basic.py modified: langchain-crash-course/5_agents_tools/logs/agent_tools.log modified: langchain-crash-course/5_agents_tools/rag.py
1 parent a9188a8 commit 253610f

5 files changed

Lines changed: 83 additions & 40 deletions

File tree

langchain-crash-course/5_agents_tools/agent_react_chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def get_session_history(session_id: str) -> BaseChatMessageHistory:
155155

156156
# Run agent executor with chat history
157157
async def main() -> None:
158-
logger.info(msg="Start Agent React Chat Application...")
158+
logger.info(msg="========= Start Agent React Chat Application ==========")
159159
print("Type 'exit' to end the conversation.")
160160

161161
session_id: str = "chat_session"
@@ -181,7 +181,7 @@ async def main() -> None:
181181
logger.info(msg=f"Agent: {response['output'][:100]}.....")
182182
print(f"Agent: {response['output']}")
183183

184-
except (KeyboardInterrupt, EOFError):
184+
except (KeyboardInterrupt, EOFError, asyncio.CancelledError):
185185
logger.info(msg="Keyboard interrupt or EOF error")
186186
print("Exiting...")
187187
break

langchain-crash-course/5_agents_tools/agent_react_rag_context.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,18 @@
7070

7171
# Load vector store and create retriever
7272
try:
73-
# Initialize vector store if it doesn't exist
74-
initialize_vector_store(
75-
books_dir=books_dir, persistent_directory=persistent_directory
76-
)
73+
# Check vector store
74+
if not persistent_directory.exists():
75+
# Initialize vector store
76+
db_instance = initialize_vector_store(
77+
books_dir=books_dir, persistent_directory=persistent_directory
78+
)
79+
# initialization failed, the directory might not exist.
80+
if db_instance is None and not persistent_directory.exists():
81+
logger.error(
82+
"Failed to initialize the vector store. Please check the logs for details."
83+
)
84+
sys.exit(1)
7785

7886
logger.info(msg=f"Loading vector store '{store_name}'...")
7987
# Load the Chroma vector store

langchain-crash-course/5_agents_tools/agent_tools_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async def main() -> None:
164164
logger.info(msg=f"Agent: {response['output']}")
165165
print(f"Agent: {response['output']}")
166166

167-
except (KeyboardInterrupt, EOFError):
167+
except (KeyboardInterrupt, EOFError, asyncio.CancelledError):
168168
logger.info(msg="Keyboard interrupt or EOF error")
169169
print("Exiting...")
170170
break
Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,59 @@
1-
2025-09-25 15:26:55,633 - agent_tools_basic.py - INFO - Start Agent Tools Basic Application...
2-
2025-09-25 15:27:19,931 - agent_tools_basic.py - INFO - Agent response generated successfully
3-
2025-09-25 15:27:35,672 - agent_tools_basic.py - INFO - User exited conversation
4-
2025-09-25 15:52:42,338 - agent_react_chat.py - INFO - Start Agent React Chat Application...
5-
2025-09-25 15:53:17,567 - agent_react_chat.py - INFO - Getting Wikipedia summary: Microsoft Corporation is an American multinational corporation and technology conglomerate headquart.....
6-
2025-09-25 15:53:29,552 - agent_react_chat.py - INFO - Getting Wikipedia summary: Microsoft Corporation is an American multinational corporation and technology conglomerate headquart.....
7-
2025-09-25 15:53:50,242 - agent_react_chat.py - INFO - Agent: Microsoft is a massive technology company with a rich history. It was founded in 1975 by Bill Gates .....
8-
2025-09-25 15:54:34,323 - agent_react_chat.py - ERROR - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
9-
2025-09-25 15:54:45,004 - agent_react_chat.py - INFO - Agent: Bill Gates is a prominent American business magnate, investor, and philanthropist. He co-founded Mic.....
10-
2025-09-25 15:55:00,779 - agent_react_chat.py - INFO - User exited conversation
11-
2025-11-07 16:40:33,066 - INFO - agent_react_rag_context - Keyboard interrupt or EOF error
12-
2025-11-07 16:40:52,754 - INFO - rag - ========= Starting Create RAG With Metadata Application ==========
13-
2025-11-07 16:40:52,754 - INFO - rag - Books Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books
14-
2025-11-07 16:40:52,754 - INFO - rag - Persistent Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\db\chroma_db_with_metadata
15-
2025-11-07 16:40:52,754 - INFO - agent_react_rag_context - ========= Starting ReAct Agent with RAG Context Application ==========
16-
2025-11-07 16:40:54,831 - INFO - rag - Vector store already exists. No need to initialize.
17-
2025-11-07 16:40:54,831 - INFO - agent_react_rag_context - Loading vector store 'chroma_db_with_metadata'...
18-
2025-11-07 16:40:55,023 - INFO - agent_react_rag_context - Created retriever from vector store 'chroma_db_with_metadata' successfully.
19-
2025-11-07 16:41:45,455 - INFO - agent_react_rag_context - Processing user query through ReAct Agent with RAG context...
20-
2025-11-07 16:42:51,393 - INFO - agent_react_rag_context - AI response generated successfully
21-
2025-11-07 16:42:51,395 - INFO - agent_react_rag_context - Chat history updated successfully
22-
2025-11-07 16:43:10,967 - INFO - agent_react_rag_context - User exited conversation
1+
2025-11-07 16:53:55,877 - INFO - agent_tools_basic - ========= Start Agent Tools Basic Application ==========
2+
2025-11-07 16:55:25,273 - INFO - agent_tools_basic - Agent: 2025-11-07 16:55:21
3+
2025-11-07 16:55:56,756 - INFO - agent_tools_basic - Keyboard interrupt or EOF error
4+
2025-11-07 17:03:09,943 - INFO - agent_react_chat - ========= Start Agent React Chat Application ==========
5+
2025-11-07 17:06:11,380 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
6+
2025-11-07 17:06:17,242 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
7+
2025-11-07 17:06:22,305 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
8+
2025-11-07 17:06:27,571 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
9+
2025-11-07 17:06:32,986 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
10+
2025-11-07 17:06:38,767 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
11+
2025-11-07 17:06:43,819 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
12+
2025-11-07 17:06:49,191 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
13+
2025-11-07 17:06:54,262 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
14+
2025-11-07 17:06:59,852 - ERROR - agent_react_chat - Error getting Wikipedia summary: Page id "bill games" does not match any pages. Try another id!
15+
2025-11-07 17:07:07,572 - INFO - agent_react_chat - Agent: Bill Gates is an American business magnate, investor, and philanthropist. He co-founded Microsoft, o.....
16+
2025-11-07 17:09:23,226 - INFO - agent_react_chat - User exited conversation
17+
2025-11-07 18:10:52,135 - INFO - rag - ======== Starting Create RAG With Metadata Application ========
18+
2025-11-07 18:10:52,150 - INFO - rag - Books Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books
19+
2025-11-07 18:10:52,150 - INFO - rag - Persistent Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\db\chroma_db_with_metadata
20+
2025-11-07 18:10:52,150 - INFO - rag - Initializing new vector store...
21+
2025-11-07 18:10:52,150 - INFO - rag - Successfully loaded document: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books\langchain_demo.txt
22+
2025-11-07 18:10:52,150 - INFO - rag - Successfully created 4 document chunks
23+
2025-11-07 18:10:52,150 - INFO - rag - Creating embeddings...
24+
2025-11-07 18:10:53,229 - INFO - rag - Embeddings created successfully
25+
2025-11-07 18:10:53,229 - INFO - rag - Initializing Chroma vector store...
26+
2025-11-07 18:10:55,072 - INFO - rag - Vector store initialized successfully
27+
2025-11-07 18:12:09,615 - INFO - rag - ======== Starting Create RAG With Metadata Application ========
28+
2025-11-07 18:12:09,615 - INFO - rag - Books Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books
29+
2025-11-07 18:12:09,615 - INFO - rag - Persistent Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\db\chroma_db_with_metadata
30+
2025-11-07 18:12:09,615 - INFO - rag - Vector store already exists. No need to initialize.
31+
2025-11-07 18:12:09,615 - INFO - rag - Vector store already exists. Standalone execution complete.
32+
2025-11-07 18:25:37,063 - INFO - agent_react_rag_context - ========= Starting ReAct Agent with RAG Context Application ==========
33+
2025-11-07 18:25:39,092 - INFO - rag - ======== Starting Create RAG With Metadata Application ========
34+
2025-11-07 18:25:39,092 - INFO - rag - Books Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books
35+
2025-11-07 18:25:39,092 - INFO - rag - Persistent Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\db\chroma_db_with_metadata
36+
2025-11-07 18:25:39,092 - INFO - rag - Initializing new vector store...
37+
2025-11-07 18:25:39,092 - ERROR - rag - The directory 'C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books' does not exist. Please check the path.
38+
2025-11-07 18:25:39,092 - ERROR - agent_react_rag_context - Failed to initialize the vector store. Please check the logs for details.
39+
2025-11-07 18:28:30,459 - INFO - agent_react_rag_context - ========= Starting ReAct Agent with RAG Context Application ==========
40+
2025-11-07 18:28:32,486 - INFO - rag - ======== Starting Create RAG With Metadata Application ========
41+
2025-11-07 18:28:32,487 - INFO - rag - Books Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books
42+
2025-11-07 18:28:32,487 - INFO - rag - Persistent Directory: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\db\chroma_db_with_metadata
43+
2025-11-07 18:28:32,488 - INFO - rag - Initializing new vector store...
44+
2025-11-07 18:28:32,489 - INFO - rag - Successfully loaded document: C:\Users\path_to_file\LangChain\langchain_tutorial\langchain-crash-course\5_agents_tools\books\langchain_demo.txt
45+
2025-11-07 18:28:32,489 - INFO - rag - Successfully created 4 document chunks
46+
2025-11-07 18:28:32,490 - INFO - rag - Creating embeddings...
47+
2025-11-07 18:28:33,563 - INFO - rag - Embeddings created successfully
48+
2025-11-07 18:28:33,564 - INFO - rag - Initializing Chroma vector store...
49+
2025-11-07 18:28:34,978 - INFO - rag - Vector store initialized successfully
50+
2025-11-07 18:28:34,978 - INFO - agent_react_rag_context - Loading vector store 'chroma_db_with_metadata'...
51+
2025-11-07 18:28:35,046 - INFO - agent_react_rag_context - Created retriever from vector store 'chroma_db_with_metadata' successfully.
52+
2025-11-07 18:29:00,819 - INFO - agent_react_rag_context - Processing user query through ReAct Agent with RAG context...
53+
2025-11-07 18:30:04,321 - INFO - agent_react_rag_context - AI response generated successfully
54+
2025-11-07 18:30:04,322 - INFO - agent_react_rag_context - Chat history updated successfully
55+
2025-11-07 18:30:17,985 - INFO - agent_react_rag_context - User exited conversation
56+
2025-11-07 18:30:30,502 - INFO - agent_react_rag_context - ========= Starting ReAct Agent with RAG Context Application ==========
57+
2025-11-07 18:30:32,580 - INFO - agent_react_rag_context - Loading vector store 'chroma_db_with_metadata'...
58+
2025-11-07 18:30:32,761 - INFO - agent_react_rag_context - Created retriever from vector store 'chroma_db_with_metadata' successfully.
59+
2025-11-07 18:30:41,058 - INFO - agent_react_rag_context - Keyboard interrupt or EOF error

langchain-crash-course/5_agents_tools/rag.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@
3333
# Set up logger
3434
logger: Logger = ReActAgentLogger.get_logger(module_name=module_path.name)
3535

36-
# Log create rag with metadata
37-
logger.info(msg="========= Starting Create RAG With Metadata Application ==========")
38-
39-
# Log directories
40-
logger.info(msg=f"Books Directory: {books_dir}")
41-
logger.info(msg=f"Persistent Directory: {persistent_directory}")
42-
4336

4437
# Load documents
4538
def load_documents(books_dir: Path) -> List[Document]:
@@ -73,7 +66,7 @@ def load_documents(books_dir: Path) -> List[Document]:
7366
for doc in docs:
7467
doc.metadata = {"source": file_path.name}
7568
documents.append(doc)
76-
logger.info(msg=f"Successfully loaded: {file_path}")
69+
logger.info(msg=f"Successfully loaded document: {file_path}")
7770
except Exception as e:
7871
error_msg: str = f"Error loading {file_path}: {str(object=e)}"
7972
failed_files.append((file_path, str(object=e)))
@@ -147,6 +140,12 @@ def initialize_vector_store(
147140
Optional[Chroma]: The created Chroma vector store instance if a new
148141
store was initialized, otherwise None.
149142
"""
143+
144+
# Log application startup
145+
logger.info(msg="======== Starting Create RAG With Metadata Application ========")
146+
logger.info(msg=f"Books Directory: {books_dir}")
147+
logger.info(msg=f"Persistent Directory: {persistent_directory}")
148+
150149
try:
151150
# Check vector store existence
152151
if persistent_directory.exists():
@@ -216,6 +215,7 @@ def main() -> None:
216215
This function calls the necessary functions to set up the vector store.
217216
It handles the main logic flow and logs the outcome of the initialization.
218217
"""
218+
219219
try:
220220
# Initialize vector store
221221
db: Optional[Chroma] = initialize_vector_store(
@@ -226,9 +226,7 @@ def main() -> None:
226226
# Check initialization result
227227
if db is None and persistent_directory.exists():
228228
logger.info("Vector store already exists. Standalone execution complete.")
229-
elif db is not None:
230-
logger.info("Vector store initialization completed successfully")
231-
else:
229+
elif db is None:
232230
logger.error(
233231
"Vector store initialization failed - System may not function correctly!"
234232
)

0 commit comments

Comments
 (0)