sherpa_ai.database package

In This Page:

sherpa_ai.database package#

Submodules#

sherpa_ai.database.user_usage_tracker module#

class sherpa_ai.database.user_usage_tracker.UsageTracker(**kwargs)[source]#

Bases: Base

SQLAlchemy base model for tracking LLM token usage on per-user basis

id#
reminded_timestamp#
reset_timestamp#
timestamp#
token#
user_id#
class sherpa_ai.database.user_usage_tracker.UserUsageTracker(db_name='token_counter.db', db_url='sqlite:///token_counter.db', s3_file_key='token_counter.db', bucket_name='sherpa-sqlight', verbose_logger: ~sherpa_ai.verbose_loggers.base.BaseVerboseLogger = <sherpa_ai.verbose_loggers.verbose_loggers.DummyVerboseLogger object>, engine=None, session=None)[source]#

Bases: object

Enables an app to track LLM token usage on per-user basis

add_and_check_data(user_id, token, reset_timestamp=False, reminded_timestamp=False)[source]#

Add usage data for a user and check for reminders.

Parameters:
  • user_id (str) – ID of the user.

  • token (int) – Number of tokens used.

  • reset_timestamp (bool) – Whether to reset the timestamp.

  • reminded_timestamp (bool) – Set reminded_timestamp.

add_data(user_id, token, reset_timestamp=False, reminded_timestamp=False)[source]#

Add usage data for a user.

Parameters:
  • user_id (str) – ID of the user.

  • token (int) – Number of tokens used.

  • reset_timestamp (bool) – Whether to reset the timestamp.

  • reminded_timestamp (bool) – Set reminded_timestamp.

add_to_whitelist(user_id)[source]#

Add a user to the whitelist table.

Parameters:

user_id (str) – ID of the user to be added to the whitelist.

check_if_reminded(user_id)[source]#
check_usage(user_id, token_amount)[source]#

Check user usage and determine whether user is allowed to consume more tokens.

Parameters:
  • user_id (str) – ID of the user.

  • token_amount (int) – Number of tokens to check.

Returns:

Result containing information about tokens remaining,

whether more tokens can be consumed (can_execute), any associated message, and the time left.

Return type:

dict

close_connection()[source]#

Close the database connection.

create_table()[source]#

Create the necessary tables in the database.

classmethod download_from_s3(db_name='token_counter.db', db_url='sqlite:///token_counter.db', s3_file_key='token_counter.db', bucket_name='sherpa-sqlight', verbose_logger: ~sherpa_ai.verbose_loggers.base.BaseVerboseLogger = <sherpa_ai.verbose_loggers.verbose_loggers.DummyVerboseLogger object>)[source]#

Download usage tracking database from Amazon S3 to local file.

Parameters:
  • bucket_name (str) – Name of the S3 bucket.

  • s3_file_key (str) – Key of the file in the S3 bucket.

  • local_file_path (str) – Local path where the file will be downloaded.

get_all_data()[source]#
get_all_whitelisted_ids()[source]#

Get a list of all user IDs in the whitelist.

get_data_since_last_reset(user_id)[source]#

Get usage since the user’s usage data was last reset.

Parameters:

user_id (str) – ID of the user.

Returns:

List of dictionaries containing usage data.

Return type:

list

get_last_reset_info(user_id)[source]#

Get information about the most recent usage data reset for a user.

Parameters:

user_id (str) – ID of the user.

Returns:

Dictionary containing last reset information or None if not found.

Return type:

dict or None

get_sum_of_tokens_since_last_reset(user_id)[source]#

Calculate the sum of tokens used since the last reset for a user.

Parameters:

user_id (str) – ID of the user.

Returns:

Sum of tokens used since the last reset.

Return type:

int

get_whitelist_by_user_id(user_id)[source]#

Get whitelist information for a specific user.

Parameters:

user_id (str) – ID of the user.

Returns:

List of dictionaries containing whitelist information.

Return type:

list

is_in_whitelist(user_id)[source]#

Check if a user is in the whitelist.

Parameters:

user_id (str) – ID of the user.

Returns:

True if the user is in the whitelist, False otherwise.

Return type:

bool

percentage_used(user_id)[source]#

Calculate the percentage of daily token quota used by a user.

Parameters:

user_id (str) – ID of the user.

Returns:

Percentage of daily tokens used since last reset.

Return type:

float

remind_user_of_daily_token_limit(user_id)[source]#

Remind the user when their token usage exceeds a certain percentage.

Parameters:

user_id (str) – ID of the user.

reset_usage(user_id, token_amount)[source]#

Reset the usage data for a user to zero.

Parameters:
  • user_id (str) – ID of the user.

  • token_amount (int) – Number of tokens to reset.

seconds_to_hms(seconds)[source]#

Convert seconds to hours, minutes, and seconds.

Parameters:

seconds (int) – Number of seconds.

Returns:

Formatted string in the format “hours : minutes : seconds”.

Return type:

str

upload_to_s3()[source]#

Upload usage tracking database file from local file to Amazon S3.

