Kani(カニ)は、ツールの使用/関数呼び出しを備えたチャットベースの言語モデル向けの軽量で高度にハッキング可能なフレームワークです。
他のLMフレームワークと比較して、Kaniは意見を述べておらず、重要なコントロールフローの部分に対してより微調整されたカスタマイズ可能性を提供し、NLPの研究者、愛好家、開発者にとっても最適です。
Kaniには、次のモデルのサポートが箱から出され、さらに多くのサポートを追加するためのモデルに依存しないフレームワークが付属しています。
ホストモデル
オープンソースモデル
Kaniは、 transformers
またはllama.cpp
を介して顔を抱き締めることで利用できるすべてのチャットモデルをサポートしています!
特に、次のベースモデルの参照実装とそれらの微調整があります。
モデル動物園をチェックして、アプリケーションでこれらの各モデルを使用する方法を確認してください!
貢献に興味がありますか?ガイドをご覧ください。
readthedocsのドキュメントを読んでください!
Arxivに関する論文を読んでください!
KaniにはPython 3.10以上が必要です。モデル固有の依存関係をインストールするために、Kaniはさまざまなエクストラ( pip install
のライブラリ名の後のブラケット)を使用します。インストールする余分な(s)を決定するには、モデルテーブルを参照するか、すべてをインストールするために[all]
追加を使用します。
# for OpenAI models
$ pip install " kani[openai] "
# for Hugging Face models
$ pip install " kani[huggingface] " torch
# or install everything:
$ pip install " kani[all] "
最新の変更と新しいモデルについては、Gitのmain
ブランチから開発バージョンをインストールすることもできます。
$ pip install " kani[all] @ git+https://github.com/zhudotexe/kani.git@main "
KaniにはPython 3.10以上が必要です。
まず、ライブラリをインストールします。このQuickStartでは、Kaniはモデルに依存していますが、Openaiエンジンを使用します。
$ pip install " kani[openai] "
次に、Kaniを使用して、ChatGPTをバックエンドとして使用してシンプルなチャットボットを作成しましょう。
# import the library
import asyncio
from kani import Kani , chat_in_terminal
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-4o-mini" )
# 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...
chat_in_terminal ( 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は、作業チャットモデルを短く設定する時間を作成し、プログラマーにすべてのプロンプト、機能コール、さらには基礎となる言語モデルで深いカスタマイズ可能性を提供します。
関数呼び出しにより、言語モデルは、ドキュメントに基づいて提供する関数を呼び出すときを選択する機能を提供します。
Kaniを使用すると、Pythonで関数を書き込み、1つのコードのみでモデルに公開できます: @ai_function
Decrator。
# import the library
import asyncio
from typing import Annotated
from kani import AIParam , Kani , ai_function , chat_in_terminal , ChatRole
from kani . engines . openai import OpenAIEngine
# set up the engine as above
api_key = "sk-..."
engine = OpenAIEngine ( api_key , model = "gpt-4o-mini" )
# subclass Kani to add AI functions
class MyKani ( Kani ):
# Adding the annotation to a method exposes it to the AI
@ ai_function ()
def get_weather (
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...
chat_in_terminal ( 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は、コードの作成に集中できるようにしながら、メソッドに到達するまでに関数呼び出しが有効であることを保証します。詳細については、docsを呼び出す関数をご覧ください。
Kaniは、関数呼び出しが存在する場合でも、基礎となる言語モデルトークンごとのストリーミング応答をサポートしています。ストリーミングは、 chat_round
およびfull_round
メソッドのドロップインスーパーセットになるように設計されており、コードを壊れた状態にしておくことなく徐々にリファクタリングできます。
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 () # or `await stream`
async def stream_with_function_calling ():
async for stream in ai . full_round_stream ( "What's the weather in Tokyo?" ):
async for token in stream :
print ( token , end = "" )
print ()
msg = await stream . message ()
LangchainやSimpleaichatなどの言語モデルの既存のフレームワークは、意見を述べたり、ヘビー級です。彼らは、ボンネットの下で開発者のプロンプトを編集し、学習するのが難しく、コードベースに多くのメンテナンスの膨張を追加せずにカスタマイズすることが困難です。
私たちは、より柔軟でシンプルで堅牢な代替品としてKaniを構築しました。フレームワーク間の良い類似性は、フラスコ(またはFastapi)がDjangoにあるので、KaniがLangchainであると言うことです。
Kaniは、学術研究者から業界の専門家、愛好家まで、フード下のハッキングを心配することなく使用するために使用するために、すべての人に適しています。
独自の迅速なラッパー、機能呼び出しなどでKaniをカスタマイズする方法の詳細については、ドキュメントをお読みください!
または、このリポジトリの実践的な例をご覧ください。
カニが動作しているのを見たいですか? 4ビットの量子化を使用してモデルを縮小すると、GitHubアクションでテストスイートの一部としてLlama V2を実行します。
https://github.com/zhudotexe/kani/actions/workflows/pytest.yml?query=branch%3amain+is%3asuccess
最新のビルドをクリックして、Llamaの出力を確認してください!
コア開発チームは、ペンシルベニア大学のコンピューター情報科学部の3人の博士課程学生で作られています。私たちは皆、Chris Callison-Burch教授の研究室のメンバーであり、NLPの未来を前進させることに取り組んでいます。
私たちは私たちの研究で積極的にKaniを使用しており、最新のNLPプラクティスを最新の状態に保つことを目指しています。
Kaniを使用している場合は、次のように引用してください。
@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",
}
Chris Callison-Burchのラボのメンバーに、テストと、私たちの論文とKaniリポジトリの両方の内容に関する詳細なフィードバックに感謝します。さらに、プロジェクトの初期かつ熱狂的なサポートについて、ヘンリーズー(最初の著者とは関係ありません)に感謝したいと思います。
この研究は、空軍研究所(契約FA8750-23-C-0507)、IARPA HIATUSプログラム(契約2022-220722200005)、およびNSF(賞1928631)によって部分的にサポートされている作業に基づいています。パブリックリリース、Distribution Unlimitedが承認されました。本明細書に含まれる見解と結論は著者の見解であり、IARPA、NSF、または米国政府の表明または黙示のいずれかの公式政策を必ずしも表現するものとして解釈されるべきではない。