Package: classy-fire
🤵🔥 Classy-Fire 🔥🤵Classy-fire is a pretrained multiclass text classification approach that leverages Azure OpenAI's LLM APIs using clever parameter tuning and prompting for classification.Why?Tired of having to beg your LLM to pick from a set of options / actions?Tired of working hard on cleaning and parsing its responses to trigger a flow?Struggling to strip unhelpful prefixes (such as "Sure! " or "I am just a language model!")?Having to wait on retries in cases of unexpected outputs?Getting random responses on the same query?Need a "quick and dirty" text classifier? Don't have enough training data?Start hereInstallationpip install classy-fireUsage examplefromclassy_fireimportLLMClassifier,MCMCClassifierclassifier=LLMClassifier(["Banana","Watermelon","Apple","Grape"])result=classifier("Has an elongated shape")print(result)>>>('Banana',0)mcmc_classifier=MCMCClassifier(["Banana","Watermelon","Apple","Grape"])result=mcmc_classifier("Has an oval or spherical shape")print(result)>>>[('Banana',0,0.0),('Watermelon',1,0.02),('Apple',2,0.81),('Grape',3,0.17)]Prerequisitesmake sure you have OPENAI_API_BASE, OPENAI_API_VERSION, OPENAI_API_TYPE and OPENAI_API_KEY environment variables populated beforehand and a deployment of gpt-3.5-turbo named gpt-35-turbo-0301 (or pass deployment_name and model_name parameters to the LLMClassifier constructor).
One way to accomplish this:Create a file called ".env" in the path of your script containing:OPENAI_API_BASE=https://<endpoint>/
OPENAI_API_VERSION=2023-05-15  # for example
OPENAI_API_TYPE=azure  # or openai
OPENAI_API_KEY=<private key guid>install python-dotenvadd to the beginning your scriptload_dotenv()Continue hereLLMClassifier optional parametersLLMClassifier can be initialized with added parameters that can help instruct and ground it to the classification task at hand.task_description = "Ability to provide additional context on the classification options and overall context on inputs"few_shot_examples = "Ability to provide instances of inputs and corresponding expected output values as a string"The premise behind classy-fireIn Classy-fire, we instruct the LLM to provide the most likely classification for an input string to a set of predetermined classes (also strings).
Formally, given a string instance $x_i$ and a set of $k$ classes provided as strings, $C=(C_1, ..., C_k)$, classy-fire determines$argmax_j Pr[x_i \in C_j | C, \Theta]$Where $\Theta$ is the parameters (knowledge of the world) of the language model.Classy-fire does this efficiently by mapping class strings to single tokens and providing a strong prior probability for these tokens. We instruct the model to generate a single token response, which allows for optimized inference runtime.Classy-fire does this deterministically and with less sensitivity to confabulation (hallucination) by setting the model temperature to 0, thereby guaranteeing the returned response is the argmax of the model posterior probability.New!MCMCClassifier can now estimate the posterior distribution over classes using a Markov Chain Monte Carlo approach!Quality of resultsWe ran a preliminary experiment to classify a sample of 100 tweets from thetweet_eval dataset.
The resultsappear to beat the SOTA.precisionrecallf1-scoresupportanger0.970.810.8842joy0.740.920.8225optimism1.000.250.408sadness0.751.000.8621accuracy0.8396macro avg0.870.740.7496weighted avg0.870.830.8296See evaluate.ipynb for the details behind this experiment.We encourage the community to benchmark and explore this method against larger or more standardized datasets.We have not evaluated alternative prompting strategies or the impact of adding few shot examples, there may be flexibility in the achievable quality.Other stuffContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visithttps://cla.opensource.microsoft.com.When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor
contactopencode@microsoft.comwith any additional questions or comments.TrademarksThis project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
trademarks or logos is subject to and must followMicrosoft's Trademark & Brand Guidelines.
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
Any use of third-party trademarks or logos are subject to those third-party's policies.SecurityMicrosoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which includeMicrosoft,Azure,DotNet,AspNet,Xamarin, andour GitHub organizations.If you believe you have found a security vulnerability in any Microsoft-owned repository that meetsMicrosoft's definition of a security vulnerability, please report it to us as described below.Reporting Security IssuesPlease do not report security vulnerabilities through public GitHub issues.Instead, please report them to the Microsoft Security Response Center (MSRC) athttps://msrc.microsoft.com/create-report.If you prefer to submit without logging in, send email tosecure@microsoft.com.  If possible, encrypt your message with our PGP key; please download it from theMicrosoft Security Response Center PGP Key page.You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found atmicrosoft.com/msrc.Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)Full paths of source file(s) related to the manifestation of the issueThe location of the affected source code (tag/branch/commit or direct URL)Any special configuration required to reproduce the issueStep-by-step instructions to reproduce the issueProof-of-concept or exploit code (if possible)Impact of the issue, including how an attacker might exploit the issueThis information will help us triage your report more quickly.If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit ourMicrosoft Bug Bounty Programpage for more details about our active programs.Preferred LanguagesWe prefer all communications to be in English.PolicyMicrosoft follows the principle ofCoordinated Vulnerability Disclosure.