File size: 3,629 Bytes
b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a b7e6c9d b73205a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
---
language: ko
license: apache-2.0
tags:
- function-calling
- korean
- hybridko
base_model: Yaongi/hybridko-exp6
datasets:
- heegyu/glaive-function-calling-v2-ko
---
# HybriKo-117M Function Calling
HybriKo-117M (checkpoint 1962) ๋ชจ๋ธ์ Function Calling ๋ฐ์ดํฐ๋ก ๋ฏธ์ธ์กฐ์ ํ ๋ชจ๋ธ์
๋๋ค.
## ํ์ต ์ ๋ณด
- **Base Model**: Yaongi/hybridko-exp6
- **Dataset**: heegyu/glaive-function-calling-v2-ko (5,000 samples)
- **Epochs**: 2
- **Final Loss**: ~0.14
- **Performance**: ๊ธฐ๋ณธ ํฌ๋งท ํ์ต ์๋ฃ (Calculation, Search, Weather ๋ฑ ์ง์)
## ์ฌ์ฉ๋ฒ (Colab)
```python
import torch
import torch.nn.functional as F
import sentencepiece as spm
from transformers import AutoModelForCausalLM
from huggingface_hub import hf_hub_download
# 1. ๋ชจ๋ธ ๋ก๋
print("๐ฅ Model loading...")
model = AutoModelForCausalLM.from_pretrained(
"Yaongi/HybriKo-117M-Exp6-FunctionCall",
trust_remote_code=True,
torch_dtype=torch.float32
)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()
# 2. ํ ํฌ๋์ด์ ๋ก๋
print("๐ฅ Tokenizer loading...")
sp_path = hf_hub_download("Yaongi/HybriKo-117M-Exp6-FunctionCall", "HybriKo_tok.model")
sp = spm.SentencePieceProcessor()
sp.Load(sp_path)
# 3. ์์ฑ ํจ์ (Stop Logic ํฌํจ)
def generate(text, max_len=200, temp=0.01, top_k=1):
input_ids = torch.tensor([[sp.bos_id()] + sp.EncodeAsIds(text)]).to(device)
# ์ค์ง ํ
์คํธ ๋ฆฌ์คํธ
stop_sequences = ["<|im_end|>", "</tool_code>"]
print("๐ค Generating...", end="", flush=True)
with torch.no_grad():
for _ in range(max_len):
outputs = model(input_ids[:, -512:])
logits = outputs.logits[:, -1] / temp
if top_k:
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
logits[logits < v[:, [-1]]] = float("-inf")
probs = F.softmax(logits, dim=-1)
next_token = torch.multinomial(probs, 1)
# EOS ํ ํฐ ์ฒดํฌ
if next_token.item() == sp.eos_id():
break
input_ids = torch.cat([input_ids, next_token], dim=1)
# ๐ก Stop Sequence ์ฒดํฌ (๋งค ์คํ
๋์ฝ๋ฉํ์ฌ ํ์ธ)
curr_text = sp.DecodeIds(input_ids[0].tolist())
# ํ๋กฌํํธ ์ดํ ์์ฑ๋ ๋ถ๋ถ๋ง ์๋ผ์ ํ์ธ
# (SentencePiece ํน์ฑ์ ์ ํํ ์ฌ๋ผ์ด์ฑ์ ์ํด ์ ์ฒด ๋์ฝ๋ฉ ํ ๋น๊ต๊ฐ ์์ )
gen_part = curr_text[len(text):] # ๊ทผ์ฌ์ ์ธ ๋ฐฉ๋ฒ
# ์ ํ๋๋ฅผ ์ํด full text์์ ๊ฒ์
should_stop = False
for seq in stop_sequences:
if seq in curr_text and not (seq in text): # ํ๋กฌํํธ์ ์ด๋ฏธ ์๋ ๊ฒฝ์ฐ๋ ์ ์ธ
# ๋ฐฉ๊ธ ์์ฑ๋ ๋ถ๋ถ์ ํ ํฐ์ด ์์ฑ๋์๋์ง ํ์ธ
should_stop = True
break
if should_stop:
break
return sp.DecodeIds(input_ids[0].tolist())
# 4. ์คํ ์์
prompt = '''<|im_start|>system
๋น์ ์ ๋๊ตฌ ํธ์ถ(function calling)์ด ๊ฐ๋ฅํ AI ์ด์์คํดํธ์
๋๋ค.
<tools>
{"name": "get_news_headlines", "parameters": {"country": "string"}}
</tools><|im_end|>
<|im_start|>user
ํ๊ตญ์ ์ต์ ๋ด์ค ์๋ ค์ค<|im_end|>
<|im_start|>assistant
'''
print("\nPrompt:")
print(prompt)
result = generate(prompt, max_len=200)
# ์ถ๋ ฅ ๊น๋ํ๊ฒ ์ ๋ฆฌ
print("\n" + "="*50)
print("Result:")
print(result)
print("="*50)
''' |