File size: 26,213 Bytes
d4c9525
 
 
 
 
 
 
 
 
 
 
c1063c2
d4c9525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66acac6
d4c9525
66acac6
 
 
 
 
 
d4c9525
66acac6
d4c9525
 
66acac6
 
d4c9525
66acac6
 
d4c9525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d4382d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
from dotenv import load_dotenv
from typing import Union, Literal, Annotated, Sequence, TypedDict  # Type hinting utilities
from langgraph.graph import StateGraph, START, END  # Graph structure and node types
from langgraph.graph.message import add_messages  # Message handling utility reducer function
from langchain_openai import ChatOpenAI  # OpenAI-compatible chat model wrapper
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage  # Message types
from langchain_core.messages import ToolMessage
from langchain_community.document_loaders import PyPDFLoader
from langchain_chroma import Chroma  # Correct import for Chroma
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document  # Use correct import for Document
from langchain_huggingface import HuggingFaceEmbeddings
import glob
from langgraph.prebuilt import ToolNode  # Prebuilt nodes for tool integration
from pydantic import BaseModel, Field
import os  # Operating system interface for environment variables
import sys  # For error handling and debugging
import shutil  # For directory manipulation
import argparse  # For command-line argument parsing

# # Load environment variables from .env file, override existing ones
load_dotenv(override=True)

# Error handling for initialization
try:
    # Initialize the Large Language Model (LLM) with OpenRouter configuration
    llm = ChatOpenAI(
        # model="deepseek/deepseek-r1-distill-qwen-32b:free",  # Free model for testing
        model = "google/gemini-2.0-flash-001", # very cheap model and low latency
        api_key=os.getenv("OPENROUTER_KEY"),  # Authentication key for API access
        base_url="https://openrouter.ai/api/v1",  # Custom endpoint for OpenRouter service
        temperature=0.7,  # Controls randomness in responses (0.0 = deterministic, 1.0 = very random)
        max_tokens=10000  # Maximum number of tokens in the response
    )
except Exception as e:
    print(f"Error initializing LLM: {e}")
    sys.exit(1)

# Define the state structure for the conversation graph
class State(TypedDict):
    """State structure for the conversation graph."""
    messages: list[Union[HumanMessage, SystemMessage, AIMessage]]
    roleplay_messages: list[Union[HumanMessage, SystemMessage, AIMessage]]  # Separate roleplay history
    message_type: str  # Add message type for routing
    next: str  # Add next field for routing
    session_mode: str  # Track current session mode
    total_input_tokens: int  # Track total input tokens used
    total_output_tokens: int  # Track total output tokens used

