Instructions to use ibm-granite/granite-3.3-2b-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ibm-granite/granite-3.3-2b-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ibm-granite/granite-3.3-2b-instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.3-2b-instruct") model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-3.3-2b-instruct", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ibm-granite/granite-3.3-2b-instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ibm-granite/granite-3.3-2b-instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-3.3-2b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ibm-granite/granite-3.3-2b-instruct
- SGLang
How to use ibm-granite/granite-3.3-2b-instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "ibm-granite/granite-3.3-2b-instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-3.3-2b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "ibm-granite/granite-3.3-2b-instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-3.3-2b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use ibm-granite/granite-3.3-2b-instruct with Docker Model Runner:
docker model run hf.co/ibm-granite/granite-3.3-2b-instruct
Token Overflow Error
Hi,
I am currently building a Gen AI model which can summarize the uploaded documents or can do QA based on user intent. However, I am facing issue with the Summarization part. I am always getting "Token indices sequence length is longer than the specified maximum sequence length for this model". Please help in correcting my code. I am using "ibm-granite/granite-3.3-2b-instruct" for this model. Please help I am stuck for past few weeks in this only.
Also, pasting my get_summary function for your reference:
def get_summary(text: str) -> str:
splitter = RecursiveCharacterTextSplitter(chunk_size=3000, chunk_overlap=40)
chunks = splitter.split_text(text)
docs = [Document(page_content=c) for c in chunks]
print(f"π¦ Chunks: {len(chunks)}")
for i,c in enumerate(chunks,1):
print(f" Chunk {i}: ~{len(c.split())} words")
# 2. Prompts
map_prompt = PromptTemplate(
input_variables=["text"],
template="""
<|system|>
You are a concise assistant for banking docs.
<|user|>
Summarize in under 400 tokens, no hallucinations:
{text}
<|assistant|>
"""
)
combine_prompt = PromptTemplate(
input_variables=["text"],
template="""
<|system|>
You are a banking reports summarization expert.
<|user|>
Combine chunk summaries into one concise summary (β€800 tokens), strictly factual:
{text}
<|assistant|>
"""
)
# 3. Summarize
chain = load_summarize_chain(
llm=llm,
chain_type="map_reduce",
map_prompt=map_prompt,
combine_prompt=combine_prompt
)
return chain.invoke(docs)["output_text"]
Hi @AyushBhargav , thanks for exploring this use case with Granite! Based on your snippets, it looks like you may be running with LangChain. Can you post the full script you're using? My best guess is that when your llm is being loaded, there's a field that needs to be set to override the max sequence length to match the model's parameters.