AI-Powered Answer Suggestions
Relevant source files
The following files were used as context for generating this wiki page:The AI-Powered Answer Suggestions feature enhances the Q&A dashboard by providing two core functionalities: generating automated answer suggestions for existing questions and proactively checking for duplicate questions as a user types a new one. This system leverages Google's Gemini models via the langchain-google-genai library for natural language understanding and generation.
The backend implementation includes a semantic cache using FAISS to store question embeddings, allowing for efficient similarity searches. This cache helps in both finding duplicate questions and potentially retrieving cached answers to avoid redundant calls to the AI model. The feature is designed to operate in a "mock mode" if the required GOOGLE_API_KEY is not configured, ensuring the application remains functional.
Architecture Overview
The feature's architecture spans both the frontend and backend. The frontend components trigger API calls, and the backend orchestrates the AI service, caching, and database interactions.
This diagram shows the high-level interaction between the components involved in the AI features.
Backend Implementation
The backend logic is primarily encapsulated within the AIService and SemanticCache singletons, exposed via API endpoints in questions.py.
AI Service (AIService)
The AIService is a singleton class that manages interactions with the Gemini LLM.
- Initialization: On startup, it checks for the
GOOGLE_API_KEYenvironment variable. If the key is not found, it operates in a "mock mode," returning placeholder responses. If the key is present, it initializes aChatGoogleGenerativeAIinstance.
Sources: backend/services/ai_service.py:17-32 - Mock Mode: When active, any call to generate an answer returns a static, mocked response, preventing application errors and informing the user that the service is not fully configured.
Sources: backend/services/ai_service.py:70-76 - Answer Generation: The
generate_suggested_answermethod first checks the semantic cache for a similar question. If a sufficiently similar question with an answer is found (cache hit), it returns the cached answer. On a cache miss, it constructs a prompt and invokes the Gemini model. The newly generated answer is then stored in the cache for future requests.
Sources: backend/services/ai_service.py:34-68 - Duplicate Checking: The
check_duplicatemethod uses theSemanticCacheto perform a similarity search on the incoming text. If a similar document is found with a score below a certain threshold (e.g.,< 0.1), it is flagged as a duplicate.
Sources: backend/services/ai_service.py:78-117
Semantic Cache (SemanticCache)
The SemanticCache is a singleton responsible for creating and managing a FAISS vector store for semantic similarity searches.
- Initialization: It uses
GoogleGenerativeAIEmbeddingswith thegemini-embedding-001model to generate vector embeddings for questions. It attempts to load a pre-existing FAISS index from the local filesystem (faiss_index). If not found, it creates a new one.
Sources: backend/services/cache_service.py:19-35 - Caching: The
cache_answermethod adds a new question's text and its corresponding answer to the FAISS vector store. The vector store is then saved back to disk.
Sources: backend/services/cache_service.py:72-84 - Retrieval: The
get_cached_answermethod performs a similarity search. If a result is found with a high similarity score (a low distance score,< 0.1), it returns the answer from the document's metadata, indicating a "Cache Hit".
Sources: backend/services/cache_service.py:56-70
The relationship between the core backend services is shown below.
API Endpoints
Two main endpoints in questions.py expose the AI functionalities.
| Method | Path | Description | Response Schema |
|---|---|---|---|
GET | /questions/{question_id}/suggest-answer | Generates an AI-powered answer for a question. | SuggestedAnswer |
POST | /questions/check-duplicate | Checks if a new question is a potential duplicate. | DuplicateCheckResponse |
Sources: backend/api/questions.py:202-223, 226-229, backend/schemas.py:79-88
Frontend Integration
The frontend integrates these AI features into the user workflow to provide assistance seamlessly.
Answer Suggestion Flow
The QuestionCard component contains the logic for fetching and displaying an AI-generated answer suggestion.
- A user clicks the "💡 Get AI Suggestion" button on a question card.
- The
handleGetSuggestedAnswerfunction is triggered. - A
fetchrequest is made to theGET /questions/{id}/suggest-answerendpoint. - The
loadingSuggestedAnswerstate is set totrue, disabling the button. - Upon receiving a response, the
suggestedAnswerstate is updated with the answer text. - The component re-renders to display the suggestion in a styled
div. The button to get a suggestion is hidden after a suggestion has been successfully fetched.
Sources: web/src/components/QuestionCard.js:49-65, 115-128, web/src/config/api.js:15
This sequence diagram illustrates the answer suggestion flow.
Duplicate Question Check
The QuestionForm component implements a real-time check for duplicate questions to guide users before they submit.
- A user types into the
<textarea>of the question form. - The
messagestate is updated on every keystroke. - A
useDebouncehook creates adebouncedMessagevalue that updates only after 500ms of inactivity. - A
useEffecthook listens for changes todebouncedMessage. - When
debouncedMessagechanges and its length is sufficient (>= 10 characters), afetchrequest is sent toPOST /questions/check-duplicatewith the text. - If the API response indicates
is_duplicate: true, theduplicateWarningstate is updated with the similar question and its existing answer. - The component re-renders to show a warning message to the user, including the text of the similar question and its answer if available.
Sources: web/src/components/QuestionForm.js:16-60, 107-133, web/src/config/api.js:16
This sequence diagram shows the duplicate check flow.
Summary
The AI-powered assistance is a key feature of the QA-Dashboard, aimed at improving user experience and data quality. By automatically suggesting answers, it provides immediate value and reduces response times. By proactively identifying duplicate questions, it prevents content redundancy and directs users to existing answers. The system's modular design, with its mock mode and semantic caching, makes it both robust and efficient.