模型概述
模型特點
模型能力
使用案例
🚀 xLAM模型家族
大動作模型(LAMs)是先進的大語言模型,旨在增強決策能力,並將用戶意圖轉化為與外界交互的可執行動作。LAMs能自主規劃和執行任務以實現特定目標,可作為AI智能體的核心。它們有潛力自動化跨領域的工作流程,在廣泛的應用場景中發揮重要作用。
🚀 快速開始
歡迎來到xLAM模型家族!大動作模型(LAMs)是先進的大語言模型,用於增強決策能力並將用戶意圖轉化為可執行動作,與外界進行交互。LAMs可自主規劃和執行任務以實現特定目標,是AI智能體的核心。它們有潛力自動化跨領域的工作流程,適用於廣泛的應用場景。
✨ 主要特性
- 多尺寸模型系列:提供不同大小的xLAM模型,包括針對函數調用和通用智能體應用優化的模型。
- 函數調用優化:
fc
系列模型針對函數調用能力進行了優化,能根據輸入查詢和可用API提供快速、準確和結構化的響應。 - 高效部署:提供量化的GGUF文件,便於在資源有限的本地設備上進行高效部署和執行,支持離線功能並增強隱私保護。
- 優秀的基準測試結果:在伯克利函數調用排行榜(BFCL)上表現出色,
xLAM-7b-fc-r
獲得第3名,xLAM-1b-fc-r
作為小於2B參數的小模型也取得了有競爭力的成績。
📦 安裝指南
使用Huggingface的基本安裝
要使用Huggingface上的xLAM-1b-fc-r
模型,請先安裝transformers
庫:
pip install transformers>=4.41.0
使用vLLM的安裝
要使用vllm
部署模型並運行推理,首先安裝所需的包:
pip install vllm openai argparse jinja2
💻 使用示例
基礎用法(使用Huggingface)
以下示例展示瞭如何使用我們的模型執行函數調用任務。強烈建議使用我們提供的提示格式和輔助函數,以獲得最佳的函數調用性能。
import json
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
torch.random.manual_seed(0)
model_name = "Salesforce/xLAM-1b-fc-r"
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 請使用我們提供的指令提示以獲得最佳性能
task_instruction = """
You are an expert in composing functions. You are given a question and a set of possible functions.
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
If none of the functions can be used, point it out and refuse to answer.
If the given question lacks the parameters required by the function, also point it out.
""".strip()
format_instruction = """
The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make tool_calls an empty list '[]'.
{ "tool_calls": [ {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}}, ... (more tool calls as required) ] }
""".strip()
# 定義輸入查詢和可用工具
query = "What's the weather like in New York in fahrenheit?"
get_weather_api = {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, New York"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature to return"
}
},
"required": ["location"]
}
}
search_api = {
"name": "search",
"description": "Search for information on the internet",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query, e.g. 'latest news on AI'"
}
},
"required": ["query"]
}
}
openai_format_tools = [get_weather_api, search_api]
# 輔助函數,將OpenAI格式的工具轉換為更簡潔的xLAM格式
def convert_to_xlam_tool(tools):
''''''
if isinstance(tools, dict):
return {
"name": tools["name"],
"description": tools["description"],
"parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
}
elif isinstance(tools, list):
return [convert_to_xlam_tool(tool) for tool in tools]
else:
return tools
# 輔助函數,為我們的模型構建輸入提示
def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(xlam_format_tools)}\n[END OF AVAILABLE TOOLS]\n\n"
prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
return prompt
# 構建輸入並開始推理
xlam_format_tools = convert_to_xlam_tool(openai_format_tools)
content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)
messages=[
{ 'role': 'user', 'content': content}
]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
# tokenizer.eos_token_id 是 <|EOT|> 標記的ID
outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))
運行上述代碼後,你應該能看到以下JSON格式的輸出字符串:
{"tool_calls": [{"name": "get_weather", "arguments": {"location": "New York", "unit": "fahrenheit"}}]}
高級用法(使用vLLM)
我們提供了示例腳本,用於使用vllm
部署模型並運行推理。示例腳本位於examples文件夾中。
1. 測試提示模板
要使用聊天模板構建提示並輸出適用於各種測試用例的格式化提示,請運行:
python test_prompt_template.py --model
2. 使用手動服務的端點測試xLAM模型
a. 使用vLLM服務模型:
python -m vllm.entrypoints.openai.api_server --model Salesforce/xLAM-1b-fc-r --served-model-name xlam-1b-fc-r --dtype bfloat16 --port 8001
b. 運行測試腳本:
python test_xlam_model_with_endpoint.py --model_name xlam-1b-fc-r --port 8001 [OPTIONS]
選項:
--temperature
:默認值為0.3--top_p
:默認值為1.0--max_tokens
:默認值為512
3. 直接使用vLLM庫測試xLAM模型
要直接使用vLLM庫測試xLAM模型,請運行:
python test_xlam_model_with_vllm.py --model Salesforce/xLAM-1b-fc-r [OPTIONS]
選項與端點測試相同。此測試腳本還提供了一個處理程序實現,可輕鬆應用於自定義的函數調用應用程序。
📚 詳細文檔
模型系列
我們提供了一系列不同大小的xLAM模型,以滿足各種應用需求,包括針對函數調用和通用智能體應用優化的模型:
模型名稱 | 總參數數量 | 上下文長度 | 發佈日期 | 類別 | 下載模型 | 下載GGUF文件 |
---|---|---|---|---|---|---|
xLAM-7b-r | 7.24B | 32k | 2024年9月5日 | 通用,函數調用 | 🤗 鏈接 | -- |
xLAM-8x7b-r | 46.7B | 32k | 2024年9月5日 | 通用,函數調用 | 🤗 鏈接 | -- |
xLAM-8x22b-r | 141B | 64k | 2024年9月5日 | 通用,函數調用 | 🤗 鏈接 | -- |
xLAM-1b-fc-r | 1.35B | 16k | 2024年7月17日 | 函數調用 | 🤗 鏈接 | 🤗 鏈接 |
xLAM-7b-fc-r | 6.91B | 4k | 2024年7月17日 | 函數調用 | 🤗 鏈接 | 🤗 鏈接 |
xLAM-v0.1-r | 46.7B | 32k | 2024年3月18日 | 通用,函數調用 | 🤗 鏈接 | -- |
fc
系列模型針對函數調用能力進行了優化,能根據輸入查詢和可用API提供快速、準確和結構化的響應。這些模型基於deepseek-coder模型進行微調,並且足夠小,可以部署在手機或電腦等個人設備上。
我們還提供了量化的GGUF文件,用於高效部署和執行。GGUF是一種用於高效存儲和加載大語言模型的文件格式,非常適合在資源有限的本地設備上運行AI模型,支持離線功能並增強隱私保護。
倉庫概述
本倉庫主要關注我們的小型xLAM-1b-fc-r
模型,該模型針對函數調用進行了優化,可以輕鬆部署在個人設備上。
函數調用(或工具使用)是AI智能體的關鍵能力之一。它要求模型不僅能夠理解和生成類似人類的文本,還能根據自然語言指令執行功能性API調用。這將大語言模型的實用性從簡單的對話任務擴展到與各種數字服務和應用程序的動態交互,例如檢索天氣信息、管理社交媒體平臺和處理金融服務。
本指南將指導你完成xLAM-1b-fc-r
與HuggingFace和vLLM的設置、使用和集成。我們將首先介紹基本用法,然後詳細介紹examples文件夾中提供的教程和示例腳本。
框架版本
- Transformers 4.41.0
- Pytorch 2.3.0+cu121
- Datasets 2.19.1
- Tokenizers 0.19.1
基準測試結果
我們主要在伯克利函數調用排行榜(BFCL)上測試我們的函數調用模型,該排行榜提供了一個全面的評估框架,用於評估大語言模型在各種編程語言和應用領域(如Java、JavaScript和Python)中的函數調用能力。
我們的xLAM-7b-fc-r
模型在排行榜上以88.24%的總體準確率獲得第3名,超過了許多強大的模型。值得注意的是,我們的xLAM-1b-fc-r
模型是排行榜上唯一參數少於2B的小型模型,但仍取得了78.94%的有競爭力的總體準確率,超過了GPT3-Turbo和許多更大的模型。兩個模型在各個類別中都表現出均衡的性能,儘管尺寸較小,但顯示出強大的函數調用能力。
🔧 技術細節
本倉庫專注於我們的小型xLAM-1b-fc-r
模型,該模型針對函數調用進行了優化,可輕鬆部署在個人設備上。函數調用(或工具使用)是AI智能體的關鍵能力之一,它要求模型不僅要理解和生成類人文本,還要根據自然語言指令執行功能性API調用,將大語言模型的應用範圍從簡單對話擴展到與各種數字服務和應用的動態交互。
我們主要在伯克利函數調用排行榜(BFCL)上測試函數調用模型,該排行榜提供了全面的評估框架,用於評估大語言模型在多種編程語言和應用領域的函數調用能力。我們的xLAM-7b-fc-r
和xLAM-1b-fc-r
模型在排行榜上表現出色,展示了強大的函數調用能力。
📄 許可證
xLAM-1b-fc-r
模型遵循CC-BY-NC-4.0許可證進行分發,具體附加條款請參考Deepseek許可證。
🔖 引用
如果您覺得本倉庫對您有幫助,請引用我們的論文:
@article{zhang2024xlam,
title={xlam: A family of large action models to empower ai agent systems},
author={Zhang, Jianguo and Lan, Tian and Zhu, Ming and Liu, Zuxin and Hoang, Thai and Kokane, Shirley and Yao, Weiran and Tan, Juntao and Prabhakar, Akshara and Chen, Haolin and others},
journal={arXiv preprint arXiv:2409.03215},
year={2024}
}
@article{liu2024apigen,
title={APIGen: Automated Pipeline for Generating Verifiable and Diverse Function-Calling Datasets},
author={Liu, Zuxin and Hoang, Thai and Zhang, Jianguo and Zhu, Ming and Lan, Tian and Kokane, Shirley and Tan, Juntao and Yao, Weiran and Liu, Zhiwei and Feng, Yihao and others},
journal={arXiv preprint arXiv:2406.18518},
year={2024}
}
@article{zhang2025actionstudio,
title={ActionStudio: A Lightweight Framework for Data and Training of Action Models},
author={Zhang, Jianguo and Hoang, Thai and Zhu, Ming and Liu, Zuxin and Wang, Shiyu and Awalgaonkar, Tulika and Prabhakar, Akshara and Chen, Haolin and Yao, Weiran and Liu, Zhiwei and others},
journal={arXiv preprint arXiv:2503.22673},
year={2025}
}
⚠️ 重要提示
本版本僅用於支持學術論文的研究目的。我們的模型、數據集和代碼並非專門為所有下游用途設計或評估。強烈建議用戶在部署此模型之前,評估並解決與準確性、安全性和公平性相關的潛在問題。鼓勵用戶考慮人工智能的常見侷限性,遵守適用法律,並在選擇用例時採用最佳實踐,特別是在高風險場景中,錯誤或濫用可能會對人們的生活、權利或安全產生重大影響。有關用例的進一步指導,請參考我們的AUP和AI AUP。
💡 使用建議
強烈建議使用我們提供的提示格式和輔助函數,以獲得最佳的函數調用性能。在使用vLLM部署模型時,可以根據GPU容量修改
--dtype
參數。有關更多詳細配置選項,請參考vLLM文檔。可以探索demo.ipynb
文件,以全面瞭解整個工作流程,包括如何執行API。



