This API predicts the number of completion tokens for a given prompt and model using an advanced neural network. It provides confidence intervals for more reliable estimates.
- Predicts completion tokens for a given prompt and model.
- Provides 95% confidence intervals using Monte Carlo Dropout.
- Supports multiple models.
- API endpoints to list available models and make predictions.
- Gated Linear Unit (GLU) activation functions.
- Multiple hidden layers with deep architecture.
- Layer normalization for improved training stability.
- Trained with 50/20/30 train/validation/test split.
- Uses MSE loss for accurate token prediction.
- Trained for up to 100 epochs with early stopping.
- Optimized with AdamW and learning rate scheduling.
- Streamlined architecture focusing on prompt and model features.
The API provides 95% confidence intervals for token predictions using Monte Carlo Dropout:
- Multiple forward passes with dropout enabled.
- Statistical analysis of prediction distribution.
- Lower and upper bounds with 95% confidence.
-
POST /predict: Predicts completion tokens for a given prompt and model.-
Request body:
{ "prompt": "Your prompt text here", "model": "model-name" } -
Response:
{ "prompt": "Your prompt text here", "model": "model-name", "predicted_completion_tokens": 123, "confidence_interval": { "lower": 100, "upper": 150, "confidence": "95%" }, "message": "95% confidence interval: 100 to 150 tokens" }
-
-
GET /models: Lists available models.-
Response:
{ "models": ["gpt-3.5-turbo", "gpt-4o-mini", "o1-mini", "o3-mini", "claude"] }
-
-
Install dependencies:
pip install -r requirements.txt
-
Set API Keys:
-
Ensure you have an OpenAI API key.
-
Set the
OPENAI_API_KEYenvironment variable in your.envfile.OPENAI_API_KEY=your_openai_api_key -
If you want to use Anthropic models, ensure you have an Anthropic API key.
-
Set the
ANTHROPIC_API_KEYenvironment variable in your.envfile.ANTHROPIC_API_KEY=your_anthropic_api_key
-
-
Run the Flask app:
python app.py
The app will start on
http://0.0.0.0:5001.
To make the API accessible over the internet, you can use ngrok.
ngrok creates a secure tunnel between your local machine and the ngrok cloud, providing a public URL for accessing your local server.
-
Sign up for ngrok:
- Create a free account at ngrok.com.
-
Install ngrok:
-
macOS:
-
Using Homebrew:
brew install ngrok
-
Alternatively, download the macOS package from the ngrok website and follow the installation instructions.
-
-
Windows:
- Download the Windows ZIP file from the ngrok website.
- Extract the
ngrok.exeexecutable to a directory of your choice. - Add the directory containing
ngrok.exeto your system'sPATHenvironment variable.
-
-
Authenticate ngrok:
-
Get your authtoken from the ngrok dashboard after signing up.
-
Run the following command in your terminal, replacing
<YOUR_AUTHTOKEN>with your actual authtoken:ngrok config add-authtoken <YOUR_AUTHTOKEN>
-
-
Start ngrok tunnel:
-
To tunnel your Flask app running on port 5001, run:
ngrok http 5001
-
-
Access your application:
- ngrok will display a public URL (e.g.,
https://your-ngrok-url.ngrok.io) in the terminal. - Use this URL to access your API from any device connected to the internet.
- ngrok will display a public URL (e.g.,
-
Start the Flask app:
python app.py
-
Start the ngrok tunnel (as described above).
-
Access the API endpoints using the ngrok URL:
- For example, to predict tokens, send a POST request to
https://your-ngrok-url.ngrok.io/predictwith the required JSON payload. - To list available models, send a GET request to
https://your-ngrok-url.ngrok.io/models.
- For example, to predict tokens, send a POST request to
Send a POST request to /predict with the following JSON payload:
{
"prompt": "Translate 'hello' to French.",
"model": "gpt-3.5-turbo"
}The API will return a JSON response with the predicted number of completion tokens and confidence intervals.
Send a GET request to /models. The API will return a JSON response with a list of available models.
{
"models": ["gpt-3.5-turbo", "gpt-4o-mini", "o1-mini", "o3-mini", "claude"]
}- Ensure your OpenAI API key is correctly set in the
.envfile. - The Flask app must be running for ngrok to tunnel the connection.
- The ngrok URL will change each time you start ngrok unless you have a paid ngrok plan with reserved domains.