File size: 2,443 Bytes
a2348fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Minimal inference script for devneko/Inkling-2-Mini-JA (llama-server / OpenAI-compatible & Transformers/Unsloth).

1) llama.cpp (works on SM80+ GPUs, e.g. A100/RTX 3090/4090/H100):
   llama-server \
     -m gguf/Inkling-2-Mini-JA-Q8_0.gguf \
     --host 127.0.0.1 --port 8000 \
     -ngl 99 -fa on --jinja \
     --chat-template-kwargs '{"reasoning_effort": "low"}'

2) Then run this script:
   python inference.py
"""

from openai import OpenAI


def run_openai_compatible(
    prompt: str = "江戸時代の参勤交代制度の目的と影響を簡潔に説明してください。",
    base_url: str = "http://127.0.0.1:8000/v1",
    model: str = "devneko/Inkling-2-Mini-JA",
    reasoning_effort: str = "low",
) -> None:
    client = OpenAI(base_url=base_url, api_key="EMPTY")
    resp = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
        max_tokens=1024,
        temperature=0.6,
        top_p=0.95,
        extra_body={"chat_template_kwargs": {"reasoning_effort": reasoning_effort}},
    )
    msg = resp.choices[0].message
    reasoning = getattr(msg, "reasoning_content", None) or getattr(msg, "reasoning", "") or ""
    if reasoning:
        print("=== Reasoning ===")
        print(reasoning)
    print("=== Answer ===")
    print(msg.content)


def run_transformers_example(
    prompt: str = "日本の五節句をすべて挙げ、それぞれの日付と代表的な行事を簡潔に説明してください。",
    model_id: str = "devneko/Inkling-2-Mini-JA",
    reasoning_effort: str = "low",
) -> None:
    import torch
    from transformers import AutoModelForCausalLM, AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        model_id,
        torch_dtype=torch.bfloat16,
        device_map="auto",
        trust_remote_code=True,
    )
    text = tokenizer.apply_chat_template(
        [{"role": "user", "content": prompt}],
        tokenize=False,
        add_generation_prompt=True,
        reasoning_effort=reasoning_effort,
    )
    inputs = tokenizer(text, return_tensors="pt").to(model.device)
    out = model.generate(**inputs, max_new_tokens=1024, do_sample=True, temperature=0.6, top_p=0.95)
    print(tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False))


if __name__ == "__main__":
    run_openai_compatible()