### Install Open WebUI Token Tracking Source: https://dartmouth.github.io/openwebui-token-tracking/readme Installs the openwebui-token-tracking library from PyPI using pip. ```Shell pip install openwebui-token-tracking ``` -------------------------------- ### Install Open WebUI Token Tracking Source: https://dartmouth.github.io/openwebui-token-tracking/index Installs the openwebui-token-tracking library from PyPI using pip. This is the primary method for setting up the library in your environment. ```Shell pip install openwebui-token-tracking ``` -------------------------------- ### TokenTracker: Main Execution Example Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/tracking Demonstrates the basic usage of the TokenTracker class by initializing it with a database URL and then calling methods to retrieve model information. ```Python if __name__ == "__main__": fromdotenvimport find_dotenv, load_dotenv importos load_dotenv(find_dotenv()) logging.basicConfig(level=logging.INFO) acc = TokenTracker(os.environ["DATABASE_URL"]) print(acc.get_models()) print(acc.get_models(provider="anthropic")) ``` -------------------------------- ### Anthropic Tracked Pipe Setup Source: https://dartmouth.github.io/openwebui-token-tracking/readme Sets up a tracked pipe for the Anthropic provider using the `openwebui-token-tracking` library. This involves defining a Python class that inherits from `BaseTrackedPipe` and assigning an implementation, such as `AnthropicTrackedPipe`, to the `Pipe` variable. The provider name in the pricing table must match the function name case-insensitively. ```Python """ title: Anthropic Pipe author: Simon Stone requirements: openwebui-token-tracking version: 0.1.0 """ fromopenwebui_token_tracking.pipes.anthropicimport AnthropicTrackedPipe Pipe = AnthropicTrackedPipe ``` -------------------------------- ### Anthropic Tracked Pipe Setup Source: https://dartmouth.github.io/openwebui-token-tracking/index Sets up a tracked pipe for the Anthropic provider using the `openwebui-token-tracking` library. This involves defining a Python class that inherits from `BaseTrackedPipe` and assigning it to the `Pipe` variable. ```python """ title: Anthropic Pipe author: Simon Stone requirements: openwebui-token-tracking version: 0.1.0 """ fromopenwebui_token_tracking.pipes.anthropicimport AnthropicTrackedPipe Pipe = AnthropicTrackedPipe ``` -------------------------------- ### Get Available Models Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/tracking Fetches all available models, optionally filtered by a specific provider. It returns a list of model pricing schemas. ```Python [docs] defget_models( self, provider: str = None, id: str = None ) -> list[ModelPricingSchema]: """Get all available models. :param provider: If not None, only returns the models by this provider. Defaults to None :type provider: str, optional :return: A description of the models' pricing schema :rtype: list[ModelPricingSchema] """ with Session(self.db_engine) as session: if provider is None: models = session.query(ModelPricing).all() else: models = ( session.query(ModelPricing) .filter(ModelPricing.provider == provider) .all() ) return [ ModelPricingSchema.model_validate(m, from_attributes=True) for m in models ] ``` -------------------------------- ### Database Initialization and Settings Source: https://dartmouth.github.io/openwebui-token-tracking/genindex Functions for initializing the database and base settings. ```Python init_db() (in module openwebui_token_tracking.db) init_base_settings() (in module openwebui_token_tracking.settings) ``` -------------------------------- ### Build Documentation Locally Source: https://dartmouth.github.io/openwebui-token-tracking/readme Builds the project's documentation locally using Sphinx. This command navigates to the docs directory and executes the sphinx-build command. ```Shell ".[docs]" sphinx-build ``` -------------------------------- ### Get Credit Group Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/credit_groups Retrieves a specific credit group from the database by its name and returns its properties as a dictionary. If the credit group is not found, a KeyError is raised. ```Python import os import sqlalchemy as db from sqlalchemy.orm import Session from openwebui_token_tracking.db import init_db from openwebui_token_tracking.db.credit_group import CreditGroup, CreditGroupUser from openwebui_token_tracking.db.user import User from openwebui_token_tracking.user import serialize_user def get_credit_group(credit_group_name: str, database_url: str = None) -> dict: """Retrieves a credit group from the database by its name and returns it as a dictionary. :param credit_group_name: Name of the credit group to retrieve :type credit_group_name: str :param database_url: URL of the database. If None, uses env variable ``DATABASE_URL`` :type database_url: str, optional :return: Dictionary containing the credit group properties (id, name, max_credit, description) :rtype: dict :raises KeyError: Raised if the credit group of that name could not be found """ if database_url is None: database_url = os.environ["DATABASE_URL"] engine = init_db(database_url) with Session(engine) as session: credit_group = ( session.query(CreditGroup).filter_by(name=credit_group_name).first() ) if not credit_group: raise KeyError(f"Could not find credit group: {credit_group_name}") return { "id": str(credit_group.id), # Convert UUID to string "name": credit_group.name, "max_credit": credit_group.max_credit, "description": credit_group.description, } ``` -------------------------------- ### Initialize OpenWebUI Token Tracking Source: https://dartmouth.github.io/openwebui-token-tracking/readme Initializes the Open WebUI token tracking system. This command migrates the database, adds default pricing information for supported model providers, and sets a baseline daily token credit allowance for all users. Custom pricing can be provided via a JSON file using the --json option. ```Shell owui-token-tracking init ``` -------------------------------- ### Build Documentation Locally Source: https://dartmouth.github.io/openwebui-token-tracking/index Builds the project documentation locally using Sphinx. This command navigates to the docs directory and executes the Sphinx build process. ```bash ".[docs]" sphinx-build ``` -------------------------------- ### Get Sponsored Allowances Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/sponsored Retrieves a list of all sponsored allowances from the database. Optionally filters the results by a specific sponsor ID. This function is useful for listing all allowances or those belonging to a particular sponsor. ```Python import os from sqlalchemy.orm import Session from openwebui_token_tracking.db import ( init_db, SponsoredAllowance, ) def get_sponsored_allowances( database_url: str = None, sponsor_id: str = None, ): """Get all sponsored allowances, optionally filtered by sponsor ID. :param database_url: The database connection URL. If None, uses the DATABASE_URL environment variable. :type database_url: str, optional :param sponsor_id: Filter allowances by sponsor ID. :type sponsor_id: str, optional ``` -------------------------------- ### Initialize Open WebUI Token Tracking Source: https://dartmouth.github.io/openwebui-token-tracking/index Initializes the Open WebUI token tracking system. This command migrates the database, adds default pricing information for supported model providers, and sets a baseline daily token credit allowance for users. ```bash owui-token-tracking init ``` -------------------------------- ### Get Sponsored Allowance Credit Limit Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/tracking Retrieves the daily credit limit for a sponsored allowance based on its name or ID. It queries the database to find the relevant allowance and returns its credit limit. ```Python session.query(SponsoredAllowance.daily_credit_limit) .filter(SponsoredAllowance.name == sponsored_allowance_name) .scalar() ) elif sponsored_allowance_id is not None: max_credits = ( session.query(SponsoredAllowance.daily_credit_limit) .filter(SponsoredAllowance.id == sponsored_allowance_id) .scalar() ) ``` -------------------------------- ### Database Migration and Initialization Source: https://dartmouth.github.io/openwebui-token-tracking/api/db Functions to create database tables and initialize the database engine. `migrate_database` creates necessary tables, while `init_db` sets up the SQLAlchemy engine. ```Python openwebui_token_tracking.db.migrate_database(_database_url_) Creates the tables required for token tracking in the specified database Parameters: **database_url** (_str_) – A database URL in SQLAlchemy format ``` ```Python openwebui_token_tracking.db.init_db(_database_url_) Initialize the database connection. Creates and returns a SQLAlchemy database engine using the provided connection URL. Parameters: **database_url** (_str_) – URL for connecting to the database Returns: Configured SQLAlchemy database engine Return type: `sqlalchemy.engine.Engine` Example: ``` >>> engine = init_db("postgresql://user:password@localhost/dbname") ``` ``` -------------------------------- ### Get Remaining Sponsored Credits Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/tracking Retrieves the remaining credits in a specified sponsored allowance. It calculates used credits based on token usage logs and compares it against the total credit limit of the allowance. ```Python def_remaining_sponsored_credits(self, sponsored_allowance_id: str): """Get remaining credits in a sponsored allowance :param sponsored_allowance_id: ID of the sponsored allowance :type sponsored_allowance_id: str :return: Remaining credits in the allowance :rtype: int """ with Session(self.db_engine) as session: query = db.select( SponsoredAllowance.creation_date, SponsoredAllowance.total_credit_limit, ).where(SponsoredAllowance.id == sponsored_allowance_id) creation_date, total_credit_limit = session.execute(query).first() models = self.get_models() model_list = [m.id for m in models] query = ( db.select( TokenUsageLog.model_id, db.func.sum(TokenUsageLog.prompt_tokens).label("prompt_tokens_sum"), db.func.sum(TokenUsageLog.response_tokens).label( "response_tokens_sum" ), ) .where( TokenUsageLog.sponsored_allowance_id == sponsored_allowance_id, db.func.date(TokenUsageLog.log_date) <= creation_date, TokenUsageLog.model_id.in_(model_list), ) .group_by(TokenUsageLog.model_id) ) results = session.execute(query).fetchall() total_credits_used = self._calc_credits_from_tokens( records=results, models=models ) return int(total_credit_limit - total_credits_used) ``` -------------------------------- ### Get Sponsored Allowances Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/sponsored Retrieves a list of sponsored allowances from the database. Allows filtering by sponsor ID and orders the results by name for consistency. Each allowance is returned as a dictionary containing its ID, name, sponsor ID, credit limits, and base models. ```Python def get_sponsored_allowances(database_url: str = None, sponsor_id: str = None): """Get a list of sponsored allowances. :param database_url: The database connection URL. If None, uses the DATABASE_URL environment variable. :type database_url: str, optional :param sponsor_id: Filter by sponsor ID. If None, returns all sponsored allowances. :type sponsor_id: str, optional :return: List of sponsored allowances, each as a dictionary. :rtype: list[dict] """ if database_url is None: database_url = os.environ["DATABASE_URL"] engine = init_db(database_url) with Session(engine) as session: query = session.query(SponsoredAllowance) if sponsor_id is not None: query = query.filter(SponsoredAllowance.sponsor_id == sponsor_id) # Order by name for consistent results query = query.order_by(SponsoredAllowance.name) sponsored_allowances = query.all() result = [] for allowance in sponsored_allowances: result.append( { "id": str(allowance.id), "name": allowance.name, "sponsor_id": allowance.sponsor_id, "total_credit_limit": allowance.total_credit_limit, "daily_credit_limit": allowance.daily_credit_limit, "base_models": allowance.base_models, } ) return result ``` -------------------------------- ### Database Operations and Models Source: https://dartmouth.github.io/openwebui-token-tracking/api Provides functions for database initialization and migration, along with data models for credit groups, pricing, settings, allowances, and user logs. ```APIDOC migrate_database() init_db() CreditGroup CreditGroupUser ModelPricing BaseSetting SponsoredAllowance SponsoredAllowanceBaseModels TokenUsageLog User ``` -------------------------------- ### Open WebUI Database Initialization and Migration Source: https://dartmouth.github.io/openwebui-token-tracking/api/db Provides functions for initializing and migrating the database for token tracking in Open WebUI. `init_db()` sets up the database schema, and `migrate_database()` handles schema updates. ```Python def init_db(): """Initializes the database schema for token tracking.""" pass def migrate_database(): """Migrates the database to the latest schema version.""" pass ``` -------------------------------- ### Get Sponsored Allowance Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/sponsored Retrieves a single sponsored allowance by its name or ID. Returns the allowance details as a dictionary, including its ID, name, sponsor ID, credit limits, and associated base models. Raises KeyError if no allowance matches the provided identifier. ```Python import os from sqlalchemy.orm import Session from openwebui_token_tracking.db import ( init_db, SponsoredAllowance, ) def get_sponsored_allowance( database_url: str = None, name: str = None, id: str = None, ): """Get a single sponsored allowance by name or ID. :param database_url: The database connection URL. If None, uses the DATABASE_URL environment variable. :type database_url: str, optional :param name: The name of the allowance to retrieve. :type name: str, optional :param id: The ID of the allowance to retrieve. :type id: str, optional :return: The sponsored allowance as a dictionary. :rtype: dict :raises KeyError: If no allowance is found with the given name or ID. """ if database_url is None: database_url = os.environ["DATABASE_URL"] if name is None and id is None: raise ValueError("Either name or id must be provided") engine = init_db(database_url) with Session(engine) as session: query = session.query(SponsoredAllowance) if name is not None: query = query.filter(SponsoredAllowance.name == name) if id is not None: query = query.filter(SponsoredAllowance.id == id) sponsored_allowance = query.first() if sponsored_allowance is None: raise KeyError(f"Could not find sponsored allowance: {id=}, {name=}") return { "id": str(sponsored_allowance.id), "name": sponsored_allowance.name, "sponsor_id": sponsored_allowance.sponsor_id, "total_credit_limit": sponsored_allowance.total_credit_limit, "daily_credit_limit": sponsored_allowance.daily_credit_limit, "base_models": sponsored_allowance.base_models, } ``` -------------------------------- ### Settings Initialization Source: https://dartmouth.github.io/openwebui-token-tracking/api Function to initialize base settings for the application. ```APIDOC init_base_settings() ``` -------------------------------- ### OpenWebUI Token Tracking - Modules Source: https://dartmouth.github.io/openwebui-token-tracking/genindex Lists all the Python modules included in the OpenWebUI token tracking project. This provides an overview of the project's organizational structure. ```python openwebui_token_tracking.actions openwebui_token_tracking.credit_groups openwebui_token_tracking.db openwebui_token_tracking.model_pricing openwebui_token_tracking.models openwebui_token_tracking.pipes openwebui_token_tracking.settings openwebui_token_tracking.sponsored openwebui_token_tracking.tracking openwebui_token_tracking.user ``` -------------------------------- ### BaseTrackedPipe Initialization and Token Tracking Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/pipes/base_tracked_pipe Initializes the BaseTrackedPipe with provider details and sets up token tracking using an environment variable for the database URL. It defines constants for the database URL environment variable and a model ID prefix. ```Python classBaseTrackedPipe(ABC): DATABASE_URL_ENV = "DATABASE_URL" MODEL_ID_PREFIX = "." def__init__(self, provider, url): self.provider = provider self.url = url self.type = "manifold" self.valves = self.Valves() self.token_tracker = TokenTracker(os.environ[BaseTrackedPipe.DATABASE_URL_ENV]) ``` -------------------------------- ### Open WebUI Token Tracking Modules Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/index Lists all available modules within the Open WebUI Token Tracking project, covering actions, credit groups, database interactions, models, pipes for different AI providers, settings, sponsorship, and user management. ```Python openwebui_token_tracking.actions.credit_balance openwebui_token_tracking.credit_groups openwebui_token_tracking.db.credit_group openwebui_token_tracking.db.db openwebui_token_tracking.db.model_pricing openwebui_token_tracking.db.settings openwebui_token_tracking.db.sponsored openwebui_token_tracking.db.token_usage openwebui_token_tracking.db.user openwebui_token_tracking.model_pricing openwebui_token_tracking.models openwebui_token_tracking.pipes.anthropic openwebui_token_tracking.pipes.base_tracked_pipe openwebui_token_tracking.pipes.google_genai openwebui_token_tracking.pipes.mistral openwebui_token_tracking.pipes.openai openwebui_token_tracking.settings openwebui_token_tracking.sponsored openwebui_token_tracking.tracking openwebui_token_tracking.user ``` -------------------------------- ### OpenWebUI Token Tracking API Modules Source: https://dartmouth.github.io/openwebui-token-tracking/_sources/api Lists the available API modules for the OpenWebUI token tracking project. This includes tracking, database operations, credit groups, model pricing, models, pipes, settings, sponsored features, user management, and actions. ```APIDOC .. toctree:: :maxdepth: 2 api/tracking api/db api/credit_groups api/model_pricing api/models api/pipes api/settings api/sponsored api/user api/actions ``` -------------------------------- ### Initialize Base Settings Source: https://dartmouth.github.io/openwebui-token-tracking/api/settings Initializes the base settings table with default values. It takes a database URL and an optional list of settings dictionaries. If no settings are provided, default settings are used. ```APIDOC openwebui_token_tracking.settings.init_base_settings(_database_url_, _settings =None_) Initializes the base settings table with default values Parameters: * **database_url** (_str_) – A database URL in SQLAlchemy format * **settings** (_list_ _[__dict_ _[__str_ _,__str_ _]__]_) – A list of dictionaries of settings to use. If None, uses default settings. ``` -------------------------------- ### Initialize Base Settings Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/settings Initializes the base settings table in the database with default or provided values. It takes a database URL and an optional list of settings dictionaries. ```Python from sqlalchemy.orm import Session from openwebui_token_tracking.db import init_db, BaseSetting def init_base_settings(database_url: str, settings: list[dict[str, str]] | None = None): """Initializes the base settings table with default values :param database_url: A database URL in `SQLAlchemy format `_ :type database_url: str :param settings: A list of dictionaries of settings to use. If None, uses default settings. :type settings: list[dict[str, str]] """ if settings is None: settings = [ { "setting_key": "base_credit_allowance", "setting_value": "1000", "description": "Baseline credit allowance for all users.", } ] engine = init_db(database_url) with Session(engine) as session: for setting in settings: session.merge(BaseSetting(**setting)) session.commit() ``` -------------------------------- ### OpenWebUI Token Tracking - Attributes and Methods Source: https://dartmouth.github.io/openwebui-token-tracking/genindex Lists key attributes and methods found within the OpenWebUI token tracking project, categorized by their respective modules or classes. This includes database fields, model attributes, and method functionalities. ```python max_credit (openwebui_token_tracking.db.CreditGroup attribute) max_credits() (openwebui_token_tracking.tracking.TokenTracker method) migrate_database() (in module openwebui_token_tracking.db) MISTRAL_API_KEY (openwebui_token_tracking.pipes.MistralTrackedPipe.Valves attribute) model_config (openwebui_token_tracking.models.ModelPricingSchema attribute) model_id (openwebui_token_tracking.db.TokenUsageLog attribute) MODEL_ID_PREFIX (openwebui_token_tracking.pipes.BaseTrackedPipe attribute) name (openwebui_token_tracking.db.CreditGroup attribute) non_stream_response() (openwebui_token_tracking.pipes.BaseTrackedPipe method) output_cost_credits (openwebui_token_tracking.db.ModelPricing attribute) per_input_tokens (openwebui_token_tracking.db.ModelPricing attribute) per_output_tokens (openwebui_token_tracking.db.ModelPricing attribute) pipe() (openwebui_token_tracking.pipes.BaseTrackedPipe method) pipes() (openwebui_token_tracking.pipes.BaseTrackedPipe method) prompt_tokens (openwebui_token_tracking.db.TokenUsageLog attribute) provider (openwebui_token_tracking.db.ModelPricing attribute) PROVIDER (openwebui_token_tracking.pipes.OpenAITrackedPipe.Valves attribute) remaining_credits() (openwebui_token_tracking.tracking.TokenTracker method) remove_user() (in module openwebui_token_tracking.credit_groups) response_tokens (openwebui_token_tracking.db.TokenUsageLog attribute) serialize_user() (in module openwebui_token_tracking.user) setting_key (openwebui_token_tracking.db.BaseSetting attribute) setting_value (openwebui_token_tracking.db.BaseSetting attribute) sponsor_id (openwebui_token_tracking.db.SponsoredAllowance attribute) ponsored_allowance (openwebui_token_tracking.db.SponsoredAllowanceBaseModels attribute) ponsored_allowance_id (openwebui_token_tracking.db.SponsoredAllowanceBaseModels attribute) stream_response() (openwebui_token_tracking.pipes.BaseTrackedPipe method) ``` -------------------------------- ### List All Model Pricing Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/model_pricing Retrieves all model pricing entries from the database, with an option to filter by provider. Returns a list of dictionaries containing pricing details. ```Python [docs] deflist_model_pricing(database_url: str, provider: str = None) -> list[dict]: """Retrieve model pricing entries from the database, optionally filtered by provider :param database_url: A database URL in `SQLAlchemy format `_ :type database_url: str :param provider: Optional provider name to filter results :type provider: str, optional :return: List of dictionaries containing model pricing information :rtype: list[dict] """ engine = init_db(database_url) with Session(engine) as session: query = session.query(ModelPricing) if provider: query = query.filter(ModelPricing.provider == provider) models = query.all() return [ { "provider": model.provider, "id": model.id, "name": model.name, "input_cost_credits": model.input_cost_credits, "per_input_tokens": model.per_input_tokens, "output_cost_credits": model.output_cost_credits, "per_output_tokens": model.per_output_tokens, } for model in models ] ``` -------------------------------- ### Create Credit Group Source: https://dartmouth.github.io/openwebui-token-tracking/readme Creates a new credit group for managing token allowances within Open WebUI. This command allows specifying a name, an initial credit amount, and a description for the credit group. ```Shell owui-token-tracking credit-group create "my credit group" 2000 "my credit group granting an additional 2000 credits" ``` -------------------------------- ### Credit and Pricing Management Functions Source: https://dartmouth.github.io/openwebui-token-tracking/genindex Provides functions for updating credit group information, model pricing, and sponsored allowance details. ```python update_credit_group() update_model_pricing() update_sponsored_allowance() upsert_credit_group() upsert_model_pricing() ``` -------------------------------- ### Open WebUI BaseSetting Data Model Source: https://dartmouth.github.io/openwebui-token-tracking/api/db Defines the `BaseSetting` data model for storing general settings in Open WebUI. It includes keys and values for various configuration options. ```Python class BaseSetting: setting_key: str setting_value: str description: str ``` -------------------------------- ### OpenWebUI Token Tracking - Classes Source: https://dartmouth.github.io/openwebui-token-tracking/genindex Lists the main classes defined within the OpenWebUI token tracking project. These classes represent core components such as data models and tracking pipes. ```python MistralTrackedPipe (class in openwebui_token_tracking.pipes) MistralTrackedPipe.Valves (class in openwebui_token_tracking.pipes) ModelPricing (class in openwebui_token_tracking.db) ModelPricingSchema (class in openwebui_token_tracking.models) OpenAITrackedPipe (class in openwebui_token_tracking.pipes) OpenAITrackedPipe.Valves (class in openwebui_token_tracking.pipes) SponsoredAllowance (class in openwebui_token_tracking.db) SponsoredAllowanceBaseModels (class in openwebui_token_tracking.db) ``` -------------------------------- ### Create Credit Group Source: https://dartmouth.github.io/openwebui-token-tracking/index Creates a new credit group for managing token allowances. This command allows specifying a group name, the number of credits to allocate, and a description for the group. ```bash owui-token-tracking credit-group create "my credit group" 2000 "my credit group granting an additional 2000 credits" ``` -------------------------------- ### Open WebUI Token Tracking API Reference Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/tracking Contains the API reference for the Open WebUI Token Tracking project. ```APIDOC API Reference This section details the available API endpoints and their usage for the Open WebUI Token Tracking system. ``` -------------------------------- ### Migrate Database Schema (Python) Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/db/db Creates the necessary tables for token tracking in the specified database using Alembic. This function applies database migrations to set up the schema required for the token tracking functionality. ```python defmigrate_database(database_url: str): """Creates the tables required for token tracking in the specified database :param database_url: A database URL in `SQLAlchemy format `_ :type database_url: str """ alembic_cfg = Config() alembic_cfg.set_main_option( "script_location", str(Path(__file__).parent.parent / "migrations/alembic") ) alembic_cfg.set_main_option("sqlalchemy.url", database_url) command.stamp(alembic_cfg, "base") command.upgrade(alembic_cfg, "token_tracking@head") ``` -------------------------------- ### BaseSetting SQLAlchemy Model Source: https://dartmouth.github.io/openwebui-token-tracking/api/db SQLAlchemy model for the 'base_setting' table, storing global configuration settings. It uses a key-value pair structure for system-wide parameters like default credits and rate limits. ```Python _class_ openwebui_token_tracking.db.BaseSetting(_** kwargs_) Bases: `Base` SQLAlchemy model for the baseline settings table Stores global configuration settings for the token tracking system as key-value pairs. Used for system-wide settings like default credit allowances and rate limits. description Human-readable description of what the setting controls and its purpose setting_key Primary key representing the unique setting identifier setting_value Value of the setting stored as a string (may need conversion to appropriate type when used) ``` -------------------------------- ### Create Sponsored Allowance Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/sponsored Creates a new sponsored allowance entry in the database. It associates the allowance with specific base models and sets total and daily credit limits. Requires a database URL, sponsor ID, name, a list of model IDs, and credit limits. ```Python import os from typing import Iterable from sqlalchemy.orm import Session from openwebui_token_tracking.db import ( init_db, SponsoredAllowance, SponsoredAllowanceBaseModels, ) def create_sponsored_allowance( database_url: str, sponsor_id: str, name: str, models: Iterable[str], total_credit_limit: int, daily_credit_limit: int, ): if database_url is None: database_url = os.environ["DATABASE_URL"] engine = init_db(database_url) with Session(engine) as session: sponsored_allowance = SponsoredAllowance( sponsor_id=sponsor_id, name=name, total_credit_limit=total_credit_limit, daily_credit_limit=daily_credit_limit, ) # Create the base model associations for base_model_id in models: association = SponsoredAllowanceBaseModels( sponsored_allowance=sponsored_allowance, base_model_id=base_model_id ) sponsored_allowance.base_models.append(association) session.add(sponsored_allowance) session.commit() ``` -------------------------------- ### Model Pricing Management Functions Source: https://dartmouth.github.io/openwebui-token-tracking/genindex Functions for managing model pricing information, including adding, deleting, and listing pricing details for different models. ```Python add_model_pricing() (in module openwebui_token_tracking.model_pricing) delete_model_pricing() (in module openwebui_token_tracking.model_pricing) list_model_pricing() (in module openwebui_token_tracking.model_pricing) get_model_pricing() (in module openwebui_token_tracking.model_pricing) ``` -------------------------------- ### OpenAIModel API Reference Source: https://dartmouth.github.io/openwebui-token-tracking/api/actions Details the OpenAIModel class, including its constructor and methods. It specifies parameters for model initialization and provides insights into model attributes and actions. ```APIDOC OpenAIModel: __init__(model_name: str, provider: str = 'openai') model_name: The name of the OpenAI model to use. provider: The provider to use (defaults to 'openai'). Attributes: valves: Represents the valves associated with the model. is_visible: Boolean indicating if the model is visible. DATABASE_URL_ENV: Environment variable for the database URL. Methods: action(): Performs an action related to the model. ``` -------------------------------- ### Retrieve Model Pricing Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/model_pricing Fetches model pricing entries from the database. Supports filtering by model ID and provider. Returns a list of dictionaries, each containing pricing details. ```Python fromopenwebui_token_tracking.dbimport init_db, ModelPricing fromopenwebui_token_tracking.modelsimport ModelPricingSchema fromsqlalchemy.ormimport Session [docs] defget_model_pricing( database_url: str, model_id: str = None, provider: str = None ) -> list[dict]: """Retrieve specific model pricing entries from the database :param database_url: A database URL in `SQLAlchemy format `_ :type database_url: str :param model_id: Model ID to filter results :type model_id: str, optional :param provider: Provider name to filter results :type provider: str, optional :return: List of dictionaries containing model pricing information :rtype: list[dict] """ engine = init_db(database_url) with Session(engine) as session: query = session.query(ModelPricing) if model_id: query = query.filter(ModelPricing.id == model_id) if provider: query = query.filter(ModelPricing.provider == provider) models = query.all() return [ { "provider": model.provider, "id": model.id, "name": model.name, "input_cost_credits": model.input_cost_credits, "per_input_tokens": model.per_input_tokens, "output_cost_credits": model.output_cost_credits, "per_output_tokens": model.per_output_tokens, } for model in models ] ``` -------------------------------- ### OpenWebUI Token Tracking Model Pricing Module Documentation Source: https://dartmouth.github.io/openwebui-token-tracking/_sources/api/model_pricing This section details the openwebui_token_tracking.model_pricing module. It includes all public members, undocumented members, and information about the inheritance hierarchy of the classes within this module. ```APIDOC .. automodule:: openwebui_token_tracking.model_pricing :members: :undoc-members: :show-inheritance: ``` -------------------------------- ### Open WebUI Token Tracking API Reference Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/pipes/mistral Provides details on the API endpoints and methods available for the Open WebUI Token Tracking system. This section covers how to interact with the token tracking functionalities. ```APIDOC Module: openwebui_token_tracking.pipes.mistral This module likely contains pipe implementations related to Mistral models within the Open WebUI token tracking system. Specific functions or classes are not detailed here but would typically involve processing or tracking tokens for Mistral AI models. ``` -------------------------------- ### BaseSetting SQLAlchemy Model Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/db/settings Defines the SQLAlchemy model for the 'token_tracking_base_settings' table. This table stores global configuration settings as key-value pairs, such as default credit allowances and rate limits. ```Python import sqlalchemy as sa from .base import Base class BaseSetting(Base): """SQLAlchemy model for the baseline settings table Stores global configuration settings for the token tracking system as key-value pairs. Used for system-wide settings like default credit allowances and rate limits. """ __tablename__ = "token_tracking_base_settings" setting_key = sa.Column(sa.String(length=255), primary_key=True) """Primary key representing the unique setting identifier""" setting_value = sa.Column(sa.String(length=255)) """Value of the setting stored as a string (may need conversion to appropriate type when used)""" description = sa.Column(sa.String(length=255)) """Human-readable description of what the setting controls and its purpose""" ``` -------------------------------- ### Open WebUI Token Tracking Module Code Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/pipes/base_tracked_pipe Details the module code for Open WebUI Token Tracking, specifically referencing the base_tracked_pipe. ```English openwebui_token_tracking.pipes.base_tracked_pipe ``` -------------------------------- ### Google Gemini API Configuration Source: https://dartmouth.github.io/openwebui-token-tracking/api/pipes Defines configuration parameters for the Google Gemini pipe, including API key, safety settings, and debug logging. ```Python class Valves(_** data_): """Configuration parameters for the Google Gemini pipe.""" Parameters: * **GOOGLE_API_KEY** (_str_) – API key for authenticating with Google’s API * **USE_PERMISSIVE_SAFETY** (_bool_) – Whether to use permissive safety settings * **DEBUG** (_bool_) – Enable debug logging model_config _: ClassVar[ConfigDict]__={}_ ``` -------------------------------- ### OpenAI Tracked Pipe Configuration Source: https://dartmouth.github.io/openwebui-token-tracking/api/pipes Details the configuration parameters for OpenAI-compatible API connections, including API key, base URL, provider, and debug logging. ```Python class Valves(_** data): """Configuration parameters for OpenAI (compatible) API connections.""" API_KEY: str API_BASE_URL: str PROVIDER: str DEBUG: bool model_config: ClassVar[ConfigDict] = {} API_KEY: str API_BASE_URL: str PROVIDER: str DEBUG: bool ``` -------------------------------- ### Open WebUI Token Tracking Module Code Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/model_pricing This section details the module code for Open WebUI Token Tracking, specifically referencing the model_pricing module. ```English openwebui_token_tracking.model_pricing ``` -------------------------------- ### ModelPricing SQLAlchemy Model Source: https://dartmouth.github.io/openwebui-token-tracking/_modules/openwebui_token_tracking/db/model_pricing Defines the SQLAlchemy model for the 'token_tracking_model_pricing' table. This table stores pricing details for various AI models, including costs per input and output tokens. ```Python import sqlalchemy as sa from .base import Base class ModelPricing(Base): """SQLAlchemy model for the model pricing table Stores pricing information for AI models, including credit costs for input and output tokens. """ __tablename__ = "token_tracking_model_pricing" provider = sa.Column(sa.String(length=255), primary_key=True) """Provider of the AI model (e.g., 'openai', 'anthropic'), part of the composite primary key""" id = sa.Column(sa.String(length=255), primary_key=True) """Identifier of the model (e.g., 'gpt-4', 'claude-3'), part of the composite primary key""" name = sa.Column(sa.String(length=255)) """Display name of the model""" input_cost_credits = sa.Column(sa.Integer()) """Number of credits charged for input tokens""" per_input_tokens = sa.Column(sa.Integer()) """Number of input tokens per credit charge (e.g., 1000000 tokens per `input_cost_credits`)""" output_cost_credits = sa.Column(sa.Integer()) """Number of credits charged for output tokens""" per_output_tokens = sa.Column(sa.Integer()) """Number of output tokens per credit charge (e.g., 1000000 tokens per `output_cost_credits`)""" ``` -------------------------------- ### Model Pricing Schema Source: https://dartmouth.github.io/openwebui-token-tracking/api Defines the schema for model pricing data. ```APIDOC ModelPricingSchema ```