CAPPr is a Python package which makes your LLM pick from a list of choices. In other words, CAPPr does text classification.

It’s compatible with GGUF models, PyTorch HuggingFace models, GPTQ models, and AWQ models.

GitHub: https://github.com/kddubey/cappr

Docs: https://cappr.readthedocs.io/

Why?

Text classification is currently performed by prompting a model to generate a choice. But because standard text generation algorithms are not guaranteed to output one of these choices, the output/completion must be post-processed. Post-processing introduces unneeded engineering complexity.

Moreover, while open source models are pretty smart, smaller or less-trained ones are not great at generating constrained outputs. Consider the tame COPA classification task. Prompting a quantized Llama 2 chat model using a multiple choice format is 63% accurate. Using CAPPr with a more natural prompt, accuracy jumps to 83%. All while cutting down on engineering complexity: you will never have to worry about invalid outputs, e.g., the LLM saying “I don’t know”. (These results are pulled from the demo here.) Similar but less wild results have been observed in a few other experiments:

​

accuracy comparison

Why not other tools?

There are other LLM structuring tools which support this type of “just pick” functionality. Many use a pretty neat and scalable algorithm. Honestly, there aren’t compelling reasons for not using these tools instead of CAPPr.

But CAPPr doesn’t try to be a heavyweight LLM query/programming language. It’s aimed at solving text classification problems, and is hopefully quite easy to pick up. CAPPr also implements intermediate caching with batching, in case you’re interested in that. And you can easily compute probabilities, which may be useful in high-stakes applications.

How does it work?

CAPPr = Completion After Prompt Probability.

You input a prompt string and a set of candidate completion strings such that the string {prompt} {completion} is a naturally flowing thought. CAPPr picks the completion which is mostly likely to follow prompt by aggregating token log-probabilities. This algorithm is quite well-known. You’ll find it as a subroutine in papers from GPT-2 to Self-Consistency. My implementation includes a few computational and statistical optimizations, while maintaining a simple interface. See the end-to-end examples in the README.

Feel free to install it and mess around :-)

  • oinkyDoinkyDoink@alien.top
    cake
    B
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 years ago

    Very neat. Will try.

    Do you have any examples with few shot prompting? Would this would better with a finetuned model?