transformers v0.0.7
[!warning] This crate is under active development. APIs may change as features are still being added, and things tweaked.
Transformers provides a simple, intuitive interface for Rust developers who want to work with Large Language Models locally, powered by the Candle crate. It offers an API inspired by Python's Transformers, tailored for Rust developers.
Supported Models & Pipelines
Text Generation:
Fill-Mask:
- ModernBERT
- Base
- Large
Sentiment Analysis:
- ModernBERT-multilingual-sentiment
- Base
- Large
Zero-Shot Classification:
- ModernBERT-zeroshot
- Base
- Large
Installation
cargo add transformers
Usage
At this point in development the only real way to interact with the models is through the given pipelines, I plan to eventually provide you with a simple interface to work with the models directly if you would like.
Inference will be quite slow at the moment, this is mostly due to not using the CUDA feature when compiling candle. I will be working on integrating this smoothly in future updates for much faster inference.
Some examples of how to use the existing pipelines:
Text Generation
There are two ways to generate text: by providing a simple prompt string, or by providing a list of messages for chat-like interactions.
Using prompt_completion
use ;
Using message_completion
with the Message
Type
For more conversational interactions, you can use the message_completion
method, which takes a vector of Message
structs.
The Message
struct represents a single message in a chat and has a role
(system, user, or assistant) and content
. You can create messages using:
Message::system(content: &str)
: For system prompts.Message::user(content: &str)
: For user prompts.Message::assistant(content: &str)
: For model responses.
use ;
use Message; // Import the Message type
Fill Mask (ModernBERT)
use ;
Sentiment Analysis (ModernBERT Finetune)
use ;
use Result;
Zero-Shot Classification (ModernBERT NLI Finetune)
use ;
use Result;
Future Plans
- Add more model families and sizes
- Support additional pipelines (summarization, classification)
- Improve performance and error handling
Credits
A special thanks to Diaconu Radu-Mihai for transferring the transformers
crate name on crates.io