SkelterLabsInc/JaQuAD
Viewer • Updated • 35.7k • 370 • 12
How to use oshizo/qa-refine-japanese-gpt-1b with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="oshizo/qa-refine-japanese-gpt-1b") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("oshizo/qa-refine-japanese-gpt-1b")
model = AutoModelForCausalLM.from_pretrained("oshizo/qa-refine-japanese-gpt-1b", device_map="auto")How to use oshizo/qa-refine-japanese-gpt-1b with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "oshizo/qa-refine-japanese-gpt-1b"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "oshizo/qa-refine-japanese-gpt-1b",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/oshizo/qa-refine-japanese-gpt-1b
How to use oshizo/qa-refine-japanese-gpt-1b with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "oshizo/qa-refine-japanese-gpt-1b" \
--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": "oshizo/qa-refine-japanese-gpt-1b",
"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 "oshizo/qa-refine-japanese-gpt-1b" \
--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": "oshizo/qa-refine-japanese-gpt-1b",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use oshizo/qa-refine-japanese-gpt-1b with Docker Model Runner:
docker model run hf.co/oshizo/qa-refine-japanese-gpt-1b
このモデルはrinna/japanese-gpt-1bをベースモデルとして、 コンテキストからの抽出型QAと、解答を新たなコンテキストでリファインするための学習を行ったモデルです。
gpt-index(v0.2.5)で利用することを前提に学習をしており、通常のQAタスクで使用することは想定していません。
利用例はこのリポジトリを参照してください。 https://github.com/oshizo/gpt_index_japanese_trial
モデルは2種類のpromptテンプレートに対してQA応答するように訓練されています。
DEFAULT_PROMPT = """
文脈情報は以下です。
---
{context_str}
---
事前知識ではなく、文脈情報を参考に質問に答えてください。:{query_str}
"""
REFINE_PROMPT = """
質問は以下です。:{query_str}
すでに答えの候補があります。:{existing_answer}
必要な場合のみ、以下の文脈情報を使ってこの答えを改良することができます。
---
{context_msg}
---
この文脈情報により、元の答えを改良して質問に答えてください。
文脈情報が有用でない場合は元の答えをそのまま返してください。
"""
import torch
from transformers import T5Tokenizer, AutoModelForCausalLM
tokenizer = T5Tokenizer.from_pretrained("rinna/japanese-gpt-1b")
model = AutoModelForCausalLM.from_pretrained("oshizo/qa-refine-japanese-gpt-1b").to("cuda")
prompt = DEFAULT_PROMPT.format(
context_str="山路を登りながら、こう考えた。智に働けば角が立つ。情に棹させば流される。意地を通せば窮屈だ。とかくに人の世は住みにくい。住みにくさが高じると、安い所へ引き越したくなる。どこへ越しても住みにくいと悟った時、詩が生れて、画が出来る。",
query_str="意地を通すとどうなってしまう?"
)
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
n = len(token_ids[0])
with torch.no_grad():
output_ids = model.generate(
token_ids.to(model.device),
max_length=n+100,
min_length=n+2,
do_sample=False,
pad_token_id=tokenizer.pad_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id,
)
output = tokenizer.decode(output_ids.tolist()[0][n:])
output.replace("</s>", "")
# -> 窮屈
JGLUE/JSQuADとJaQuADを用いて、コンテキストからの抽出型QAと、解答を新たなコンテキストでリファインするための学習を行いました。
学習スクリプトについてはこのリポジトリを参照してください。 https://github.com/oshizo/gpt_index_japanese_trial
Google Colab Pro A100 で約3.5時間、9.9kステップ学習しました。