Skip to main content

Unofficial python bindings for llm-rs. 🐍❤️🦀

Project description

llm-rs-python: Python Bindings for Rust's llm Library

PyPI PyPI - License Downloads

Welcome to llm-rs, an unofficial Python interface for the Rust-based llm library, made possible through PyO3. Our package combines the convenience of Python with the performance of Rust to offer an efficient tool for your machine learning projects. 🐍❤️🦀

With llm-rs, you can operate a variety of Large Language Models (LLMs) including LLama and GPT-NeoX directly on your CPU or GPU.

For a detailed overview of all the supported architectures, visit the llm project page.

Integrations:

Installation

Simply install it via pip: pip install llm-rs

Installation with GPU Acceleration Support

llm-rs incorporates support for various GPU-accelerated backends to facilitate enhanced inference times. To enable GPU-acceleration the use_gpu parameter of your SessionConfig must be set to True. We distribute prebuilt binaries for the following operating systems and graphics APIs:

MacOS (Using Metal)

For MacOS users, the Metal-supported version of llm-rs can be easily installed via pip:

pip install llm-rs-metal

Windows/Linux (Using CUDA for Nvidia GPUs)

Due to the significant file size, CUDA-supported packages cannot be directly uploaded to pip. To install them, download the appropriate *.whl file from the latest Release and install it using pip as follows:

pip install [wheelname].whl

Windows/Linux (Using OpenCL for All GPUs)

For universal GPU support on Windows and Linux, we offer an OpenCL-supported version. It can be installed via pip:

pip install llm-rs-opencl

Usage

Running local GGML models:

Models can be loaded via the AutoModel interface.

from llm_rs import AutoModel, KnownModels

#load the model
model = AutoModel.from_pretrained("path/to/model.bin",model_type=KnownModels.Llama)

#generate
print(model.generate("The meaning of life is"))

Streaming Text

Text can be yielded from a generator via the stream function:

from llm_rs import AutoModel, KnownModels

#load the model
model = AutoModel.from_pretrained("path/to/model.bin",model_type=KnownModels.Llama)

#generate
for token in model.stream("The meaning of life is"):
    print(token)

Running GGML models from the Hugging Face Hub

GGML converted models can be directly downloaded and run from the hub.

from llm_rs import AutoModel

model = AutoModel.from_pretrained("rustformers/mpt-7b-ggml",model_file="mpt-7b-q4_0-ggjt.bin")

If there are multiple models in a repo the model_file has to be specified. If you want to load repositories which were not created throught this library, you have to specify the model_type parameter as the metadata files needed to infer the architecture are missing.

Running Pytorch Transfomer models from the Hugging Face Hub

llm-rs supports automatic conversion of all supported transformer architectures on the Huggingface Hub.

To run covnersions additional dependencies are needed which can be installed via pip install llm-rs[convert].

The models can then be loaded and automatically converted via the from_pretrained function.

from llm_rs import AutoModel

model = AutoModel.from_pretrained("mosaicml/mpt-7b")

Convert Huggingface Hub Models

The following example shows how a Pythia model can be covnverted, quantized and run.

from llm_rs.convert import AutoConverter
from llm_rs import AutoModel, AutoQuantizer
import sys

#define the model which should be converted and an output directory
export_directory = "path/to/directory" 
base_model = "EleutherAI/pythia-410m"

#convert the model
converted_model = AutoConverter.convert(base_model, export_directory)

#quantize the model (this step is optional)
quantized_model = AutoQuantizer.quantize(converted_model)

#load the quantized model
model = AutoModel.load(quantized_model,verbose=True)

#generate text
def callback(text):
    print(text,end="")
    sys.stdout.flush()

model.generate("The meaning of life is",callback=callback)

🦜️🔗 LangChain Usage

Utilizing llm-rs-python through langchain requires additional dependencies. You can install these using pip install llm-rs[langchain]. Once installed, you gain access to the RustformersLLM model through the llm_rs.langchain module. This particular model offers features for text generation and embeddings.

Consider the example below, demonstrating a straightforward LLMchain implementation with MPT-Instruct:

from llm_rs.langchain import RustformersLLM
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

template="""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
Answer:"""

