AI Healthcare Support Bot
April 2, 2026 ยท View on GitHub
This example demonstrates how to build an AI Healthcare Support Bot using PowerMem for intelligent memory management and LangChain for conversation handling, with OceanBase as the database backend.
Features
- ๐ฅ Patient Memory Management: Persistent storage of patient information, symptoms, and medical history
- ๐ง Intelligent Fact Extraction: Automatic extraction of medical facts from conversations
- ๐ฌ Context-Aware Responses: Personalized responses based on patient history
- ๐ Multi-Turn Conversations: Support for continuous dialogue with context preservation
- ๐ Privacy Protection: Patient data isolation through user_id
- ๐ Scalable Storage: OceanBase database backend for enterprise-scale deployments
Architecture
โโโโโโโโโโโโโโโโโโโ
โ LangChain 1.1+ โ Conversation handling using LCEL (LangChain Expression Language)
โ (Runnable API) โ - ChatPromptTemplate
โ โ - Runnable chains
โ โ - Message management
โโโโโโโโโโฌโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ PowerMem โ Intelligent memory management
โ (Memory Layer) โ - Fact extraction
โ โ - Semantic search
โ โ - Context retrieval
โโโโโโโโโโฌโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ OceanBase โ Vector database for scalable storage
โ (Database) โ - Patient memories
โ โ - Medical history
โ โ - Symptom tracking
โโโโโโโโโโโโโโโโโโโ
Prerequisites
- Python 3.11+
- OceanBase Database (configured and running)
- API Keys:
- LLM API key (OpenAI, Qwen, etc.)
- Embedding API key (if different from LLM)
Installation
1. Install Dependencies
Option 1: Install from requirements.txt (Recommended)
cd examples/langchain
pip install -r requirements.txt
Option 2: Install manually
# Core dependencies
pip install powermem python-dotenv
# LangChain dependencies
pip install langchain>=1.1.0 langchain-core>=1.1.0 langchain-openai>=1.1.0 langchain-community>=0.4.1
# OceanBase dependencies (if not already installed)
pip install pyobvector sqlalchemy
Option 3: Install all at once
pip install powermem python-dotenv langchain>=1.1.0 langchain-core>=1.1.0 langchain-openai>=1.1.0 langchain-community>=0.4.1 pyobvector sqlalchemy
2. Configure OceanBase
Copy the configuration template and edit it:
# From project root
cp .env.example .env
Edit .env and configure:
# Database Configuration
DATABASE_PROVIDER=oceanbase
OCEANBASE_HOST=127.0.0.1
OCEANBASE_PORT=2881
OCEANBASE_USER=root@sys
OCEANBASE_PASSWORD=password
OCEANBASE_DATABASE=powermem
OCEANBASE_COLLECTION=healthcare_memories
# LLM Configuration
LLM_PROVIDER=qwen # or openai
LLM_API_KEY=your_llm_api_key
LLM_MODEL=qwen-plus # or gpt-3.5-turbo
# Embedding Configuration
EMBEDDING_PROVIDER=qwen # or openai
EMBEDDING_API_KEY=your_embedding_api_key
EMBEDDING_MODEL=text-embedding-v4
EMBEDDING_DIMS=1536
3. Verify OceanBase Connection
Ensure your OceanBase instance is running and accessible:
# Test connection (adjust host/port as needed)
mysql -h 127.0.0.1 -P 2881 -u root -p
Usage
Demo Mode
Run a predefined conversation demonstration:
cd examples/langchain
python healthcare_support_bot.py --mode demo
This will:
- Initialize the bot with OceanBase
- Run through a sample patient conversation
- Demonstrate memory storage and retrieval
- Show patient information summary
Interactive Mode
Run the bot in interactive mode for real-time conversations:
cd examples/langchain
python healthcare_support_bot.py --mode interactive
Interactive Commands:
- Type your message to chat with the bot
- Type
summaryto see patient information summary - Type
quitorexitto end the conversation
Custom Patient ID
Specify a patient ID for the conversation:
python healthcare_support_bot.py --mode interactive --patient-id patient_john_001
How It Works
1. Memory Integration
The HealthcarePowerMemMemory class integrates PowerMem with LangChain 1.1.0+ using the new API:
- Message Management: Manages conversation history as a list of
BaseMessageobjects - Save Context: Automatically saves conversations to PowerMem with intelligent fact extraction
- Load Context: Retrieves relevant patient history based on current query
- Privacy: Isolates patient data by
user_id
The implementation uses LangChain's new Runnable API instead of the legacy ConversationBufferMemory class.
2. Intelligent Fact Extraction
PowerMem automatically extracts medical facts from conversations:
- Symptoms: Headaches, pain, discomfort, etc.
- Medications: Current prescriptions, dosages
- Medical History: Past conditions, diagnoses
- Patient Information: Demographics, preferences
3. Context-Aware Responses
The bot uses retrieved patient context to provide personalized responses. The implementation uses LangChain's ChatPromptTemplate and RunnableLambda to dynamically inject patient context:
# Patient context is automatically retrieved and included in prompts
Patient Context (from previous conversations):
- Patient Alice has been experiencing headaches
- Headaches occur in the afternoon, moderate intensity
- Currently taking ibuprofen 200mg twice daily
- Has history of migraines
The conversation chain is built using LangChain Expression Language (LCEL):
chain = (
RunnableLambda(format_messages) # Retrieve patient context
| ChatPromptTemplate # Format prompt with context
| ChatOpenAI # Generate response
)
4. OceanBase Storage
All patient memories are stored in OceanBase with:
- Vector Embeddings: For semantic search
- Metadata: Category, timestamps, importance scores
- Scalability: Handles large-scale patient data
Example Conversation
Patient: Hello, I'm Alice. I've been experiencing headaches for the past few days.
Bot: Hello Alice, I'm sorry to hear about your headaches. I'm here to help provide
general guidance. Can you tell me more about when these headaches typically occur
and their intensity?
Patient: The headaches usually happen in the afternoon, and they're moderate in intensity.
Bot: Thank you for that information. Afternoon headaches can have various causes.
Are you experiencing any other symptoms along with the headaches?
Patient: I'm currently taking ibuprofen 200mg twice daily for the pain.
Bot: I understand you're managing the pain with ibuprofen. It's important to follow
the recommended dosage. Have you consulted with a healthcare professional about
these headaches?
[Memory stored: Patient Alice experiences afternoon headaches, moderate intensity,
taking ibuprofen 200mg twice daily]
Patient Summary
The bot can provide a summary of stored patient information:
summary = bot.get_patient_summary()
# Returns:
# {
# "total_memories": 15,
# "symptom_mentions": 8,
# "medication_mentions": 3,
# "history_mentions": 4,
# "recent_memories": [...]
# }
Configuration Options
Database Settings
DATABASE_PROVIDER: Set tooceanbaseOCEANBASE_HOST: OceanBase server hostnameOCEANBASE_PORT: OceanBase port (default: 2881)OCEANBASE_DATABASE: Database nameOCEANBASE_COLLECTION: Collection/table name for memories
LLM Settings
LLM_PROVIDER:qwen,openai, or other supported providersLLM_MODEL: Model name (e.g.,qwen-plus,gpt-3.5-turbo)LLM_TEMPERATURE: Response creativity (0.0-1.0)
Embedding Settings
EMBEDDING_PROVIDER: Embedding model providerEMBEDDING_MODEL: Embedding model nameEMBEDDING_DIMS: Vector dimensions (must match model)
Troubleshooting
Connection Issues
Problem: Cannot connect to OceanBase
Solution:
- Verify OceanBase is running:
mysql -h 127.0.0.1 -P 2881 -u root -p - Check configuration in
.env - Verify network connectivity and firewall settings
Import Errors
Problem: ModuleNotFoundError: No module named 'langchain'
Solution:
pip install langchain>=1.1.0 langchain-core>=1.1.0 langchain-openai>=1.1.0 langchain-community>=0.4.1
API Key Issues
Problem: LLM or embedding API errors
Solution:
- Verify API keys in
.env - Check API key validity and quotas
- Ensure correct provider is configured
Memory Not Saving
Problem: Conversations not being stored
Solution:
- Check OceanBase connection
- Verify
infer=Trueis set insave_to_powermemmethod - Check database permissions
- Review error messages in console
Best Practices
- Patient Privacy: Always use unique
user_idfor each patient - Data Security: Encrypt sensitive medical information
- Regular Backups: Backup OceanBase database regularly
- Monitoring: Monitor memory usage and database performance
- Compliance: Ensure compliance with healthcare data regulations (HIPAA, etc.)
Limitations & Disclaimers
โ ๏ธ Important: This is a demonstration example and should NOT be used for actual medical diagnosis or treatment.
- The bot provides general health information only
- Always recommend consulting healthcare professionals
- Never diagnose medical conditions
- Not a replacement for professional medical advice
Related Examples
- Basic Usage - Simple memory operations
- Agent Memory - Multi-agent memory management
- Intelligent Memory - Advanced memory features
Support
For issues or questions:
- Check the main README
- Review PowerMem documentation
- Open an issue on GitHub