1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

# 25行のPythonで作るJev

1
Posted at

僕の名前はJev

猫も杓子もJevの話をしている。Jevがどうした、Jevがこうした。Twitterでは誰もがJevに夢中で、大規模言語モデルの次なるフロンティアだとか、AIの新しいパラダイムだとか言っている。正直、僕たちはそうは思わない。というわけで、25行のPythonで書いたJevをどうぞ。

モデルを読み込む。

# /// script
# requires-python = ">=3.12"
# dependencies = ["huggingface-hub", "llama-cpp-python", "numpy"]
# ///

import numpy
from llama_cpp import Llama

# 実際のところ、https://huggingface.co/models?library=gguf にあるGGUFモデルならどれでも使える

model = Llama.from_pretrained(
    repo_id="Qwen/Qwen3-0.6B-GGUF",
    filename="Qwen3-0.6B-Q8_0.gguf",
    n_ctx=512,
    logits_all=True,
    verbose=False,
)

プロンプトを読み込み、選択肢を定義する。

labels = ["A", "B", "C"]
choices = ["Legitimate", "Spam", "Phishing"]
email = "Payroll asks for your password on a non-company sign-in page."
options = "\n".join(
    f"{label}. {choice}" for label, choice in zip(labels, choices, strict=True)
)
prompt = f"""<|im_start|>system
Choose one option.<|im_end|>
<|im_start|>user
Email: {email}\n\n{options}<|im_end|>
<|im_start|>assistant
<think>\n\n</think>\n\n"""
model.eval(tokens=model.tokenize(text=prompt.encode(), add_bos=False, special=True))

ロジットをいい感じに確率へ変換する。

logits = model.scores[model.n_tokens - 1]
token_ids = [model.tokenize(text=label.encode(), add_bos=False)[0] for label in labels]
choice_logits = numpy.asarray([logits[token_id] for token_id in token_ids])
logprobs = choice_logits - numpy.logaddexp.reduce(choice_logits)
probabilities = numpy.exp(logprobs)

for name, scores in (
    ("Logits", choice_logits),
    ("Log probabilities", logprobs),
    ("Probabilities", probabilities),
):
    values = numpy.round(scores.astype(float), 3).tolist()
    print(f"{name}:", dict(zip(choices, values, strict=True)))

# Logits: {'Legitimate': 26.254, 'Spam': 27.262, 'Phishing': 29.614}
# Log probabilities: {'Legitimate': -3.482, 'Spam': -2.474, 'Phishing': -0.122}
# Probabilities: {'Legitimate': 0.031, 'Spam': 0.084, 'Phishing': 0.885}

はい。これがJevです。

いや、君はJevを分かってない!

うん、知ってる。

  • 僕たちはこれを「System One意思決定モデル」とは呼んでいない。
  • APIも呼び出していない。
  • 合成データを大量に作ったりもしていない。
  • 判断と確率をキャリブレーションするために、強化学習(Reinforcement Learning for Calibrated Decisions、RLCD)でモデルを訓練したりもしていない(どのみち常に正しいわけじゃないし)。

でも、そう。これがJevなんだ。

  • プロンプトと選択肢を受け取って、確率を出力する。つまり分類する。
  • 速い。
  • ローカルで動く。
  • データをどこにも送らない。

そして僕たちは、あなたのデータをどこにも送らないのが好きだ。ぜひNobodyWhoをチェックしてみて。

(注:これはパロディのブログ記事です。より優れた、より完全なJevのオープン実装については、OpenJev、openjev-sglang、DiffusionGemma上のOpenJevを参照してください。)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?