卡尼(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或美国政府的官方政策,即表示或暗示的官方政策。