
kani 是一个轻量级且高度可破解的框架,用于基于聊天的语言模型,具有工具使用/函数调用功能。
与其他 LM 框架相比,kani 不那么固执己见,并且对重要的控制流部分提供了更细粒度的可定制性,使其成为 NLP 研究人员、爱好者和开发人员的完美选择。
kani 开箱即用地支持 OpenAI 模型和 LLaMA v2,并具有与模型无关的框架来添加对更多模型的支持。
kani 需要 Python 3.10 或更高版本。
首先,安装库。在本快速入门中,我们将使用 OpenAI 引擎,但 kani 与模型无关。
$ pip install "kani[openai]"然后,让我们使用 kani 创建一个简单的聊天机器人,并使用 ChatGPT 作为后端。
# import the library
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-3.5-turbo")
# 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! Check out
# the docs for how to use kani programmatically.
chat_in_terminal(ai)kani 缩短了建立工作聊天模型的时间,同时为程序员提供了对每个提示、函数调用甚至底层语言模型的深度可定制性。
函数调用使语言模型能够根据其文档选择何时调用您提供的函数。
借助 kani,您可以在 Python 中编写函数,并只需一行代码即可将它们公开给模型: @ai_function 装饰器。
# import the library
from typing import Annotated
from kani import AIParam, Kani, ai_function, chat_in_terminal
from kani.engines.openai import OpenAIEngine
# set up the engine as above
api_key = "sk-..."
engine = OpenAIEngine(api_key, model="gpt-3.5-turbo")
# 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)
chat_in_terminal(ai)langchain 和 simpleaichat 等语言模型的现有框架都是固执己见和/或重量级的 - 它们在幕后编辑开发人员的提示,学习起来很困难,并且很难在不向代码库添加大量高维护性膨胀的情况下进行自定义。
我们将 kani 构建得更加灵活、简单和健壮。kani 适合从学术研究人员到行业专业人士再到业余爱好者的每个人使用,而无需担心幕后黑客攻击。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。