Gorilla: Large Language Model Connected with Massive APIs

Shishir G Patil, Tianjun Zhang, Xin Wang, Joseph E. Gonzalez

TL;DR

Gorilla is a specialized LLM (fine-tuned from LLaMA-7B) that outperforms GPT-4 at generating accurate API calls by using a novel technique called Retriever-Aware Training (RAT). By learning to effectively use—and "judge"—retrieved documentation, Gorilla significantly reduces hallucinations and can adapt to real-time changes in API versions without retraining.

What Problem Does This Paper Address?

While Large Language Models (LLMs) like GPT-4 are excellent at reasoning and conversation, they often fail when asked to use specific "tools" via API calls. This happens for three main reasons:

  1. Massive Scale: There are millions of APIs, far more than can fit in a model's training context.
  2. Frequent Updates: API documentation changes constantly. A model trained six months ago might generate code for a deprecated version.
  3. Hallucination: When unsure, LLMs tend to "hallucinate" plausible-sounding but non-existent arguments or library functions.
Examples of API Hallucinations

The authors aim to bridge the gap between natural language intent (e.g., "I want to identify objects in this image") and the precise, executable code required to invoke the right machine learning model.

Methods

The researchers developed Gorilla, a model fine-tuned from LLaMA-7B, using several key innovations:

  • Self-Instruct Data Generation: They used GPT-4 to generate a massive synthetic dataset of instruction-API pairs based on documentation from major ML hubs.
  • Retriever-Aware Training (RAT): This is the paper's core technical contribution. Instead of just training the model on static API calls, they trained it to look at a piece of retrieved documentation. Crucially, they taught the model to "judge" the retriever: if the retrieved document is relevant, the model uses it; if it’s irrelevant (a common failure in retrieval systems), the model ignores it and relies on its internal knowledge.
  • AST Sub-Tree Matching: To evaluate the model, the authors moved beyond traditional text-matching metrics (like BLEU scores), which are poor for code. Instead, they used Abstract Syntax Tree (AST) matching to check if the generated code is functionally correct and to precisely measure when a model is hallucinating a tool.

Datasets

The authors introduced APIBench, a comprehensive benchmark consisting of approximately 1,600 machine learning APIs scraped from three major hubs:

  • HuggingFace: 925 models across various domains (CV, NLP, Audio, etc.).
  • TorchHub: 94 models.
  • TensorHub: 626 models.

They generated 10 unique user instructions for each API, resulting in a dataset of ~16,000 {instruction, API} pairs for training and evaluation.

Key Insights and Ideas

  • Retrieval is a Double-Edged Sword: Simply "plugging in" a retriever to a standard LLM at inference time can actually decrease performance if the model isn't trained to handle potentially noisy or irrelevant context. RAT is essential for making retrieval-augmented generation (RAG) robust.
  • Adapting to Change: Because Gorilla is trained to parse documentation on the fly, it can handle "test-time" changes. If a library updates from version 1.0 to 2.0, you simply update the documentation in the retriever's database, and Gorilla can generate the new syntax without needing to be fine-tuned again.
  • Constraint Reasoning: Gorilla demonstrates a sophisticated ability to handle nested constraints. For example, if a user asks for a model with "less than 10M parameters" and "at least 70% accuracy," Gorilla can reason through the API metadata to select the correct model.

Key Results

Gorilla sets a new state-of-the-art for API call generation:

Accuracy vs. Hallucination across Retrieval Settings
  • Accuracy: In zero-shot settings, Gorilla outperformed GPT-4 and Claude across all three hubs. On HuggingFace, Gorilla achieved 71.68% accuracy, while GPT-4 lagged at 19.80%.
  • Hallucination Reduction: Gorilla significantly mitigated the hallucination problem. On TorchHub, Gorilla's hallucination rate was only 6.98%, compared to 36.55% for GPT-4.
  • Robustness: When paired with an "Oracle" retriever (perfect retrieval), Gorilla’s accuracy on HuggingFace jumped to over 91%, showing that the model is highly capable of following instructions when the right documentation is provided.

Limitations and Future Work

  • Supporting Code Bugs: While Gorilla is excellent at the specific API call, the "boilerplate" or supporting Python code it generates can still contain logic errors (e.g., trying to iterate over a non-iterable float).
  • Domain Specificity: Currently, the model is heavily focused on Machine Learning APIs. The authors suggest that future work should expand this to RESTful APIs, SQL, and more general-purpose software tools.
  • Execution Sandbox: The paper focuses on generating the call; the authors note that building a safe, automated environment to execute these calls and handle errors in real-time is a logical next step.

Summary updated

BibTeX
@inproceedings{patil2024gorilla,
  author = {Shishir G Patil and Tianjun Zhang and Xin Wang and Joseph E. Gonzalez},
  title = {{Gorilla: Large Language Model Connected with Massive APIs}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=tBRNC6YemY},
}
Figure