Instructions to use wealthcoders/gpt-oss-20b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use wealthcoders/gpt-oss-20b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="wealthcoders/gpt-oss-20b") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("wealthcoders/gpt-oss-20b") model = AutoModelForCausalLM.from_pretrained("wealthcoders/gpt-oss-20b", 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=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use wealthcoders/gpt-oss-20b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "wealthcoders/gpt-oss-20b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "wealthcoders/gpt-oss-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/wealthcoders/gpt-oss-20b
- SGLang
How to use wealthcoders/gpt-oss-20b 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 "wealthcoders/gpt-oss-20b" \ --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": "wealthcoders/gpt-oss-20b", "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 "wealthcoders/gpt-oss-20b" \ --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": "wealthcoders/gpt-oss-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use wealthcoders/gpt-oss-20b with Docker Model Runner:
docker model run hf.co/wealthcoders/gpt-oss-20b
Download handler.py from wealthcoders/gpt-oss-20b: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/wealthcoders/gpt-oss-20b/resolve/main/handler.py
- Command line
-
hf download hf://wealthcoders/gpt-oss-20b/handler.py
-
curl -L -o handler.py https://huggingface.co/wealthcoders/gpt-oss-20b/resolve/main/handler.py
4.53 kB
| from typing import Dict, List, Any | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer | |
| from fastapi.responses import StreamingResponse | |
| import uuid | |
| import time | |
| import json | |
| from threading import Thread | |
| class EndpointHandler: | |
| def __init__(self, path: str = "openai/gpt-oss-20b"): | |
| # Load tokenizer and model | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| self.model = AutoModelForCausalLM.from_pretrained(path) | |
| self.model.eval() | |
| # Determine the computation device | |
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| self.model.to(self.device) | |
| def openai_id(prefix: str) -> str: | |
| return f"{prefix}-{uuid.uuid4().hex[:24]}" | |
| def format_non_stream(self, model: str, text: str, prompt_length: int, completion_length: int, total_tokens: int): | |
| # Create OpenAI-compatible payload | |
| return { | |
| "id": self.openai_id("chatcmpl"), | |
| "object": "chat.completion", | |
| "created": int(time.time()), | |
| "model": model, | |
| "choices": [{ | |
| "index": 0, | |
| "message": {"role": "assistant", "content": text}, | |
| "finish_reason": "stop" | |
| }], | |
| "usage": { | |
| "prompt_tokens": prompt_length, | |
| "completion_tokens": completion_length, | |
| "total_tokens": total_tokens | |
| } | |
| } | |
| def format_stream(self, model: str, token: str, usage) -> bytes: | |
| payload = { | |
| "id": self.openai_id("chatcmpl"), | |
| "object": "chat.completion.chunk", | |
| "created": int(time.time()), | |
| "model": model, | |
| "choices": [{ | |
| "index": 0, | |
| "delta": { | |
| "content": token, | |
| "function_call": None, | |
| "refusal": None, | |
| "role": None, | |
| "tool_calls": None | |
| }, | |
| "finish_reason": None, | |
| "logprobs": None | |
| }], | |
| "usage": usage | |
| } | |
| return f"data: {json.dumps(payload)}\n\n".encode('utf-8') | |
| def generate(self, messages, model: str): | |
| model_inputs = self.tokenizer(messages, return_tensors="pt").to(self.device) | |
| full_output = self.model.generate(**model_inputs, max_new_tokens=2048) | |
| generated_ids = [ | |
| output_ids[len(input_ids):] | |
| for input_ids, output_ids in zip(model_inputs.input_ids, full_output) | |
| ] | |
| text = self.tokenizer.batch_decode(generated_ids, skip_special_tokens=False)[0] | |
| input_length = model_inputs.input_ids.shape[1] # Prompt tokens | |
| output_length = full_output.shape[1] # Total tokens (prompt + completion) | |
| completion_tokens = output_length - input_length | |
| return self.format_non_stream(model, text, input_length, completion_tokens, output_length) | |
| def stream(self, messages, model): | |
| model_inputs = self.tokenizer(messages, return_tensors="pt").to(self.device) | |
| input_len = model_inputs.input_ids.shape[1] | |
| streamer = TextIteratorStreamer( | |
| self.tokenizer, | |
| skip_prompt=True, | |
| skip_special_tokens=True | |
| ) | |
| generation_kwargs = dict( | |
| **model_inputs, | |
| streamer=streamer, | |
| max_new_tokens=2048 | |
| ) | |
| thread = Thread(target=self.model.generate, kwargs=generation_kwargs) | |
| thread.start() | |
| completion_tokens = 0 | |
| for token in streamer: | |
| # Count tokens in each chunk | |
| token_ids = self.tokenizer.encode(token, add_special_tokens=False) | |
| token_count = len(token_ids) | |
| completion_tokens += token_count | |
| yield self.format_stream(model, token, None) | |
| # Final chunk with stop reason and token counts | |
| yield self.format_stream(model, "", { | |
| "prompt_tokens": input_len, | |
| "completion_tokens": completion_tokens, | |
| "total_tokens": input_len + completion_tokens | |
| }) | |
| def __call__(self, data: Dict[str, Any]): | |
| messages = data.get("messages") | |
| model = data.get("model") | |
| stream = data.get("stream", False) | |
| if stream is False: | |
| return self.generate(messages, model) | |
| else: | |
| return StreamingResponse( | |
| self.stream(messages, model), | |
| media_type="text/event-stream" | |
| ) |