Skip to main content

Input and output processing for IBM Granite models

Project description

Granite IO Processing

Introduction

Granite IO Processing is a framework which enables you to transform how a user calls or infers an IBM Granite model and how the output from the model is returned to the user. In other words, the framework allows you to extend the functionality of calling the model.

Getting Started

Requirements

  • Python 3.10+

Installation

We recommend using a Python virtual environment with Python 3.10+. Here is how to setup a virtual environment using Python venv:

python3 -m venv granite_io_venv
source granite_io_venv/bin/activate

[!TIP] If you use pyenv, Conda Miniforge or other such tools for Python version management, create the virtual environment with that tool instead of venv. Otherwise, you may have issues with installed packages not being found as they are linked to your Python version management tool and not venv.

There are 2 ways to install the Granite IO Processor as follows:

From Release

To install from release (PyPi package):

python3 -m venv granite_io_venv
source granite_io_venv/bin/activate
pip install granite-io
python -m nltk.downloader punkt_tab

[!NOTE] granite-io uses NLTK Data Punkt Sentence Tokenizer for extracting contents when parsing output from a model. The command above shows how to install the required NLTK data. Check out Installing NLTK Data for more detailed instructions.

From Source

To install from source(GitHub Repository):

python3 -m venv granite_io_venv
source granite_io_venv/bin/activate
git clone https://github.com/ibm-granite/granite-io
cd granite-io
pip install -e .
python -m nltk.downloader punkt_tab

[!NOTE] granite-io uses NLTK Data Punkt Sentence Tokenizer for extracting contents when parsing output from a model. The command above shows how to install the required NLTK data. Check out Installing NLTK Data for more detailed instructions.

Framework Example

Sample code snippet showing how to use the framework:

from granite_io import make_backend, make_io_processor
from granite_io.types import ChatCompletionInputs, UserMessage

model_name = "granite3.2:8b"
io_processor = make_io_processor(
    model_name, backend=make_backend("openai", {"model_name": model_name})
)
messages=[
    UserMessage(
        content="What's the fastest way for a seller to visit all the cities in their region?",
    )
]

# Without Thinking
outputs = io_processor.create_chat_completion(ChatCompletionInputs(messages=messages))
print("------ WITHOUT THINKING ------")
print(outputs.results[0].next_message.content)

# With Thinking
outputs = io_processor.create_chat_completion(
    ChatCompletionInputs(messages=messages, thinking=True)
)
print("------ WITH THINKING ------")
print(">> Thoughts:")
print(outputs.results[0].next_message.reasoning_content)
print(">> Response:")
print(outputs.results[0].next_message.content)

[!IMPORTANT] To get started with the examples, make sure you have followed the Installation steps first. You will need additional packages to be able to run the OpenAI example. They can be installed by running pip install -e "granite-io[openai]". Replace package name granite-io with . if installing from source.

To be able to run the above code snippet, you will need an Ollama server running locally and IBM Granite 3.2 model cached (ollama pull granite3.2:8b).

Try It Out!

To help you get up and running as quickly as possible with the Granite IO Processing framework, check out the following resources which demonstrate further how to use the framework:

  1. Python script examples:

[!IMPORTANT] To get started with the examples, make sure you have followed the Installation steps first. You will need additional packages to be able to run the examples. They can be installed by running pip install -e "granite-io[openai]" and pip install -e "granite-io[litellm]. Replace package name granite-io with . if installing from source.

You will also need an Ollama server running locally and IBM Granite 3.2 model cached (ollama pull granite3.2:8b).

  1. Jupyter notebook tutorials:

[!IMPORTANT] To get started with the examples, make sure you have followed the Installation steps first. You will also need additional packages to be able to run the Jupyter notebook. They can be installed by running pip install -e "granite-io[transformers]" and pip install -e "granite-io[notebook]". Replace package name granite-io with . if installing from source. The notebooks can be then run with following command jupyter notebook <path_to_notebook>.

Architecture

For more information about architecture and design decisions, refer to docs/design.md.

Contributing

Check out our contributing guide to learn how to contribute.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

granite_io-0.4.0.tar.gz (975.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

granite_io-0.4.0-py3-none-any.whl (100.9 kB view details)

Uploaded Python 3

File details

Details for the file granite_io-0.4.0.tar.gz.

File metadata

  • Download URL: granite_io-0.4.0.tar.gz
  • Upload date:
  • Size: 975.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for granite_io-0.4.0.tar.gz
Algorithm Hash digest
SHA256 095e2299decaef25cd61db260725e3e2c7f0f31a9fb130a5cb78c19e9b831ae6
MD5 110fef238290b97914bc7cb96b5b0344
BLAKE2b-256 03e17d3d22d36ff34bdcd82a06e698293866ad7f4037e6d4e22e9e2f393becc3

See more details on using hashes here.

Provenance

The following attestation bundles were made for granite_io-0.4.0.tar.gz:

Publisher: pypi.yml on ibm-granite/granite-io

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file granite_io-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: granite_io-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 100.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for granite_io-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5916bd5b9617acb976cf37fe70661ddbe42dcd38b9c9e92eb53d43bf8127d622
MD5 2bb74627669160f54be2a6d65bb49a0a
BLAKE2b-256 7cba22945c46a0c52fd8b574f99e83e01ca745c0ce1ea84b6e8684d4d44ad4b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for granite_io-0.4.0-py3-none-any.whl:

Publisher: pypi.yml on ibm-granite/granite-io

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page