Skip to content
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

Validate and normalize emails #107

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jinja2
langdetect
# should be >= 2 in order for "from langfuse.decorators" to work
langfuse>=2.0.0
litellm
litellm>=1.59.6,<1.59.9
loguru
openai
pandas
Expand All @@ -24,4 +24,5 @@ tiktoken
typer
uvicorn
wheel
zxcvbn
zxcvbn
email-validator
71 changes: 37 additions & 34 deletions src/ansari/ansari_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,12 @@ def validate_reset_token(self, token: str) -> dict[str, str]:

def register(self, email, first_name, last_name, password_hash):
try:
normalized_email = email.strip().lower()
insert_cmd = """INSERT INTO users (email, password_hash, first_name, last_name) values (%s, %s, %s, %s);"""
self._execute_query(insert_cmd, (email, password_hash, first_name, last_name))
self._execute_query(insert_cmd, (normalized_email, password_hash, first_name, last_name))
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def register_whatsapp(self, phone_num: str, db_cols_to_vals: dict) -> dict:
Expand Down Expand Up @@ -283,16 +284,17 @@ def register_whatsapp(self, phone_num: str, db_cols_to_vals: dict) -> dict:

return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def account_exists(self, email):
try:
normalized_email = email.strip().lower()
select_cmd = """SELECT id FROM users WHERE email = %s;"""
result = self._execute_query(select_cmd, (email,), "one")[0]
result = self._execute_query(select_cmd, (normalized_email,), "one")[0]
return result is not None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return False

def account_exists_whatsapp(self, phone_num):
Expand All @@ -301,7 +303,7 @@ def account_exists_whatsapp(self, phone_num):
result = self._execute_query(select_cmd, (phone_num,), "one")[0]
return result is not None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return False

def save_access_token(self, user_id, token):
Expand All @@ -315,7 +317,7 @@ def save_access_token(self, user_id, token):
"token_db_id": inserted_id,
}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def save_refresh_token(self, user_id, token, access_token_id):
Expand All @@ -324,7 +326,7 @@ def save_refresh_token(self, user_id, token, access_token_id):
self._execute_query(insert_cmd, (user_id, token, access_token_id))
return {"status": "success", "token": token}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def save_reset_token(self, user_id, token):
Expand All @@ -336,19 +338,20 @@ def save_reset_token(self, user_id, token):
self._execute_query(insert_cmd, (user_id, token, token))
return {"status": "success", "token": token}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def retrieve_user_info(self, email):
try:
normalized_email = email.strip().lower()
select_cmd = "SELECT id, password_hash, first_name, last_name FROM users WHERE email = %s;"
result = self._execute_query(select_cmd, (email,), "one")[0]
result = self._execute_query(select_cmd, (normalized_email,), "one")[0]
if result:
user_id, existing_hash, first_name, last_name = result
return user_id, existing_hash, first_name, last_name
return None, None, None, None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return None, None, None, None

def retrieve_user_info_whatsapp(self, phone_num: str, db_cols: Union[list, str]) -> Optional[Tuple]:
Expand Down Expand Up @@ -384,7 +387,7 @@ def retrieve_user_info_whatsapp(self, phone_num: str, db_cols: Union[list, str])
return result
return None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return None