class CounselingKnowledgeBase:
    def __init__(self, pdf_directory="./counseling_materials/", persist_directory="./chroma_db", force_reload=False):
        """Initialize the knowledge base with PDF materials."""
        self.pdf_directory = pdf_directory
        self.persist_directory = persist_directory
        self.vectorstore = None
        self.force_reload = force_reload
        self.full_documents = []  # Store full document text
        
    # Error handling for embedding model initialization
        try:
            # Use a local embeddings model that works well on HF Spaces
            print("Initializing HuggingFace embeddings model...")
            self.embeddings = HuggingFaceEmbeddings(
                model_name="sentence-transformers/all-MiniLM-L6-v2",  # Lightweight and fast
                model_kwargs={'device': 'cpu'},  # Force CPU usage for HF Spaces
                encode_kwargs={'normalize_embeddings': True}
            )
            print("✅ HuggingFace embeddings model initialized successfully")
            self.load_documents()
        except Exception as e:
            print(f"❌ Error initializing embeddings model: {e}")
            print("Failed to initialize HuggingFace embeddings")
            self.embeddings = None
            # Don't exit, but warn that knowledge base won't work
            print("⚠️  Knowledge base will not be available")
    
    def load_documents(self):
        """Load and process PDF documents into a vector store."""
        if self.embeddings is None:
            print("Cannot load documents: embedding model not initialized")
            return
        
        # If force reload is enabled, delete the existing database
        if self.force_reload and os.path.exists(self.persist_directory):
            print(f"Forcing reload of embeddings. Removing existing database at {self.persist_directory}")
            try:
                shutil.rmtree(self.persist_directory)
                print(f"Removed existing database directory: {self.persist_directory}")
            except Exception as e:
                print(f"Error removing directory {self.persist_directory}: {e}")
            
        # Check if vectorstore already exists
        if not self.force_reload and os.path.exists(self.persist_directory) and os.listdir(self.persist_directory):
            print(f"Loading existing knowledge base from {self.persist_directory}")
            try:
                self.vectorstore = Chroma(
                    persist_directory=self.persist_directory,
                    embedding_function=self.embeddings
                )
                # We still need to load the full documents
                self.load_full_documents()
                print("Successfully loaded existing knowledge base")
                return
            except Exception as e:
                print(f"Error loading existing knowledge base: {e}")
                print("Creating new knowledge base...")
        
        # Load full documents regardless of whether we're using the vectorstore
        self.load_full_documents()
        
        # The rest of the method for creating the vectorstore (still useful for future functionality)
        documents = []
        
        # Find all PDF files in the directory
        pdf_files = glob.glob(f"{self.pdf_directory}*.pdf")
        
        if not pdf_files:
            print(f"Warning: No PDF files found in {self.pdf_directory}")
            return
        
        # Load each PDF
        for pdf_file in pdf_files:
            try:
                loader = PyPDFLoader(pdf_file)
                docs = loader.load()
                
                # Add source metadata
                for doc in docs:
                    doc.metadata['source'] = pdf_file
                
                documents.extend(docs)
                print(f"Loaded: {pdf_file}")
            except Exception as e:
                print(f"Error loading {pdf_file}: {e}")
        
        if documents:
            # Split documents into chunks
            text_splitter = RecursiveCharacterTextSplitter(
                chunk_size=1000,
                chunk_overlap=200
            )
            splits = text_splitter.split_documents(documents)
            
            # Create vector store
            print("Creating embeddings... This may take a few minutes.")
            try:
                self.vectorstore = Chroma.from_documents(
                    documents=splits,
                    embedding=self.embeddings,
                    persist_directory=self.persist_directory
                )
                print(f"Created knowledge base with {len(splits)} document chunks")
            except Exception as e:
                print(f"Error creating vector store: {e}")
        else:
            print("No documents were successfully loaded")
    
    def load_full_documents(self):
        """Load the full text of all PDF documents."""
        self.full_documents = []
        
        # Find all PDF files in the directory
        pdf_files = glob.glob(f"{self.pdf_directory}*.pdf")
        
        if not pdf_files:
            print(f"Warning: No PDF files found in {self.pdf_directory}")
            return
        
        # Load each PDF
        for pdf_file in pdf_files:
            try:
                loader = PyPDFLoader(pdf_file)
                docs = loader.load()
                
                # Extract file name without path and extension
                file_name = os.path.basename(pdf_file)
                file_name = os.path.splitext(file_name)[0]
                
                # Combine all pages into a single document
                full_text = "\n\n".join([doc.page_content for doc in docs])
                
                # Add to our full documents collection
                self.full_documents.append({
                    "title": file_name,
                    "content": full_text[:50000]  # Limit content size to avoid context overflow
                })
                
                print(f"Loaded full text: {file_name}")
            except Exception as e:
                print(f"Error loading full text of {pdf_file}: {e}")
    
    def search_relevant_content(self, query, k=3):
        """Search for relevant counseling content."""
        if not self.vectorstore:
            return []
        
        try:
            docs = self.vectorstore.similarity_search(query, k=k)
            return docs
        except Exception as e:
            print(f"Error searching knowledge base: {e}")
            return []

    def get_all_documents(self):
        """Return all stored document contents."""
        return self.full_documents


# Initialize knowledge base globally
knowledge_base = None

def initialize_knowledge_base(force_reload=False):
    """Initialize the counseling knowledge base."""
    global knowledge_base
    try:
        knowledge_base = CounselingKnowledgeBase(force_reload=force_reload)
        return knowledge_base
    except Exception as e:
        print(f"Warning: Could not initialize knowledge base: {e}")
        return None

# Define a Pydantic model for classifying messages
class MessageClassifier(BaseModel):
    message_type: Literal["roleplay", "expert"] = Field(
        default="roleplay",
        description="Classify if the message is asking for feedback or not."
    )

