baeitzman commited on
Commit
d4c9525
·
verified ·
1 Parent(s): 82946df

Update bumain2.py

Browse files
Files changed (1) hide show
  1. bumain2.py +569 -571
bumain2.py CHANGED
@@ -1,572 +1,570 @@
1
- from dotenv import load_dotenv
2
- from typing import Union, Literal, Annotated, Sequence, TypedDict # Type hinting utilities
3
- from langgraph.graph import StateGraph, START, END # Graph structure and node types
4
- from langgraph.graph.message import add_messages # Message handling utility reducer function
5
- from langchain_openai import ChatOpenAI # OpenAI-compatible chat model wrapper
6
- from langchain_core.messages import HumanMessage, SystemMessage, AIMessage # Message types
7
- from langchain_core.messages import ToolMessage
8
- from langchain_community.document_loaders import PyPDFLoader
9
- from langchain_chroma import Chroma # Correct import for Chroma
10
- import ollama # Ollama integration for local model execution
11
- from langchain_ollama import OllamaEmbeddings # Correct import for OllamaEmbeddings
12
- from langchain_text_splitters import RecursiveCharacterTextSplitter
13
- from langchain_core.documents import Document # Use correct import for Document
14
- import glob
15
- from langgraph.prebuilt import ToolNode # Prebuilt nodes for tool integration
16
- from pydantic import BaseModel, Field
17
- import os # Operating system interface for environment variables
18
- import sys # For error handling and debugging
19
- import shutil # For directory manipulation
20
- import argparse # For command-line argument parsing
21
-
22
- # # Load environment variables from .env file, override existing ones
23
- load_dotenv(override=True)
24
-
25
- # Error handling for initialization
26
- try:
27
- # Initialize the Large Language Model (LLM) with OpenRouter configuration
28
- llm = ChatOpenAI(
29
- # model="deepseek/deepseek-r1-distill-qwen-32b:free", # Free model for testing
30
- model = "google/gemini-2.0-flash-001", # very cheap model and low latency
31
- api_key=os.getenv("OPENROUTER_KEY"), # Authentication key for API access
32
- base_url="https://openrouter.ai/api/v1", # Custom endpoint for OpenRouter service
33
- temperature=0.7, # Controls randomness in responses (0.0 = deterministic, 1.0 = very random)
34
- max_tokens=10000 # Maximum number of tokens in the response
35
- )
36
- except Exception as e:
37
- print(f"Error initializing LLM: {e}")
38
- sys.exit(1)
39
-
40
- # Define the state structure for the conversation graph
41
- class State(TypedDict):
42
- """State structure for the conversation graph."""
43
- messages: list[Union[HumanMessage, SystemMessage, AIMessage]]
44
- roleplay_messages: list[Union[HumanMessage, SystemMessage, AIMessage]] # Separate roleplay history
45
- message_type: str # Add message type for routing
46
- next: str # Add next field for routing
47
- session_mode: str # Track current session mode
48
- total_input_tokens: int # Track total input tokens used
49
- total_output_tokens: int # Track total output tokens used
50
-
51
- class CounselingKnowledgeBase:
52
- def __init__(self, pdf_directory="./counseling_materials/", persist_directory="./chroma_db", force_reload=False):
53
- """Initialize the knowledge base with PDF materials."""
54
- self.pdf_directory = pdf_directory
55
- self.persist_directory = persist_directory
56
- self.vectorstore = None
57
- self.force_reload = force_reload
58
- self.full_documents = [] # Store full document text
59
-
60
- # Error handling for embedding model initialization
61
- try:
62
- self.embeddings = OllamaEmbeddings(
63
- model="mxbai-embed-large"
64
- )
65
- self.load_documents()
66
- except Exception as e:
67
- print(f"Error initializing embeddings model: {e}")
68
- print("Make sure Ollama is running and mxbai-embed-large model is available")
69
- self.embeddings = None
70
-
71
- def load_documents(self):
72
- """Load and process PDF documents into a vector store."""
73
- if self.embeddings is None:
74
- print("Cannot load documents: embedding model not initialized")
75
- return
76
-
77
- # If force reload is enabled, delete the existing database
78
- if self.force_reload and os.path.exists(self.persist_directory):
79
- print(f"Forcing reload of embeddings. Removing existing database at {self.persist_directory}")
80
- try:
81
- shutil.rmtree(self.persist_directory)
82
- print(f"Removed existing database directory: {self.persist_directory}")
83
- except Exception as e:
84
- print(f"Error removing directory {self.persist_directory}: {e}")
85
-
86
- # Check if vectorstore already exists
87
- if not self.force_reload and os.path.exists(self.persist_directory) and os.listdir(self.persist_directory):
88
- print(f"Loading existing knowledge base from {self.persist_directory}")
89
- try:
90
- self.vectorstore = Chroma(
91
- persist_directory=self.persist_directory,
92
- embedding_function=self.embeddings
93
- )
94
- # We still need to load the full documents
95
- self.load_full_documents()
96
- print("Successfully loaded existing knowledge base")
97
- return
98
- except Exception as e:
99
- print(f"Error loading existing knowledge base: {e}")
100
- print("Creating new knowledge base...")
101
-
102
- # Load full documents regardless of whether we're using the vectorstore
103
- self.load_full_documents()
104
-
105
- # The rest of the method for creating the vectorstore (still useful for future functionality)
106
- documents = []
107
-
108
- # Find all PDF files in the directory
109
- pdf_files = glob.glob(f"{self.pdf_directory}*.pdf")
110
-
111
- if not pdf_files:
112
- print(f"Warning: No PDF files found in {self.pdf_directory}")
113
- return
114
-
115
- # Load each PDF
116
- for pdf_file in pdf_files:
117
- try:
118
- loader = PyPDFLoader(pdf_file)
119
- docs = loader.load()
120
-
121
- # Add source metadata
122
- for doc in docs:
123
- doc.metadata['source'] = pdf_file
124
-
125
- documents.extend(docs)
126
- print(f"Loaded: {pdf_file}")
127
- except Exception as e:
128
- print(f"Error loading {pdf_file}: {e}")
129
-
130
- if documents:
131
- # Split documents into chunks
132
- text_splitter = RecursiveCharacterTextSplitter(
133
- chunk_size=1000,
134
- chunk_overlap=200
135
- )
136
- splits = text_splitter.split_documents(documents)
137
-
138
- # Create vector store
139
- print("Creating embeddings... This may take a few minutes.")
140
- try:
141
- self.vectorstore = Chroma.from_documents(
142
- documents=splits,
143
- embedding=self.embeddings,
144
- persist_directory=self.persist_directory
145
- )
146
- print(f"Created knowledge base with {len(splits)} document chunks")
147
- except Exception as e:
148
- print(f"Error creating vector store: {e}")
149
- else:
150
- print("No documents were successfully loaded")
151
-
152
- def load_full_documents(self):
153
- """Load the full text of all PDF documents."""
154
- self.full_documents = []
155
-
156
- # Find all PDF files in the directory
157
- pdf_files = glob.glob(f"{self.pdf_directory}*.pdf")
158
-
159
- if not pdf_files:
160
- print(f"Warning: No PDF files found in {self.pdf_directory}")
161
- return
162
-
163
- # Load each PDF
164
- for pdf_file in pdf_files:
165
- try:
166
- loader = PyPDFLoader(pdf_file)
167
- docs = loader.load()
168
-
169
- # Extract file name without path and extension
170
- file_name = os.path.basename(pdf_file)
171
- file_name = os.path.splitext(file_name)[0]
172
-
173
- # Combine all pages into a single document
174
- full_text = "\n\n".join([doc.page_content for doc in docs])
175
-
176
- # Add to our full documents collection
177
- self.full_documents.append({
178
- "title": file_name,
179
- "content": full_text[:50000] # Limit content size to avoid context overflow
180
- })
181
-
182
- print(f"Loaded full text: {file_name}")
183
- except Exception as e:
184
- print(f"Error loading full text of {pdf_file}: {e}")
185
-
186
- def search_relevant_content(self, query, k=3):
187
- """Search for relevant counseling content."""
188
- if not self.vectorstore:
189
- return []
190
-
191
- try:
192
- docs = self.vectorstore.similarity_search(query, k=k)
193
- return docs
194
- except Exception as e:
195
- print(f"Error searching knowledge base: {e}")
196
- return []
197
-
198
- def get_all_documents(self):
199
- """Return all stored document contents."""
200
- return self.full_documents
201
-
202
-
203
- # Initialize knowledge base globally
204
- knowledge_base = None
205
-
206
- def initialize_knowledge_base(force_reload=False):
207
- """Initialize the counseling knowledge base."""
208
- global knowledge_base
209
- try:
210
- knowledge_base = CounselingKnowledgeBase(force_reload=force_reload)
211
- return knowledge_base
212
- except Exception as e:
213
- print(f"Warning: Could not initialize knowledge base: {e}")
214
- return None
215
-
216
- # Define a Pydantic model for classifying messages
217
- class MessageClassifier(BaseModel):
218
- message_type: Literal["roleplay", "expert"] = Field(
219
- default="roleplay",
220
- description="Classify if the message is asking for feedback or not."
221
- )
222
-
223
- # Define the function to classify messages based on their content
224
- def classify_message(state: State):
225
- # Get the last message in the conversation
226
- last_message = state["messages"][-1]
227
-
228
- # Use a simple keyword-based classification
229
- message_content = last_message.content.lower()
230
-
231
- # Check for keywords that indicate expert/feedback requests
232
- expert_keywords = ['feedback', 'review', 'expert', 'advice', 'help me improve', 'how did i do']
233
- return_keywords = ['continue', 'back to roleplay', 'return', 'keep going']
234
-
235
- # If currently in expert mode, only return to roleplay if explicitly requested
236
- if state.get("session_mode") == "expert":
237
- if any(keyword in message_content for keyword in return_keywords):
238
- return {"message_type": "roleplay"}
239
- else:
240
- return {"message_type": "expert"} # Stay in expert mode
241
-
242
- # If in roleplay mode, check for expert requests
243
- if any(keyword in message_content for keyword in expert_keywords):
244
- return {"message_type": "expert"}
245
- else:
246
- return {"message_type": "roleplay"}
247
-
248
- def clear_screen():
249
- """Clear the terminal screen."""
250
- if os.name == 'nt': # Windows
251
- os.system('cls')
252
- else: # macOS and Linux
253
- os.system('clear')
254
-
255
- def display_roleplay_history(state: State):
256
- """Display the roleplay conversation history without system messages."""
257
- if "roleplay_messages" not in state or not state["roleplay_messages"]:
258
- return
259
-
260
- print("=== Conversation History ===\n")
261
-
262
- for msg in state["roleplay_messages"]:
263
- if isinstance(msg, SystemMessage):
264
- continue # Skip system messages
265
- elif isinstance(msg, HumanMessage):
266
- print(f"You: {msg.content}")
267
- elif isinstance(msg, AIMessage):
268
- print(f"Client: {msg.content}")
269
-
270
- print("\n" + "="*50)
271
- print("Continuing roleplay...\n")
272
-
273
- # Define the routing function to direct the conversation flow based on message type
274
- def router(state: State):
275
- message_type = state["message_type"]
276
- if message_type == "expert":
277
- return {"next": "expert"}
278
- else:
279
- return {"next": "roleplay"}
280
-
281
- # create the agent functions with different system messages
282
-
283
- def roleplay_agent(state: State):
284
- # Initialize roleplay_messages if not present
285
- if "roleplay_messages" not in state:
286
- state["roleplay_messages"] = []
287
-
288
- # Add system message only if roleplay_messages is empty
289
- if not state["roleplay_messages"]:
290
- state["roleplay_messages"] = [
291
- SystemMessage(content="""
292
- You are Margaret, an 82-year-old woman who has just lost her house in a landslide.
293
- You are currently at a Red Cross shelter seeking help.
294
- You are worried about your cat name Whiskers (a grey tabby with white paws) who is missing.
295
- You are feeling overwhelmed, scared, and uncertain about the future.
296
- You are having a real conversation with a counselor.
297
- CRITICAL INSTRUCTIONS:
298
- - You must respond with ONLY ONE response as Margaret
299
- - NEVER provide multiple options or numbered lists
300
- - NEVER generate examples or alternatives
301
- - You are having a real conversation, not creating training data
302
- - Respond naturally as if you are actually Margaret talking to the counselor
303
- - NEVER use descriptive cues, stage directions, or actions in asterisks like *chokes up*, *cries*, *sighs*
304
- - NEVER use parenthetical descriptions or emotional indicators
305
- - Only speak your actual words - no narration or description of how you're saying them
306
- - Use only dialogue - what Margaret would actually say out loud
307
- - You speak in first person as yourself (I feel..., My cat..., I need...)
308
- - Keep most responses under 30 words and very realistic
309
- - If the counselor has built rapport, you can open up more and give more of the story of what happened and some of your past history.
310
- - Don't use quotation marks.
311
- - You are talking TO the counselor, not helping them
312
- NEVER break character. NEVER offer help. NEVER provide lists. NEVER use stage directions. Give ONE authentic response only.
313
- """)
314
- ]
315
-
316
- # Add the current user message to roleplay history
317
- current_user_message = state["messages"][-1]
318
- state["roleplay_messages"].append(current_user_message)
319
-
320
- # Add a reinforcement message every few exchanges to maintain character
321
- if len(state["roleplay_messages"]) > 1 and len(state["roleplay_messages"]) % 6 == 0:
322
- state["roleplay_messages"].append(
323
- SystemMessage(content="Remember: You are Margaret in a real conversation. Give ONE authentic response, not multiple options.")
324
- )
325
-
326
- reply = llm.invoke(state["roleplay_messages"])
327
-
328
- # Track token usage
329
- if hasattr(reply, 'usage_metadata') and reply.usage_metadata:
330
- input_tokens = reply.usage_metadata.get('input_tokens', 0)
331
- output_tokens = reply.usage_metadata.get('output_tokens', 0)
332
- state["total_input_tokens"] = state.get("total_input_tokens", 0) + input_tokens
333
- state["total_output_tokens"] = state.get("total_output_tokens", 0) + output_tokens
334
-
335
- # Add AI response to roleplay history
336
- state["roleplay_messages"].append(reply)
337
-
338
- return {
339
- "messages": state["messages"] + [reply],
340
- "roleplay_messages": state["roleplay_messages"],
341
- "session_mode": "roleplay",
342
- "total_input_tokens": state.get("total_input_tokens", 0),
343
- "total_output_tokens": state.get("total_output_tokens", 0)
344
- }
345
-
346
- # Modify the expert agent to fix the error with the reply variable
347
- def expert_agent(state: State):
348
- # Get the current user message
349
- current_message = state["messages"][-1].content.lower()
350
-
351
- # Check if this is a return-to-roleplay command
352
- return_keywords = ['continue', 'back to roleplay', 'return', 'keep going']
353
- if any(keyword in current_message for keyword in return_keywords):
354
- # Signal that we want to return to roleplay without generating a response
355
- return {
356
- "messages": [], # No response needed for mode switch
357
- "session_mode": "roleplay",
358
- "switching_to_roleplay": True, # Flag to indicate mode switch
359
- "total_input_tokens": state.get("total_input_tokens", 0),
360
- "total_output_tokens": state.get("total_output_tokens", 0)
361
- }
362
-
363
- # Get the roleplay conversation without system messages for analysis
364
- roleplay_conversation = [
365
- msg for msg in state.get("roleplay_messages", [])
366
- if not isinstance(msg, SystemMessage)
367
- ]
368
-
369
- # Get all counseling documents instead of searching
370
- counseling_materials = ""
371
- if knowledge_base:
372
- docs = knowledge_base.get_all_documents()
373
- if docs:
374
- # Format all documents with their titles
375
- doc_texts = []
376
- for doc in docs:
377
- # Limit each document to first 10000 characters to prevent context length issues
378
- excerpt = doc["content"][:10000] + ("..." if len(doc["content"]) > 10000 else "")
379
- doc_texts.append(f"--- {doc['title']} ---\n{excerpt}")
380
-
381
- counseling_materials = "\n\nCounseling Materials:\n\n" + "\n\n".join(doc_texts)
382
-
383
- # Create expert prompt with full roleplay context and materials
384
- expert_messages = [
385
- SystemMessage(content=f"""
386
- You are an expert in teaching counseling skills to new counselors.
387
- Review the conversation between the counselor and client.
388
- Use the provided counseling materials to inform your feedback and suggestions.
389
- If the user asks for help in a particular area,
390
- provide specific feedback on the counselor's responses using the provided materials.
391
- If the user only asks for general feedback,
392
- provide a comprehensive review of the conversation covering these areas:
393
- - What they did well (active listening, empathy, validation)
394
- - Areas for improvement
395
- - Specific examples of better responses they could have used
396
- - Counseling techniques they should consider (reflection, paraphrasing, open-ended questions)
397
- - Reference relevant counseling principles from the materials when applicable
398
- Be constructive and educational. Reference specific parts of their conversation.
399
- {counseling_materials}
400
- """),
401
- HumanMessage(content=f"Here's the conversation so far:\n\n" +
402
- "\n".join([f"{'Counselor' if isinstance(msg, HumanMessage) else 'Client'}: {msg.content}"
403
- for msg in roleplay_conversation]) +
404
- f"\n\nCurrent request: {state['messages'][-1].content}")
405
- ]
406
-
407
- # Invoke the LLM to get a response
408
- reply = llm.invoke(expert_messages)
409
-
410
- # print out what is sent to the expert agent
411
- print("\n=== Expert Agent Input ===")
412
- print(f"System: (Counseling materials with {len(counseling_materials)} characters)")
413
- print(f"Human: Here's the conversation so far: (with {len(roleplay_conversation)} messages)")
414
- print(f"Current request: {state['messages'][-1].content}")
415
- print("\n=== Expert Agent Response ===")
416
- print(reply.content)
417
-
418
- # Track token usage
419
- if hasattr(reply, 'usage_metadata') and reply.usage_metadata:
420
- input_tokens = reply.usage_metadata.get('input_tokens', 0)
421
- output_tokens = reply.usage_metadata.get('output_tokens', 0)
422
- state["total_input_tokens"] = state.get("total_input_tokens", 0) + input_tokens
423
- state["total_output_tokens"] = state.get("total_output_tokens", 0) + output_tokens
424
-
425
- return {
426
- "messages": [reply], # Only return expert response
427
- "session_mode": "expert",
428
- "total_input_tokens": state.get("total_input_tokens", 0),
429
- "total_output_tokens": state.get("total_output_tokens", 0)
430
- }
431
-
432
- # Create a state graph builder for defining the conversation flow
433
- graph_builder = StateGraph(State)
434
- # Build the conversation graph structure
435
- graph_builder.add_node("classifier", classify_message) # Add message classification node
436
- graph_builder.add_node("router", router) # Add the chatbot function as a node
437
- graph_builder.add_node("roleplay", roleplay_agent) # Add roleplay agent node
438
- graph_builder.add_node("expert", expert_agent) # Add expert agent node
439
-
440
- # Define the flow of the conversation graph
441
- graph_builder.add_edge(START, "classifier") # Start with the classifier node
442
- graph_builder.add_edge("classifier", "router") # Route to the router node
443
-
444
- # Route based on the classification result
445
- graph_builder.add_conditional_edges(
446
- "router",
447
- lambda state: state.get("next"),
448
- path_map={
449
- "expert": "expert", # If classified as expert, go to expert agent
450
- "roleplay": "roleplay" # If classified as roleplay, go to roleplay agent
451
- }
452
- )
453
-
454
- # End the graph after each response - let the main loop handle the next user input
455
- graph_builder.add_edge("roleplay", END) # After roleplay, end this graph execution
456
- graph_builder.add_edge("expert", END) # After expert feedback, end this graph execution
457
-
458
- # Compile the graph into an executable workflow
459
- graph = graph_builder.compile()
460
-
461
- def run_chatbot(state: State=None, force_reload_embeddings=False):
462
- """Run the chatbot interaction loop, allowing users to input messages and receive responses."""
463
- # Initialize knowledge base if not already done
464
- if knowledge_base is None:
465
- print("Initializing counseling knowledge base...")
466
- initialize_knowledge_base(force_reload=force_reload_embeddings)
467
-
468
- # Initialize the state if not provided
469
- if state is None:
470
- state = {
471
- "messages": [],
472
- "roleplay_messages": [],
473
- "message_type": "",
474
- "next": "",
475
- "session_mode": "roleplay",
476
- "total_input_tokens": 0,
477
- "total_output_tokens": 0
478
- }
479
-
480
- # Print a welcome message
481
- print("=== Counseling Skills Practice ===")
482
- print("You'll roleplay with an elderly woman who lost her house in a landslide.")
483
- if knowledge_base:
484
- print("Expert feedback enhanced with counseling materials.")
485
- print("\nCommands:")
486
- print("- Type 'feedback', 'advice', or 'expert' to get coaching on your counseling skills")
487
- print("- Type 'continue' or 'back to roleplay' to return to the roleplay after feedback")
488
- print("- Type 'exit' or 'quit' to end the session")
489
- print("\nStarting roleplay...\n")
490
-
491
- # Start the conversation loop
492
- while True:
493
- # Show current mode
494
- mode_indicator = f"[{state.get('session_mode', 'roleplay').upper()}] "
495
- user_input = input(f"\n{mode_indicator}You: ")
496
-
497
- if user_input.lower() in ["exit", "quit"]:
498
- # Print token usage summary
499
- total_input = state.get("total_input_tokens", 0)
500
- total_output = state.get("total_output_tokens", 0)
501
- total_tokens = total_input + total_output
502
-
503
- print("\n" + "="*50)
504
- print("SESSION SUMMARY")
505
- print("="*50)
506
- print(f"Total Input Tokens: {total_input:,}")
507
- print(f"Total Output Tokens: {total_output:,}")
508
- print(f"Total Tokens Used: {total_tokens:,}")
509
- print("="*50)
510
- print("Session ended. Thanks for practicing!")
511
- break
512
-
513
- # Add the user's message to the conversation state
514
- state["messages"] = [HumanMessage(content=user_input)]
515
-
516
- # Execute the graph with the current state
517
- result = graph.invoke(state)
518
-
519
- # Update state with result
520
- state.update(result)
521
-
522
- # Check if we're switching back to roleplay
523
- if state.get("switching_to_roleplay"):
524
- clear_screen()
525
- display_roleplay_history(state)
526
- state.pop("switching_to_roleplay", None) # Remove the flag
527
- continue # Skip the response printing and go to next input
528
-
529
- # Print the AI's response only if there is one
530
- if state.get("messages") and len(state["messages"]) > 0 and state["messages"][-1].content.strip():
531
- response = state["messages"][-1].content
532
- agent_name = "Expert Coach" if state.get("session_mode") == "expert" else "Client"
533
- print(f"{agent_name}: {response}")
534
-
535
- # Add helpful hints based on mode
536
- if state.get("session_mode") == "expert":
537
- print("\n(Type 'continue' to return to roleplay, or ask for more specific feedback)")
538
- elif state.get("session_mode") == "roleplay":
539
- print("(Type 'feedback' anytime to get expert coaching on your responses)")
540
-
541
- # Parse command-line arguments
542
- def parse_arguments():
543
- parser = argparse.ArgumentParser(description='Run the counseling skills practice chatbot')
544
- parser.add_argument('--embed', action='store_true',
545
- help='Force creation of new embeddings from documents in the counseling_materials folder')
546
- return parser.parse_args()
547
-
548
- # Add this at the end of the file, just before the if __name__ == "__main__": block
549
-
550
- def create_new_state():
551
- """Create and return a new state object for use in the Gradio interface."""
552
- return {
553
- "messages": [],
554
- "roleplay_messages": [],
555
- "message_type": "",
556
- "next": "",
557
- "session_mode": "roleplay",
558
- "total_input_tokens": 0,
559
- "total_output_tokens": 0
560
- }
561
-
562
- # Run the chatbot interaction loop
563
- if __name__ == "__main__":
564
- try:
565
- args = parse_arguments()
566
- run_chatbot(force_reload_embeddings=args.embed)
567
- except KeyboardInterrupt:
568
- print("\nProgram interrupted. Exiting gracefully...")
569
- except Exception as e:
570
- print(f"Error running chatbot: {e}")
571
- import traceback
572
  traceback.print_exc()
 
