kani

kani (カニ) is a highly hackable microframework for tool-calling language models. (NLP-OSS @ EMNLP 2023)

https://github.com/zhudotexe/kani

Science Score: 77.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 1 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org
  • Committers with academic emails
    2 of 7 committers (28.6%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.6%) to scientific vocabulary

Keywords

chatgpt framework function-calling gpt-4 large-language-models llama llms microframework openai tool-use

Keywords from Contributors

hack
Last synced: 4 months ago · JSON representation ·

Repository

kani (カニ) is a highly hackable microframework for tool-calling language models. (NLP-OSS @ EMNLP 2023)

Basic Info
Statistics
  • Stars: 587
  • Watchers: 10
  • Forks: 31
  • Open Issues: 6
  • Releases: 40
Topics
chatgpt framework function-calling gpt-4 large-language-models llama llms microframework openai tool-use
Created over 2 years ago · Last pushed 4 months ago
Metadata Files
Readme License Citation

README.md

kani

Test Package Documentation Status PyPI Quickstart in Colab Discord
Model zoo Retrieval example

kani (カニ)

kani (カニ) is a lightweight and highly hackable framework for chat-based language models with tool usage/function calling.

Compared to other LM frameworks, kani is less opinionated and offers more fine-grained customizability over the parts of the control flow that matter, making it the perfect choice for NLP researchers, hobbyists, and developers alike.

kani comes with support for the following models out of the box, with a model-agnostic framework to add support for many more:

  • OpenAI Models (pip install "kani[openai]")
  • Anthropic Models (pip install "kani[anthropic]")
  • Google AI Models (pip install "kani[google]")
  • and every chat model available on Hugging Face through transformers or llama.cpp! (pip install "kani[huggingface]")

Check out the Model Zoo for code examples of loading popular models in Kani!

Interested in contributing? Check out our guide.

Read the docs on ReadTheDocs!

Read our paper on arXiv!

Installation

kani requires Python 3.10 or above. To install model-specific dependencies, kani uses various extras (brackets after the library name in pip install). To determine which extra(s) to install, see the model table, or use the [all] extra to install everything.

```shell

for OpenAI models

$ pip install "kani[openai]"

for Hugging Face models

$ pip install "kani[huggingface]" torch

for multimodal inputs

$ pip install "kani[multimodal]"

or install everything:

$ pip install "kani[all]" ```

For the most up-to-date changes and new models, you can also install the development version from Git's main branch:

shell $ pip install "kani[all] @ git+https://github.com/zhudotexe/kani.git@main"

Quickstart

Quickstart in Colab

kani requires Python 3.10 or above.

First, install the library. In this quickstart, we'll use the OpenAI engine, though kani is model-agnostic.

shell $ pip install "kani[openai]"

Then, let's use kani to create a simple chatbot using ChatGPT as a backend.

```python

import the library

import asyncio from kani import Kani, chatinterminal from kani.engines.openai import OpenAIEngine

Replace this with your OpenAI API key: https://platform.openai.com/account/api-keys

api_key = "sk-..."

kani uses an Engine to interact with the language model. You can specify other model

parameters here, like temperature=0.7.

engine = OpenAIEngine(api_key, model="gpt-5-nano")

The kani manages the chat state, prompting, and function calling. Here, we only give

it the engine to call ChatGPT, but you can specify other parameters like

system_prompt="You are..." here.

ai = Kani(engine)

kani comes with a utility to interact with a kani through your terminal...

chatinterminal(ai)

or you can use kani programmatically in an async function!

async def main(): resp = await ai.chat_round("What is the airspeed velocity of an unladen swallow?") print(resp.text)

asyncio.run(main()) ```

kani makes the time to set up a working chat model short, while offering the programmer deep customizability over every prompt, function call, and even the underlying language model.

Function Calling

Function calling gives language models the ability to choose when to call a function you provide based off its documentation.

With kani, you can write functions in Python and expose them to the model with just one line of code: the @ai_function decorator.