# Define the function to classify messages based on their content
def classify_message(state: State):
    # Get the last message in the conversation
    last_message = state["messages"][-1]

    # Use a simple keyword-based classification
    message_content = last_message.content.lower()
    
    # Check for keywords that indicate expert/feedback requests
    expert_keywords = ['feedback', 'review', 'expert', 'advice', 'help me improve', 'how did i do']
    return_keywords = ['continue', 'back to roleplay', 'return', 'keep going']
    
    # If currently in expert mode, only return to roleplay if explicitly requested
    if state.get("session_mode") == "expert":
        if any(keyword in message_content for keyword in return_keywords):
            return {"message_type": "roleplay"}
        else:
            return {"message_type": "expert"}  # Stay in expert mode
    
    # If in roleplay mode, check for expert requests
    if any(keyword in message_content for keyword in expert_keywords):
        return {"message_type": "expert"}
    else:
        return {"message_type": "roleplay"}

def clear_screen():
    """Clear the terminal screen."""
    if os.name == 'nt':  # Windows
        os.system('cls')
    else:  # macOS and Linux
        os.system('clear')

def display_roleplay_history(state: State):
    """Display the roleplay conversation history without system messages."""
    if "roleplay_messages" not in state or not state["roleplay_messages"]:
        return
    
    print("=== Conversation History ===\n")
    
    for msg in state["roleplay_messages"]:
        if isinstance(msg, SystemMessage):
            continue  # Skip system messages
        elif isinstance(msg, HumanMessage):
            print(f"You: {msg.content}")
        elif isinstance(msg, AIMessage):
            print(f"Client: {msg.content}")
    
    print("\n" + "="*50)
    print("Continuing roleplay...\n")

# Define the routing function to direct the conversation flow based on message type
def router(state: State):
    message_type = state["message_type"] 
    if message_type == "expert":
        return {"next": "expert"}  
    else:
        return {"next": "roleplay"}
   
# create the agent functions with different system messages

def roleplay_agent(state: State):
    # Initialize roleplay_messages if not present
    if "roleplay_messages" not in state:
        state["roleplay_messages"] = []
    
    # Add system message only if roleplay_messages is empty
    if not state["roleplay_messages"]:
        state["roleplay_messages"] = [
            SystemMessage(content="""
                You are Margaret, an 82-year-old woman who has just lost her house in a landslide. 
                You are currently at a Red Cross shelter seeking help.
                You are worried about your cat name Whiskers (a grey tabby with white paws) who is missing.
                You are feeling overwhelmed, scared, and uncertain about the future.           
                You are having a real conversation with a counselor.
            CRITICAL INSTRUCTIONS:
                - You must respond with ONLY ONE response as Margaret
                - NEVER provide multiple options or numbered lists
                - NEVER generate examples or alternatives
                - You are having a real conversation, not creating training data
                - Respond naturally as if you are actually Margaret talking to the counselor
                - NEVER use descriptive cues, stage directions, or actions in asterisks like *chokes up*, *cries*, *sighs*
                - NEVER use parenthetical descriptions or emotional indicators
                - Only speak your actual words - no narration or description of how you're saying them
                - Use only dialogue - what Margaret would actually say out loud
                - You speak in first person as yourself (I feel..., My cat..., I need...)
                - Keep most responses under 30 words and very realistic
                - 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.
                - Don't use quotation marks.
                - You are talking TO the counselor, not helping them                
            NEVER break character. NEVER offer help. NEVER provide lists. NEVER use stage directions. Give ONE authentic response only.
                          """)
        ]
    
    # Add the current user message to roleplay history
    current_user_message = state["messages"][-1]
    state["roleplay_messages"].append(current_user_message)
    
    # Add a reinforcement message every few exchanges to maintain character
    if len(state["roleplay_messages"]) > 1 and len(state["roleplay_messages"]) % 6 == 0:
        state["roleplay_messages"].append(
            SystemMessage(content="Remember: You are Margaret in a real conversation. Give ONE authentic response, not multiple options.")
        )
    
    reply = llm.invoke(state["roleplay_messages"])
    
    # Track token usage
    if hasattr(reply, 'usage_metadata') and reply.usage_metadata:
        input_tokens = reply.usage_metadata.get('input_tokens', 0)
        output_tokens = reply.usage_metadata.get('output_tokens', 0)
        state["total_input_tokens"] = state.get("total_input_tokens", 0) + input_tokens
        state["total_output_tokens"] = state.get("total_output_tokens", 0) + output_tokens
    
    # Add AI response to roleplay history
    state["roleplay_messages"].append(reply)
    
    return {
        "messages": state["messages"] + [reply],
        "roleplay_messages": state["roleplay_messages"],
        "session_mode": "roleplay",
        "total_input_tokens": state.get("total_input_tokens", 0),
        "total_output_tokens": state.get("total_output_tokens", 0)
    }

