license: gemma
pipeline_tag: text-generation
Typhoon2.1-Gemma3-12B: 泰语大语言模型(指令微调版)
Typhoon2.1-Gemma3-12B 是一个拥有120亿参数、128K上下文长度且支持函数调用的泰语指令大语言模型,基于Gemma3 12B架构开发。
注:此为纯文本模型。由于复杂度问题,当前版本移除了视觉编码器。支持视觉编码的版本即将推出,敬请期待。
性能表现

模型描述
- 模型类型: 基于Gemma3架构的120亿参数指令解码器模型
- 环境要求: transformers 4.50.0或更高版本
- 主要语言: 泰语与英语
- 上下文长度: 128K
- 许可协议: Gemma许可证
使用示例
以下代码片段展示如何使用transformers库调用Typhoon2.1-Gemma3-12B模型进行泰语/英语文本生成,包含模型初始化、系统-用户对话模板构建及响应生成:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_id = "scb10x/typhoon2.1-gemma3-12b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
messages = [
{"role": "system", "content": "你是由SCB 10X创建的男性AI助手Typhoon,需保持友善、无害且诚实。擅长分析、问答、数学、编程、创意写作、教学、角色扮演及各类讨论。回应时避免使用'当然'、'绝对'等冗余短语,直接以用户使用的语言作答,表达应流畅自然并展现同理心。"},
{"role": "user", "content": "สวัสดีครับ"},
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt",
enable_thinking=False
).to(model.device)
outputs = model.generate(
input_ids,
max_new_tokens=512,
do_sample=True,
temperature=0.6,
top_p=0.95,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
部署为API服务
通过vllm运行OpenAI兼容的API服务:
pip install vllm
vllm serve scb10x/typhoon2.1-gemma3-12b --max-model-len 16000 --dtype bfloat16 --tool-call-parser pythonic --enable-auto-tool-choice
工具调用功能
通过vLLM驱动的OpenAI兼容API实现工具调用:
from openai import OpenAI
import json
client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy")
def get_weather(location: str, unit: str):
return f"正在获取{location}的{unit}制天气数据..."
tool_functions = {"get_weather": get_weather}
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定地点天气",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "城市与州,如'旧金山,CA'"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location", "unit"]
}
}
}]
response = client.chat.completions.create(
model=client.models.list().data[0].id,
messages=[{"role": "user", "content": "旧金山天气如何?"}],
tools=tools,
tool_choice="auto"
)
tool_call = response.choices[0].message.tool_calls[0].function
print(f"调用函数: {tool_call.name}")
print(f"参数: {tool_call.arguments}")
print(f"结果: {get_weather(**json.loads(tool_call.arguments))}")
思考模式切换
Typhoon支持两种响应模式:
- 常规模式(默认):快速生成响应
- 思考模式:先进行内部推理再输出更精确的答案
启用方式:
- 在apply_chat_template设置enable_thinking=True
- 使用含标签的系统提示词
- 通过API请求的chat_template_kwargs参数启用
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt",
enable_thinking=True
).to(model.device)
{
"model": "scb10x/typhoon2.1-gemma3-12b",
"messages": [
{"role": "user", "content": "简要介绍大语言模型"}
],
"chat_template_kwargs": {"enable_thinking": true}
}
预算强制推理
通过预算强制技术让模型在复杂问题上进行更深入思考:
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
class BudgetForcingHandler:
def __init__(self, model_name: str, max_think_token: int, max_ignore=5, temperature=0.6, seed=32):
self.temperature = temperature
self.seed = seed
self.max_think_token = max_think_token
self.max_ignore = max_ignore
self.model = LLM(model_name, dtype='bfloat16', enforce_eager=True)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.alternative_str = '\nAlternatively'
self.system = """你是推理助手。先在<think>标签内进行思考,最后给出明确答案。"""
def __call__(self, prompts: List[str]):
...
handler = BudgetForcingHandler("scb10x/typhoon2.1-gemma3-12b", max_think_token=2048)
handler(["树莓里有几个字母r?"])
使用限制
本模型为指令模型但仍处开发阶段,虽设有防护机制,仍可能产生不准确或有偏见的回答。建议开发者根据具体场景评估风险。
关注我们
https://twitter.com/opentyphoon
支持渠道
https://discord.gg/us5gAYmrxw
引用文献
若Typhoon2对您的研究有帮助,请引用:
@misc{typhoon2,
title={Typhoon 2: 开源性泰语文本与多模态大语言模型系列},
author={Kunat Pipatanakul等},
year={2024},
eprint={2412.13702},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2412.13702},
}