Created
May 4, 2025 02:22
-
-
Save hotchpotch/8b9c9c43e6aacc14b4b47801de063d64 to your computer and use it in GitHub Desktop.
query-crafter-japanese-example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
query-crafter-japanese のサンプルコード。 | |
実際に大量に処理するときは、vllm などを利用することで、高速処理が可能 | |
""" | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
model_name = "hotchpotch/query-crafter-japanese-Qwen3-1.7B" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained( | |
model_name, torch_dtype="auto", device_map="auto" | |
) | |
text = """ | |
夕方、開発合宿の成果発表会。私以外は、AI関連のちゃんとしたテーマに取り組んで、クオリティも高く、いやー面白い。I氏はエンジニアでもないのに、Figmaプラグインを作ったり、vercelにデプロイしてたり(ほぼcursorが書いた)して、AIによって大きく幅が広がる一例を間近に見る。私は何かのテーマに取り組んだわけではなく、Vibe Cording を一度もしたことがなかったので、cursor でコードをいかに触らず・見ずに作れるかを試した。 | |
毎年のこの日記を要約してdiscordなどに投稿するツール(以前も作ったものの仕様を書いて新機能などを追加)を作成したり、この日記のタイトルがないものに自動でタイトルをつけたりするツールを作成する。Vibe Cording は思った通りの感じで、なるほど便利。 | |
コードは見ずにブラックボックス的な開発(出力成果物だけをみる)をしたので、出来上がったコードを後で見ると本番運用前提のコードでは全くないが、書き殴りのツールを作るには十分。また自分が指示するのは仕様のみで、仕様書も随時アップデートされるようにしてるので、機能を変えたくなったら仕様変更・追加するだけでいいし、楽で良いね。 | |
""" | |
instructions = [ | |
"keywords", | |
"synonym_keywords", | |
"query", | |
"alt_query", | |
"title", | |
"faq", | |
"summary", | |
] | |
target_texts = [] | |
for instruction in instructions: | |
messages = [ | |
{"role": "system", "content": instruction}, | |
{"role": "user", "content": text}, | |
] | |
target_text = tokenizer.apply_chat_template( | |
messages, | |
tokenize=False, | |
add_generation_prompt=True, | |
enable_thinking=False, | |
) | |
target_texts.append(target_text) | |
model_inputs = tokenizer.batch_encode_plus( | |
target_texts, padding=True, truncation=True, return_tensors="pt" | |
).to(model.device) | |
generated_ids = model.generate(**model_inputs, max_new_tokens=256) | |
for i, instruction in enumerate(instructions): | |
output_ids = generated_ids[i][len(model_inputs.input_ids[i]) :].tolist() | |
generated_text = tokenizer.decode(output_ids, skip_special_tokens=True) | |
print(f"{instruction}: {generated_text}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment