Implement AI Chatbot for Dive Discovery & Planning#173
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
ChatServicewith sophisticated intent classification to handle diverse user queries:OpenAIService: Robust LLM interaction with retry logic, token usage tracking, and PII masking.OpenMeteoService: Integration for fetching and analyzing weather conditions./api/v1/chat/message,/api/v1/chat/feedback.ChatSession,ChatMessage, andChatFeedbackmodels.0063through0066to set up tables and track token usage.Frontend
ChatWidgetwith markdown support, history view, and suggestion chips.AdminChatHistoryandAdminChatFeedbackpages using TanStack Table for advanced filtering and review.DivingOrganizationsPageto support deep linking via query parameters, enabling direct navigation from chat responses.Documentation
README.md: Added a new AI Assistant section detailing the chatbot's capabilities.docs/maintenance/changelog.md: Added a new release entry for February 15, 2026, documenting the feature.frontend/src/pages/Changelog.js: Added the "AI Chatbot & Intelligent Discovery" release to the frontend changelog page.docs/development/done/and updatedspec/todo.md.Security & Privacy
Testing
backend/tests/test_chat_api.pybackend/tests/test_chat_recommendation.pybackend/tests/test_chat_comparison.pybackend/tests/test_chat_gear.pybackend/tests/test_chat_ratings.pybackend/tests/test_chat_extra_intents.pybackend/tests/test_admin_chat.pyRelated Issues
feature/implement-chatbotAdditional Notes
0063,0064,0065, and0066.OPENAI_API_KEYenvironment variable is set in the production environment.