-
Notifications
You must be signed in to change notification settings - Fork 48
Open
Labels
Description
Currently, we are calling the Gemini API each time a user wants to take a quiz, even if different users are attempting the same quiz. This increases API costs unnecessarily. The following improvements are proposed:
-
Schema Update:
- Introduce a schema to store quiz data (
quiz_idandquiz_data) and track user attempts (user_idandquiz_id). - Implement a
UserQuizmapping to track which quizzes a user has already attempted, so we can serve existing quizzes from the database if available.
- Introduce a schema to store quiz data (
-
User Identification:
- Implement a method to identify logged-in users through authentication tokens (JWT or session tokens).
- For non-logged-in users, create a temporary session ID to track quiz attempts, ensuring that quizzes can be fetched based on session instead of user login state.
-
Optimize Quiz Retrieval:
- Before calling the Gemini API, check if the quiz data already exists in the database for the user or session. If it does, serve it from the database, avoiding unnecessary API calls.
- If the quiz is not available, make the API call to fetch it, then store the data in the database for future use.
-
API Response Format:
- Ensure the Gemini API returns data in a structured JSON format (instead of raw data) for easier processing and display.
- Implement an optimistic response pattern on the frontend, where quiz data is displayed (or a loading indicator) while the API fetches the actual content in the background.
Challenges:
- User Identification: Properly identifying and managing sessions for non-logged-in users.
- Database Management: Efficiently storing and retrieving quiz data, especially with large numbers of users and quizzes.
- API Cost: Minimizing unnecessary Gemini API calls to optimize cost and performance.
By implementing these changes, we can reduce API calls, lower costs, and improve the user experience.
Please assign me so I can take it over and raise a PR.