```python

import the library

import asyncio from typing import Annotated from kani import AIParam, Kani, aifunction, chatin_terminal, ChatRole from kani.engines.openai import OpenAIEngine

set up the engine as above

apikey = "sk-..." engine = OpenAIEngine(apikey, model="gpt-4o-mini")

subclass Kani to add AI functions

class MyKani(Kani): # Adding the annotation to a method exposes it to the AI @aifunction() def getweather( self, # and you can provide extra documentation about specific parameters location: Annotated[str, AIParam(desc="The city and state, e.g. San Francisco, CA")], ): """Get the current weather in a given location.""" # In this example, we mock the return, but you could call a real weather API return f"Weather in {location}: Sunny, 72 degrees fahrenheit."

ai = MyKani(engine)

the terminal utility allows you to test function calls...

chatinterminal(ai)

and you can track multiple rounds programmatically.

async def main(): async for msg in ai.full_round("What's the weather in Tokyo?"): print(msg.role, msg.text)

asyncio.run(main()) ```

kani guarantees that function calls are valid by the time they reach your methods while allowing you to focus on writing code. For more information, check out the function calling docs.

Streaming

kani supports streaming responses from the underlying language model token-by-token, even in the presence of function calls. Streaming is designed to be a drop-in superset of the chat_round and full_round methods, allowing you to gradually refactor your code without ever leaving it in a broken state.

``python async def stream_chat(): stream = ai.chat_round_stream("What does kani mean?") async for token in stream: print(token, end="") print() msg = await stream.message() # orawait stream`

async def streamwithfunctioncalling(): async for stream in ai.fullround_stream("What's the weather in Tokyo?"): async for token in stream: print(token, end="") print() msg = await stream.message() ```

Multimodal Inputs

kani optionally supports multimodal inputs (images, audio, video) for various language models. To use multimodal inputs, install the kani-multimodal-core extension package or use pip install "kani[multimodal]". See the kani-multimodal-core documentation for more info.

Read the kani-multimodal-core docs!

```python from kani import Kani from kani.engines.openai import OpenAIEngine from kani.ext.multimodal_core import ImagePart

engine = OpenAIEngine(model="gpt-4.1-nano") ai = Kani(engine)

notice how the arg is a list of parts rather than a single str!

msg = await ai.chatroundstr([ "Please describe these images:", ImagePart.fromfile("path/to/image.png"), await ImagePart.fromurl( "https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Whitehead%27sTrogon0A2A6014.jpg/1024px-Whitehead%27sTrogon0A2A6014.jpg" ), ]) print(msg)

