Multiple Choice
Transformers
Safetensors
Korean
qwen3
text-generation
korean
classification
natural-language-inference
function-calling
tool-use
calibrated
Eval Results (legacy)
text-generation-inference
Instructions to use coderecode95/KJH-Judge-4B-ko with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use coderecode95/KJH-Judge-4B-ko with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("coderecode95/KJH-Judge-4B-ko") model = AutoModelForCausalLM.from_pretrained("coderecode95/KJH-Judge-4B-ko", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download judge.py from coderecode95/KJH-Judge-4B-ko: direct link, hf CLI and curl.
- Browser
- Download file 20 kB
-
https://huggingface.co/coderecode95/KJH-Judge-4B-ko/resolve/main/judge.py
- Command line
-
hf download hf://coderecode95/KJH-Judge-4B-ko/judge.py
-
curl -L -o judge.py https://huggingface.co/coderecode95/KJH-Judge-4B-ko/resolve/main/judge.py
20 kB
| """KJH-Judge-4B-ko ํ์ ๋์ฐ๋ฏธ โ ์ ํด์ง ๋ณด๊ธฐ ์ค ํ๋๋ฅผ ๊ณจ๋ผ ๋ณด์ ๋ ํ๋ฅ ๊ณผ ํจ๊ป ๋๋ ค์ค๋ค. | |
| from transformers import pipeline | |
| judge = pipeline(model="coderecode95/KJH-Judge-4B-ko", trust_remote_code=True, device=0, dtype="bfloat16") | |
| # ๋๋: from judge import Judge; judge = Judge() (์ด ํ์ผ์ ๋ด๋ ค๋ฐ์ ์ธ ๋) | |
| 1) JEV ํ์ โ ์ํฉ(state) ํ๋์ ์ด๋ฆ ๋ถ์ธ ์ง๋ฌธ ์ฌ๋ฌ ๊ฐ. ๋ณด๊ธฐ ID๋ ๋ง์๋๋ก ์ ํ๋ค. | |
| judge.decide( | |
| state={"๊ณ ๊ฐ ๋ฌธ์": "์ง๋๋ฌ ์๊ธ์ด ๋ ๋ฒ ๋น ์ ธ๋๊ฐ์ด์. ์ค๋ ์์ ์ฒ๋ฆฌํด ์ฃผ์ธ์."}, | |
| questions={ | |
| "๋ถ์": {"instructions": "๊ณ ๊ฐ ๋ฌธ์๋ฅผ ๋ด๋น ๋ถ์๋ก ๋ถ๋ฅํ๋ค.", | |
| "criteria": {"billing": "๊ฒฐ์ ยทํ๋ถ", "delivery": "๋ฐฐ์ก", "account": "๊ณ์ ยท๋ก๊ทธ์ธ"}}, | |
| "๊ธด๊ธ": {"instructions": "๊ณ ๊ฐ์ด ์ฆ์ ์ฒ๋ฆฌ๋ฅผ ์๊ตฌํ๋์ง ํ๋จํ๋ค.", | |
| "criteria": {"urgent": "์ฆ์ ์ฒ๋ฆฌ ์๊ตฌ", "normal": "์ผ๋ฐ ๋ฌธ์"}}, | |
| }, | |
| ) | |
| # โ {'๋ถ์': {'choice': 'billing', 'confidence': 0.98, 'probabilities': {...}}, | |
| # '๊ธด๊ธ': {'choice': 'urgent', 'confidence': 0.91, 'probabilities': {...}}} | |
| 2) ์์ฃผ ์ฐ๋ ํ์ โ ํ์ต ๋์ ๊ฐ์ ์ง์นจยท๋ณด๊ธฐ๋ฅผ ์๋์ผ๋ก ๋ฃ๋๋ค. | |
| judge.sentiment("๋ฐฐ์ก์ ๋๋ ธ์ง๋ง ๋ฌผ๊ฑด์ ์ ๋ง ๋ง์์ ๋ค์ด์") # โ choice: ๊ธ์ / ๋ถ์ | |
| judge.nli("๋ณด์ฆ๊ธ์ 30์ผ ์ด๋ด ๋ฐํํ๋ค.", "๋ณด์ฆ๊ธ์ 40์ผ ๋ค์ ๋ฐ๋๋ค.") # โ entailment / contradiction / neutral | |
| judge.next_action("ํ์ ๋ถ๋ฌ์ค", tools) # โ CALL:call_taxi / ASK / RESPOND | |
| 3) ํ ์ค๋ก โ judge(์ํฉ, ๋ณด๊ธฐ, instruction=โฆ). ๊ฒฐ๊ณผ ํ์์ ๋ชจ๋ ๊ฐ๋ค. | |
| judge("์ง๋๋ฌ ์๊ธ์ด ๋ ๋ฒ ๋น ์ ธ๋๊ฐ์ด์", ["๊ฒฐ์ ยทํ๋ถ", "๋ฐฐ์ก", "๊ธฐํ"], instruction="๊ณ ๊ฐ ๋ฌธ์๋ฅผ ๋ด๋น ๋ถ์๋ก ๋ถ๋ฅํ๋ค.") | |
| ๋์: ์๋ ํ์์ ํ๋กฌํํธ๋ฅผ ํ ๋ฒ ๊ณ์ฐํ๊ณ , ๋ค์ ํ ํฐ ์๋ฆฌ์์ ๋ณด๊ธฐ ๊ธฐํธ(A, B, โฆ) ํ ํฐ์ ์ ์๋ง ๊บผ๋ด | |
| calibration.json ์ ์จ๋๋ก ๋๋ softmax ํ๋ค. ๊ธ์ ์์ฑํ์ง ์์ผ๋ฏ๋ก ๋น ๋ฅด๋ค. | |
| <|im_start|>user | |
| {์ง์นจ} | |
| {ํญ๋ชฉ ์ด๋ฆ}: {๊ฐ} โ state ์ ํค๋ง๋ค ํ ์ค (๊ฐ์ด ๋ฌธ์์ด์ด ์๋๋ฉด JSON). ๋ฌธ์์ด๋ง ์ฃผ๋ฉด "text: {๋ฌธ์์ด}" | |
| ๋ณด๊ธฐ: | |
| A. {๋ณด๊ธฐ 1} | |
| B. {๋ณด๊ธฐ 2} | |
| ์ ๋ต ๋ณด๊ธฐ์ ๊ธฐํธ ํ๋๋ง ๋ตํ์ธ์.<|im_end|> | |
| <|im_start|>assistant | |
| """ | |
| import json | |
| import warnings | |
| from pathlib import Path | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| REPO = "coderecode95/KJH-Judge-4B-ko" | |
| HINT = "์ ๋ต ๋ณด๊ธฐ์ ๊ธฐํธ ํ๋๋ง ๋ตํ์ธ์." | |
| DEFAULT_INSTRUCTION = "์ฃผ์ด์ง ๋ด์ฉ์ ์ฝ๊ณ ๊ฐ์ฅ ์๋ง์ ๋ณด๊ธฐ๋ฅผ ๊ณ ๋ฅธ๋ค." | |
| # ๊ณผ์ ๋ณ ์ง์นจ โ ํ์ตยทKorean JEV Benchmark ์ ๊ฐ์ ๋ฌธ๊ตฌ (๋ฐ๊พธ๋ฉด ์ ํ๋๊ฐ ๋จ์ด์ง ์ ์๋ค) | |
| TASKS = { | |
| "sentiment": "๋ฌธ์ฅ์ ์ ๋ฐ์ ์ธ ๊ฐ์ ๊ทน์ฑ์ ํ๋จํ๋ค. ๋ถ์ ํํ๊ณผ ๋ฐ์ด๋ฅผ ๊ณ ๋ คํ๋ค.", | |
| "nli": ("์ ์ ๋ฅผ ์ฐธ์ด๋ผ๊ณ ํ ๋ ๊ฐ์ค์ ๋ถ๋ฅํ๋ค. entailment: ์ ์ ๋ก๋ถํฐ ๊ฐ์ค์ด ๋ฐ๋ผ์จ๋ค. contradiction: ์ ์ ์ ๊ฐ์ค์ด ์๋ฆฝํ ์ ์๋ค. neutral: ๋ ๋ค ์๋๋ค. " | |
| "์ ๋ณด๊ฐ ์๋ค๋ ์ด์ ๋ง์ผ๋ก contradiction์ ๊ณ ๋ฅด์ง ์๋๋ค. neutral์ ์ ์ ์ ๋ต์ด๋ฉฐ insufficient_context๊ฐ ์๋๋ค. ํต์์ ์ธ ๋ฌธ์ฅ ์๋ฏธ๋ก ํ๋จํ๋ฉฐ ํฌ๋ฐํ ๊ฐ์ ์ ๋ง๋ค์ด ๋ชจํธํ๋ค๊ณ ํ์ง ์๋๋ค. " | |
| "์ ์ ์ ์ด๊ฑฐ ๋์๊ณผ ๊ฐ์ค์ ๋ฒ์๋ฅผ ๋น ์ง์์ด ๋์กฐํ๋ค. ๊ฐ์ค์ ๋ง/๋ฐ์/์ค์ง/๋ชจ๋ ๊ฐ์ ํํ์ด ์ ์ ์ ๋ช ์๋ ๋ค๋ฅธ ๋์์ด๋ ์ฌ๋ก๋ฅผ ๋ฐฐ์ ํ๋์ง ํ์ธํ๋ค. " | |
| "์ ์ ์ ๋ฐฐํ ํํ์ด ์๋ค๋ ์ฌ์ค๋ง์ผ๋ก ์ค๋ฆฝ์ด๋ผ๊ณ ํ์ง ์๋๋ค. ํต์์ ์ธ ๋ฌธ๋งฅ์ด ๋ช ํํ๋ฉด ๋ค๋ฅธ ์์ธยท์ํฉ์ ์์๋ก ์ถ๊ฐํ์ง ์๋๋ค."), | |
| "topic": "ํ๊ตญ์ด ๋ด์ค ์ ๋ชฉ์ ์ค์ฌ ์ฃผ์ ๋ฅผ ๊ณ ๋ฅธ๋ค. ์ฌ๋ฌ ๋ถ์ผ๊ฐ ์ธ๊ธ๋์ด๋ ๊ธฐ์ฌ์ ์ฃผ๋ ์ฌ๊ฑด์ ์ฐ์ ํ๋ค.", | |
| "hate": ("APEACH ์ ๋ ผ๋ฌธ์ ์์ฑ ๊ธฐ์ค์ ๋ฐ๋ฅธ๋ค. ์ฑ๋ณยท์ฑ์ ์งํฅ/์ ์ฒด์ฑยท๋์ด/์ ๋ถ/๊ฒฝ๋ ฅยท๊ตญ์ /์ธ์ข ยท์ถ์ /๊ฑฐ์ฃผ์งยท์ธ๋ชจยท์ง๋ณ/์ฅ์ ยท์์ต๊ดยทํ๋ ฅ/์ ๊ณต ๊ด๋ จ ์ฐจ๋ณยท๋นํยท๊ณต๊ฒฉ์ ํฌํจํ๋ค. " | |
| "๋ณดํธ์ง๋จ ์ ์ฒด์ฑ์ ๋ช ์ํด์ผ ํ๋ค๋ ์ ํ์ ์๋ค. ๋ถํ์ํ๊ฑฐ๋ ๊ณต๊ฒฉ์ ์ธ ์ธ๋ชจ ํ๊ฐ, ์์ต๊ด ๊ฐ์/ํํ๋ ํด๋นํ๋ค. ๋น๊ณต๊ฒฉ์ ์ธ ์ฃผ์ ์ธ๊ธ์ด๋ ์ฐจ๋ณ์ ๋ํ ๋ฐ๋ ๋ฐ์ธ์ ๋นํ์ค๋ค."), | |
| "offensive": "BEEP ์ ์ง์นจ์ ๋ฐ๋ฅธ ๊ณต๊ฒฉ์ฑ ์์ค. hate ๋์์ ๋ณดํธ ์ง๋จ์ ํ์ ๋์ง ์๊ณ ๊ธฐ์ฌ ์ ์ธ๋ฌผยท์์ฑ์ ๋ฑ ๊ฐ์ธ๋ ํฌํจํ๋ค. ๋น์์ด ์กด์ฌ๋ง์ผ๋ก hate๊ฐ ๋์ง๋ ์๋๋ค. ๋ด์ค ์ ๋ชฉ์ ๋ฌธ๋งฅ์ผ๋ก ์ฌ์ฉํ๋ค.", | |
| "yes_no": "๋ฌธ๋จ์ ๊ทผ๊ฑฐํ์ฌ ์ง๋ฌธ์ ์/์๋์ค๋ก ๋ตํ๋ค.", | |
| "same_meaning": "๋ ๋ฌธ๋งฅ์์ ํ์๋ ๋จ์ด์ ์๋ฏธ๊ฐ ๊ฐ์์ง ํ๋จํ๋ค.", | |
| "similar": "๋ ๋ฌธ์ฅ์ ์๋ฏธ๊ฐ ๊ฐ์์ง ํ๋จํ๋ค. ํํ์ด ๋ฌ๋ผ๋ ์ ๋ฌํ๋ ๋ด์ฉ์ด ๊ฐ์ผ๋ฉด ๊ฐ์ ์๋ฏธ๋ค.", | |
| "reading": "์ง๋ฌธ์ ๊ทผ๊ฑฐํ์ฌ ์ง๋ฌธ์ ๋ต์ ๊ณ ๋ฅธ๋ค. ๋ถ์ ์ง๋ฌธ์ ์ฃผ์ํ๋ค.", | |
| "next_action": ("๋ค์ ํ ๋จ๊ณ์ ํ๋์ ์ ํํ๋ค. CALL:<name>: ์ ๊ณต๋ ๋๊ตฌ๋ก ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๊ณ ํ์ํ ํ์ ์ธ์๊ฐ ๋ํ์ ๋ชจ๋ ์๋ค. " | |
| "ASK: ๊ด๋ จ ๋๊ตฌ๋ ์์ผ๋ ํ์ ์ธ์๊ฐ ๋ถ์กฑํด ์ง๋ฌธ์ด ํ์ํ๋ค. RESPOND: ๋๊ตฌ๊ฐ ๋ถํ์ํ ์ค๋ช /์๋ฃ ์๋ด ๋๋ ์ ๊ณต ๋๊ตฌ๋ก ์ง์ํ์ง ์๋ ์์ฒญ์ ๋ํ ์๋ด. " | |
| "์ค์ ๋๊ตฌ ์คํยท์ธ์ ์์ฑ์ ํ์ง ์๋๋ค. ๊ณผ๊ฑฐ ๋๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ํ์ฉํ๊ณ ํ์ฌ ์ ๋ต์ด๋ ๋ฏธ๋ ํด์ ์ถ์ธกํ์ง ์๋๋ค. " | |
| "๋๊ตฌ schema๊ฐ ํ์ ๊ฐ์ ๊ธฐ๋ณธ๊ฐยท๋ฌธ๋งฅ ์ถ๋ก ยท๋ช ์นญ ์์ฑ์ ๋ช ์์ ์ผ๋ก ํ์ฉํ๋ฉด ๊ทธ ์ ์๋ฅผ ๋ฐ๋ฅธ๋ค. ๊ทผ๊ฑฐ ์๋ ๊ฐ์ ๋ง๋๋ ๊ฒ๊ณผ ๋๊ตฌ ์ ์๊ฐ ํ์ฉํ๋ ๊ฐ์ ๋์ถํ๋ ๊ฒ์ ๊ตฌ๋ณํ๋ค. " | |
| "ํ์ฌ๊น์ง์ ๋๊ตฌ ๊ฒฐ๊ณผ๋ก ์์ฒญ์ด ํด๊ฒฐ๋์ผ๋ฉด ๊ฒฐ๊ณผ ์๋ด๊ฐ ๋ค์ ํ๋์ด๋ฉฐ ๊ฐ์ ๋๊ตฌ๋ฅผ ๋ฐ๋ณต ํธ์ถํ์ง ์๋๋ค."), | |
| "policy_action": ("๋ช ์๋ ์ ์ฑ ๊ณผ ์์ธ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค์ ํ๋์ ๊ณ ๋ฅธ๋ค. ์์ธ๋ ์ฐ์ธ ์ ์ฉ ์กฐ๊ฑด์ ๋ง์กฑํ ๋๋ง ๊ธฐ๋ณธ ๊ท์น์ ๋ฎ์ด์ด๋ค. ๋ช ์๋์ง ์์ ์ค์ ๊ธ์ต ์ค๋ฌด ์๊ฑด์ ์ถ๊ฐํ์ง ์๋๋ค. " | |
| "ASK_CLARIFICATION๊ณผ ASK_CONFIRMATION์ ๊ตฌ๋ณํ๋ค. ๋จผ์ ๊ธฐ๋ณธ ๊ท์น์ ์ ์ฉ ์กฐ๊ฑด๊ณผ ์์ธ์ ์ ์ฉ ์กฐ๊ฑด์ ๊ฐ๊ฐ ํ์ธํ๋ค. " | |
| "์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ช ์์ ํ๋ ์ง์๋ฅผ ์ฐ์ ํ๋ฉฐ, ๊ณ ๊ฐ ๋ฌธ์ฅ์ด ์ง๋ฌธ ํํ๋ผ๋ ์ด์ ๋ก ์ด๋ฅผ ๋ฌด์ํ์ง ์๋๋ค. ์กฐ๊ฑด์ด ๋ง์ง ์๋ ์์ธ๋ ์ ์ฉํ์ง ์๋๋ค. " | |
| "์ ์ฑ ์ ๋ณ๋ ํ๋ ์ง์๊ฐ ์์ผ๋ฉด ์ฃผ์ด์ง ํ๋ ์ ์์ ์ค์ ์์ฒญ์ ๋ง์ถฐ ํ๋จํ๋ค. ์ ์ฑ ยท์ผ๋ฐ ์ฌ์ค ์ค๋ช ๊ณผ ์ธ๋ถ ๊ณ์ข ์ ๋ณด ์กฐํ๋ฅผ ๊ตฌ๋ณํ๋ค. " | |
| "๋ช ์๋ ํ๋์ ๊ณ ๋ฅด๋ ์ถ์ ๊ณผ์ ์ด๋ฏ๋ก ์ค์ ๋๊ตฌ ์คํค๋ง๋ ๊ณ์ข ์ธ์ฆ ์ ์ฐจ๋ฅผ ์ถ๊ฐ๋ก ์๊ตฌํ์ง ์๋๋ค. ๋จ, ์๋ณธ์ ์๋ ๊ธฐ๋ณธ ํ๋์ด๋ ์ ์ฑ ์ ์๋ก ๋ง๋ค์ง ์๋๋ค."), | |
| } | |
| # ๊ณผ์ ๋ณ ๋ณด๊ธฐ โ {๋๋ ค์ค ID: ๋ชจ๋ธ์๊ฒ ๋ณด์ฌ ์ค ๋ณด๊ธฐ ๋ฌธ๊ตฌ} | |
| CRITERIA = { | |
| "sentiment": {"๊ธ์ ": "๊ธ์ ์ ๊ฐ์ ", "๋ถ์ ": "๋ถ์ ์ ๊ฐ์ "}, | |
| "nli": {"entailment": "entailment", "contradiction": "contradiction", "neutral": "neutral"}, | |
| "topic": {k: k for k in ["์ฌํ", "IT๊ณผํ", "์ธ๊ณ", "์ํ๋ฌธํ", "์ ์น", "์คํฌ์ธ ", "๊ฒฝ์ "]}, | |
| "hate": {"๋นํ์ค": "๋นํ์ค: ๋นํยท๊ณต๊ฒฉ์ฑ์ด ์๋ ํํ", "ํ์ค": "ํ์คยท๋นํยท๊ณต๊ฒฉ์ ํํ"}, | |
| "offensive": {"์์": "ํ์คยท๋ชจ์ยท๋ถ์พ ํํ ์์", | |
| "๊ณต๊ฒฉ์ ": "๋ฌด๋กยท๊ณต๊ฒฉยท๋น๊ผผ ๋ฑ ๋ถ์พํ์ง๋ง ๊ฐํ ํ์ค/๋ชจ์ ์์ค์ ์๋", | |
| "ํ์ค": "๊ฐ์ธ/์ง๋จ์ ๋ํ ๊ฐํ ํ์ค ๋๋ ์ฌํ์ ์ง์๋ฅผ ์ฌํ๊ฒ ํผ์ํ๋ ๋ชจ์; ์ฑํฌ๋กฑ ํฌํจ"}, | |
| "yes_no": {"์": "์", "์๋์ค": "์๋์ค"}, | |
| "same_meaning": {"๊ฐ์": "๊ฐ์ ์๋ฏธ", "๋ค๋ฆ": "์๋ก ๋ค๋ฅธ ์๋ฏธ"}, | |
| "similar": {"๊ฐ์": "๊ฐ์ ์๋ฏธ", "๋ค๋ฆ": "๋ค๋ฅธ ์๋ฏธ"}, | |
| "policy_action": {"ASK_CLARIFICATION": "ASK_CLARIFICATION: ๋ถ์กฑํ ์ ๋ณด๋ฅผ ๊ณ ๊ฐ์๊ฒ ๋๋ฌป๋๋ค", | |
| "ASK_CONFIRMATION": "ASK_CONFIRMATION: ์คํ ์ ์ ๋ช ์์ ํ์ธยท๋์๋ฅผ ๋ฐ๋๋ค", | |
| "ANSWER": "ANSWER: ๋๊ตฌ ํธ์ถ์ด๋ ์คํ ์์ด ์ ์ฑ ยท์ฌ์ค ์ ๋ณด๋ก ๋ตํ๋ค", | |
| "CALL_TOOL": "CALL_TOOL: ๋๊ตฌ ํ๋๋ก ์คํํ๊ฑฐ๋ ์กฐํํ๋ค", | |
| "CALL_MULTIPLE_TOOLS": "CALL_MULTIPLE_TOOLS: ๋ ์ด์์ ๋๊ตฌ๋ฅผ ์ฐจ๋ก๋ก ํธ์ถํ๋ค", | |
| "ESCALATE": "ESCALATE: ์๋ ์ฒ๋ฆฌ๋ฅผ ๋ฉ์ถ๊ณ ์ฌ๋ ์๋ด์ฌ์๊ฒ ๋๊ธด๋ค", | |
| "REFUSE": "REFUSE: ์์ฒญ์ ์ดํํ์ง ์๊ณ ์ฌ์ ๋ฅผ ์๋ดํ๋ค"}, | |
| } | |
| ASK = "ASK: ๋ถ์กฑํ ํ์ ์ ๋ณด ์ง๋ฌธ" | |
| RESPOND = "RESPOND: ์ง์ ์ค๋ช ยท์๋ฃยท๋ฏธ์ง์ ์๋ด" | |
| def render_prompt(instruction, item, choices): | |
| if isinstance(item, str): | |
| item = {"text": item} | |
| lines = [f"{k}: {v if isinstance(v, str) else json.dumps(v, ensure_ascii=False)}" for k, v in item.items()] | |
| opts = "\n".join(f"{chr(65 + i)}. {c}" for i, c in enumerate(choices)) | |
| return "\n\n".join([(instruction or DEFAULT_INSTRUCTION).strip(), "\n".join(lines), "๋ณด๊ธฐ:\n" + opts, HINT]) | |
| def chat_prefix(prompt): | |
| return f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n" | |
| def check_choices(choices): | |
| choices = [c["text"] if isinstance(c, dict) else str(c) for c in choices] | |
| if not 2 <= len(choices) <= 26: | |
| raise ValueError(f"๋ณด๊ธฐ๋ 2~26๊ฐ์ฌ์ผ ํฉ๋๋ค (์ง๊ธ {len(choices)}๊ฐ)") | |
| if len(set(choices)) != len(choices): | |
| raise ValueError("๋ณด๊ธฐ ๋ฌธ๊ตฌ๊ฐ ๊ฒน์นฉ๋๋ค. ๋ณด๊ธฐ๋ง๋ค ๋ค๋ฅธ ๋ฌธ๊ตฌ๋ฅผ ์จ ์ฃผ์ธ์") | |
| return choices | |
| def load_temperature(model): | |
| p = Path(model) / "calibration.json" | |
| if not p.is_file(): | |
| try: | |
| from huggingface_hub import hf_hub_download | |
| p = Path(hf_hub_download(model, "calibration.json")) | |
| except Exception: | |
| warnings.warn(f"{model} ์์ calibration.json ์ ์ฐพ์ง ๋ชปํด ๋ณด์ ์์ด(์จ๋ 1.0) ํ๋ฅ ์ ๊ณ์ฐํฉ๋๋ค.") | |
| return 1.0 | |
| return float(json.loads(p.read_text(encoding="utf-8"))["temperature"]) | |
| def letter_ids(tokenizer): | |
| return [tokenizer.encode(chr(65 + i), add_special_tokens=False)[0] for i in range(26)] | |
| def as_result(ids, probs): | |
| """๋ณด๊ธฐ ์์์ ํ๋ฅ โ {'choice', 'confidence', 'probabilities'}""" | |
| b = max(range(len(probs)), key=probs.__getitem__) | |
| return {"choice": ids[b], "confidence": round(probs[b], 4), "probabilities": {i: round(p, 4) for i, p in zip(ids, probs)}} | |
| def _criteria(criteria): | |
| """{ID: ์ค๋ช } ๋๋ [์ค๋ช , โฆ] โ (ID ๋ชฉ๋ก, ์ค๋ช ๋ชฉ๋ก). ๋ฆฌ์คํธ๋ฉด ์ค๋ช ์ด ๊ณง ID.""" | |
| if isinstance(criteria, dict): | |
| return [str(k) for k in criteria], check_choices(list(criteria.values())) | |
| texts = check_choices(criteria) | |
| return texts, texts | |
| def _rows(**fields): | |
| """๊ฐ์ด ๋ชจ๋ ๋ฌธ์์ด์ด๋ฉด ํญ๋ชฉ ํ๋, ๋ชจ๋ ๊ฐ์ ๊ธธ์ด์ ๋ฆฌ์คํธ๋ฉด ํญ๋ชฉ ์ฌ๋ฌ ๊ฐ.""" | |
| vals = list(fields.values()) | |
| if all(isinstance(v, list) for v in vals): | |
| if len({len(v) for v in vals}) != 1: | |
| raise ValueError("๋ฆฌ์คํธ๋ก ์ฌ๋ฌ ๊ฐ๋ฅผ ๋ฃ์ ๋๋ ๊ธธ์ด๊ฐ ๊ฐ์์ผ ํฉ๋๋ค") | |
| return [dict(zip(fields, row)) for row in zip(*vals)], True | |
| return dict(fields), False | |
| def _tool_name(t): | |
| return (t.get("function") or t)["name"] if isinstance(t, dict) else str(t) | |
| class Tasks: | |
| """JEV ํ์ decide() ์ ์์ฃผ ์ฐ๋ ํ์ . self(์ํฉ, choices=โฆ, instruction=โฆ) ๋ง ์์ผ๋ฉด ๋๋ค (Judge ์ pipeline ์ด ํจ๊ป ์ด๋ค).""" | |
| def _choose(self, state, criteria, instructions, many=False): | |
| ids, texts = _criteria(criteria) | |
| res = self(state, choices=texts, instruction=instructions) | |
| def rename(r): | |
| return as_result(ids, [r["probabilities"][t] for t in texts]) | |
| return [rename(r) for r in res] if many else rename(res) | |
| def decide(self, state, questions): | |
| """JEV ํ์. state: ์ํฉ(dict ๋๋ ๋ฌธ์ฅ). questions: {์ง๋ฌธ ์ด๋ฆ: {"instructions": ์ง์นจ, "criteria": {ID: ์ค๋ช }}}. | |
| โ {์ง๋ฌธ ์ด๋ฆ: {'choice': ID, 'confidence': ํ๋ฅ , 'probabilities': {ID: ํ๋ฅ }}}""" | |
| out = {} | |
| for name, q in questions.items(): | |
| if q.get("type", "choice") != "choice": | |
| raise ValueError(f"'{name}': ์ง์ํ๋ type ์ 'choice' ๋ฟ์ ๋๋ค") | |
| out[name] = self._choose(state, q["criteria"], q.get("instructions")) | |
| return out | |
| def sentiment(self, text): | |
| """๊ฐ์ โ ๊ธ์ / ๋ถ์ . ๋ฌธ์ฅ ํ๋ ๋๋ ๋ฆฌ์คํธ.""" | |
| return self._choose(text, CRITERIA["sentiment"], TASKS["sentiment"], isinstance(text, list)) | |
| def topic(self, headline): | |
| """๋ด์ค ์ ๋ชฉ ์ฃผ์ โ ์ฌํ / IT๊ณผํ / ์ธ๊ณ / ์ํ๋ฌธํ / ์ ์น / ์คํฌ์ธ / ๊ฒฝ์ .""" | |
| rows, many = _rows(headline=headline) | |
| return self._choose(rows, CRITERIA["topic"], TASKS["topic"], many) | |
| def hate(self, text): | |
| """ํ์คยท๋นํ ํํ ์ฌ๋ถ โ ๋นํ์ค / ํ์ค.""" | |
| return self._choose(text, CRITERIA["hate"], TASKS["hate"], isinstance(text, list)) | |
| def offensive(self, comment, news_title=None): | |
| """๋๊ธ ๊ณต๊ฒฉ์ฑ 3๋จ๊ณ โ ์์ / ๊ณต๊ฒฉ์ / ํ์ค. ๋ด์ค ์ ๋ชฉ์ด ์์ผ๋ฉด ๋ฌธ๋งฅ์ผ๋ก ์ด๋ค.""" | |
| rows, many = _rows(news_title=news_title, comments=comment) if news_title is not None else _rows(comments=comment) | |
| return self._choose(rows, CRITERIA["offensive"], TASKS["offensive"], many) | |
| def nli(self, premise, hypothesis): | |
| """๋ฌธ์ฅ ๊ด๊ณ โ entailment / contradiction / neutral.""" | |
| rows, many = _rows(premise=premise, hypothesis=hypothesis) | |
| return self._choose(rows, CRITERIA["nli"], TASKS["nli"], many) | |
| def similar(self, sentence1, sentence2): | |
| """๋ ๋ฌธ์ฅ์ด ๊ฐ์ ๋ป์ธ์ง โ ๊ฐ์ / ๋ค๋ฆ.""" | |
| rows, many = _rows(sentence1=sentence1, sentence2=sentence2) | |
| return self._choose(rows, CRITERIA["similar"], TASKS["similar"], many) | |
| def yes_no(self, question, passage): | |
| """์ง๋ฌธ์ ๊ทผ๊ฑฐํ ์/์๋์ค โ ์ / ์๋์ค.""" | |
| rows, many = _rows(paragraph=passage, question=question) | |
| return self._choose(rows, CRITERIA["yes_no"], TASKS["yes_no"], many) | |
| def same_meaning(self, word, context_1, context_2): | |
| """๋ ๋ฌธ๋งฅ์์ ๋จ์ด ๋ป์ด ๊ฐ์์ง โ ๊ฐ์ / ๋ค๋ฆ. ๋ฌธ๋งฅ ์์ ๋จ์ด๋ [๋จ์ด]๋ก ํ์ํ๋ฉด ์ข๋ค.""" | |
| rows, many = _rows(word=word, context_1=context_1, context_2=context_2) | |
| return self._choose(rows, CRITERIA["same_meaning"], TASKS["same_meaning"], many) | |
| def reading(self, passage, question, choices): | |
| """์ง๋ฌธ ๋ ํด ๊ฐ๊ด์ โ ๋ณด๊ธฐ ์ค ํ๋ (๋ฆฌ์คํธ๋ฉด ๋ณด๊ธฐ ๋ฌธ๊ตฌ, dict ๋ฉด ID).""" | |
| return self._choose({"passage": passage, "question": question}, choices, TASKS["reading"]) | |
| def next_action(self, messages, tools): | |
| """์์ด์ ํธ์ ๋ค์ ํ๋ โ CALL:<๋๊ตฌ ์ด๋ฆ> / ASK (ํ์ ์ ๋ณด ๋ถ์กฑ) / RESPOND (์ง์ ๋ต๋ณ). | |
| messages: ์ฌ์ฉ์ ๋ฌธ์ฅ(str) ๋๋ [{"role", "content"}, โฆ]. tools: OpenAI ํ์ ๋๊ตฌ ๋ชฉ๋ก ๋๋ ์ด๋ฆ ๋ชฉ๋ก.""" | |
| if isinstance(messages, str): | |
| messages = [{"role": "user", "content": messages}] | |
| criteria = {f"CALL:{_tool_name(t)}": f"CALL:{_tool_name(t)}" for t in tools} | {"ASK": ASK, "RESPOND": RESPOND} | |
| return self._choose({"messages": messages, "tools": tools}, criteria, TASKS["next_action"]) | |
| def policy_action(self, policy, request): | |
| """์ ์ฑ ์ ๋ฐ๋ฅธ ํ๋ โ ASK_CLARIFICATION / ASK_CONFIRMATION / ANSWER / CALL_TOOL / CALL_MULTIPLE_TOOLS / ESCALATE / REFUSE.""" | |
| text = f"์ ์ฑ : {policy}\n\n๊ณ ๊ฐ ์์ฒญ: \"{request}\"\n์ ์ ์ฑ ์ ๋ฐ๋ผ ์ฌ๋ฐ๋ฅด๊ฒ ์ฒ๋ฆฌํ๋ผ." | |
| return self._choose({"policy_and_request": text}, CRITERIA["policy_action"], TASKS["policy_action"]) | |
| class Judge(Tasks): | |
| def __init__(self, model=REPO, device=None, dtype=None): | |
| device = device or ("cuda" if torch.cuda.is_available() else "cpu") | |
| dtype = dtype or (torch.bfloat16 if device != "cpu" else torch.float32) | |
| self.tok = AutoTokenizer.from_pretrained(model) | |
| self.model = AutoModelForCausalLM.from_pretrained(model, dtype=dtype).to(device).eval() | |
| self.device = device | |
| self.temperature = load_temperature(model) | |
| self.letter_ids = letter_ids(self.tok) | |
| def __call__(self, inputs, choices, instruction=None): | |
| """inputs: ๋ฌธ์ฅ(str) ยท ์ํฉ(dict) ยท ๊ทธ ๋ฆฌ์คํธ. choices: [์ค๋ช , โฆ] ๋๋ {ID: ์ค๋ช }. ๋ฆฌ์คํธ๋ฅผ ์ฃผ๋ฉด ๊ฒฐ๊ณผ๋ ๋ฆฌ์คํธ.""" | |
| many = isinstance(inputs, list) | |
| ids, texts = _criteria(choices) | |
| out = self.decide_batch([{"instruction": instruction, "item": x, "choices": texts} for x in (inputs if many else [inputs])]) | |
| res = [as_result(ids, [r["probs"][chr(65 + i)] for i in range(len(texts))]) for r in out] | |
| return res if many else res[0] | |
| def decide_batch(self, requests, batch_size=16, max_tokens=8000): | |
| """์ ์์ค ๋ฌถ์ ํ์ (ํ๊ฐ ์คํฌ๋ฆฝํธ์ฉ). | |
| requests: [{"instruction": str, "item": dict|str, "choices": [str, โฆ]}, โฆ] โ ๊ฐ์ ์์์ ๊ฒฐ๊ณผ ๋ชฉ๋ก. | |
| ๊ฒฐ๊ณผ: {'choice': 'A', 'index': 0, 'text': ๋ณด๊ธฐ, 'confidence': ํ๋ฅ , 'probs': {'A': โฆ, 'B': โฆ}} | |
| ๊ธธ์ด๊ฐ ๋น์ทํ ๊ฒ๋ผ๋ฆฌ ๋ฌถ์ด ๊ณ์ฐํ๋ค (์ค๋ฅธ์ชฝ์ ์ฑ์ฐ๊ณ ๊ฐ ๋ฌธ์ฅ์ ๋ง์ง๋ง ์๋ฆฌ๋ง ์ฝ์ผ๋ฏ๋ก ๊ฒฐ๊ณผ๋ ํ๋์ฉ ๊ณ์ฐํ ๊ฒ๊ณผ ๊ฐ๋ค).""" | |
| seqs = [] | |
| for r in requests: | |
| ch = check_choices(r["choices"]) | |
| ids = self.tok.encode(chat_prefix(render_prompt(r.get("instruction"), r["item"], ch)), add_special_tokens=False) | |
| seqs.append((ids, ch)) | |
| order = sorted(range(len(seqs)), key=lambda i: len(seqs[i][0])) | |
| out = [None] * len(seqs) | |
| W = self.model.get_output_embeddings().weight | |
| i = 0 | |
| while i < len(order): | |
| grp = [order[i]] | |
| while i + len(grp) < len(order) and len(grp) < batch_size and (len(grp) + 1) * len(seqs[order[i + len(grp)]][0]) <= max_tokens: | |
| grp.append(order[i + len(grp)]) | |
| i += len(grp) | |
| n = max(len(seqs[k][0]) for k in grp) | |
| x = torch.zeros(len(grp), n, dtype=torch.long) | |
| for r, k in enumerate(grp): | |
| x[r, :len(seqs[k][0])] = torch.tensor(seqs[k][0]) | |
| h = self.model.model(input_ids=x.to(self.device)).last_hidden_state # ์ต์ข ์ ๊ทํ๊น์ง ๋ง์น ์๋ ์ํ | |
| for r, k in enumerate(grp): | |
| ids, ch = seqs[k] | |
| rows = torch.tensor(self.letter_ids[:len(ch)], device=self.device) | |
| p = torch.softmax((h[r, len(ids) - 1].float() @ W[rows].float().T) / self.temperature, -1).tolist() | |
| b = max(range(len(p)), key=p.__getitem__) | |
| out[k] = {"choice": chr(65 + b), "index": b, "text": ch[b], "confidence": round(p[b], 4), | |
| "probs": {chr(65 + j): round(v, 4) for j, v in enumerate(p)}} | |
| return out | |
| if __name__ == "__main__": | |
| import sys | |
| j = Judge(sys.argv[1] if len(sys.argv) > 1 else ".") | |
| print(j.sentiment("๋ฐฐ์ก์ ๋๋ ธ์ง๋ง ๋ฌผ๊ฑด์ ์ ๋ง ๋ง์์ ๋ค์ด์")) | |