# Modify the expert agent to fix the error with the reply variable
def expert_agent(state: State):
    # Get the current user message
    current_message = state["messages"][-1].content.lower()
    
    # Check if this is a return-to-roleplay command
    return_keywords = ['continue', 'back to roleplay', 'return', 'keep going']
    if any(keyword in current_message for keyword in return_keywords):
        # Signal that we want to return to roleplay without generating a response
        return {
            "messages": [],  # No response needed for mode switch
            "session_mode": "roleplay",
            "switching_to_roleplay": True,  # Flag to indicate mode switch
            "total_input_tokens": state.get("total_input_tokens", 0),
            "total_output_tokens": state.get("total_output_tokens", 0)
        }
    
    # Get the roleplay conversation without system messages for analysis
    roleplay_conversation = [
        msg for msg in state.get("roleplay_messages", []) 
        if not isinstance(msg, SystemMessage)
    ]
    
    # Get all counseling documents instead of searching
    counseling_materials = ""
    if knowledge_base:
        docs = knowledge_base.get_all_documents()
        if docs:
            # Format all documents with their titles
            doc_texts = []
            for doc in docs:
                # Limit each document to first 10000 characters to prevent context length issues
                excerpt = doc["content"][:10000] + ("..." if len(doc["content"]) > 10000 else "")
                doc_texts.append(f"--- {doc['title']} ---\n{excerpt}")
            
            counseling_materials = "\n\nCounseling Materials:\n\n" + "\n\n".join(doc_texts)
    
    # Create expert prompt with full roleplay context and materials
    expert_messages = [
        SystemMessage(content=f"""
            You are an expert in teaching counseling skills to new counselors.
            Review the conversation between the counselor and client.
            Use the provided counseling materials to inform your feedback and suggestions.
            If the user asks for help in a particular area, 
                provide specific feedback on the counselor's responses using the provided materials.
            If the user only asks for general feedback, 
                provide a comprehensive review of the conversation covering these areas:
            - What they did well (active listening, empathy, validation)
            - Areas for improvement 
            - Specific examples of better responses they could have used
            - Counseling techniques they should consider (reflection, paraphrasing, open-ended questions)
            - Reference relevant counseling principles from the materials when applicable
            Be constructive and educational. Reference specific parts of their conversation.
            {counseling_materials}
            """),
        HumanMessage(content=f"Here's the conversation so far:\n\n" + 
                    "\n".join([f"{'Counselor' if isinstance(msg, HumanMessage) else 'Client'}: {msg.content}" 
                              for msg in roleplay_conversation]) +
                    f"\n\nCurrent request: {state['messages'][-1].content}")
    ]
    
    # Invoke the LLM to get a response
    reply = llm.invoke(expert_messages)
    
    # print out what is sent to the expert agent
    print("\n=== Expert Agent Input ===")
    print(f"System: (Counseling materials with {len(counseling_materials)} characters)")
    print(f"Human: Here's the conversation so far: (with {len(roleplay_conversation)} messages)")
    print(f"Current request: {state['messages'][-1].content}")
    print("\n=== Expert Agent Response ===")
    print(reply.content)

    # Track token usage
    if hasattr(reply, 'usage_metadata') and reply.usage_metadata:
        input_tokens = reply.usage_metadata.get('input_tokens', 0)
        output_tokens = reply.usage_metadata.get('output_tokens', 0)
        state["total_input_tokens"] = state.get("total_input_tokens", 0) + input_tokens
        state["total_output_tokens"] = state.get("total_output_tokens", 0) + output_tokens
    
    return {
        "messages": [reply],  # Only return expert response
        "session_mode": "expert",
        "total_input_tokens": state.get("total_input_tokens", 0),
        "total_output_tokens": state.get("total_output_tokens", 0)
    }

# Create a state graph builder for defining the conversation flow
graph_builder = StateGraph(State)
# Build the conversation graph structure
graph_builder.add_node("classifier", classify_message)  # Add message classification node
graph_builder.add_node("router", router)  # Add the chatbot function as a node
graph_builder.add_node("roleplay", roleplay_agent)  # Add roleplay agent node
graph_builder.add_node("expert", expert_agent)  # Add expert agent node

# Define the flow of the conversation graph
graph_builder.add_edge(START, "classifier")  # Start with the classifier node
graph_builder.add_edge("classifier", "router")  # Route to the router node

# Route based on the classification result
graph_builder.add_conditional_edges(
    "router",
    lambda state: state.get("next"),
    path_map={
        "expert": "expert",  # If classified as expert, go to expert agent
        "roleplay": "roleplay"  # If classified as roleplay, go to roleplay agent
    }
)