prompt = PromptTemplate(input_variables=["instruction"],template=template,)

llm = RustformersLLM(model_path_or_repo_id="rustformers/mpt-7b-ggml",model_file="mpt-7b-instruct-q5_1-ggjt.bin",callbacks=[StreamingStdOutCallbackHandler()])

chain = LLMChain(llm=llm, prompt=prompt)

chain.run("Write a short post congratulating rustformers on their new release of their langchain integration.")

🌾🔱 Haystack Usage

Utilizing llm-rs-python through haystack requires additional dependencies. You can install these using pip install llm-rs[haystack]. Once installed, you gain access to the RustformersInvocationLayer model through the llm_rs.haystack module. This particular model offers features for text generation.

Consider the example below, demonstrating a straightforward Haystack-Pipeline implementation with OpenLLama-3B:

from haystack.nodes import PromptNode, PromptModel
from llm_rs.haystack import RustformersInvocationLayer

model = PromptModel("rustformers/open-llama-ggml",
                    max_length=1024,
                    invocation_layer_class=RustformersInvocationLayer,
                    model_kwargs={"model_file":"open_llama_3b-q5_1-ggjt.bin"})

pn = PromptNode(
    model,
    max_length=1024
)

pn("Write me a short story about a lama riding a crab.",stream=True)

Documentation

For in-depth information on customizing the loading and generation processes, refer to our detailed documentation.

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

llm_rs-0.2.13.tar.gz (55.5 kB view details)

Uploaded Source

Built Distributions

llm_rs-0.2.13-cp37-abi3-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.7+ Windows x86-64

llm_rs-0.2.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

llm_rs-0.2.13-cp37-abi3-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

llm_rs-0.2.13-cp37-abi3-macosx_10_9_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.7+ macOS 10.9+ x86-64

File details

Details for the file llm_rs-0.2.13.tar.gz.

File metadata

  • Download URL: llm_rs-0.2.13.tar.gz
  • Upload date:
  • Size: 55.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.1.0

File hashes

Hashes for llm_rs-0.2.13.tar.gz
Algorithm Hash digest
SHA256 16ec8a8bf7f0695649eb68772e6864fba6fe94942522fe4f15d4b6664c8f872b
MD5 654322f90ecba15fdbaa946dec6e6fab
BLAKE2b-256 ffc137c285843107ce9388016c8837d28fadb580d17107782a7b51295b7a29aa

See more details on using hashes here.

File details

Details for the file llm_rs-0.2.13-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for llm_rs-0.2.13-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ba04e4642cf5f2209c68fb456e7351e42a0ab19449b8f6add3ca87b92d2e717f
MD5 466dd7d3229a2ebc8a9bcefe938ed9e8
BLAKE2b-256 7315887422c6e9c3016c04a4ec418c33a6600c26dd7c39f9c0f7e7eea4d606fb

See more details on using hashes here.

File details

Details for the file llm_rs-0.2.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for llm_rs-0.2.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 44a6592461d8cc10c71220cb6d8438c31a364c520754b8654c2292835408410a
MD5 2799387014369db8c5c343e18432e9db
BLAKE2b-256 9bc5e8fcf194b1661ef18c18d6980b21f5578430f377e1283494469c1adab38d

See more details on using hashes here.

File details

Details for the file llm_rs-0.2.13-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for llm_rs-0.2.13-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 71a65c4db65ce4a8a6b0de9bae198a2cddfc7168dbfaf67bd28c4e6230eae88b
MD5 5b090b1b8d1a6fe507a50014ce7e92b6
BLAKE2b-256 a8c764c7fc1788f7b7624eda7fed25621cf96cde3d6bbe4de07c6ca58f87b5a7

See more details on using hashes here.

File details

Details for the file llm_rs-0.2.13-cp37-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for llm_rs-0.2.13-cp37-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4dfbbce4341d887d9a1d32c2d0ef61d63329fef9989760f44d8608904dc84303
MD5 e471c492574e058a54c8194a47c75a85
BLAKE2b-256 8c68583550ebd3752543206c89391f9349a595eb96227d69676e568d7863ccc5

See more details on using hashes here.

Supported by

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