Module: Tracking
Goal
The tracking
module is responsible for tracking and analyzing the usage and cost of various APIs used in the project. It includes two files: api_tracking.py
and cost_analytics.py
.
File: api_tracking.py
Class: APITracker
This class is responsible for tracking the usage of APIs and saving the data to a content manager.
Method: __init__()
- Initializes the APITracker object.
- Calls the
initiateAPITracking()
method.
Method: setDataManager(contentManager: ContentDataManager)
- Sets the content manager for storing the API usage data.
- Raises an exception if the content manager is null.
Method: openAIWrapper(gptFunc)
- Wrapper function for OpenAI API calls.
- Saves the API usage data to the content manager.
- Returns the result of the API call.
Method: elevenWrapper(audioFunc)
- Wrapper function for Eleven API calls.
- Saves the API usage data to the content manager.
- Returns the result of the API call.
Method: wrap_turbo()
- Wraps the
gpt3Turbo_completion
function from thegpt_utils
module using theopenAIWrapper
method. - Replaces the original function with the wrapped function.
Method: wrap_eleven()
- Wraps the
generateVoice
function from theaudio_generation
module using theelevenWrapper
method. - Replaces the original function with the wrapped function.
Method: initiateAPITracking()
- Initiates the tracking of APIs by wrapping the necessary functions using the
wrap_turbo
andwrap_eleven
methods.
File: cost_analytics.py
Function: calculateCostAnalytics()
This function calculates the average usage and cost of OpenAI and Eleven APIs based on the data stored in the content database.
- Initializes the content database.
- Retrieves the API usage data from the database.
- Calculates the average usage and cost for OpenAI and Eleven APIs.
- Prints the results.
Usage example:
calculateCostAnalytics()
Note: The commented code at the end of the file is unrelated and can be ignored.