# End the graph after each response - let the main loop handle the next user input
graph_builder.add_edge("roleplay", END)  # After roleplay, end this graph execution
graph_builder.add_edge("expert", END)    # After expert feedback, end this graph execution

# Compile the graph into an executable workflow
graph = graph_builder.compile()

def run_chatbot(state: State=None, force_reload_embeddings=False):
    """Run the chatbot interaction loop, allowing users to input messages and receive responses."""
    # Initialize knowledge base if not already done
    if knowledge_base is None:
        print("Initializing counseling knowledge base...")
        initialize_knowledge_base(force_reload=force_reload_embeddings)
    
    # Initialize the state if not provided
    if state is None:
        state = {
            "messages": [], 
            "roleplay_messages": [],
            "message_type": "", 
            "next": "",
            "session_mode": "roleplay",
            "total_input_tokens": 0,
            "total_output_tokens": 0
        }
    
    # Print a welcome message
    print("=== Counseling Skills Practice ===")
    print("You'll roleplay with an elderly woman who lost her house in a landslide.")
    if knowledge_base:
        print("Expert feedback enhanced with counseling materials.")
    print("\nCommands:")
    print("- Type 'feedback', 'advice', or 'expert' to get coaching on your counseling skills")
    print("- Type 'continue' or 'back to roleplay' to return to the roleplay after feedback")
    print("- Type 'exit' or 'quit' to end the session")
    print("\nStarting roleplay...\n")
    
    # Start the conversation loop
    while True:
        # Show current mode
        mode_indicator = f"[{state.get('session_mode', 'roleplay').upper()}] "
        user_input = input(f"\n{mode_indicator}You: ")
        
        if user_input.lower() in ["exit", "quit"]:
            # Print token usage summary
            total_input = state.get("total_input_tokens", 0)
            total_output = state.get("total_output_tokens", 0)
            total_tokens = total_input + total_output
            
            print("\n" + "="*50)
            print("SESSION SUMMARY")
            print("="*50)
            print(f"Total Input Tokens:  {total_input:,}")
            print(f"Total Output Tokens: {total_output:,}")
            print(f"Total Tokens Used:   {total_tokens:,}")
            print("="*50)
            print("Session ended. Thanks for practicing!")
            break
        
        # Add the user's message to the conversation state
        state["messages"] = [HumanMessage(content=user_input)]

        # Execute the graph with the current state
        result = graph.invoke(state)
        
        # Update state with result
        state.update(result)
        
        # Check if we're switching back to roleplay
        if state.get("switching_to_roleplay"):
            clear_screen()
            display_roleplay_history(state)
            state.pop("switching_to_roleplay", None)  # Remove the flag
            continue  # Skip the response printing and go to next input
    
        # Print the AI's response only if there is one
        if state.get("messages") and len(state["messages"]) > 0 and state["messages"][-1].content.strip():
            response = state["messages"][-1].content
            agent_name = "Expert Coach" if state.get("session_mode") == "expert" else "Client"
            print(f"{agent_name}: {response}")
            
            # Add helpful hints based on mode
            if state.get("session_mode") == "expert":
                print("\n(Type 'continue' to return to roleplay, or ask for more specific feedback)")
            elif state.get("session_mode") == "roleplay":
                print("(Type 'feedback' anytime to get expert coaching on your responses)")

# Parse command-line arguments
def parse_arguments():
    parser = argparse.ArgumentParser(description='Run the counseling skills practice chatbot')
    parser.add_argument('--embed', action='store_true', 
                        help='Force creation of new embeddings from documents in the counseling_materials folder')
    return parser.parse_args()

# Add this at the end of the file, just before the if __name__ == "__main__": block

def create_new_state():
    """Create and return a new state object for use in the Gradio interface."""
    return {
        "messages": [], 
        "roleplay_messages": [],
        "message_type": "", 
        "next": "",
        "session_mode": "roleplay",
        "total_input_tokens": 0,
        "total_output_tokens": 0
    }

# Run the chatbot interaction loop
if __name__ == "__main__":
    try:
        args = parse_arguments()
        run_chatbot(force_reload_embeddings=args.embed)
    except KeyboardInterrupt:
        print("\nProgram interrupted. Exiting gracefully...")
    except Exception as e:
        print(f"Error running chatbot: {e}")
        import traceback
        traceback.print_exc()