scikit-ollama

Leverage the power of Scikit-LLM and the security of self-hosted LLMs for advanced NLP.

  • Use self-hosted LLMs for NLP tasks
  • Scikit-Learn compatible
  • Built on Scikit-LLM and Ollama
  • Open source, commercially usable - MIT License
  • Introduction

    Quick start

    Scikit-Ollama builds on Scikit-LLM and allows you to query large language models running through Ollama for advanced natural language processing.

    Let's see how to use llama3 for zero-shot text classification.


    Installation

    First of all, install scikit-ollama using the following command:

    pip install scikit-ollama
    

    Note

    Scikit-Ollama requires setting up Ollama to run on your machine. For more information, please refer to the Ollama setup section.


    Zero-Shot Text Classification

    If you have setup Ollama correctly and loaded a model (e.g. llama3), we are ready to perform zero-shot text classification. We can use scikit-llm's dataset module for showcasing:

    from skllm.datasets import get_classification_dataset
    
    # demo sentiment analysis dataset
    # labels: positive, negative, neutral
    X, y = get_classification_dataset()
    

    Scikit-Ollama uses the same syntax as Scikit-LLM and therefore scikit-learn:

    from skollama.models.ollama.classification.zero_shot import ZeroShotOllamaClassifier
    
    clf = ZeroShotOllamaClassifier(model="llama3")
    clf.fit(X,y)
    clf.predict(X)
    

    Scikit-Ollama uses the Client class from the Ollama Python SDK and queries the (by default) local server and return a list of labels.

    Like Scikit-LLM, Scikit-Ollama ensures that a valid label is returned. If this is not the case, a label will be selected randomly (label probabilities are proportional to label occurrences in the "training" set).

    The implemented approaches don't do actual training but rather so-called in-context-learning. Meaning that the additional information in the prompt is meant to guide the classification. Furthermore, since the "training" data is not strictly required, it can be fully omitted. The only thing that has to be provided is the list of candidate labels.

    from skollama.models.ollama.classification.zero_shot import ZeroShotOllamaClassifier
    
    clf = ZeroShotOllamaClassifier(model="llama3")
    clf.fit(None, ["positive", "negative", "neutral"])
    clf.predict(X)