Anthropic/hh-rlhf
Viewer • Updated • 169k • 38.3k • 1.74k
How to use RaushanTurganbay/GPT2_instruct_tuned with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="RaushanTurganbay/GPT2_instruct_tuned") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned")
model = AutoModelForCausalLM.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned")How to use RaushanTurganbay/GPT2_instruct_tuned with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "RaushanTurganbay/GPT2_instruct_tuned"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "RaushanTurganbay/GPT2_instruct_tuned",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/RaushanTurganbay/GPT2_instruct_tuned
How to use RaushanTurganbay/GPT2_instruct_tuned with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "RaushanTurganbay/GPT2_instruct_tuned" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "RaushanTurganbay/GPT2_instruct_tuned",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'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 "RaushanTurganbay/GPT2_instruct_tuned" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "RaushanTurganbay/GPT2_instruct_tuned",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use RaushanTurganbay/GPT2_instruct_tuned with Docker Model Runner:
docker model run hf.co/RaushanTurganbay/GPT2_instruct_tuned
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 "RaushanTurganbay/GPT2_instruct_tuned" \
--host 0.0.0.0 \
--port 30000# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "RaushanTurganbay/GPT2_instruct_tuned",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'This repository houses a GPT-2 Medium model fine-tuned on the Anthropic-hh dataset. The fine-tuning process involved masking Human's utterances, with the loss computed exclusively on the Assistant's responses.
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load tokenizer and model
tokenizer = GPT2Tokenizer.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned")
model = GPT2LMHeadModel.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned")
# Generate responses
class StoppingCriteriaSub(StoppingCriteria):
def __init__(self, stops=[], encounters=1):
super().__init__()
self.stops = [stop.to("cuda") for stop in stops]
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor):
for stop in self.stops:
if torch.all((stop == input_ids[0][-len(stop):])).item():
return True
return False
def stopping_criteria(tokenizer, stop_words):
stop_words_ids = [tokenizer(stop_word, return_tensors='pt')['input_ids'].squeeze() for stop_word in stop_words]
stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])
return stopping_criteria
# Generate responses
stopping = stopping_criteria(tokenizer, ["\n\nHuman:"])
prompt = "\n\nHuman: {your_instruction}\n\nAssistant:"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, stopping_criteria=stopping, max_length=150)
print("Model Response:", tokenizer.batch_decode(outputs))
Install from pip and serve model
# Install SGLang from pip: pip install sglang# Start the SGLang server: python3 -m sglang.launch_server \ --model-path "RaushanTurganbay/GPT2_instruct_tuned" \ --host 0.0.0.0 \ --port 30000# Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "RaushanTurganbay/GPT2_instruct_tuned", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'