-
Notifications
You must be signed in to change notification settings - Fork 25
Add usage quota to AI creation of flashcards and collections #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
6d8a0d4
feat-72: ai usage quota
sebampuerocursor 0b58fd8
feat-72: remove unneeded reference
sebampuerocursor 531eacc
feat-72: include new schema version for new table
sebampuerocursor 9a3f24c
feat-72: minor fixes
sebampuerocursor 048a10a
feat-72: include tests for services
sebampuerocursor 904542a
feat-72: include tests for new service call
sebampuerocursor 76a7eb1
feat-72: run ruff formatter and linter
sebampuerocursor d9518c7
feat-72: generate openapi for new route
sebampuerocursor 69d7323
Merge branch 'main' into feat-72-usage-quota-ai
sebampuerocursor 1685399
feat-72: add state for loading ai usage quota
sebampuerocursor 96bbc39
feat-72: only allow creation if quota is not 100%
sebampuerocursor ae98d08
run formatting
sebampuerocursor 94c4aaf
feat: apply suggested changes
sebampuerocursor 11ac736
tests: update tests for user services
sebampuerocursor 2097f5b
fix more tests
sebampuerocursor be5539c
fix linting
sebampuerocursor fd69629
run client and regenerate openapi spec
sebampuerocursor 9ab4443
update cheking function
sebampuerocursor 21d9fc7
fix linting
sebampuerocursor 9e91cf5
run formatter
sebampuerocursor 72f2408
remove unneeded hook for ai prompt dialog, only one query is needed
sebampuerocursor 8ef3ed4
change UI so that usage left is shown
sebampuerocursor e18423c
Merge remote-tracking branch 'origin/main' into feat-72-usage-quota-ai
sebampuerocursor ba9109c
feat: apply suggested changes
sebampuerocursor 373e0b0
fix: refactor check_and_increment_ai_usage_quota
0010aor 9db02b1
refactor: format backend files
0010aor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
backend/src/alembic/versions/d1ea38d75310_add_ai_usage_quota_tables.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """Add AI Usage Quota tables | ||
|
|
||
| Revision ID: d1ea38d75310 | ||
| Revises: cb16ae472c1e | ||
| Create Date: 2025-05-04 09:59:20.325131 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| import sqlmodel.sql.sqltypes | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = 'd1ea38d75310' | ||
| down_revision = 'cb16ae472c1e' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| op.create_table( | ||
| 'aiusagequota', | ||
| sa.Column('id', sa.UUID(), primary_key=True, nullable=False), | ||
| sa.Column('user_id', sa.UUID(), sa.ForeignKey('user.id', ondelete='CASCADE'), index=True, nullable=False), | ||
| sa.Column('usage_count', sa.Integer, default=0, nullable=False), | ||
| sa.Column('last_reset_time', sa.DateTime(timezone=True), nullable=False), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_table('aiusagequota') |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| from src.ai_models.gemini import GeminiProviderDep | ||
| from src.ai_models.gemini.exceptions import AIGenerationError | ||
| from src.auth.services import CurrentUser, SessionDep | ||
| from src.users.services import check_and_increment_ai_usage_quota | ||
|
|
||
| from . import services | ||
| from .exceptions import EmptyCollectionError | ||
|
|
@@ -52,6 +53,10 @@ async def create_collection( | |
|
|
||
| if collection_in.prompt: | ||
| try: | ||
| if not check_and_increment_ai_usage_quota(session, current_user): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we should use if not await asyncio.to_thread here right? |
||
| raise HTTPException( | ||
| status_code=429, detail="Quota for AI usage is reached." | ||
| ) | ||
| flashcard_collection = await services.generate_ai_collection( | ||
| provider, collection_in.prompt | ||
| ) | ||
|
|
@@ -145,6 +150,10 @@ async def create_card( | |
| if not access_checked: | ||
| raise HTTPException(status_code=404, detail="Collection not found") | ||
| if card_in.prompt: | ||
| if not check_and_increment_ai_usage_quota(session, current_user): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here? |
||
| raise HTTPException( | ||
| status_code=429, detail="Quota for AI usage is reached." | ||
| ) | ||
| card_base = await services.generate_ai_flashcard(card_in.prompt, provider) | ||
| card_in.front = card_base.front | ||
| card_in.back = card_base.back | ||
|
|
||
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from sqlmodel import Session | ||
|
|
||
| from src.users.schemas import UserCreate | ||
| from src.users.services import create_user | ||
| from tests.utils.utils import random_email, random_lower_string | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def test_user(db: Session) -> dict[str, Any]: | ||
| email = random_email() | ||
| password = random_lower_string() | ||
| full_name = random_lower_string() | ||
|
|
||
| user_in = UserCreate(email=email, password=password, full_name=full_name) | ||
| user = create_user(session=db, user_create=user_in) | ||
|
|
||
| return user |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the project can run without requiring to define those variables in the .env