🚀 Phi-4
Phi-4是一系列强大的模型,涵盖了多种变体,如推理、多模态指令、迷你指令等版本,还支持ONNX格式。它基于合成数据和高质量公开网站数据构建,专注于推理密集型任务,支持128K token上下文长度,在多语言商业和研究领域有广泛应用。
🚀 快速开始
你可以通过以下链接尝试使用Phi-4模型:
✨ 主要特性
- 多语言支持:支持阿拉伯语、中文、捷克语等多种语言。
- 轻量级设计:适合内存/计算受限环境和低延迟场景。
- 强大推理能力:在数学和逻辑推理方面表现出色。
- 长上下文支持:支持128K token上下文长度。
📦 安装指南
使用vLLM进行推理
所需包
flash_attn==2.7.4.post1
torch==2.5.1
vllm>=0.7.3
示例代码
from vllm import LLM, SamplingParams
llm = LLM(model="microsoft/Phi-4-mini-instruct", trust_remote_code=True)
messages = [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
{"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
{"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
]
sampling_params = SamplingParams(
max_tokens=500,
temperature=0.0,
)
output = llm.chat(messages=messages, sampling_params=sampling_params)
print(output[0].outputs[0].text)
使用Transformers进行推理
所需包
flash_attn==2.7.4.post1
torch==2.5.1
transformers==4.49.0
accelerate==1.3.0
示例代码
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
torch.random.manual_seed(0)
model_path = "microsoft/Phi-4-mini-instruct"
model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="auto",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(model_path)
messages = [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
{"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
{"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
]
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
output = pipe(messages, **generation_args)
print(output[0]['generated_text'])
💻 使用示例
输入格式
聊天格式
<|system|>Insert System Message<|end|><|user|>Insert User Message<|end|><|assistant|>
工具启用的函数调用格式
<|system|>You are a helpful assistant with some tools.<|tool|>[{"name": "get_weather_updates", "description": "Fetches weather updates for a given city using the RapidAPI Weather API.", "parameters": {"city": {"description": "The name of the city for which to retrieve weather information.", "type": "str", "default": "London"}}}]<|/tool|><|end|><|user|>What is the weather like in Paris today?<|end|><|assistant|>
📚 详细文档
预期用途
主要用例
该模型适用于广泛的多语言商业和研究用途,可用于以下场景:
- 内存/计算受限的环境。
- 低延迟场景。
- 需要强大推理能力(特别是数学和逻辑推理)的通用AI系统和应用。
使用案例考虑因素
开发者在选择使用案例时,应考虑语言模型的常见局限性以及不同语言之间的性能差异,并在特定下游用例中使用之前评估和缓解准确性、安全性和公平性问题,特别是在高风险场景中。同时,开发者应遵守适用的法律法规。
发布说明
本次发布的Phi-4-mini-instruct基于Phi-3系列的用户反馈,采用了新架构、更大的词汇表和更好的训练后技术,在关键能力上有显著提升。建议用户在特定AI应用中进行测试。
模型质量
通过内部基准平台对Phi-4-mini-instruct模型与一组模型在各种基准测试中进行了比较,结果显示该模型在多语言理解和推理能力方面达到了与更大模型相似的水平,但在某些任务上仍受限于其规模。
负责任的AI考虑
开发者应应用负责任的AI最佳实践,包括评估和缓解与特定用例和文化、语言背景相关的风险。在部署模型时,应考虑模型在资源分配、高风险场景、错误信息、有害内容生成和滥用等方面的适用性。
训练
模型信息
属性 |
详情 |
模型类型 |
基于Transformer的轻量级开放模型 |
输入 |
文本,适合聊天格式的提示 |
上下文长度 |
128K tokens |
GPU |
512 A100-80G |
训练时间 |
21天 |
训练数据 |
5T tokens |
输出 |
生成的文本 |
训练日期 |
2024年11月至12月 |
状态 |
基于2024年6月截止的离线数据集训练的静态模型 |
支持语言 |
阿拉伯语、中文、捷克语、丹麦语、荷兰语、英语、芬兰语、法语、德语、希伯来语、匈牙利语、意大利语、日语、韩语、挪威语、波兰语、葡萄牙语、俄语、西班牙语、瑞典语、泰语、土耳其语、乌克兰语 |
发布日期 |
2025年2月 |
训练数据集
训练数据包括公开文档、合成数据和高质量聊天格式监督数据,经过去污染处理以提高模型的推理能力。
微调
提供了使用TRL和Accelerate模块进行多GPU监督微调的示例代码。
安全评估和红队测试
通过多种评估技术对Phi-4模型在多种语言和风险类别下产生不良输出的倾向进行了评估,结果表明安全训练后模型在拒绝输出不良内容和抵抗越狱技术方面有积极影响,但在某些情况下仍可能存在问题。
软件和硬件要求
软件
硬件
默认使用flash attention,需要特定类型的GPU硬件。在NVIDIA V100或更早一代的GPU上运行时,需要指定attn_implementation="eager"
。
许可证
该模型遵循MIT许可证。
商标
使用微软商标或标志需遵循微软商标和品牌指南。
附录A:基准测试方法
介绍了基准测试的方法和数据集,包括在某些情况下对系统消息、少样本设置和提示格式的调整,但保持少样本示例和提示格式不变。
🔧 技术细节
基准测试方法
在基准测试中,为确保不同模型之间的公平比较,通常不改变提示。但在某些情况下,如模型不遵守输出格式或少样本影响性能时,会尝试不同的系统消息、允许0样本测试或进行特定模型的提示映射。
基准测试数据集
模型在广泛的公共和内部基准测试中进行了评估,包括推理、语言理解、函数调用、世界知识、数学、代码、指令遵循、多语言、流行聚合数据集、多轮对话、单轮可信度评估和红队测试等方面。