Octopus V2:设备端super agent的高级语言模型

发布于:2024-04-08 ⋅ 阅读:(35) ⋅ 点赞:(0)
  • 论文:Octopus v2: On-device language model for super agent
  • 论文地址:https://arxiv.org/abs/2404.01744
  • 模型主页:https://huggingface.co/NexaAIDev/Octopus-v2

Octopus-V2-2B

Octopus-V2-2B 是一款具有20亿参数的开源先进语言模型,它体现了Nexa AI在将大型语言模型(LLM)应用于函数调用方面的研究成果,并且是针对Android API特别设计的。与传统的检索增强生成(RAG)方法不同,后者在处理潜在的函数参数时需要进行详尽的描述(有时涉及数万个输入标记),Octopus-V2-2B在训练和推理过程中采用了独特的函数标记策略。这种策略不仅让它能够与GPT-4的性能相媲美,还大幅提升了推理速度,优于基于RAG的方法。这一改进特别适合边缘计算设备,使得Octopus-V2-2B在这些平台上更具优势

📱设备端应用程序:Octopus-V2-2B 旨在与 Android 设备完美兼容,其应用范围广泛,涵盖了从 Android 系统管理到多设备协调等多个方面。

🚀推理速度:在基准测试中,Octopus-V2-2B 表现出了卓越的推理速度,在单个 A100 GPU 上的性能比“Llama7B + RAG 解决方案”组合高出 36 倍。此外,与依赖集群 A100/H100 GPU 的 GPT-4-turbo (gpt-4-0125-preview) 相比,Octopus-V2-2B 速度提高了 168%。这种效率归功于我们的functional token设计。

20 亿参数的 Octopus v2 可以在智能手机、汽车、个人电脑等端侧运行,在准确性和延迟方面超越了 GPT-4,并将上下文长度减少了 95%。此外,Octopus v2 比 Llama7B + RAG 方案快 36 倍。

🐙准确度:Octopus-V2-2B 不仅在速度上表现出色,而且在准确度上也表现出色,在函数调用准确度上超越“Llama7B + RAG 方案”31%。它实现了与 GPT-4 和 RAG + GPT-3.5 相当的函数调用精度,在基准数据集上的得分范围在 98% 到 100% 之间。

💪函数调用功能:Octopus-V2-2B 能够在各种复杂场景中生成单独的、嵌套的和并行的函数调用。

示例用例

您可以在 GPU 上使用以下代码来运行模型。

from transformers import AutoTokenizer, GemmaForCausalLM
import torch
import time

def inference(input_text):
    start_time = time.time()
    input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)
    input_length = input_ids["input_ids"].shape[1]
    outputs = model.generate(
        input_ids=input_ids["input_ids"], 
        max_length=1024,
        do_sample=False)
    generated_sequence = outputs[:, input_length:].tolist()
    res = tokenizer.decode(generated_sequence[0])
    end_time = time.time()
    return {"output": res, "latency": end_time - start_time}

model_id = "NexaAIDev/Octopus-v2"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = GemmaForCausalLM.from_pretrained(
    model_id, torch_dtype=torch.bfloat16, device_map="auto"
)

input_text = "Take a selfie for me with front camera"
nexa_query = f"Below is the query from the users, please call the correct function and generate the parameters to call the function.\n\nQuery: {input_text} \n\nResponse:"
start_time = time.time()
print("nexa model result:\n", inference(nexa_query))
print("latency:", time.time() - start_time," s")

评估

基准测试结果可以在这个excel中查看,这是手动验证的。基准测试中的所有查询均由 Gemini 采样。

Octopus-V2-2B 在基准测试中表现出卓越的推理速度,在单个 A100 GPU 上比「Llama7B + RAG 解决方案」快 36 倍。此外,与依赖集群 A100/H100 GPU 的 GPT-4-turbo 相比,Octopus-V2-2B 速度提高了 168%。这种效率突破归功于 Octopus-V2-2B 的函数性 token 设计。

图片

Octopus-V2-2B 不仅在速度上表现出色,在准确率上也表现出色,在函数调用准确率上超越「Llama7B + RAG 方案」31%。Octopus-V2-2B 实现了与 GPT-4 和 RAG + GPT-3.5 相当的函数调用准确率。

图片

注意:人们可以注意到查询包括用于函数的所有必需参数。预计查询也包括推理期间的所有参数。

数据集

为了训练、验证和测试阶段采用高质量数据集,特别是实现高效训练,研究团队用三个关键阶段创建数据集:

  • 生成相关的查询及其关联的函数调用参数;
  • 由适当的函数组件生成不相关的查询; 
  • 通过 Google Gemini 实现二进制验证支持。

图片

训练数据

该研究采用 Google Gemma-2B 模型作为框架中的预训练模型,并采用两种不同的训练方法:完整模型训练和 LoRA 模型训练。

在完整模型训练中,该研究使用 AdamW 优化器,学习率设置为 5e-5,warm-up 的 step 数设置为 10,采用线性学习率调度器。

LoRA 模型训练采用与完整模型训练相同的优化器和学习率配置,LoRA rank 设置为 16,并将 LoRA 应用于以下模块:q_proj、k_proj、v_proj、o_proj、up_proj、down_proj。其中,LoRA alpha 参数设置为 32。

对于两种训练方法,epoch 数均设置为 3。

我们编写了 20 个 Android API 描述用于训练模型,请参阅此文件了解详细信息。我们的演示的 Android API 实现以及我们的训练数据将在稍后发布。下面是一个Android API描述示例

def get_trending_news(category=None, region='US', language='en', max_results=5):
    """
    Fetches trending news articles based on category, region, and language.

    Parameters:
    - category (str, optional): News category to filter by, by default use None for all categories. Optional to provide.
    - region (str, optional): ISO 3166-1 alpha-2 country code for region-specific news, by default, uses 'US'. Optional to provide.
    - language (str, optional): ISO 639-1 language code for article language, by default uses 'en'. Optional to provide.
    - max_results (int, optional): Maximum number of articles to return, by default, uses 5. Optional to provide.

    Returns:
    - list[str]: A list of strings, each representing an article. Each string contains the article's heading and URL.
    """