Parameters:
  • local_file_path (str) – Local path of the file to be uploaded.

  • bucket_name (str) – Name of the S3 bucket.

  • s3_file_key (str) – Key of the file in the S3 bucket.

class sherpa_ai.database.user_usage_tracker.Whitelist(**kwargs)[source]#

Bases: Base

Represents a trusted list of users whose usage is not tracked

id#
user_id#

Module contents#

class sherpa_ai.database.UserUsageTracker(db_name='token_counter.db', db_url='sqlite:///token_counter.db', s3_file_key='token_counter.db', bucket_name='sherpa-sqlight', verbose_logger: ~sherpa_ai.verbose_loggers.base.BaseVerboseLogger = <sherpa_ai.verbose_loggers.verbose_loggers.DummyVerboseLogger object>, engine=None, session=None)[source]#

Bases: object

Enables an app to track LLM token usage on per-user basis

add_and_check_data(user_id, token, reset_timestamp=False, reminded_timestamp=False)[source]#

Add usage data for a user and check for reminders.

Parameters:
  • user_id (str) – ID of the user.

  • token (int) – Number of tokens used.

  • reset_timestamp (bool) – Whether to reset the timestamp.

  • reminded_timestamp (bool) – Set reminded_timestamp.

add_data(user_id, token, reset_timestamp=False, reminded_timestamp=False)[source]#

Add usage data for a user.

Parameters:
  • user_id (str) – ID of the user.

  • token (int) – Number of tokens used.

  • reset_timestamp (bool) – Whether to reset the timestamp.

  • reminded_timestamp (bool) – Set reminded_timestamp.

add_to_whitelist(user_id)[source]#

Add a user to the whitelist table.

Parameters:

user_id (str) – ID of the user to be added to the whitelist.

check_if_reminded(user_id)[source]#
check_usage(user_id, token_amount)[source]#

Check user usage and determine whether user is allowed to consume more tokens.

Parameters:
  • user_id (str) – ID of the user.

  • token_amount (int) – Number of tokens to check.

Returns:

Result containing information about tokens remaining,

whether more tokens can be consumed (can_execute), any associated message, and the time left.

Return type:

dict

close_connection()[source]#

Close the database connection.

create_table()[source]#

Create the necessary tables in the database.

classmethod download_from_s3(db_name='token_counter.db', db_url='sqlite:///token_counter.db', s3_file_key='token_counter.db', bucket_name='sherpa-sqlight', verbose_logger: ~sherpa_ai.verbose_loggers.base.BaseVerboseLogger = <sherpa_ai.verbose_loggers.verbose_loggers.DummyVerboseLogger object>)[source]#

Download usage tracking database from Amazon S3 to local file.

Parameters:
  • bucket_name (str) – Name of the S3 bucket.

  • s3_file_key (str) – Key of the file in the S3 bucket.

  • local_file_path (str) – Local path where the file will be downloaded.

get_all_data()[source]#
get_all_whitelisted_ids()[source]#

Get a list of all user IDs in the whitelist.

get_data_since_last_reset(user_id)[source]#

Get usage since the user’s usage data was last reset.

Parameters:

user_id (str) – ID of the user.

Returns:

List of dictionaries containing usage data.

Return type:

list

get_last_reset_info(user_id)[source]#

Get information about the most recent usage data reset for a user.

Parameters:

user_id (str) – ID of the user.

Returns:

Dictionary containing last reset information or None if not found.

Return type:

dict or None

get_sum_of_tokens_since_last_reset(user_id)[source]#

Calculate the sum of tokens used since the last reset for a user.

Parameters:

user_id (str) – ID of the user.

Returns:

Sum of tokens used since the last reset.

Return type:

int

get_whitelist_by_user_id(user_id)[source]#

Get whitelist information for a specific user.

Parameters:

user_id (str) – ID of the user.

Returns:

List of dictionaries containing whitelist information.

Return type:

list

is_in_whitelist(user_id)[source]#

Check if a user is in the whitelist.

Parameters:

user_id (str) – ID of the user.

Returns:

True if the user is in the whitelist, False otherwise.

Return type:

bool

percentage_used(user_id)[source]#

Calculate the percentage of daily token quota used by a user.

Parameters:

user_id (str) – ID of the user.

Returns:

Percentage of daily tokens used since last reset.

Return type:

float

remind_user_of_daily_token_limit(user_id)[source]#

Remind the user when their token usage exceeds a certain percentage.

Parameters:

user_id (str) – ID of the user.

reset_usage(user_id, token_amount)[source]#

Reset the usage data for a user to zero.

Parameters:
  • user_id (str) – ID of the user.

  • token_amount (int) – Number of tokens to reset.

seconds_to_hms(seconds)[source]#

Convert seconds to hours, minutes, and seconds.

Parameters:

seconds (int) – Number of seconds.

Returns:

Formatted string in the format “hours : minutes : seconds”.

Return type:

str

upload_to_s3()[source]#

Upload usage tracking database file from local file to Amazon S3.

Parameters:
  • local_file_path (str) – Local path of the file to be uploaded.

  • bucket_name (str) – Name of the S3 bucket.

  • s3_file_key (str) – Key of the file in the S3 bucket.