Spaces:
Sleeping
Sleeping
Update bumain2.py
Browse files- 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 |
-
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
from
|
| 14 |
-
import
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import
|
| 18 |
-
import
|
| 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 |
-
self.
|
| 55 |
-
self.
|
| 56 |
-
self.
|
| 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 |
-
print(f"
|
| 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 |
-
print(f"
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
print(f"
|
| 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 |
-
print(f"
|
| 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 |
-
return {"message_type": "
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
return {"message_type": "
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
os.system('
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
elif isinstance(msg,
|
| 266 |
-
print(f"
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
return {"next": "
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
You are
|
| 293 |
-
You are
|
| 294 |
-
You are
|
| 295 |
-
|
| 296 |
-
You
|
| 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 |
-
If the user asks for
|
| 390 |
-
provide
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
-
|
| 394 |
-
-
|
| 395 |
-
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
"\n
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
print("
|
| 412 |
-
print(f"
|
| 413 |
-
print(
|
| 414 |
-
print(
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
"
|
| 427 |
-
"
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
#
|
| 433 |
-
graph_builder
|
| 434 |
-
#
|
| 435 |
-
graph_builder.add_node("
|
| 436 |
-
graph_builder.add_node("
|
| 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 |
-
print("
|
| 486 |
-
print("- Type '
|
| 487 |
-
print("
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
user_input
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
print("
|
| 504 |
-
print("
|
| 505 |
-
print("
|
| 506 |
-
print(f"Total
|
| 507 |
-
print(
|
| 508 |
-
print(
|
| 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 |
-
print("
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
parser.
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
"
|
| 554 |
-
"
|
| 555 |
-
"
|
| 556 |
-
"
|
| 557 |
-
"
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
except
|
| 568 |
-
print("
|
| 569 |
-
|
| 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()
|