Kani: A Lightweight and Highly Hackable Framework for Building Language Model Applications

Andrew Zhu, Liam Dugan, Alyssa Hwang, Chris Callison-Burch

TL;DR

Kani is a new open-source framework designed to simplify the development of complex language model (LM) applications. It offers a lightweight, flexible, and model-agnostic approach, focusing on core functionalities like chat management and robust function calling, while allowing extensive customization and interoperability.

What Problem Does This Paper Address?

Building sophisticated language model applications often involves intricate tasks like integrating external tools, managing conversational context, and ensuring reliable function execution. Existing frameworks can be overly opinionated, imposing specific structures for prompts and limiting developer control over customizability and reproducibility. This can lead to a steep learning curve and hinder the ability to adapt to the rapidly evolving LM landscape. Kani aims to solve this by providing a foundational, unopinionated framework that empowers developers with fine-grained control over their applications.

Methods

Kani is built around a core Kani object that encapsulates three key components:

  1. Inference Engine: An interface to various language models, allowing developers to easily swap them out.
  2. Chat History: Manages the conversation's state, including token counts, to prevent exceeding model context limits.
  3. Function Context: A registry of callable functions that the LM can invoke.

Kani's design philosophy emphasizes minimalism and extensive documentation. Core functionalities like prompt generation, chat history management, and function calling are implemented with a small, universally useful set of features. Crucially, these core functions are designed to be easily overridable, allowing developers to customize behavior extensively. For instance, developers can implement custom prompt templating, error handling for function calls, and even create entirely new engine classes for unsupported models. The framework also supports dynamic function loading and the concept of "sub-Kani" for more complex agentic behaviors.

Datasets

Not discussed in the paper.

Key Insights and Ideas

  • Unopinionated Framework: Kani's core strength lies in its unopinionated nature. Instead of dictating how prompts should be structured, it provides developers with full control, fostering greater flexibility and reproducibility.
  • Robust Function Calling: Kani goes beyond simply passing function calls to models. It validates function calls before they reach the developer's code, providing feedback to the model for retries and ensuring more reliable tool integration.
  • Extensibility through Overriding: The framework is designed for hackability. By subclassing Kani or its components, developers can easily customize everything from prompt construction to error handling, making it adaptable to a wide range of use cases.
  • Interoperability: Kani's model-agnostic design, coupled with its extensible engine layer, allows seamless integration with various LM backends and libraries, future-proofing applications against the rapid pace of LM development.
  • "Sub-Kani" for Agentic Behavior: The ability to spawn "sub-Kani" agents within a running Kani instance opens up possibilities for creating more complex, multi-agent systems where different agents can leverage different models or functionalities.

Key Results

The paper demonstrates Kani's capabilities through several examples:

  • A minimal Kani application can be initialized and used for chatting with models like GPT-4 in just five lines of code.
  • Kani automatically manages chat history, ensuring context window limits are respected.
  • Function calling is demonstrated with examples like retrieving weather data or Wikipedia articles, showcasing robust error handling and validation.
  • Customization examples include overriding prompt generation for persona-based responses and implementing sarcastic error handling for function calls.
  • The framework is shown to be deployable as a web service using FastAPI and WebSockets.

Limitations and Future Work

The authors acknowledge two primary limitations:

  • Non-Chat Models: Kani's internal state management is designed for chat models. For traditional completion-based models (like some versions of LLaMA), developers might need to create custom engine classes with specific output parsing logic to adapt them.
  • Function Calling Support: Not all models natively support function calling. While Kani provides a robust framework for it, its effectiveness is dependent on the underlying model's capabilities.

Future work could involve further expanding the library of pre-built engines and providing more advanced examples of complex applications. The open-source nature of Kani also invites community contributions for new engine implementations and features.

Summary updated

BibTeX
@inproceedings{zhu2023kani,
  author = {Andrew Zhu and Liam Dugan and Alyssa Hwang and Chris Callison-Burch},
  title = {{Kani: A Lightweight and Highly Hackable Framework for Building Language Model Applications}},
  booktitle = {NLP-OSS @ EMNLP},
  year = {2023},
  url = {https://openreview.net/forum?id=N3RhfMQiPJ},
}
Figure