Skip to content

Implement AI Chatbot for Dive Discovery & Planning#173

Merged
kargig merged 6 commits intomainfrom
feature/implement-chatbot
Feb 15, 2026
Merged

Implement AI Chatbot for Dive Discovery & Planning#173
kargig merged 6 commits intomainfrom
feature/implement-chatbot

Conversation

@kargig
Copy link
Owner

@kargig kargig commented Feb 15, 2026

Summary

This PR introduces a comprehensive, context-aware AI chatbot to Divemap, designed to assist users with dive site discovery, weather suitability checks, gear rental comparisons, and certification guidance. The system integrates real-time weather data, database content, and LLM capabilities to provide personalized, actionable responses. It also includes a full suite of admin tools for monitoring chat history and gathering user feedback.

Changes Made

Backend

  • Core Logic: Implemented ChatService with sophisticated intent classification to handle diverse user queries:
    • Discovery: Find dive sites based on location, difficulty, and features.
    • ContextQA: Answer questions about specific dive sites or centers.
    • Weather: Check real-time wind and weather suitability for diving.
    • Recommendation: Personal suggestions based on certification level and history.
    • Comparison: Compare certification levels across organizations.
    • Gear Rental: Price comparison for equipment rental.
    • Career Path: Guidance on diving certifications and progression.
    • Marine Life: Find sites based on specific marine life sightings.
  • Integrations:
    • OpenAIService: Robust LLM interaction with retry logic, token usage tracking, and PII masking.
    • OpenMeteoService: Integration for fetching and analyzing weather conditions.
  • API:
    • New public endpoints: /api/v1/chat/message, /api/v1/chat/feedback.
    • New admin endpoints for reviewing chat history and feedback.
  • Database:
    • Added ChatSession, ChatMessage, and ChatFeedback models.
    • Created migrations 0063 through 0066 to set up tables and track token usage.

Frontend

  • Chat Interface: Added a persistent ChatWidget with markdown support, history view, and suggestion chips.
  • Admin Dashboard: Created AdminChatHistory and AdminChatFeedback pages using TanStack Table for advanced filtering and review.
  • Navigation: Enhanced DivingOrganizationsPage to support deep linking via query parameters, enabling direct navigation from chat responses.

Documentation

  • Updated README.md: Added a new AI Assistant section detailing the chatbot's capabilities.
  • Updated docs/maintenance/changelog.md: Added a new release entry for February 15, 2026, documenting the feature.
  • Updated frontend/src/pages/Changelog.js: Added the "AI Chatbot & Intelligent Discovery" release to the frontend changelog page.
  • Project Tracking: Moved the completed project plan to docs/development/done/ and updated spec/todo.md.

Security & Privacy

  • Implemented PII masking (e.g., coordinates) in logs.
  • Added rate limiting (5 req/min) for chat endpoints.
  • strict allowlist validation for context entity types.

Testing

  • Unit & Integration Tests: Added a comprehensive test suite covering all major intents and flows:
    • backend/tests/test_chat_api.py
    • backend/tests/test_chat_recommendation.py
    • backend/tests/test_chat_comparison.py
    • backend/tests/test_chat_gear.py
    • backend/tests/test_chat_ratings.py
    • backend/tests/test_chat_extra_intents.py
    • backend/tests/test_admin_chat.py
  • Frontend Tests: Added tests for the chat widget and admin tables.
  • Manual Testing: Verified end-to-end flows for weather queries, site recommendations, and admin feedback submission.

Related Issues

  • Resolves feature/implement-chatbot

Additional Notes

  • Deployment: Requires running database migrations 0063, 0064, 0065, and 0066.
  • Configuration: Ensure the OPENAI_API_KEY environment variable is set in the production environment.

Key changes:
- Created ChatService for intent extraction, search execution, and response generation
- Implemented OpenAIService for robust LLM interactions with retries
- Added Weather Integration to fetch batch wind data and calculate site suitability
- Created new API endpoints at /api/v1/chat/message and /api/v1/chat/feedback
- Added Pydantic schemas and database models for chat interactions
- Refactored newsletter parsing to use the new OpenAIService
- Updated SearchService to handle intent-based queries
- Added DB migration (0063)

This completes Phases 1, 2, and 3 of the implementation plan.

Phase: IMPLEMENT | Status: In Progress
Introduce a context-aware AI assistant for Divemap to help users discover dive sites, check weather conditions, and get general diving information.

Core Improvements:
- Add `ChatService` with intent classification (Discovery, ContextQA, Knowledge).
- Integrate existing `OpenMeteoService` and `WindRecommendationService`
  to provide real-time weather suitability in chat responses.
- Implement specialized "Ask for Time" logic to ensure precise
  weather forecasts for future dates.
- Add support for geocoding and cardinal direction translation.

Backend:
- Create `ChatSession`, `ChatMessage`, and `ChatFeedback` models.
- Add Admin API endpoints for reviewing chat history and feedback.
- Update `SearchIntent` to handle specific date/time and coordinates.
- Implement eager loading for chat-related admin queries.

Frontend:
- Add `ChatWidget` component with history and markdown rendering.
- Create Admin pages (`AdminChatHistory`, `AdminChatFeedback`) using
  TanStack Table v8 for consistency.
- Add `AdminChatFeedbackTable` and `AdminChatHistoryTable` components.
- Fix click-blocking issues and z-index layering for the chat overlay.

Testing:
- Add `test_chat_api.py` and `test_admin_chat.py`.
- Update `test_rate_limiting.py` to cover chat endpoints (5 req/min).

Ref: feature/implement-chatbot
- Add `PERSONAL_RECOMMENDATION` intent to suggest dive sites based on user certification level, history, and location.
- Add `COMPARISON` intent to compare certification levels across organizations.
- Update `ChatService` with logic for difficulty mapping (1-4) and shore access filtering.
- Refactor `OpenAIService` to return detailed token usage statistics.
- Update `ChatMessage` model and migration to store input/output/total tokens.
- Add comprehensive tests for recommendation and comparison logic.
- Update frontend components to support new suggestion flows.
- Update project plan documentation.
- Add `GEAR_RENTAL` intent for price comparison of rental equipment.
- Enable `highest_rated` and `popular` sorting in `DISCOVERY` search.
- Implement logic to calculate and return average ratings for dive sites.
- Improve keyword filtering to exclude location names, preventing false negatives.
- Refactor `ChatService` to clean up duplicate code and fix syntax errors.
- Secure `context_entity_type` against prompt injection via allowlist validation.
- Mask PII (coordinates) in logs for privacy compliance.
- Update response generation to strictly enforce Markdown links and display ratings.
- Add comprehensive tests for gear rental and ratings search logic.
- Update project plan documentation.
- Add `CAREER_PATH` intent to guide users through diving certifications and organizations.
- Add `MARINE_LIFE` intent to find dive sites based on marine life sightings.
- Implement logic in `ChatService` to handle these intents, including dynamic organization search and improved filtering.
- Update `DivingOrganizationsPage` frontend to support deep linking via `org` and `course` query parameters for better user navigation from chat responses.
- Add comprehensive tests for the new intents in `test_chat_extra_intents.py`.
- Fix URL encoding in chat response links to ensure they are clickable and correctly routed.
- Update README.md with new AI Assistant features.
- Add detailed release notes to docs/maintenance/changelog.md.
- Update frontend Changelog page with February 2026 release.
- Move implementation plan to docs/development/done/.
- Mark task as completed in spec/todo.md.
- Minor formatting updates in DivingOrganizationsPage.js.
@kargig kargig merged commit 17ed780 into main Feb 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant