Package: caikit-nlp
Caikit NLPCaikit-NLP is a python library providing various Natural Language Processing (NLP) capabilities built on top ofcaikitframework.IntroductionCaikit-NLP implements concept of "task" fromcaikitframework to define (and consume) interfaces for various NLP problems and implements various "modules" to provide functionalities for these "modules".Capabilities provided bycaikit-nlp:TaskModule(s)Salient Feature(s)TextGenerationTask1.PeftPromptTuning2.TextGeneration1. Prompt Tuning, Multi-task Prompt tuning2. Fine-tuning  Both modules above provide optimized inference capability using Text Generation Inference ServerTextClassificationTask1.SequenceClassification1. (Work in progress..)TokenClassificationTask1.FilteredSpanClassification1. (Work in progress..)TokenizationTask1.RegexSentenceSplitter1. Demo purposes onlyEmbeddingTaskEmbeddingTasks1.TextEmbedding1. TextEmbedding returns a text embedding vector from a local sentence-transformers model2. EmbeddingTasks takes multiple input texts and returns a corresponding list of vectors.SentenceSimilarityTaskSentenceSimilarityTasks1.TextEmbedding1. SentenceSimilarityTask compares one source_sentence to a list of sentences and returns similarity scores in order of the sentences.2. SentenceSimilarityTasks uses a list of source_sentences (each to be compared to same list of sentences) and returns corresponding lists of outputs.RerankTaskRerankTasks1.TextEmbedding1. RerankTask compares a query to a list of documents and returns top_n scores in order of relevance with indexes to the source documents and optionally returning the documents.2. RerankTasks takes multiple queries as input and returns a corresponding list of outputs. The same list of documents is used for all queries.Getting StartedNotebooksTo help you quickly get started with using Caikit, we have prepared aJupyter notebookthat can be run in Google Colab. Caikit-nlp is a powerful library that leverages prompt tuning and fine-tuning to add NLP domain capabilities to caikit.InstallationTo install from git repo:python-mvenv.venvsource.venv/bin/activate
pipinstallgit+https://github.com/caikit/caikit-nlpBootstrapping modelscaikit_nlpcan use  Hugging Face models, allowing for direct download and bootstrapping.For example, to usegoogle/flan-t5-small:importos# The env var ALLOW_DOWNLOADS has to be set to allow model downloads before importing caikit_nlpos.environ['ALLOW_DOWNLOADS']="1"importcaikit_nlpmodel_name="google/flan-t5-small"model=caikit_nlp.text_generation.TextGeneration.bootstrap(model_name)model.save(f"{model_name}-caikit")# optionally save the modelServing modelsTo serve models, the following basic configuration can be used:# config.ymlruntime:library:caikit_nlplocal_models_dir:./modelslog:formatter:pretty# optional: log formatter is set to json by defaultStart the server:envCONFIG_FILES=./config.ymlpython-mcaikit.runtimeThe model can now be queried atlocalhost:8080via http or atlocalhost:8085via grpc.For example, using the http server and using curl to send a POST request:curl--json'{"model_id": "flan-t5-small-caikit","inputs": "At what temperature does liquid Nitrogen boil?"}'localhost:8080/api/v1/task/text-generationWe get the following response:{"generated_text":"74 degrees F","generated_tokens":5,"finish_reason":"MAX_TOKENS","producer_id":{"name":"Text Generation","version":"0.1.0"},"input_token_count":10,"seed":null}All the available API endpoints and protos can be dumped usingscripts/dump_apis.sh.DockerTo build the docker image:python-mbuild--wheel
dockerbuild-tcaikit-nlp:latest.A volume can be mounted at/caikitproviding configuration and (optionally) models:mkdir-pcaikit$EDITORcaikit/config.yml# edit as requiredcp-r<path/to/models>./caikit/models
dockerrun-eCONFIG_FILES=/caikit/config.yml-v$PWD/caikit/:/caikit-p8080:8080-p8085:8085python-mcaikit.runtimeServing with containersIn order to start the serving runtime:dockerrun-eCONFIG_FILES=/caikit/config.yml\-v$PWD/caikit/:/caikit-p8080:8080-p8085\python-mcaikit.runtimeAssuming the standard configuration with port8080for the http server and8085for the grpc server.ConfigurationConfiguration can be provided via environment variables or by providing a yaml configuration file thanks toalchemy-config.For example, to set the caikit runtime, settingRUNTIME_LIBRARY=caikit_nlpvia environment variables or providing the following yaml configuration is equivalent.# config.ymlruntime:library:caikit_nlpFor configuration options seecaikit_nlp's example config:config.ymlorcaikit's examplecaikit.yml.ContributingWe welcome contributions from the community! If you would like to contribute tocaikit-nlp, please read the guidelines in the main project'sCONTRIBUTING.mdfile. It includes information on submitting bug reports, feature requests, and pull requests. Make sure to follow our coding standards,code of conduct,security standards, and documentation guidelines to streamline the contribution process.LicenseThis project is licensed under theASFv2 License.GlossaryA list of terms that either may be unfamiliar or that have nebulous definitions based on who and where you hear them, defined for how they are used/thought of in thecaikit/caikit-nlpproject:Fine tuning - trains the base model onto new data etc; this changes the base model.Prompt engineering - (usually) manually crafting texts that make models do a better job that's left appended to the input text. E.g., if you wanted to do something like sentiment on movie reviews, you might come up with a prompt like The movie was: _____ and replace the _____  with the movie review you're consider to try to get something like happy/sad out of it.PEFT - library by Huggingface containing implementations of different tuning methods that scale well - things like prompt tuning, and MPT live there. So PEFT itself isn't an approach even though parameter efficient fine-tuning sounds like one.
Prompt tuning - learning soft prompts. This is different from prompt engineering in that you're not trying to learn tokens. Instead, you're basically trying to learn new embedded representations (sometimes called virtual tokens) that can be concatenated onto your embedded input text to improve the performance. This can work well, but also can be sensitive to initialization.Multitask prompt tuning (MPT) - Tries to fix some of the issues with prompt tuning by allowing you to effectively learn 'source prompts' across different tasks & leverage them to initialize your prompt tuning etc. More information on MPT can be found at:https://arxiv.org/abs/2303.02861The important difference between fine tuning and capabilities like prompt tuning/multi-taskprompt tuning is that the latter doesn't change the base model's weights at all. So when you run inference for prompt tuned models, you can have n prompts to 1 base model, and just inject the prompt tensors you need when they're requested instead of havingnseparate fine-tuned models.Runtime Performance BenchmarkingRuntime Performance Benchmarkingfor tuning various models.NotesCurrently causal language models and sequence-to-sequence models are supported.