🚀 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樣本測試或進行特定模型的提示映射。
基準測試數據集
模型在廣泛的公共和內部基準測試中進行了評估,包括推理、語言理解、函數調用、世界知識、數學、代碼、指令遵循、多語言、流行聚合數據集、多輪對話、單輪可信度評估和紅隊測試等方面。