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

模型描述
- 模型类型:基于Gemma3架构的40亿参数指令解码模型
- 环境要求:transformers 4.50.0或更高版本
- 主要语言:泰语与英语
- 许可协议:Gemma许可证
使用示例
以下代码片段展示如何使用transformers库调用Typhoon2.1-Gemma3-4B模型进行泰语/英语文本生成,包含模型初始化、系统-用户对话模板构建及响应生成:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_id = "scb10x/typhoon2.1-gemma3-4b"
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))
服务器部署
通过vllm运行OpenAI兼容API服务:
pip install vllm
vllm serve scb10x/typhoon2.1-gemma3-4b --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
- 使用特殊系统提示词要求模型进行推理
- 在vLLM API请求中添加chat_template_kwargs参数
{
"model": "scb10x/typhoon2.1-gemma3-4b",
"messages": [
{"role": "user", "content": "简要介绍大语言模型"}
],
"chat_template_kwargs": {"enable_thinking": true}
}
预算强制推理
该技术允许模型在回答复杂问题时消耗更多token进行深度推理:
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
class BudgetForcingHandler:
...
handler = BudgetForcingHandler("scb10x/typhoon2.1-gemma3-4b", 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},
}