Introduction
Large Language Models (LLMs) like GPT-4 have demonstrated remarkable capabilities in general reasoning and task execution. However, deploying these models for specific tasks such as software API function calling presents significant challenges, including high computational costs, latency, and privacy concerns for on-device applications. Smaller models, while more efficient, often suffer from "brittleness"—failing to adhere to strict API schemas or hallucinating parameters. The Octopus framework addresses these issues by enabling small, on-device models (2B–7B parameters) to achieve and even surpass GPT-4 level performance in function calling through specialized training and a novel inference-time technique called conditional masking.
Methods
The Octopus framework employs a multi-stage optimization pipeline to specialize open-source base models like Google Gemma and CodeLlama for API syntax.
Fine-tuning and Curriculum Learning
Models are first fine-tuned using Low-Rank Adaptation (LoRA). To enhance the model's discriminative power, the researchers utilized curriculum learning. This involved starting with simple API calls and progressively introducing "hard negatives"—functions that are semantically similar but incorrect for the given query—forcing the model to learn nuanced differences in API documentation.
Conditional Masking
The core innovation of Octopus is "conditional masking," a dynamic inference-time technique. Standard decoding selects the most likely next token from the entire vocabulary. In contrast, conditional masking restricts the search space based on the API's schema:
- Function Names: A Trie-based search ensures only valid function names from the library are generated.
- Parameters: Regular expressions enforce type constraints (e.g.,
int,float,string) for generated arguments.
This technique effectively reduces validation loss and eliminates structural hallucinations by making invalid choices impossible for the model.
Datasets
To train and evaluate Octopus, the authors developed a high-quality data generation pipeline using 30,000 API function calls from the RapidAPI Hub.
The pipeline converts raw documentation into standardized JSON (Dataset A) and then refines it into a training set of approximately 150,000 data points (Dataset B). Dataset B includes positive queries, negative queries (unsolvable by available APIs), and Chain of Thought (CoT) annotations. GPT-4 was employed as a "judge" to verify and clean the final dataset, removing low-quality entries.
Results
The performance of Octopus models was evaluated against industry benchmarks, including GPT-4.
With conditional masking, Octopus-Gemma-7B achieved a peak accuracy of 97%, while the compact Octopus-Gemma-2B reached 94%. These results are comparable to or better than GPT-4's 96% accuracy. Notably, Octopus models avoided common GPT-4 errors, such as "correcting" intentional typos in API names or generating invalid parameter values, thanks to the structural constraints imposed by the mask.
Conclusion
The Octopus framework demonstrates that small, on-device models can provide high-reliability software integration when combined with constrained decoding techniques. By treating API schemas as hard features rather than suggestions, Octopus enables the deployment of sophisticated "AI agents" locally on mobile devices, offering a private, fast, and cost-effective alternative to cloud-based LLMs. Future work will focus on scaling these techniques to millions of APIs and exploring multi-step tool use.
Summary updated