Skip to content

Commit

Permalink
Add support for Gemini (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
NolanTrem authored Jan 10, 2025
1 parent 3d853e0 commit c1fe474
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion py/core/database/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ async def create_user(
is_superuser: bool = False,
name: Optional[str] = None,
bio: Optional[str] = None,
profile_picture: Optional[str] = None
profile_picture: Optional[str] = None,
) -> User:
"""Create a new user."""
# 1) Check if a user with this email already exists
Expand Down
3 changes: 1 addition & 2 deletions py/core/main/api/v3/users_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def validate_password(password: str) -> bool:
password=password,
name=name,
bio=bio,
profile_picture=profile_picture
profile_picture=profile_picture,
)

return registration_response
Expand Down Expand Up @@ -1835,7 +1835,6 @@ async def google_callback(
oauth_id=google_id,
email=email,
)
print("token_response = ", token_response)

# 4. Return tokens or redirect to your front-end
# Some people store tokens in a cookie or redirect to a front-end route passing them as a query param.
Expand Down
2 changes: 1 addition & 1 deletion py/core/main/services/auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def register(
password=password,
name=name,
bio=bio,
profile_picture=profile_picture
profile_picture=profile_picture,
)

@telemetry_event("SendVerificationEmail")
Expand Down
36 changes: 22 additions & 14 deletions py/core/providers/auth/r2r_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def register(
google_id: Optional[str] = None,
name: Optional[str] = None,
bio: Optional[str] = None,
profile_picture: Optional[str] = None
profile_picture: Optional[str] = None,
) -> User:
if account_type == "password":
if not password:
Expand Down Expand Up @@ -233,7 +233,7 @@ async def register(
google_id=google_id,
name=name,
bio=bio,
profile_picture=profile_picture
profile_picture=profile_picture,
)
default_collection: CollectionResponse = (
await self.database_provider.collections_handler.create_collection(
Expand Down Expand Up @@ -296,9 +296,9 @@ async def send_verification_email(
)

await self.email_provider.send_verification_email(
to_email=user.email,
to_email=user.email,
verification_code=verification_code,
dynamic_template_data={"first_name": first_name}
dynamic_template_data={"first_name": first_name},
)

return verification_code, expiry
Expand Down Expand Up @@ -428,11 +428,15 @@ async def change_password(
)
try:
await self.email_provider.send_password_changed_email(
to_email=user.email,
dynamic_template_data={"first_name": user.name.split(" ")[0] or 'User'}
to_email=user.email,
dynamic_template_data={
"first_name": user.name.split(" ")[0] or "User"
},
)
except Exception as e:
logger.error(f"Failed to send password change notification: {str(e)}")
logger.error(
f"Failed to send password change notification: {str(e)}"
)

return {"message": "Password changed successfully"}

Expand All @@ -456,9 +460,9 @@ async def request_password_reset(self, email: str) -> dict[str, str]:
user.name.split(" ")[0] if user.name else email.split("@")[0]
)
await self.email_provider.send_password_reset_email(
to_email=email,
reset_token=reset_token,
dynamic_template_data={"first_name": first_name}
to_email=email,
reset_token=reset_token,
dynamic_template_data={"first_name": first_name},
)

return {
Expand Down Expand Up @@ -494,18 +498,22 @@ async def confirm_password_reset(
await self.database_provider.users_handler.remove_reset_token(
id=user_id
)
# Get the user information
# Get the user information
user = await self.database_provider.users_handler.get_user_by_id(
id=user_id
)

try:
await self.email_provider.send_password_changed_email(
to_email=user.email,
dynamic_template_data={"first_name": user.name.split(" ")[0] or 'User'}
to_email=user.email,
dynamic_template_data={
"first_name": user.name.split(" ")[0] or "User"
},
)
except Exception as e:
logger.error(f"Failed to send password change notification: {str(e)}")
logger.error(
f"Failed to send password change notification: {str(e)}"
)

return {"message": "Password reset successfully"}

Expand Down
6 changes: 3 additions & 3 deletions py/core/providers/auth/supabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ async def decode_token(self, token: str) -> TokenData:

async def register(
self,
email: str,
email: str,
password: str,
name: Optional[str] = None,
bio: Optional[str] = None,
profile_picture: Optional[str] = None
) -> User: # type: ignore
profile_picture: Optional[str] = None,
) -> User: # type: ignore
# Use Supabase client to create a new user

if user := self.supabase.auth.sign_up(email=email, password=password):
Expand Down
2 changes: 1 addition & 1 deletion py/core/providers/email/console_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def send_password_changed_email(
Subject: Your Password Has Been Changed
Body:
Your password has been successfully changed.
For security reasons, you will need to log in again on all your devices.
-----------------------------
"""
Expand Down
9 changes: 6 additions & 3 deletions py/core/providers/email/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,13 @@ async def send_password_changed_email(
to_email: str,
dynamic_template_data: Optional[dict] = None,
*args,
**kwargs
**kwargs,
) -> None:
try:
if hasattr(self, 'password_changed_template_id') and self.password_changed_template_id:
if (
hasattr(self, "password_changed_template_id")
and self.password_changed_template_id
):
await self.send_email(
to_email=to_email,
template_id=self.password_changed_template_id,
Expand All @@ -233,7 +236,7 @@ async def send_password_changed_email(
Your password has been successfully changed.
If you did not make this change, please contact support immediately and secure your account.
"""
html_body = """
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
Expand Down
5 changes: 1 addition & 4 deletions py/core/providers/email/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ async def send_password_reset_email(
)

async def send_password_changed_email(
self,
to_email: str,
*args,
**kwargs
self, to_email: str, *args, **kwargs
) -> None:
body = """
Your password has been successfully changed.
Expand Down
3 changes: 3 additions & 0 deletions py/r2r/compose.full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ services:
- VERTEX_PROJECT=${VERTEX_PROJECT:-}
- VERTEX_LOCATION=${VERTEX_LOCATION:-}

# Google Gemini
- GEMINI_API_KEY=${GEMINI_API_KEY:-}

# AWS Bedrock
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-}
Expand Down
3 changes: 3 additions & 0 deletions py/r2r/compose.full_with_replicas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ services:
- VERTEX_PROJECT=${VERTEX_PROJECT:-}
- VERTEX_LOCATION=${VERTEX_LOCATION:-}

# Google Gemini
- GEMINI_API_KEY=${GEMINI_API_KEY:-}

# AWS Bedrock
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-}
Expand Down
3 changes: 3 additions & 0 deletions py/r2r/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ services:
- VERTEX_PROJECT=${VERTEX_PROJECT:-}
- VERTEX_LOCATION=${VERTEX_LOCATION:-}

# Google Gemini
- GEMINI_API_KEY=${GEMINI_API_KEY:-}

# AWS Bedrock
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-}
Expand Down

0 comments on commit c1fe474

Please sign in to comment.