def add_feedback(self, user_id, thread_id, message_id, feedback_class, comment):
Expand All @@ -395,7 +398,7 @@ def add_feedback(self, user_id, thread_id, message_id, feedback_class, comment):
self._execute_query(insert_cmd, (user_id, thread_id, message_id, feedback_class, comment))
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def create_thread(self, user_id):
Expand All @@ -405,7 +408,7 @@ def create_thread(self, user_id):
inserted_id = result[0] if result else None
return {"status": "success", "thread_id": inserted_id}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def create_thread_whatsapp(self, user_id_whatsapp: int, thread_name: str) -> str:
Expand All @@ -421,14 +424,14 @@ def create_thread_whatsapp(self, user_id_whatsapp: int, thread_name: str) -> str
"""
try:
insert_cmd = """
INSERT INTO threads_whatsapp (user_id_whatsapp, name)
VALUES (%s, %s)
INSERT INTO threads_whatsapp (user_id_whatsapp, name)
VALUES (%s, %s)
RETURNING id;
"""
result = self._execute_query(insert_cmd, (user_id_whatsapp, thread_name), "one")[0]
return result[0] if result else None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return None

def get_all_threads(self, user_id):
Expand All @@ -437,7 +440,7 @@ def get_all_threads(self, user_id):
result = self._execute_query(select_cmd, (user_id,), "all")[0]
return [{"thread_id": x[0], "thread_name": x[1], "updated_at": x[2]} for x in result] if result else []
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return []

def set_thread_name(self, thread_id, user_id, thread_name):
Expand All @@ -456,7 +459,7 @@ def set_thread_name(self, thread_id, user_id, thread_name):
)
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def append_message(self, user_id, thread_id, role, content, tool_name=None):
Expand All @@ -476,7 +479,7 @@ def append_message(self, user_id, thread_id, role, content, tool_name=None):

return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def append_message_whatsapp(self, user_id_whatsapp: int, thread_id: int, db_cols_to_vals: dict) -> dict:
Expand Down Expand Up @@ -513,7 +516,7 @@ def append_message_whatsapp(self, user_id_whatsapp: int, thread_id: int, db_cols

return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def get_thread(self, thread_id, user_id):
Expand Down Expand Up @@ -544,7 +547,7 @@ def get_thread(self, thread_id, user_id):
}
return retval
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {}

def get_thread_llm(self, thread_id, user_id):
Expand Down Expand Up @@ -576,7 +579,7 @@ def get_thread_llm(self, thread_id, user_id):
}
return retval
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {}

def get_thread_llm_whatsapp(self, thread_id: str, user_id_whatsapp: int) -> list[dict]:
Expand All @@ -593,8 +596,8 @@ def get_thread_llm_whatsapp(self, thread_id: str, user_id_whatsapp: int) -> list
"""
try:
select_cmd = """
SELECT role, content, function_name
FROM messages_whatsapp
SELECT role, content, function_name
FROM messages_whatsapp
WHERE thread_id = %s AND user_id_whatsapp = %s
ORDER BY timestamp;
"""
Expand All @@ -605,7 +608,7 @@ def get_thread_llm_whatsapp(self, thread_id: str, user_id_whatsapp: int) -> list
else []
)
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return []

def get_last_message_time_whatsapp(self, user_id_whatsapp: int) -> tuple[Optional[str], Optional[datetime]]:
Expand All @@ -632,7 +635,7 @@ def get_last_message_time_whatsapp(self, user_id_whatsapp: int) -> tuple[Optiona
return result[0], result[1]
return None, None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return None, None

def snapshot_thread(self, thread_id, user_id):
Expand All @@ -651,7 +654,7 @@ def snapshot_thread(self, thread_id, user_id):
logger.info(f"Result is {result}")
return result[0] if result else None
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def get_snapshot(self, share_uuid):
Expand All @@ -664,7 +667,7 @@ def get_snapshot(self, share_uuid):
return json.loads(result[0])
return {}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {}

def delete_thread(self, thread_id, user_id):
Expand All @@ -677,7 +680,7 @@ def delete_thread(self, thread_id, user_id):
self._execute_query([delete_cmd_1, delete_cmd_2], [params, params])
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def delete_access_refresh_tokens_pair(self, refresh_token):
Expand Down Expand Up @@ -717,7 +720,7 @@ def delete_access_token(self, user_id, token):
self._execute_query(delete_cmd, (user_id, token))
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def logout(self, user_id, token):
Expand All @@ -727,7 +730,7 @@ def logout(self, user_id, token):
self._execute_query(delete_cmd, (user_id, token))
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def set_pref(self, user_id, key, value):
Expand All @@ -752,7 +755,7 @@ def update_password(self, user_id, new_password_hash):
self._execute_query(update_cmd, (new_password_hash, user_id))
return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def update_user_whatsapp(self, phone_num: str, db_cols_to_vals: dict) -> dict:
Expand Down Expand Up @@ -784,7 +787,7 @@ def update_user_whatsapp(self, phone_num: str, db_cols_to_vals: dict) -> dict:

return {"status": "success"}
except Exception as e:
logger.warning(f"Warning (possbile error): {e}")
logger.warning(f"Warning (possible error): {e}")
return {"status": "failure", "error": str(e)}

def convert_message(self, msg):
Expand Down
8 changes: 4 additions & 4 deletions src/ansari/app/main_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from fastapi.responses import JSONResponse, StreamingResponse
from jinja2 import Environment, FileSystemLoader
from langfuse.decorators import langfuse_context, observe
from pydantic import BaseModel
from pydantic import BaseModel, EmailStr
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from starlette.exceptions import HTTPException as StarletteHTTPException
Expand Down Expand Up @@ -99,7 +99,7 @@ def add_app_middleware():


class RegisterRequest(BaseModel):
email: str
email: EmailStr
password: str
first_name: str
last_name: str
Expand Down Expand Up @@ -136,7 +136,7 @@ async def register_user(req: RegisterRequest, cors_ok: bool = Depends(validate_c


class LoginRequest(BaseModel):
email: str
email: EmailStr
password: str


Expand Down Expand Up @@ -582,7 +582,7 @@ async def get_prefs(


class ResetPasswordRequest(BaseModel):
email: str
email: EmailStr


@app.post("/api/v2/request_password_reset")
Expand Down
Loading