• Stars
    star
    226
  • Rank 175,538 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 1 year ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimize LLM token complexity to save API costs and model computations.

PromptOptimizer

kevin inspired logo

Minimize LLM token complexity to save API costs and model computations.

Features

  • Plug and Play Optimizers: Minimize token complexity using optimization methods without any access to weights, logits or decoding algorithm. Directly applicable to virtually all NLU systems.
  • Protected Tags: Special protected tags to mark important sections of prompt that should not be removed/modified.
  • Sequential Optimization: Chain different optimizers together sequentially.
  • Optimization Metrics: Number of tokens reduced and semantic similarity before and after optimization.
  • Langhcain and JSON Support: Supports langchain style prompt chains and OpenAI request JSON Object.

Why?

  • Minimize Token Complexity: Token Complexity is the amount of prompt tokens required to achieve a given task. Reducing token complexity corresponds to linearly reducing API costs and quadratically reducing computational complexity of usual transformer models.
  • Save Money: For large businesses, saving 10% on token count can lead to saving 100k USD per 1M USD.
  • Extend Limitations: Some models have small context lengths, prompt optimizers can help them process larger than context documents.
Prompt # Tokens Correct Response?
Who is the president of the United States of America? 11 βœ…
Who president US 3 (-72%) βœ…

Installation

Quick Installation

pip install prompt-optimizer

Install from source

git clone https://github.com/vaibkumr/prompt-optimizer.git;
cd prompt-optimizer;
pip install -e .

Disclaimer

There is a compression vs performance tradeoff -- the increase in compression comes at the cost of loss in model performance. The tradeoff can be greatly mitigated by chosing the right optimize for a given task. There is no single optimizer for all cases. There is no Adam here.

Getting started

from prompt_optimizer.poptim import EntropyOptim

prompt = """The Belle Tout Lighthouse is a decommissioned lighthouse and British landmark located at Beachy Head, East Sussex, close to the town of Eastbourne."""
p_optimizer = EntropyOptim(verbose=True, p=0.1)
optimized_prompt = p_optimizer(prompt)
print(optimized_prompt)

Evaluations

Following are the results for logiqa OpenAI evals task. It is only performed for a subset of first 100 samples. Please note the optimizer performance over this task should not be generalized to other tasks, more thorough testing and domain knowledge is needed to choose the optimal optimizer.

Name % Tokens Reduced LogiQA Accuracy USD Saved Per $100
Default 0.0 0.32 0.0
Entropy_Optim_p_0.05 0.06 0.3 6.35
Entropy_Optim_p_0.1 0.11 0.28 11.19
Entropy_Optim_p_0.25 0.26 0.22 26.47
Entropy_Optim_p_0.5 0.5 0.08 49.65
SynonymReplace_Optim_p_1.0 0.01 0.33 1.06
Lemmatizer_Optim 0.01 0.33 1.01
NameReplace_Optim 0.01 0.34 1.13
Punctuation_Optim 0.13 0.35 12.81
Autocorrect_Optim 0.01 0.3 1.14
Pulp_Optim_p_0.05 0.05 0.31 5.49
Pulp_Optim_p_0.1 0.1 0.25 9.52

Cost-Performance Tradeoff

The reduction in cost often comes with a loss in LLM performance. Almost every optimizer have hyperparameters that control this tradeoff.

For example, in EntropyOptim the hyperparamter p, a floating point number between 0 and 1 controls the ratio of tokens to remove. p=1.0 corresponds to removing all tokens while p=0.0 corresponds to removing none.

The following chart shows the trade-off for different values of p as evaluated on the OpenAI evals logiqa task for a subset of first 100 samples.

tradeoff

Contributing

There are several directions to contribute to. Please see CONTRIBUTING.md for contribution guidelines and possible future directions.

Social

Contact us on twitter Vaibhav Kumar and Vaibhav Kumar.

Inspiration

Image

More Repositories

1

yTermPlayer

Stream youtube playlists as audio on linux terminal
Python
266
star
2

JobSchedulingRLenv

Reinforcement learning environment for job scheduling written in python.
Python
23
star
3

RAN-Debias

Code for the TACL paper "Nurse is Closer to Woman than Surgeon? Mitigating Gender-Biased Proximities in Word Embeddings"
Python
16
star
4

DatasetScraper

Tool to create image datasets for machine learning problems by scraping search engines like Google, Bing and Baidu.
Python
16
star
5

Turing-machine-simulator

Turing machine simulator using python
Python
8
star
6

RL_from_scratch

This is where I write RL related stuff from scratch
Jupyter Notebook
8
star
7

ML-frontend-template

ML frontend template to receive ML features from an HTML form and pass it onto the flask API to fetch the result
CSS
7
star
8

FoodWastePrevention-RL

Multi-Agent RL for dynamic pricing of food to save wastage while maximizing profit
Python
6
star
9

crypto_predictor

Crypto price predictor using machine learning
Jupyter Notebook
4
star
10

PIC-16B-TA-W23

TA Material for PIC16B Winter 2023
HTML
3
star
11

Phone-Activity-Process-Mining

Process Mining for Phone Activity Logs
Python
3
star
12

FairGAN

A GAN based approach to mitigate gender bias in word embeddings
Jupyter Notebook
3
star
13

ML-CPP

Basic ML in CPP
C++
2
star
14

stats-for-ml

This repository is my record of simple experimentations on things related to stats, probability and similar interesting stuff
Jupyter Notebook
2
star
15

PIC-20A-TA-1B-S22

Java
1
star
16

Recursive-Descent-Parser

A Simple Recursive Descent Parser for a simple grammar
Python
1
star
17

jax-haiku-ml

Learning Jax and Haiku for Machine Learning
Jupyter Notebook
1
star
18

DQN-Atari

DQN based agent for Atari games
Python
1
star
19

pyWeblog

A static web blog created in python
Python
1
star
20

deep_learning_otae_detector

Shimura Tae (Gintama) detection using deep learning
Jupyter Notebook
1
star
21

Web-Console

A simple Information Console for personal websites
JavaScript
1
star
22

Work-Compilation

Compilation of all my code
1
star
23

CacheReplacementEnv

A simple reinforcement learning environment for page cache replacement in OS.
Python
1
star
24

Data-Encryption-Standard

Python implementation of Data Encryption Standard (DES) - Computer Networks
Python
1
star
25

whatsapp.moe

whatsapp.moe is a google chrome extensions and a mozilla firefox add-on
JavaScript
1
star
26

dotfiles2

Python
1
star
27

youtube-qna-frontend

HTML
1
star
28

Image_Segmentation

A collection of various models, archtectures and augmentations for Image Segmentation
Jupyter Notebook
1
star
29

Time-Series-Deep-Learning-Kaggle

Time Series Deep Learning models for M5 Kaggle
Jupyter Notebook
1
star
30

automate-data-collection

A python script to automate C++ elf file signature data collection process
C++
1
star