初始化项目,由ModelHub XC社区提供模型

Model: Salesforce/xLAM-1b-fc-r
Source: Original Platform
This commit is contained in:
ModelHub XC
2026-04-21 19:19:05 +08:00
commit 54ef0c201e
12 changed files with 65824 additions and 0 deletions

35
.gitattributes vendored Normal file
View File

@@ -0,0 +1,35 @@
*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text

326
README.md Normal file
View File

@@ -0,0 +1,326 @@
---
datasets:
- Salesforce/xlam-function-calling-60k
language:
- en
license: cc-by-nc-4.0
pipeline_tag: text-generation
library_name: transformers
tags:
- function-calling
- LLM Agent
- tool-use
- deepseek
- pytorch
extra_gated_heading: Acknowledge to follow corresponding license to access the repository
extra_gated_button_content: Agree and access repository
extra_gated_fields:
First Name: text
Last Name: text
Country: country
Affiliation: text
---
<p align="center">
<img width="500px" alt="xLAM" src="https://huggingface.co/datasets/jianguozhang/logos/resolve/main/xlam-no-background.png">
</p>
<p align="center">
<a href="https://apigen-pipeline.github.io/">[Homepage]</a> |
<a href="https://arxiv.org/abs/2406.18518">[APIGen Paper]</a> |
<a href="https://huggingface.co/papers/2503.22673">[ActionStudio Paper]</a> |
<a href="https://discord.gg/tysWwgZyQ2">[Discord]</a> |
<a href="https://huggingface.co/datasets/Salesforce/xlam-function-calling-60k">[Dataset]</a> |
<a href="https://github.com/SalesforceAIResearch/xLAM">[Github]</a>
</p>
<hr>
Welcome to the xLAM model family! [Large Action Models (LAMs)](https://blog.salesforceairesearch.com/large-action-models/) are advanced large language models designed to enhance decision-making and translate user intentions into executable actions that interact with the world. LAMs autonomously plan and execute tasks to achieve specific goals, serving as the brains of AI agents. They have the potential to automate workflow processes across various domains, making them invaluable for a wide range of applications.
## Table of Contents
- [Model Series](#model-series)
- [Repository Overview](#repository-overview)
- [Benchmark Results](#benchmark-results)
- [Usage](#usage)
- [Basic Usage with Huggingface](#basic-usage-with-huggingface)
- [Usage with vLLM](#usage-with-vllm)
- [License](#license)
- [Citation](#citation)
## Model Series
We provide a series of xLAMs in different sizes to cater to various applications, including those optimized for function-calling and general agent applications:
| Model | # Total Params | Context Length |Release Date | Category | Download Model | Download GGUF files |
|------------------------|----------------|----------------|----|----|----------------|----------|
| xLAM-7b-r | 7.24B | 32k | Sep. 5, 2024|General, Function-calling | [🤗 Link](https://huggingface.co/Salesforce/xLAM-7b-r) | -- |
| xLAM-8x7b-r | 46.7B | 32k | Sep. 5, 2024|General, Function-calling | [🤗 Link](https://huggingface.co/Salesforce/xLAM-8x7b-r) | -- |
| xLAM-8x22b-r | 141B | 64k | Sep. 5, 2024|General, Function-calling | [🤗 Link](https://huggingface.co/Salesforce/xLAM-8x22b-r) | -- |
| xLAM-1b-fc-r | 1.35B | 16k | July 17, 2024 | Function-calling| [🤗 Link](https://huggingface.co/Salesforce/xLAM-1b-fc-r) | [🤗 Link](https://huggingface.co/Salesforce/xLAM-1b-fc-r-gguf) |
| xLAM-7b-fc-r | 6.91B | 4k | July 17, 2024| Function-calling| [🤗 Link](https://huggingface.co/Salesforce/xLAM-7b-fc-r) | [🤗 Link](https://huggingface.co/Salesforce/xLAM-7b-fc-r-gguf) |
| xLAM-v0.1-r | 46.7B | 32k | Mar. 18, 2024 |General, Function-calling | [🤗 Link](https://huggingface.co/Salesforce/xLAM-v0.1-r) | -- |
The `fc` series of models are optimized for function-calling capability, providing fast, accurate, and structured responses based on input queries and available APIs. These models are fine-tuned based on the [deepseek-coder](https://huggingface.co/collections/deepseek-ai/deepseek-coder-65f295d7d8a0a29fe39b4ec4) models and are designed to be small enough for deployment on personal devices like phones or computers.
We also provide their quantized [GGUF](https://huggingface.co/docs/hub/en/gguf) files for efficient deployment and execution. GGUF is a file format designed to efficiently store and load large language models, making GGUF ideal for running AI models on local devices with limited resources, enabling offline functionality and enhanced privacy.
For more details, check our [GitHub](https://github.com/SalesforceAIResearch/xLAM) and [paper](https://arxiv.org/abs/2406.18518).
## Repository Overview
This repository is focused on our tiny `xLAM-1b-fc-r` model, which is optimized for function-calling and can be easily deployed on personal devices.
<div align="center">
<img src="https://github.com/apigen-pipeline/apigen-pipeline.github.io/blob/main/img/function-call-overview.png?raw=true"
alt="drawing" width="620"/>
</div>
Function-calling, or tool use, is one of the key capabilities for AI agents. It requires the model not only understand and generate human-like text but also to execute functional API calls based on natural language instructions. This extends the utility of LLMs beyond simple conversation tasks to dynamic interactions with a variety of digital services and applications, such as retrieving weather information, managing social media platforms, and handling financial services.
The instructions will guide you through the setup, usage, and integration of `xLAM-1b-fc-r` with HuggingFace and vLLM.
We will first introduce the basic usage, and then walk through the provided tutorial and example scripts in the [examples](https://huggingface.co/Salesforce/xLAM-1b-fc-r/tree/main/examples) folder.
### Framework Versions
- Transformers 4.41.0
- Pytorch 2.3.0+cu121
- Datasets 2.19.1
- Tokenizers 0.19.1
## Benchmark Results
We mainly test our function-calling models on the [Berkeley Function-Calling Leaderboard (BFCL)](https://gorilla.cs.berkeley.edu/leaderboard.html), which offers a comprehensive evaluation framework for assessing LLMs' function-calling capabilities across various programming languages and application domains like Java, JavaScript, and Python.
<div align="center">
<img src="https://github.com/apigen-pipeline/apigen-pipeline.github.io/blob/main/img/table-result-0718.png?raw=true" width="620" alt="Performance comparison on Berkeley Function-Calling Leaderboard">
<p>Performance comparison on the BFCL benchmark as of date 07/18/2024. Evaluated with <code>temperature=0.001</code> and <code>top_p=1</code></p>
</div>
<p>Our <code>xLAM-7b-fc-r</code> secures the 3rd place with an overall accuracy of 88.24% on the leaderboard, outperforming many strong models. Notably, our <code>xLAM-1b-fc-r</code> model is the only tiny model with less than 2B parameters on the leaderboard, but still achieves a competitive overall accuracy of 78.94% and outperforming GPT3-Turbo and many larger models.
Both models exhibit balanced performance across various categories, showing their strong function-calling capabilities despite their small sizes.</p>
See our [paper](https://arxiv.org/abs/2406.18518) and Github [repo](https://github.com/SalesforceAIResearch/xLAM) for more detailed analysis.
## Usage
### Basic Usage with Huggingface
To use the `xLAM-1b-fc-r` model from Huggingface, please first install the `transformers` library:
```bash
pip install transformers>=4.41.0
```
We use the following example to illustrate how to use our model to perform function-calling tasks.
Please note that, our model works best with our provided prompt format.
It allows us to extract JSON output that is similar to the [function-calling mode of ChatGPT](https://platform.openai.com/docs/guides/function-calling).
````python
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)
# Please use our provided instruction prompt for best performance
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()
# Define the input query and available tools
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]
# Helper function to convert openai format tools to our more concise xLAM format
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
# Helper function to build the input prompt for our model
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
# Build the input and start the inference
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 is the id of <|EOT|> token
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))
````
Then you should be able to see the following output string in JSON format:
```shell
{"tool_calls": [{"name": "get_weather", "arguments": {"location": "New York", "unit": "fahrenheit"}}]}
```
We highly recommend to use our provided prompt format and helper functions to yield the best function-calling performance of our model.
### Usage with vLLM
We provide example scripts to deploy our model with `vllm` and run inferences. First, install the required packages:
```bash
pip install vllm openai argparse jinja2
```
The example scripts are located in the [examples](https://huggingface.co/Salesforce/xLAM-1b-fc-r/tree/main/examples) folder.
#### 1. Test Prompt Template
To build prompts using the chat template and output formatted prompts ready for various test cases, run:
```bash
python test_prompt_template.py --model
```
#### 2. Test xLAM Model with a Manually Served Endpoint
a. Serve the model with vLLM:
```bash
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. Run the test script:
```bash
python test_xlam_model_with_endpoint.py --model_name xlam-1b-fc-r --port 8001 [OPTIONS]
```
Options:
- `--temperature`: Default 0.3
- `--top_p`: Default 1.0
- `--max_tokens`: Default 512
This test script provides a handler implementation that can be easily applied to your customized function-calling applications.
#### 3. Test xLAM Model by Directly Using vLLM Library
To test the xLAM model directly with the vLLM library, run:
```bash
python test_xlam_model_with_vllm.py --model Salesforce/xLAM-1b-fc-r [OPTIONS]
```
Options are the same as for the endpoint test. This test script also provides a handler implementation that can be easily applied to your customized function-calling applications.
#### Customization
These examples are designed to be flexible and easily integrated into your own projects. Feel free to modify the scripts to suit your specific needs and applications. You can adjust test queries or API definitions in each script to test different scenarios or model capabilities.
Additional customization tips:
- Modify the `--dtype` parameter when serving the model based on your GPU capacity.
- Refer to the vLLM documentation for more detailed configuration options.
- Explore the `demo.ipynb` file for a comprehensive description of the entire workflow, including how to execute APIs.
These resources provide a robust foundation for integrating xLAM models into your applications, allowing for tailored and efficient deployment.
## License
`xLAM-1b-fc-r` is distributed under the CC-BY-NC-4.0 license, with additional terms specified in the [Deepseek license](https://github.com/deepseek-ai/DeepSeek-LLM/blob/main/LICENSE-MODEL).
## Ethical Considerations
This release is for research purposes only in support of an academic paper. Our models, datasets, and code are not specifically designed or evaluated for all downstream purposes. We strongly recommend users evaluate and address potential concerns related to accuracy, safety, and fairness before deploying this model. We encourage users to consider the common limitations of AI, comply with applicable laws, and leverage best practices when selecting use cases, particularly for high-risk scenarios where errors or misuse could significantly impact peoples lives, rights, or safety. For further guidance on use cases, refer to our AUP and AI AUP.
## Citation
If you find this repo helpful, please cite our paper:
```bibtex
@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}
}
```

30
config.json Normal file
View File

@@ -0,0 +1,30 @@
{
"architectures": [
"LlamaForCausalLM"
],
"attention_bias": false,
"attention_dropout": 0.0,
"bos_token_id": 32013,
"eos_token_id": 32021,
"hidden_act": "silu",
"hidden_size": 2048,
"initializer_range": 0.02,
"intermediate_size": 5504,
"max_position_embeddings": 16384,
"model_type": "llama",
"num_attention_heads": 16,
"num_hidden_layers": 24,
"num_key_value_heads": 16,
"pretraining_tp": 1,
"rms_norm_eps": 1e-06,
"rope_scaling": {
"factor": 4.0,
"type": "linear"
},
"rope_theta": 100000,
"tie_word_embeddings": false,
"torch_dtype": "bfloat16",
"transformers_version": "4.40.0",
"use_cache": false,
"vocab_size": 32256
}

565
examples/demo.ipynb Normal file
View File

@@ -0,0 +1,565 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# xLAM Model Function-Calling Capabilities Demo\n",
"\n",
"This notebook demonstrates the function-calling capabilities of the xLAM model. The xLAM model is designed to handle various tasks by generating appropriate function calls based on the given query and available tools.\n",
"\n",
"We will cover the following steps:\n",
"1. Setup and Initialization\n",
"2. Example Usage with Provided Demo APIs\n",
"3. Executing Real-Time Weather API Calls\n",
"\n",
"Let's get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Setup and Initialization\n",
"\n",
"First, we need to set up the environment and initialize the xLAMHandler class. Ensure you have all the necessary dependencies installed:\n",
"- `vllm`\n",
"- `jinja2`\n",
"- `requests`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we'll import the necessary modules and define the xLAMHandler class and utility functions. You can find the script provided earlier in the cell below."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/export/home/conda/envs/rl/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"2024-07-18 07:25:11,294\tINFO util.py:154 -- Missing packages: ['ipywidgets']. Run `pip install -U ipywidgets`, then restart the notebook server for rich notebook output.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO 07-18 07:25:13 llm_engine.py:161] Initializing an LLM engine (v0.5.0) with config: model='Salesforce/xLAM-1b-fc-r', speculative_config=None, tokenizer='Salesforce/xLAM-1b-fc-r', skip_tokenizer_init=False, tokenizer_mode=auto, revision=None, rope_scaling=None, rope_theta=None, tokenizer_revision=None, trust_remote_code=False, dtype=torch.bfloat16, max_seq_len=65536, download_dir=None, load_format=LoadFormat.AUTO, tensor_parallel_size=1, disable_custom_all_reduce=False, quantization=None, enforce_eager=False, kv_cache_dtype=auto, quantization_param_path=None, device_config=cuda, decoding_config=DecodingConfig(guided_decoding_backend='outlines'), seed=0, served_model_name=Salesforce/xLAM-1b-fc-r)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO 07-18 07:25:24 weight_utils.py:218] Using model weights format ['*.safetensors']\n",
"INFO 07-18 07:25:24 weight_utils.py:261] No model.safetensors.index.json found in remote.\n",
"INFO 07-18 07:25:25 model_runner.py:159] Loading model weights took 2.5583 GB\n",
"INFO 07-18 07:25:31 gpu_executor.py:83] # GPU blocks: 10075, # CPU blocks: 1365\n",
"INFO 07-18 07:25:40 model_runner.py:878] Capturing the model for CUDA graphs. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI.\n",
"INFO 07-18 07:25:40 model_runner.py:882] CUDA graphs can take additional 1~3 GiB memory per GPU. If you are running out of memory, consider decreasing `gpu_memory_utilization` or enforcing eager mode. You can also reduce the `max_num_seqs` as needed to decrease memory usage.\n",
"INFO 07-18 07:26:02 model_runner.py:954] Graph capturing finished in 22 secs.\n"
]
}
],
"source": [
"import json\n",
"import time\n",
"from typing import List, Dict\n",
"\n",
"from vllm import LLM, SamplingParams\n",
"from jinja2 import Template\n",
"\n",
"\n",
"TASK_INSTRUCTION = \"\"\"\n",
"You are an expert in composing functions. You are given a question and a set of possible functions. \n",
"Based on the question, you will need to make one or more function/tool calls to achieve the purpose. \n",
"If none of the functions can be used, point it out and refuse to answer. \n",
"If the given question lacks the parameters required by the function, also point it out.\n",
"\"\"\".strip()\n",
"\n",
"FORMAT_INSTRUCTION = \"\"\"\n",
"The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.\n",
"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 '[]'\n",
"```\n",
"{\n",
" \"tool_calls\": [\n",
" {\"name\": \"func_name1\", \"arguments\": {\"argument1\": \"value1\", \"argument2\": \"value2\"}},\n",
" ... (more tool calls as required)\n",
" ]\n",
"}\n",
"```\n",
"\"\"\".strip()\n",
"\n",
"class XLAMHandler:\n",
" def __init__(self, \n",
" model: str, \n",
" temperature: float = 0.3, \n",
" top_p: float = 1, \n",
" max_tokens: int = 512,\n",
" tensor_parallel_size: int = 1,\n",
" dtype: str = \"bfloat16\"):\n",
" \n",
" # Initialize LLM with GPU specifications\n",
" self.llm = LLM(model=model,\n",
" tensor_parallel_size=tensor_parallel_size,\n",
" dtype=dtype)\n",
" \n",
" self.sampling_params = SamplingParams(\n",
" temperature=temperature,\n",
" top_p=top_p,\n",
" max_tokens=max_tokens\n",
" )\n",
" self.chat_template = self.llm.get_tokenizer().chat_template\n",
" \n",
" @staticmethod\n",
" def apply_chat_template(template, messages):\n",
" jinja_template = Template(template)\n",
" return jinja_template.render(messages=messages)\n",
"\n",
" def process_query(self, query: str, tools: list, task_instruction: str, format_instruction: str):\n",
" # Convert tools to XLAM format\n",
" xlam_tools = self.convert_to_xlam_tool(tools)\n",
"\n",
" # Build the input prompt\n",
" prompt = self.build_prompt(query, xlam_tools, task_instruction, format_instruction)\n",
"\n",
" messages = [\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ]\n",
" formatted_prompt = self.apply_chat_template(self.chat_template, messages)\n",
"\n",
" # Make inference\n",
" start_time = time.time()\n",
" outputs = self.llm.generate([formatted_prompt], self.sampling_params)\n",
" latency = time.time() - start_time\n",
"\n",
" # Calculate tokens per second\n",
" tokens_generated = sum(len(output.text.split()) for output in outputs[0].outputs)\n",
" tokens_per_second = tokens_generated / latency\n",
"\n",
" # Parse response\n",
" result = outputs[0].outputs[0].text\n",
" parsed_result, success, _ = self.parse_response(result)\n",
"\n",
" # Prepare metadata\n",
" metadata = {\n",
" \"latency\": latency,\n",
" \"tokens_per_second\": tokens_per_second,\n",
" \"success\": success,\n",
" }\n",
"\n",
" return parsed_result, metadata\n",
"\n",
" def convert_to_xlam_tool(self, tools):\n",
" if isinstance(tools, dict):\n",
" return {\n",
" \"name\": tools[\"name\"],\n",
" \"description\": tools[\"description\"],\n",
" \"parameters\": {k: v for k, v in tools[\"parameters\"].get(\"properties\", {}).items()}\n",
" }\n",
" elif isinstance(tools, list):\n",
" return [self.convert_to_xlam_tool(tool) for tool in tools]\n",
" else:\n",
" return tools\n",
"\n",
" def build_prompt(self, query, tools, task_instruction=TASK_INSTRUCTION, format_instruction=FORMAT_INSTRUCTION):\n",
" prompt = f\"[BEGIN OF TASK INSTRUCTION]\\n{task_instruction}\\n[END OF TASK INSTRUCTION]\\n\\n\"\n",
" prompt += f\"[BEGIN OF AVAILABLE TOOLS]\\n{json.dumps(tools)}\\n[END OF AVAILABLE TOOLS]\\n\\n\"\n",
" prompt += f\"[BEGIN OF FORMAT INSTRUCTION]\\n{format_instruction}\\n[END OF FORMAT INSTRUCTION]\\n\\n\"\n",
" prompt += f\"[BEGIN OF QUERY]\\n{query}\\n[END OF QUERY]\\n\\n\"\n",
" return prompt\n",
"\n",
" def parse_response(self, response):\n",
" try:\n",
" data = json.loads(response)\n",
" tool_calls = data.get('tool_calls', []) if isinstance(data, dict) else data\n",
" result = [\n",
" {tool_call['name']: tool_call['arguments']}\n",
" for tool_call in tool_calls if isinstance(tool_call, dict)\n",
" ]\n",
" return result, True, []\n",
" except json.JSONDecodeError:\n",
" return [], False, [\"Failed to parse JSON response\"]\n",
"\n",
"handler = XLAMHandler(model=\"Salesforce/xLAM-1b-fc-r\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Example Usage with Demo APIs\n",
"\n",
"In this section, we'll demonstrate how to use the xLAMHandler class with some example APIs. We'll start by defining several API tools and some test queries."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Query: What's the weather like in New York in Fahrenheit?\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Processed prompts: 100%|██████████| 1/1 [00:00<00:00, 4.51it/s, Generation Speed: 176.89 toks/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: [\n",
" {\n",
" \"get_weather\": {\n",
" \"location\": \"New York\",\n",
" \"unit\": \"fahrenheit\"\n",
" }\n",
" }\n",
"]\n",
"Latency: 0.22673869132995605\n",
"Speed: 39.69326958363258\n",
"--------------------------------------------------\n",
"Query: What is the stock price of CRM?\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Processed prompts: 100%|██████████| 1/1 [00:00<00:00, 5.86it/s, Generation Speed: 182.37 toks/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: [\n",
" {\n",
" \"get_stock_price\": {\n",
" \"symbol\": \"CRM\"\n",
" }\n",
" }\n",
"]\n",
"Latency: 0.17523670196533203\n",
"Speed: 34.23940266341585\n",
"--------------------------------------------------\n",
"Query: Tell me the temperature in London in Celsius\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Processed prompts: 100%|██████████| 1/1 [00:00<00:00, 5.08it/s, Generation Speed: 183.60 toks/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: [\n",
" {\n",
" \"get_weather\": {\n",
" \"location\": \"London\",\n",
" \"unit\": \"celsius\"\n",
" }\n",
" }\n",
"]\n",
"Latency: 0.20116281509399414\n",
"Speed: 39.768781304148916\n",
"--------------------------------------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"get_weather_api = {\n",
" \"name\": \"get_weather\",\n",
" \"description\": \"Get the current weather for a location\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, New York\"\n",
" },\n",
" \"unit\": {\n",
" \"type\": \"string\",\n",
" \"enum\": [\"celsius\", \"fahrenheit\"],\n",
" \"description\": \"The unit of temperature to return\"\n",
" }\n",
" },\n",
" \"required\": [\"location\"]\n",
" }\n",
"}\n",
"\n",
"search_api = {\n",
" \"name\": \"search\",\n",
" \"description\": \"Search for information on the internet\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"query\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The search query, e.g. 'latest news on AI'\"\n",
" }\n",
" },\n",
" \"required\": [\"query\"]\n",
" }\n",
"}\n",
"\n",
"get_stock_price_api = {\n",
" \"name\": \"get_stock_price\",\n",
" \"description\": \"Get the current stock price for a company\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"symbol\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The stock symbol, e.g. 'AAPL' for Apple Inc.\"\n",
" }\n",
" },\n",
" \"required\": [\"symbol\"]\n",
" }\n",
"}\n",
"\n",
"get_news_api = {\n",
" \"name\": \"get_news\",\n",
" \"description\": \"Get the latest news headlines\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"topic\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The news topic, e.g. 'technology', 'sports'\"\n",
" }\n",
" },\n",
" \"required\": [\"topic\"]\n",
" }\n",
"}\n",
"\n",
"all_apis = [get_weather_api, search_api, get_stock_price_api, get_news_api]\n",
"\n",
"test_queries = [\n",
" \"What's the weather like in New York in Fahrenheit?\",\n",
" \"What is the stock price of CRM?\",\n",
" \"Tell me the temperature in London in Celsius\",\n",
"]\n",
"\n",
"for query in test_queries:\n",
" print(f\"Query: {query}\")\n",
" result, metadata = handler.process_query(query, all_apis, TASK_INSTRUCTION, FORMAT_INSTRUCTION)\n",
" print(f\"Result: {json.dumps(result, indent=2)}\")\n",
" print(\"Latency: \", metadata[\"latency\"])\n",
" print(\"Speed: \", metadata[\"tokens_per_second\"])\n",
" print(\"-\" * 50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Executing Real-Time Weather API Calls\n",
"\n",
"To make real-time weather API calls, we'll use the `requests` library to fetch data from a weather service. After obtaining the weather data, we will ask our xLAM model to summarize the results."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The current weather in San Francisco is 16.0 celsius\n"
]
}
],
"source": [
"import ast\n",
"import requests\n",
"\n",
"def get_weather(location, unit):\n",
" \"\"\"\n",
" Get the current weather for a specified location.\n",
"\n",
" Args:\n",
" location (str): The city and state, e.g. San Francisco, New York.\n",
" unit (str): The unit of temperature to return, either 'celsius' or 'fahrenheit'.\n",
"\n",
" Returns:\n",
" float: The temperature in the corresponding unit.\n",
" \"\"\"\n",
" base_url = \"https://wttr.in\"\n",
" unit_param = \"m\" if unit == \"celsius\" else \"u\"\n",
" params = {\n",
" \"format\": \"j1\",\n",
" \"unit\": unit_param\n",
" }\n",
" response = requests.get(f\"{base_url}/{location}\", params=params)\n",
" if response.status_code == 200:\n",
" weather_data = response.json()[\"current_condition\"][0]\n",
" return float(weather_data[\"temp_C\"]) if unit == \"celsius\" else float(weather_data[\"temp_F\"])\n",
" else:\n",
" return {\"error\": \"Failed to retrieve weather data\"}\n",
" \n",
"def execute_function_calls(function_calls):\n",
" \"\"\"\n",
" Convert the dictionary function_calls to executable Python code and execute the corresponding functions.\n",
"\n",
" Args:\n",
" function_calls (list): A list of dictionaries containing function calls and their arguments.\n",
"\n",
" Returns:\n",
" list: A list of results from executing the functions.\n",
" \"\"\"\n",
" results = []\n",
" for function_call in function_calls:\n",
" for func_name, args in function_call.items():\n",
" if func_name in globals() and callable(globals()[func_name]):\n",
" try:\n",
" # Safely evaluate the arguments\n",
" safe_args = ast.literal_eval(str(args))\n",
" print(safe_args)\n",
" # Call the function with unpacked arguments\n",
" func_result = globals()[func_name](**safe_args)\n",
" results.append(func_result)\n",
" except Exception as e:\n",
" results.append(f\"Error {str(e)}\")\n",
" else:\n",
" results.append(\"Error: Function not found or not callable\")\n",
" \n",
" return results\n",
"\n",
"# Example usage\n",
"location = \"San Francisco\"\n",
"unit = \"celsius\"\n",
"weather_data = get_weather(location, unit)\n",
"print(f\"The current weather in {location} is {weather_data} {unit}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Processed prompts: 100%|██████████| 1/1 [00:00<00:00, 4.86it/s, Generation Speed: 180.67 toks/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The function call result: [\n",
" {\n",
" \"get_weather\": {\n",
" \"location\": \"San Francisco\",\n",
" \"unit\": \"celsius\"\n",
" }\n",
" }\n",
"]\n",
"{'location': 'San Francisco', 'unit': 'celsius'}\n",
"Execution results: [16.0]\n",
"--------------------------------------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Processed prompts: 100%|██████████| 1/1 [00:00<00:00, 4.67it/s, Generation Speed: 183.21 toks/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The function call result: [\n",
" {\n",
" \"get_weather\": {\n",
" \"location\": \"New York\",\n",
" \"unit\": \"fahrenheit\"\n",
" }\n",
" }\n",
"]\n",
"{'location': 'New York', 'unit': 'fahrenheit'}\n",
"Execution results: [74.0]\n"
]
}
],
"source": [
"# Example 1\n",
"query = \"I want to know the weather in San Francisco in Celsius\"\n",
"function_calls, metadata = handler.process_query(query, all_apis, TASK_INSTRUCTION, FORMAT_INSTRUCTION)\n",
"print(f\"The function call result: {json.dumps(function_calls, indent=2)}\")\n",
"execution_results = execute_function_calls(function_calls)\n",
"print(\"Execution results: \", execution_results)\n",
"print(\"-\" * 50)\n",
"\n",
"# Example 2\n",
"query = \"Tell me the temperature in New York in Fahrenheit\"\n",
"function_calls, metadata = handler.process_query(query, all_apis, TASK_INSTRUCTION, FORMAT_INSTRUCTION)\n",
"print(f\"The function call result: {json.dumps(function_calls, indent=2)}\")\n",
"execution_results = execute_function_calls(function_calls)\n",
"print(\"Execution results: \", execution_results)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,161 @@
import argparse
import json
from typing import Dict
from jinja2 import Template
from transformers import AutoTokenizer
# Default prompts
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()
class PromptAssembler:
def __init__(self, model: str):
tokenizer = AutoTokenizer.from_pretrained(model)
self.chat_template = tokenizer.chat_template
@staticmethod
def apply_chat_template(template, messages):
jinja_template = Template(template)
return jinja_template.render(messages=messages)
def assemble_prompt(self, query: str, tools: Dict, task_instruction: str, format_instruction: str):
# Convert tools to XLAM format
xlam_tools = self.convert_to_xlam_tool(tools)
# Build the input prompt
prompt = self.build_prompt(query, xlam_tools, task_instruction, format_instruction)
messages = [
{"role": "user", "content": prompt}
]
formatted_prompt = self.apply_chat_template(self.chat_template, messages)
return formatted_prompt
def convert_to_xlam_tool(self, 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 [self.convert_to_xlam_tool(tool) for tool in tools]
else:
return tools
def build_prompt(self, query, tools, task_instruction=TASK_INSTRUCTION, format_instruction=FORMAT_INSTRUCTION):
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(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
def print_prompt_template(self):
template = self.chat_template.replace("{{", "{").replace("}}", "}")
print("Prompt Template with Placeholders:")
print(template)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Assemble prompts using chat template")
parser.add_argument("--model", required=True, help="Name of the model (for chat template)")
args = parser.parse_args()
# Initialize the PromptAssembler
assembler = PromptAssembler(args.model)
# Print the prompt template with placeholders
assembler.print_prompt_template()
# Test case 1: Weather API, follows the OpenAI format: https://platform.openai.com/docs/guides/function-calling
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, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature to return"
}
},
"required": ["location"]
}
}
# Test queries
test_queries = [
"What's the weather like in New York?",
"Tell me the temperature in London in Celsius",
"What's the weather forecast for Tokyo?",
"What is the stock price of CRM?", # the model should return an empty list
"What's the current temperature in Paris in Fahrenheit?"
]
# Run test cases
for query in test_queries:
print(f"\nQuery: {query}")
formatted_prompt = assembler.assemble_prompt(query, weather_api, TASK_INSTRUCTION, FORMAT_INSTRUCTION)
print("Formatted Prompt:")
print(formatted_prompt)
print("-" * 50)
# Test case 2: Multiple APIs, follows the OpenAI format: https://platform.openai.com/docs/guides/function-calling
calculator_api = {
"name": "calculate",
"description": "Perform a mathematical calculation",
"parameters": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The mathematical operation to perform"
},
"x": {
"type": "number",
"description": "The first number"
},
"y": {
"type": "number",
"description": "The second number"
}
},
"required": ["operation", "x", "y"]
}
}
multi_api_query = "What's the weather in Miami and what's 15 multiplied by 7?"
print(f"\nMulti-API Query: {multi_api_query}")
multi_api_formatted_prompt = assembler.assemble_prompt(
multi_api_query,
[weather_api, calculator_api],
TASK_INSTRUCTION,
FORMAT_INSTRUCTION
)
print("Formatted Prompt:")
print(multi_api_formatted_prompt)

View File

@@ -0,0 +1,188 @@
import argparse
import json
import time
from openai import OpenAI
# Default prompts
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()
class XLAMHandler:
def __init__(self, model_name, temperature=0.3, top_p=1, max_tokens=512, port=8000):
self.model_name = model_name
self.temperature = temperature
self.top_p = top_p
self.max_tokens = max_tokens
base_url = f"http://localhost:{port}/v1"
self.client = OpenAI(api_key="Empty", base_url=base_url)
def process_query(self, query, tools, task_instruction, format_instruction):
# Convert tools to XLAM format
xlam_tools = self.convert_to_xlam_tool(tools)
# Build the input prompt
prompt = self.build_prompt(query, xlam_tools, task_instruction, format_instruction)
# Create message for API call
message = [{"role": "user", "content": prompt}]
# Make API call
start_time = time.time()
response = self.client.chat.completions.create(
messages=message,
model=self.model_name,
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=self.top_p,
)
latency = time.time() - start_time
# Parse response
result = response.choices[0].message.content
parsed_result, success, _ = self.parse_response(result)
# Prepare metadata
metadata = {
"input_tokens": response.usage.prompt_tokens,
"output_tokens": response.usage.completion_tokens,
"latency": latency
}
return parsed_result, metadata
def convert_to_xlam_tool(self, 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 [self.convert_to_xlam_tool(tool) for tool in tools]
else:
return tools
def build_prompt(self, query, tools, task_instruction=TASK_INSTRUCTION, format_instruction=FORMAT_INSTRUCTION):
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(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
def parse_response(self, response):
try:
data = json.loads(response)
tool_calls = data.get('tool_calls', []) if isinstance(data, dict) else data
result = [
{tool_call['name']: tool_call['arguments']}
for tool_call in tool_calls if isinstance(tool_call, dict)
]
return result, True, []
except json.JSONDecodeError:
return [], False, ["Failed to parse JSON response"]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Test XLAM model with endpoint")
parser.add_argument("--model_name", default="xlam-1b-fc-r", help="Name of the model")
parser.add_argument("--port", type=int, default=8001, help="Port number for the endpoint")
parser.add_argument("--temperature", type=float, default=0.3, help="Temperature for sampling")
parser.add_argument("--top_p", type=float, default=1.0, help="Top p for sampling")
parser.add_argument("--max_tokens", type=int, default=512, help="Maximum number of tokens to generate")
args = parser.parse_args()
# Initialize the XLAMHandler with command-line arguments
handler = XLAMHandler(args.model_name, temperature=args.temperature, top_p=args.top_p, max_tokens=args.max_tokens, port=args.port)
# Test case 1: Weather API, follows the OpenAI format: https://platform.openai.com/docs/guides/function-calling
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, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature to return"
}
},
"required": ["location"]
}
}
# Test queries
test_queries = [
"What's the weather like in New York?",
"Tell me the temperature in London in Celsius",
"What's the weather forecast for Tokyo?",
"What is the stock price of CRM?", # the model should return an empty list, meaning that it refuse to answer this irrelevant query and tools.
"What's the current temperature in Paris in Fahrenheit?"
]
# Run test cases
for query in test_queries:
print(f"Query: {query}")
result, metadata = handler.process_query(query, weather_api, TASK_INSTRUCTION, FORMAT_INSTRUCTION)
print(f"Result: {json.dumps(result, indent=2)}")
print(f"Metadata: {json.dumps(metadata, indent=2)}")
print("-" * 50)
# Test case 2: Multiple APIs, follows the OpenAI format: https://platform.openai.com/docs/guides/function-calling
calculator_api = {
"name": "calculate",
"description": "Perform a mathematical calculation",
"parameters": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The mathematical operation to perform"
},
"x": {
"type": "number",
"description": "The first number"
},
"y": {
"type": "number",
"description": "The second number"
}
},
"required": ["operation", "x", "y"]
}
}
multi_api_query = "What's the weather in Miami and what's 15 multiplied by 7?"
multi_api_result, multi_api_metadata = handler.process_query(
multi_api_query,
[weather_api, calculator_api],
TASK_INSTRUCTION,
FORMAT_INSTRUCTION
)
print("Multi-API Query Result:")
print(json.dumps(multi_api_result, indent=2))
print(f"Metadata: {json.dumps(multi_api_metadata, indent=2)}")

View File

@@ -0,0 +1,193 @@
import json
import time
import argparse
from typing import List, Dict
from vllm import LLM, SamplingParams
from jinja2 import Template
# Default prompts
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()
class XLAMHandler:
def __init__(self, model: str, temperature: float = 0.3, top_p: float = 1, max_tokens: int = 512):
self.llm = LLM(model=model)
self.sampling_params = SamplingParams(
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens
)
self.chat_template = self.llm.get_tokenizer().chat_template
@staticmethod
def apply_chat_template(template, messages):
jinja_template = Template(template)
return jinja_template.render(messages=messages)
def process_query(self, query: str, tools: Dict, task_instruction: str, format_instruction: str):
# Convert tools to XLAM format
xlam_tools = self.convert_to_xlam_tool(tools)
# Build the input prompt
prompt = self.build_prompt(query, xlam_tools, task_instruction, format_instruction)
messages = [
{"role": "user", "content": prompt}
]
formatted_prompt = self.apply_chat_template(self.chat_template, messages)
# Make inference
start_time = time.time()
outputs = self.llm.generate([formatted_prompt], self.sampling_params)
latency = time.time() - start_time
# Parse response
result = outputs[0].outputs[0].text
parsed_result, success, _ = self.parse_response(result)
# Prepare metadata
metadata = {
"latency": latency,
"success": success,
}
return parsed_result, metadata
def convert_to_xlam_tool(self, 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 [self.convert_to_xlam_tool(tool) for tool in tools]
else:
return tools
def build_prompt(self, query, tools, task_instruction=TASK_INSTRUCTION, format_instruction=FORMAT_INSTRUCTION):
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(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
def parse_response(self, response):
try:
data = json.loads(response)
tool_calls = data.get('tool_calls', []) if isinstance(data, dict) else data
result = [
{tool_call['name']: tool_call['arguments']}
for tool_call in tool_calls if isinstance(tool_call, dict)
]
return result, True, []
except json.JSONDecodeError:
return [], False, ["Failed to parse JSON response"]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Test XLAM model with vLLM")
parser.add_argument("--model", required=True, help="Path to the model")
parser.add_argument("--temperature", type=float, default=0.3, help="Temperature for sampling")
parser.add_argument("--top_p", type=float, default=1.0, help="Top p for sampling")
parser.add_argument("--max_tokens", type=int, default=512, help="Maximum number of tokens to generate")
args = parser.parse_args()
# Initialize the XLAMHandler with command-line arguments
handler = XLAMHandler(args.model, temperature=args.temperature, top_p=args.top_p, max_tokens=args.max_tokens)
# Test case 1: Weather API, follows the OpenAI format: https://platform.openai.com/docs/guides/function-calling
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, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature to return"
}
},
"required": ["location"]
}
}
# Test queries
test_queries = [
"What's the weather like in New York?",
"Tell me the temperature in London in Celsius",
"What's the weather forecast for Tokyo?",
"What is the stock price of CRM?", # the model should return an empty list
"What's the current temperature in Paris in Fahrenheit?"
]
# Run test cases
for query in test_queries:
print(f"Query: {query}")
result, metadata = handler.process_query(query, weather_api, TASK_INSTRUCTION, FORMAT_INSTRUCTION)
print(f"Result: {json.dumps(result, indent=2)}")
print(f"Metadata: {json.dumps(metadata, indent=2)}")
print("-" * 50)
# Test case 2: Multiple APIs, follows the OpenAI format: https://platform.openai.com/docs/guides/function-calling
calculator_api = {
"name": "calculate",
"description": "Perform a mathematical calculation",
"parameters": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The mathematical operation to perform"
},
"x": {
"type": "number",
"description": "The first number"
},
"y": {
"type": "number",
"description": "The second number"
}
},
"required": ["operation", "x", "y"]
}
}
multi_api_query = "What's the weather in Miami and what's 15 multiplied by 7?"
multi_api_result, multi_api_metadata = handler.process_query(
multi_api_query,
[weather_api, calculator_api],
TASK_INSTRUCTION,
FORMAT_INSTRUCTION
)
print("Multi-API Query Result:")
print(json.dumps(multi_api_result, indent=2))
print(f"Metadata: {json.dumps(multi_api_metadata, indent=2)}")

