Spaces:
Sleeping
Sleeping
Download bumain2.py from baeitzman/PracticeChat2: direct link, hf CLI and curl.
- Browser
- Download file 26.2 kB
-
https://huggingface.co/spaces/baeitzman/PracticeChat2/resolve/main/bumain2.py
- Command line
-
hf download hf://spaces/baeitzman/PracticeChat2/bumain2.py
-
curl -L -o bumain2.py https://huggingface.co/spaces/baeitzman/PracticeChat2/resolve/main/bumain2.py
26.2 kB
| 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() |