卡尼(Kani)(カニ)是一個輕巧且高度可入侵的框架,用於帶有工具使用/功能調用的基於聊天的語言模型。
與其他LM框架相比,卡尼(Kani)的主意較少,並且在控制流的各個部分方面提供了更精細的可定制性,這使其成為NLP研究人員,業餘愛好者和開發人員的理想選擇。
Kani在開箱即用的以下模型的支持下提供了支持,並帶有模型不合時宜的框架,以增加對更多支持的支持:
託管模型
開源模型
Kani支持通過transformers
或llama.cpp
擁抱面孔上可用的每個聊天模型!
特別是,我們為以下基本模型及其微型提供了參考實現:
查看模型動物園,以了解如何在應用程序中使用這些模型!
有興趣貢獻嗎?查看我們的指南。
閱讀有關ReadThedocs的文檔!
閱讀有關Arxiv的論文!
卡尼需要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 "
卡尼需要Python 3.10或更高。
首先,安裝庫。在此QuickStart中,我們將使用OpenAI引擎,儘管Kani是模型不可替代的。
$ 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中編寫功能,並僅使用一行代碼將其暴露於模型: @ai_function
Decorator。
# 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保證函數調用在達到您的方法的同時使您專注於編寫代碼時有效。有關更多信息,請查看函數調用文檔。
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)適合從學術研究人員到行業專業人士到業餘愛好者的所有人,而不必擔心黑客。
要了解有關如何使用自己的及時包裝器,功能呼叫等自定義卡尼的更多信息,請閱讀文檔!
或者查看此存儲庫中的動手實例。
想看到卡尼在行動嗎?使用4位量化來縮小模型,我們在GitHub動作上運行Llama V2作為測試套件的一部分:
https://github.com/zhudotexe/kani/actions/workflows/pytest.yml?query=branch%3amain+isas%3Asuccess
只需單擊最新的構建即可查看Llama的輸出!
核心開發團隊由賓夕法尼亞大學計算機和信息科學系的三名博士學位學生組成。我們都是克里斯·卡里森(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存儲庫的內容進行了詳細的反饋。此外,我們要感謝Henry Zhu(與第一作者無關)對該項目的早期和熱情的支持。
這項研究基於空軍研究實驗室(合同FA8750-23-C-0507),IARPA HIATUS計劃(合同2022-220722005)和NSF(1928631獎)的部分支持。批准公開發布,分銷無限。本文所包含的觀點和結論是作者的觀點,不應被解釋為一定代表IARPA,NSF或美國政府的官方政策,即表示或暗示的官方政策。