6
generation_config.json Normal file
View File

@@ -0,0 +1,6 @@
{
"_from_model_config": true,
"bos_token_id": 32013,
"eos_token_id": 32021,
"transformers_version": "4.40.0"
}

3
model.safetensors Normal file
View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:540b4f58ab7f35c7d4b81e425b52844511f37e74b6388f5c228ad48ea526cb84
size 2692969128

32
special_tokens_map.json Normal file
View File

@@ -0,0 +1,32 @@
{
"additional_special_tokens": [
{
"content": "<|EOT|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
],
"bos_token": {
"content": "<begin▁of▁sentence>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "<|EOT|>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false
},
"pad_token": {
"content": "<end▁of▁sentence>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false
}
}

64087
tokenizer.json Normal file

File diff suppressed because it is too large Load Diff

198
tokenizer_config.json Normal file
View File

@@ -0,0 +1,198 @@
{
"add_bos_token": true,
"add_eos_token": false,
"added_tokens_decoder": {
"32000": {
"content": "õ",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32001": {
"content": "÷",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32002": {
"content": "Á",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32003": {
"content": "ý",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32004": {
"content": "À",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32005": {
"content": "ÿ",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32006": {
"content": "ø",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32007": {
"content": "ú",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32008": {
"content": "þ",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32009": {
"content": "ü",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32010": {
"content": "ù",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32011": {
"content": "ö",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32012": {
"content": "û",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32013": {
"content": "<begin▁of▁sentence>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": true
},
"32014": {
"content": "<end▁of▁sentence>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": true
},
"32015": {
"content": "<fim▁hole>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32016": {
"content": "<fim▁begin>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32017": {
"content": "<fim▁end>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32018": {
"content": "<pad>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32019": {
"content": "<|User|>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32020": {
"content": "<|Assistant|>",
"lstrip": false,
"normalized": true,
"rstrip": false,
"single_word": false,
"special": false
},
"32021": {
"content": "<|EOT|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
}
},
"additional_special_tokens": [
"<|EOT|>"
],
"bos_token": "<begin▁of▁sentence>",
"chat_template": "{% set system_message = 'You are an AI assistant for function calling.For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer\\n' %}{% if messages[0]['role'] == 'system' %}{% set system_message = messages[0]['content'] %}{% endif %}{% if system_message is defined %}{{ system_message }}{% endif %}{% for message in messages %}{% set content = message['content'] %}{% if message['role'] == 'user' %}{{ '### Instruction:\\n' + content + '\\n### Response:' }}{% elif message['role'] == 'assistant' %}{{ '\\n' + content + '\\n<|EOT|>\\n' }}{% endif %}{% endfor %}",
"clean_up_tokenization_spaces": false,
"eos_token": "<|EOT|>",
"legacy": true,
"model_max_length": 16384,
"pad_token": "<end▁of▁sentence>",
"padding_side": "right",
"sp_model_kwargs": {},
"split_special_tokens": false,
"tokenizer_class": "LlamaTokenizer",
"unk_token": null,
"use_default_system_prompt": false
}