1
+ from dotenv import load_dotenv
2
+ from typing import Union, Literal, Annotated, Sequence, TypedDict # Type hinting utilities
3
+ from langgraph.graph import StateGraph, START, END # Graph structure and node types
4
+ from langgraph.graph.message import add_messages # Message handling utility reducer function
5
+ from langchain_openai import ChatOpenAI # OpenAI-compatible chat model wrapper
6
+ from langchain_core.messages import HumanMessage, SystemMessage, AIMessage # Message types
7
+ from langchain_core.messages import ToolMessage
8
+ from langchain_community.document_loaders import PyPDFLoader
9
+ from langchain_chroma import Chroma # Correct import for Chroma
10
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
11
+ from langchain_core.documents import Document # Use correct import for Document
12
+ import glob
13
+ from langgraph.prebuilt import ToolNode # Prebuilt nodes for tool integration
14
+ from pydantic import BaseModel, Field
15
+ import os # Operating system interface for environment variables
16
+ import sys # For error handling and debugging
17
+ import shutil # For directory manipulation
18
+ import argparse # For command-line argument parsing
19
+
20
+ # # Load environment variables from .env file, override existing ones
21
+ load_dotenv(override=True)
22
+
23
+ # Error handling for initialization
24
+ try:
25
+ # Initialize the Large Language Model (LLM) with OpenRouter configuration
26
+ llm = ChatOpenAI(
27
+ # model="deepseek/deepseek-r1-distill-qwen-32b:free", # Free model for testing
28
+ model = "google/gemini-2.0-flash-001", # very cheap model and low latency
29
+ api_key=os.getenv("OPENROUTER_KEY"), # Authentication key for API access
30
+ base_url="https://openrouter.ai/api/v1", # Custom endpoint for OpenRouter service
31
+ temperature=0.7, # Controls randomness in responses (0.0 = deterministic, 1.0 = very random)
32
+ max_tokens=10000 # Maximum number of tokens in the response
33
+ )
34
+ except Exception as e:
35
+ print(f"Error initializing LLM: {e}")
36
+ sys.exit(1)
37
+
38
+ # Define the state structure for the conversation graph
39
+ class State(TypedDict):
40
+ """State structure for the conversation graph."""
41
+ messages: list[Union[HumanMessage, SystemMessage, AIMessage]]
42
+ roleplay_messages: list[Union[HumanMessage, SystemMessage, AIMessage]] # Separate roleplay history
43
+ message_type: str # Add message type for routing
44
+ next: str # Add next field for routing
45
+ session_mode: str # Track current session mode
46
+ total_input_tokens: int # Track total input tokens used
47
+ total_output_tokens: int # Track total output tokens used
48
+
49
+ class CounselingKnowledgeBase:
50
+ def __init__(self, pdf_directory="./counseling_materials/", persist_directory="./chroma_db", force_reload=False):
51
+ """Initialize the knowledge base with PDF materials."""
52
+ self.pdf_directory = pdf_directory
53
+ self.persist_directory = persist_directory
54
+ self.vectorstore = None
55
+ self.force_reload = force_reload
56
+ self.full_documents = [] # Store full document text
57
+
58
+ # Error handling for embedding model initialization
59
+ try:
60
+ self.embeddings = OllamaEmbeddings(
61
+ model="mxbai-embed-large"
62
+ )
63
+ self.load_documents()
64
+ except Exception as e:
65
+ print(f"Error initializing embeddings model: {e}")
66
+ print("Make sure Ollama is running and mxbai-embed-large model is available")
67
+ self.embeddings = None
68
+
69
+ def load_documents(self):
70
+ """Load and process PDF documents into a vector store."""
71
+ if self.embeddings is None:
72
+ print("Cannot load documents: embedding model not initialized")
73
+ return
74
+
75
+ # If force reload is enabled, delete the existing database
76
+ if self.force_reload and os.path.exists(self.persist_directory):
77
+ print(f"Forcing reload of embeddings. Removing existing database at {self.persist_directory}")
78
+ try:
79
+ shutil.rmtree(self.persist_directory)
80
+ print(f"Removed existing database directory: {self.persist_directory}")
81
+ except Exception as e:
82
+ print(f"Error removing directory {self.persist_directory}: {e}")
83
+
84
+ # Check if vectorstore already exists
85
+ if not self.force_reload and os.path.exists(self.persist_directory) and os.listdir(self.persist_directory):
86
+ print(f"Loading existing knowledge base from {self.persist_directory}")
87
+ try:
88
+ self.vectorstore = Chroma(
89
+ persist_directory=self.persist_directory,
90
+ embedding_function=self.embeddings
91
+ )
92
+ # We still need to load the full documents
93
+ self.load_full_documents()
94
+ print("Successfully loaded existing knowledge base")
95
+ return
96
+ except Exception as e:
97
+ print(f"Error loading existing knowledge base: {e}")
98
+ print("Creating new knowledge base...")
99
+
100
+ # Load full documents regardless of whether we're using the vectorstore
101
+ self.load_full_documents()
102
+
103
+ # The rest of the method for creating the vectorstore (still useful for future functionality)
104
+ documents = []
105
+
106
+ # Find all PDF files in the directory
107
+ pdf_files = glob.glob(f"{self.pdf_directory}*.pdf")
108
+
109
+ if not pdf_files:
110
+ print(f"Warning: No PDF files found in {self.pdf_directory}")
111
+ return
112
+
113
+ # Load each PDF
114
+ for pdf_file in pdf_files:
115
+ try:
116
+ loader = PyPDFLoader(pdf_file)
117
+ docs = loader.load()
118
+
119
+ # Add source metadata
120
+ for doc in docs:
121
+ doc.metadata['source'] = pdf_file
122
+
123
+ documents.extend(docs)
124
+ print(f"Loaded: {pdf_file}")
125
+ except Exception as e:
126
+ print(f"Error loading {pdf_file}: {e}")
127
+
128
+ if documents:
129
+ # Split documents into chunks
130
+ text_splitter = RecursiveCharacterTextSplitter(
131
+ chunk_size=1000,
132
+ chunk_overlap=200
133
+ )
134
+ splits = text_splitter.split_documents(documents)
135
+
136
+ # Create vector store
137
+ print("Creating embeddings... This may take a few minutes.")
138
+ try:
139
+ self.vectorstore = Chroma.from_documents(
140
+ documents=splits,
141
+ embedding=self.embeddings,
142
+ persist_directory=self.persist_directory
143
+ )
144
+ print(f"Created knowledge base with {len(splits)} document chunks")
145
+ except Exception as e:
146
+ print(f"Error creating vector store: {e}")
147
+ else:
148
+ print("No documents were successfully loaded")
149
+
150
+ def load_full_documents(self):
151
+ """Load the full text of all PDF documents."""
152
+ self.full_documents = []
153
+
154
+ # Find all PDF files in the directory
155
+ pdf_files = glob.glob(f"{self.pdf_directory}*.pdf")
156
+
157
+ if not pdf_files:
158
+ print(f"Warning: No PDF files found in {self.pdf_directory}")
159
+ return
160
+
161
+ # Load each PDF
162
+ for pdf_file in pdf_files:
163
+ try:
164
+ loader = PyPDFLoader(pdf_file)
165
+ docs = loader.load()
166
+
167
+ # Extract file name without path and extension
168
+ file_name = os.path.basename(pdf_file)
169
+ file_name = os.path.splitext(file_name)[0]
170
+
171
+ # Combine all pages into a single document
172
+ full_text = "\n\n".join([doc.page_content for doc in docs])
173
+
174
+ # Add to our full documents collection
175
+ self.full_documents.append({
176
+ "title": file_name,
177
+ "content": full_text[:50000] # Limit content size to avoid context overflow
178
+ })
179
+
180
+ print(f"Loaded full text: {file_name}")
181
+ except Exception as e:
182
+ print(f"Error loading full text of {pdf_file}: {e}")
183
+
184
+ def search_relevant_content(self, query, k=3):
185
+ """Search for relevant counseling content."""
186
+ if not self.vectorstore:
187
+ return []
188
+
189
+ try:
190
+ docs = self.vectorstore.similarity_search(query, k=k)
191
+ return docs
192
+ except Exception as e:
193
+ print(f"Error searching knowledge base: {e}")
194
+ return []
195
+
196
+ def get_all_documents(self):
197
+ """Return all stored document contents."""
198
+ return self.full_documents
199
+
200
+
201
+ # Initialize knowledge base globally
202
+ knowledge_base = None
203
+
204
+ def initialize_knowledge_base(force_reload=False):
205
+ """Initialize the counseling knowledge base."""
206
+ global knowledge_base
207
+ try:
208
+ knowledge_base = CounselingKnowledgeBase(force_reload=force_reload)
209
+ return knowledge_base
210
+ except Exception as e:
211
+ print(f"Warning: Could not initialize knowledge base: {e}")
212
+ return None
213
+
214
+ # Define a Pydantic model for classifying messages
215
+ class MessageClassifier(BaseModel):
216
+ message_type: Literal["roleplay", "expert"] = Field(
217
+ default="roleplay",
218
+ description="Classify if the message is asking for feedback or not."
219
+ )
220
+
221
+ # Define the function to classify messages based on their content
222
+ def classify_message(state: State):
223
+ # Get the last message in the conversation
224
+ last_message = state["messages"][-1]
225
+
226
+ # Use a simple keyword-based classification
227
+ message_content = last_message.content.lower()
228
+
229
+ # Check for keywords that indicate expert/feedback requests
230
+ expert_keywords = ['feedback', 'review', 'expert', 'advice', 'help me improve', 'how did i do']
231
+ return_keywords = ['continue', 'back to roleplay', 'return', 'keep going']
232
+
233
+ # If currently in expert mode, only return to roleplay if explicitly requested
234
+ if state.get("session_mode") == "expert":
235
+ if any(keyword in message_content for keyword in return_keywords):
236
+ return {"message_type": "roleplay"}
237
+ else:
238
+ return {"message_type": "expert"} # Stay in expert mode
239
+
240
+ # If in roleplay mode, check for expert requests
241
+ if any(keyword in message_content for keyword in expert_keywords):
242
+ return {"message_type": "expert"}
243
+ else:
244
+ return {"message_type": "roleplay"}
245
+
246
+ def clear_screen():
247
+ """Clear the terminal screen."""
248
+ if os.name == 'nt': # Windows
249
+ os.system('cls')
250
+ else: # macOS and Linux
251
+ os.system('clear')
252
+
253
+ def display_roleplay_history(state: State):
254
+ """Display the roleplay conversation history without system messages."""
255
+ if "roleplay_messages" not in state or not state["roleplay_messages"]:
256
+ return
257
+
258
+ print("=== Conversation History ===\n")
259
+
260
+ for msg in state["roleplay_messages"]:
261
+ if isinstance(msg, SystemMessage):
262
+ continue # Skip system messages
263
+ elif isinstance(msg, HumanMessage):
264
+ print(f"You: {msg.content}")
265
+ elif isinstance(msg, AIMessage):
266
+ print(f"Client: {msg.content}")
267
+
268
+ print("\n" + "="*50)
269
+ print("Continuing roleplay...\n")
270
+
271
+ # Define the routing function to direct the conversation flow based on message type
272
+ def router(state: State):
273
+ message_type = state["message_type"]
274
+ if message_type == "expert":
275
+ return {"next": "expert"}
276
+ else:
277
+ return {"next": "roleplay"}
278
+
279
+ # create the agent functions with different system messages
280
+
281
+ def roleplay_agent(state: State):
282
+ # Initialize roleplay_messages if not present
283
+ if "roleplay_messages" not in state:
284
+ state["roleplay_messages"] = []
285
+
286
+ # Add system message only if roleplay_messages is empty
287
+ if not state["roleplay_messages"]:
288
+ state["roleplay_messages"] = [
289
+ SystemMessage(content="""
290
+ You are Margaret, an 82-year-old woman who has just lost her house in a landslide.
291
+ You are currently at a Red Cross shelter seeking help.
292
+ You are worried about your cat name Whiskers (a grey tabby with white paws) who is missing.
293
+ You are feeling overwhelmed, scared, and uncertain about the future.
294
+ You are having a real conversation with a counselor.
295
+ CRITICAL INSTRUCTIONS:
296
+ - You must respond with ONLY ONE response as Margaret
297
+ - NEVER provide multiple options or numbered lists
298
+ - NEVER generate examples or alternatives
299
+ - You are having a real conversation, not creating training data
300
+ - Respond naturally as if you are actually Margaret talking to the counselor
301
+ - NEVER use descriptive cues, stage directions, or actions in asterisks like *chokes up*, *cries*, *sighs*
302
+ - NEVER use parenthetical descriptions or emotional indicators
303
+ - Only speak your actual words - no narration or description of how you're saying them
304
+ - Use only dialogue - what Margaret would actually say out loud
305
+ - You speak in first person as yourself (I feel..., My cat..., I need...)
306
+ - Keep most responses under 30 words and very realistic
307
+ - If the counselor has built rapport, you can open up more and give more of the story of what happened and some of your past history.
308
+ - Don't use quotation marks.
309
+ - You are talking TO the counselor, not helping them
310
+ NEVER break character. NEVER offer help. NEVER provide lists. NEVER use stage directions. Give ONE authentic response only.
311
+ """)
312
+ ]
313
+
314
+ # Add the current user message to roleplay history
315
+ current_user_message = state["messages"][-1]
316
+ state["roleplay_messages"].append(current_user_message)
317
+
318
+ # Add a reinforcement message every few exchanges to maintain character
319
+ if len(state["roleplay_messages"]) > 1 and len(state["roleplay_messages"]) % 6 == 0:
320
+ state["roleplay_messages"].append(
321
+ SystemMessage(content="Remember: You are Margaret in a real conversation. Give ONE authentic response, not multiple options.")
322
+ )
323
+
324
+ reply = llm.invoke(state["roleplay_messages"])
325
+
326
+ # Track token usage
327
+ if hasattr(reply, 'usage_metadata') and reply.usage_metadata:
328
+ input_tokens = reply.usage_metadata.get('input_tokens', 0)
329
+ output_tokens = reply.usage_metadata.get('output_tokens', 0)
330
+ state["total_input_tokens"] = state.get("total_input_tokens", 0) + input_tokens
331
+ state["total_output_tokens"] = state.get("total_output_tokens", 0) + output_tokens
332
+
333
+ # Add AI response to roleplay history
334
+ state["roleplay_messages"].append(reply)
335
+
336
+ return {
337
+ "messages": state["messages"] + [reply],
338
+ "roleplay_messages": state["roleplay_messages"],
339
+ "session_mode": "roleplay",
340
+ "total_input_tokens": state.get("total_input_tokens", 0),
341
+ "total_output_tokens": state.get("total_output_tokens", 0)
342
+ }
343
+
344
+ # Modify the expert agent to fix the error with the reply variable
345
+ def expert_agent(state: State):
346
+ # Get the current user message
347
+ current_message = state["messages"][-1].content.lower()
348
+
349
+ # Check if this is a return-to-roleplay command
350
+ return_keywords = ['continue', 'back to roleplay', 'return', 'keep going']
351
+ if any(keyword in current_message for keyword in return_keywords):
352
+ # Signal that we want to return to roleplay without generating a response
353
+ return {
354
+ "messages": [], # No response needed for mode switch
355
+ "session_mode": "roleplay",
356
+ "switching_to_roleplay": True, # Flag to indicate mode switch
357
+ "total_input_tokens": state.get("total_input_tokens", 0),
358
+ "total_output_tokens": state.get("total_output_tokens", 0)
359
+ }
360
+
361
+ # Get the roleplay conversation without system messages for analysis
362
+ roleplay_conversation = [
363
+ msg for msg in state.get("roleplay_messages", [])
364
+ if not isinstance(msg, SystemMessage)
365
+ ]
366
+
367
+ # Get all counseling documents instead of searching
368
+ counseling_materials = ""
369
+ if knowledge_base:
370
+ docs = knowledge_base.get_all_documents()
371
+ if docs:
372
+ # Format all documents with their titles
373
+ doc_texts = []
374
+ for doc in docs:
375
+ # Limit each document to first 10000 characters to prevent context length issues
376
+ excerpt = doc["content"][:10000] + ("..." if len(doc["content"]) > 10000 else "")
377
+ doc_texts.append(f"--- {doc['title']} ---\n{excerpt}")
378
+
379
+ counseling_materials = "\n\nCounseling Materials:\n\n" + "\n\n".join(doc_texts)
380
+
381
+ # Create expert prompt with full roleplay context and materials
382
+ expert_messages = [
383
+ SystemMessage(content=f"""
384
+ You are an expert in teaching counseling skills to new counselors.
385
+ Review the conversation between the counselor and client.
386
+ Use the provided counseling materials to inform your feedback and suggestions.
387
+ If the user asks for help in a particular area,
388
+ provide specific feedback on the counselor's responses using the provided materials.
389
+ If the user only asks for general feedback,
390
+ provide a comprehensive review of the conversation covering these areas:
391
+ - What they did well (active listening, empathy, validation)
392
+ - Areas for improvement
393
+ - Specific examples of better responses they could have used
394
+ - Counseling techniques they should consider (reflection, paraphrasing, open-ended questions)
395
+ - Reference relevant counseling principles from the materials when applicable
396
+ Be constructive and educational. Reference specific parts of their conversation.
397
+ {counseling_materials}
398
+ """),
399
+ HumanMessage(content=f"Here's the conversation so far:\n\n" +
400
+ "\n".join([f"{'Counselor' if isinstance(msg, HumanMessage) else 'Client'}: {msg.content}"
401
+ for msg in roleplay_conversation]) +
402
+ f"\n\nCurrent request: {state['messages'][-1].content}")
403
+ ]
404
+
405
+ # Invoke the LLM to get a response
406
+ reply = llm.invoke(expert_messages)
407
+
408
+ # print out what is sent to the expert agent
409
+ print("\n=== Expert Agent Input ===")
410
+ print(f"System: (Counseling materials with {len(counseling_materials)} characters)")
411
+ print(f"Human: Here's the conversation so far: (with {len(roleplay_conversation)} messages)")
412
+ print(f"Current request: {state['messages'][-1].content}")
413
+ print("\n=== Expert Agent Response ===")
414
+ print(reply.content)
415
+
416
+ # Track token usage
417
+ if hasattr(reply, 'usage_metadata') and reply.usage_metadata:
418
+ input_tokens = reply.usage_metadata.get('input_tokens', 0)
419
+ output_tokens = reply.usage_metadata.get('output_tokens', 0)
420
+ state["total_input_tokens"] = state.get("total_input_tokens", 0) + input_tokens
421
+ state["total_output_tokens"] = state.get("total_output_tokens", 0) + output_tokens
422
+
423
+ return {
424
+ "messages": [reply], # Only return expert response
425
+ "session_mode": "expert",
426
+ "total_input_tokens": state.get("total_input_tokens", 0),
427
+ "total_output_tokens": state.get("total_output_tokens", 0)
428
+ }
429
+
430
+ # Create a state graph builder for defining the conversation flow
431
+ graph_builder = StateGraph(State)
432
+ # Build the conversation graph structure
433
+ graph_builder.add_node("classifier", classify_message) # Add message classification node
434
+ graph_builder.add_node("router", router) # Add the chatbot function as a node
435
+ graph_builder.add_node("roleplay", roleplay_agent) # Add roleplay agent node
436
+ graph_builder.add_node("expert", expert_agent) # Add expert agent node
437
+
438
+ # Define the flow of the conversation graph
439
+ graph_builder.add_edge(START, "classifier") # Start with the classifier node
440
+ graph_builder.add_edge("classifier", "router") # Route to the router node
441
+
442
+ # Route based on the classification result
443
+ graph_builder.add_conditional_edges(
444
+ "router",
445
+ lambda state: state.get("next"),
446
+ path_map={
447
+ "expert": "expert", # If classified as expert, go to expert agent
448
+ "roleplay": "roleplay" # If classified as roleplay, go to roleplay agent
449
+ }
450
+ )
451
+
452
+ # End the graph after each response - let the main loop handle the next user input
453
+ graph_builder.add_edge("roleplay", END) # After roleplay, end this graph execution
454
+ graph_builder.add_edge("expert", END) # After expert feedback, end this graph execution
455
+
456
+ # Compile the graph into an executable workflow
457
+ graph = graph_builder.compile()
458
+
459
+ def run_chatbot(state: State=None, force_reload_embeddings=False):
460
+ """Run the chatbot interaction loop, allowing users to input messages and receive responses."""
461
+ # Initialize knowledge base if not already done
462
+ if knowledge_base is None:
463
+ print("Initializing counseling knowledge base...")
464
+ initialize_knowledge_base(force_reload=force_reload_embeddings)
465
+
466
+ # Initialize the state if not provided
467
+ if state is None:
468
+ state = {
469
+ "messages": [],
470
+ "roleplay_messages": [],
471
+ "message_type": "",
472
+ "next": "",
473
+ "session_mode": "roleplay",
474
+ "total_input_tokens": 0,
475
+ "total_output_tokens": 0
476
+ }
477
+
478
+ # Print a welcome message
479
+ print("=== Counseling Skills Practice ===")
480
+ print("You'll roleplay with an elderly woman who lost her house in a landslide.")
481
+ if knowledge_base:
482
+ print("Expert feedback enhanced with counseling materials.")
483
+ print("\nCommands:")
484
+ print("- Type 'feedback', 'advice', or 'expert' to get coaching on your counseling skills")
485
+ print("- Type 'continue' or 'back to roleplay' to return to the roleplay after feedback")
486
+ print("- Type 'exit' or 'quit' to end the session")
487
+ print("\nStarting roleplay...\n")
488
+
489
+ # Start the conversation loop
490
+ while True:
491
+ # Show current mode
492
+ mode_indicator = f"[{state.get('session_mode', 'roleplay').upper()}] "
493
+ user_input = input(f"\n{mode_indicator}You: ")
494
+
495
+ if user_input.lower() in ["exit", "quit"]:
496
+ # Print token usage summary
497
+ total_input = state.get("total_input_tokens", 0)
498
+ total_output = state.get("total_output_tokens", 0)
499
+ total_tokens = total_input + total_output
500
+
501
+ print("\n" + "="*50)
502
+ print("SESSION SUMMARY")
503
+ print("="*50)
504
+ print(f"Total Input Tokens: {total_input:,}")
505
+ print(f"Total Output Tokens: {total_output:,}")
506
+ print(f"Total Tokens Used: {total_tokens:,}")
507
+ print("="*50)
508
+ print("Session ended. Thanks for practicing!")
509
+ break
510
+
511
+ # Add the user's message to the conversation state
512
+ state["messages"] = [HumanMessage(content=user_input)]
513
+
514
+ # Execute the graph with the current state
515
+ result = graph.invoke(state)
516
+
517
+ # Update state with result
518
+ state.update(result)
519
+
520
+ # Check if we're switching back to roleplay
521
+ if state.get("switching_to_roleplay"):
522
+ clear_screen()
523
+ display_roleplay_history(state)
524
+ state.pop("switching_to_roleplay", None) # Remove the flag
525
+ continue # Skip the response printing and go to next input
526
+
527
+ # Print the AI's response only if there is one
528
+ if state.get("messages") and len(state["messages"]) > 0 and state["messages"][-1].content.strip():
529
+ response = state["messages"][-1].content
530
+ agent_name = "Expert Coach" if state.get("session_mode") == "expert" else "Client"
531
+ print(f"{agent_name}: {response}")
532
+
533
+ # Add helpful hints based on mode
534
+ if state.get("session_mode") == "expert":
535
+ print("\n(Type 'continue' to return to roleplay, or ask for more specific feedback)")
536
+ elif state.get("session_mode") == "roleplay":
537
+ print("(Type 'feedback' anytime to get expert coaching on your responses)")
538
+
539
+ # Parse command-line arguments
540
+ def parse_arguments():
541
+ parser = argparse.ArgumentParser(description='Run the counseling skills practice chatbot')
542
+ parser.add_argument('--embed', action='store_true',
543
+ help='Force creation of new embeddings from documents in the counseling_materials folder')
544
+ return parser.parse_args()
545
+
546
+ # Add this at the end of the file, just before the if __name__ == "__main__": block
547
+
548
+ def create_new_state():
549
+ """Create and return a new state object for use in the Gradio interface."""
550
+ return {
551
+ "messages": [],
552
+ "roleplay_messages": [],
553
+ "message_type": "",
554
+ "next": "",
555
+ "session_mode": "roleplay",
556
+ "total_input_tokens": 0,
557
+ "total_output_tokens": 0
558
+ }
559
+
560
+ # Run the chatbot interaction loop
561
+ if __name__ == "__main__":
562
+ try:
563
+ args = parse_arguments()
564
+ run_chatbot(force_reload_embeddings=args.embed)
565
+ except KeyboardInterrupt:
566
+ print("\nProgram interrupted. Exiting gracefully...")
567
+ except Exception as e:
568
+ print(f"Error running chatbot: {e}")
569
+ import traceback
 
 
570
  traceback.print_exc()