```

Multimodal handling is deeply integrated with the rest of the kani ecosystem, so you get all the benefits of kani's fluent tool usage and automatic context management with minimal development cost!

kani CLI

kani comes with a CLI for you to chat with a model in your terminal with zero setup.

The kani CLI takes the form of $ kani <provider>:<model-id>. Use kani --help for more information.

Examples: shell $ kani openai:gpt-4.1-nano $ kani huggingface:meta-llama/Meta-Llama-3-8B-Instruct $ kani anthropic:claude-sonnet-4-0 $ kani google:gemini-2.5-flash

This CLI helper automatically creates a Engine and Kani instance, and calls chat_in_terminal() so you can test LLMs faster. When kani-multimodal-core is installed, you can provide multimodal media on your disk or on the internet to the model by prepending a path or URL with an @ symbol:

USER: Please describe this image: @path/to/image.png and also this one: @https://example.com/image.png

Why kani?

  • Lightweight and high-level - kani implements common boilerplate to interface with language models without forcing you to use opinionated prompt frameworks or complex library-specific tooling.
  • Model agnostic - kani provides a simple interface to implement: token counting and completion generation. kani lets developers switch which language model runs on the backend without major code refactors.
  • Automatic chat memory management - Allow chat sessions to flow without worrying about managing the number of tokens in the history - kani takes care of it.
  • Function calling with model feedback and retry - Give models access to functions in just one line of code. kani elegantly provides feedback about hallucinated parameters and errors and allows the model to retry calls.
  • You control the prompts - There are no hidden prompt hacks. We will never decide for you how to format your own data, unlike other popular language model libraries.
  • Fast to iterate and intuitive to learn - With kani, you only write Python - we handle the rest.
  • Asynchronous design from the start - kani can scale to run multiple chat sessions in parallel easily, without having to manage multiple processes or programs.

Existing frameworks for language models like LangChain and simpleaichat are opinionated and/or heavyweight - they edit developers' prompts under the hood, are challenging to learn, and are difficult to customize without adding a lot of high-maintenance bloat to your codebase.

kani

We built kani as a more flexible, simple, and robust alternative. A good analogy between frameworks would be to say that kani is to LangChain as Flask (or FastAPI) is to Django.

kani is appropriate for everyone from academic researchers to industry professionals to hobbyists to use without worrying about under-the-hood hacks.

Docs

To learn more about how to customize kani with your own prompt wrappers, function calling, and more, read the docs!

Or take a look at the hands-on examples in this repo.

Demo

Want to see kani in action? We run a small language model as part of our test suite right on GitHub Actions:

https://github.com/zhudotexe/kani/actions/workflows/pytest.yml?query=branch%3Amain+is%3Asuccess

Simply click on the latest build to see the model's output!

Who we are

University of Pennsylvania Logo

The core development team is made of three PhD students in the Department of Computer and Information Science at the University of Pennsylvania. We're all members of Prof. Chris Callison-Burch's lab, working towards advancing the future of NLP.

  • Andrew Zhu started in Fall 2022. His research interests include natural language processing, programming languages, distributed systems, and more. He's also a full-stack software engineer, proficient in all manner of backend, devops, database, and frontend engineering. Andrew strives to make idiomatic, clean, performant, and low-maintenance code — philosophies that are often rare in academia. His research is supported by the NSF Graduate Research Fellowship.
  • Liam Dugan started in Fall 2021. His research focuses primarily on large language models and how humans interact with them. In particular, he is interested in human detection of generated text and whether we can apply those insights to automatic detection systems. He is also interested in the practical application of large language models to education.
  • Alyssa Hwang started in Fall 2020 and is advised by Chris Callison-Burch and Andrew Head. Her research focuses on AI assistants that effectively communicate complex information, like voice assistants guiding users through instructions or audiobooks allowing users to seamlessly navigate through spoken text. Beyond research, Alyssa chairs the Penn CIS Doctoral Association, founded the CIS PhD Mentorship Program, and was supported by the NSF Graduate Research Fellowship Program.

We use kani actively in our research, and aim to keep it up-to-date with modern NLP practices.

Citation

If you use Kani, please cite us as:

@inproceedings{zhu-etal-2023-kani, title = "Kani: A Lightweight and Highly Hackable Framework for Building Language Model Applications", author = "Zhu, Andrew and Dugan, Liam and Hwang, Alyssa and Callison-Burch, Chris", editor = "Tan, Liling and Milajevs, Dmitrijs and Chauhan, Geeticka and Gwinnup, Jeremy and Rippeth, Elijah", booktitle = "Proceedings of the 3rd Workshop for Natural Language Processing Open Source Software (NLP-OSS 2023)", month = dec, year = "2023", address = "Singapore", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/2023.nlposs-1.8", doi = "10.18653/v1/2023.nlposs-1.8", pages = "65--77", }

Acknowledgements

We would like to thank the members of the lab of Chris Callison-Burch for their testing and detailed feedback on the contents of both our paper and the Kani repository. In addition, we’d like to thank Henry Zhu (no relation to the first author) for his early and enthusiastic support of the project.

This research is based upon work supported in part by the Air Force Research Laboratory (contract FA8750-23-C-0507), the IARPA HIATUS Program (contract 2022-22072200005), and the NSF (Award 1928631). Approved for Public Release, Distribution Unlimited. The views and conclusions contained herein are those of the authors and should not be interpreted as necessarily representing the official policies, either expressed or implied, of IARPA, NSF, or the U.S. Government.

Owner

  • Name: Andrew Zhu
  • Login: zhudotexe
  • Kind: user
  • Location: Philadelphia, PA
  • Company: University of Pennsylvania

PhD @ UPenn || there once was a girl from purdue / who kept a young cat in a pew / she taught it to speak / alphabetical Greek / but it never got farther than μ

Citation (citation.cff)

cff-version: 1.0.0
message: "If you use this software, please cite it as below."
authors:
- family-names: "Zhu"
  given-names: "Andrew"
  orcid: "https://orcid.org/0000-0002-6664-3215"
- family-names: "Dugan"
  given-names: "Liam"
  orcid: "https://orcid.org/0009-0006-5382-5235"
- family-names: "Hwang"
  given-names: "Alyssa"
  orcid: "https://orcid.org/0009-0006-4827-8505"
- family-names: "Callison-Burch"
  given-names: "Chris"
  orcid: "https://orcid.org/0000-0001-8196-1943"
title: "kani"
date-released: 2023-09-11
url: "https://github.com/zhudotexe/kani"
preferred-citation:
  type: generic
  authors:
  - family-names: "Zhu"
    given-names: "Andrew"
    orcid: "https://orcid.org/0000-0002-6664-3215"
  - family-names: "Dugan"
    given-names: "Liam"
    orcid: "https://orcid.org/0009-0006-5382-5235"
  - family-names: "Hwang"
    given-names: "Alyssa"
    orcid: "https://orcid.org/0009-0006-4827-8505"
  - family-names: "Callison-Burch"
    given-names: "Chris"
    orcid: "https://orcid.org/0000-0001-8196-1943"
  year: 2023
  month: 12
  title: "Kani: A Lightweight and Highly Hackable Framework for Building Language Model Applications"
  collection-title: "Proceedings of the 3rd Workshop for Natural Language Processing Open Source Software (NLP-OSS 2023)"
  collection-type: proceedings
  location: "Singapore"
  doi: "10.18653/v1/2023.nlposs-1.8"
  url: "https://aclanthology.org/2023.nlposs-1.8"
  publisher: "Association for Computational Linguistics"
  start: 65
  end: 77

GitHub Events

Total
  • Create event: 18
  • Issues event: 4
  • Release event: 10
  • Watch event: 32
  • Delete event: 11
  • Issue comment event: 7
  • Push event: 80
  • Pull request review event: 2
  • Pull request review comment event: 2
  • Pull request event: 6
  • Fork event: 3
Last Year
  • Create event: 18
  • Issues event: 4
  • Release event: 10
  • Watch event: 33
  • Delete event: 11
  • Issue comment event: 7
  • Push event: 80
  • Pull request review event: 2
  • Pull request review comment event: 2
  • Pull request event: 6
  • Fork event: 3

Committers

Last synced: 5 months ago

All Time
  • Total Commits: 528
  • Total Committers: 7
  • Avg Commits per committer: 75.429
  • Development Distribution Score (DDS): 0.044
Past Year
  • Commits: 124
  • Committers: 2
  • Avg Commits per committer: 62.0
  • Development Distribution Score (DDS): 0.008
Top Committers
Name Email Commits
Andrew Zhu me@a****m 505
Alyssa Hwang a****g@s****u 9
maknee h****2@g****m 6
ImgBotApp I****p@g****m 4
Liam Dugan l****n@s****u 2
Lawrence Akka l****a@f****l 1
Arturo León a****o@p****x 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 16
  • Total pull requests: 26
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 17 days
  • Total issue authors: 8
  • Total pull request authors: 9
  • Average comments per issue: 1.31
  • Average comments per pull request: 0.23
  • Merged pull requests: 21
  • Bot issues: 0
  • Bot pull requests: 4
Past Year
  • Issues: 2
  • Pull requests: 5
  • Average time to close issues: 8 days
  • Average time to close pull requests: 2 days
  • Issue authors: 2
  • Pull request authors: 3
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • zhudotexe (6)
  • oneilsh (2)
  • lawrenceakka (1)
  • lucifer0007 (1)
  • ranfdev (1)
  • dcssi (1)
  • evanbrociner (1)
  • ahwang16 (1)
Pull Request Authors
  • zhudotexe (17)
  • anthoeknee (4)
  • imgbot[bot] (4)
  • ahwang16 (3)
  • lawrenceakka (2)
  • Maknee (2)
  • arturoleon (1)
  • liamdugan (1)
Top Labels
Issue Labels
enhancement (10) bug (1) good first issue (1)
Pull Request Labels

Dependencies

.github/workflows/lint.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/pytest.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/pythonpublish.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • pypa/gh-action-pypi-publish release/v1 composite
pyproject.toml pypi
  • aiohttp >=3.0.0,<4.0.0
  • cachetools >=5.0.0,<6.0.0
  • pydantic >=2.0.0,<3.0.0
requirements.txt pypi
  • aiohttp >=3.0.0,<4.0.0
  • black *
  • build *
  • cachetools >=5.0.0,<6.0.0
  • furo *
  • pydantic >=2.0.0,<3.0.0
  • pytest *
  • sentencepiece *
  • sphinx *
  • sphinx-copybutton *
  • sphinx-inline-tabs *
  • sphinxemoji *
  • sphinxext-opengraph *
  • tiktoken *
  • torch *
  • transformers >=4.0.0,<5.0.0
  • twine *
.github/workflows/docs.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite