跳转至

本地模型支持

适配了OpenAI接口规范的本地部署模型

Agently能不能支持使用开源模型/本地模型来驱动Agent,进行应用开发?”这个问题是Agently开发者最常问到的一个问题。

因为本地部署的模型在输出方式上差异非常大,为了更好地进行适配,我们需要经过一层标准化封装。目前市面上主流的方式是与OpenAI的API请求格式进行标准化对齐,在模型请求设置的说明中,我们提到的月之暗面Kimi和Mixtral都使用了这种方式。目前有一些优秀的项目,能够帮助开发者快速完成开源模型下载、本地推理部署并封装成接口服务的全过程。

我们推荐您使用Xinference提供的一键部署能力,进行快速本地部署。您可以点击这里查看Xinference已经支持的模型列表。

您也可以查看LiteLLMone-api等优秀的项目来帮助您部署本地模型。

在完成模型部署之后,您可以使用我们提供的OAIClient方案进行请求,并驱动您的Agent实例。

Agently x Xinference配合使用案例

本案例提供Jupyter文件进行实际练习,您可以点击查看Github上案例文件,或者您可以直接通过Google Colab打开案例文档,在线操作。

在本案例中,我们将展示如何使用Xinference来启动一个本地私有的开源模型API服务,并使用Agently应用开发框架通过简单的设置就能访问这个私有模型API,让私有模型驱动Agent,进行应用开发。

Xinference的使用方式非常简单,并且支持你在Google Colab免费提供的T4环境中就能启动一个小参数的模型,这极大地降低了开发者上手尝试的门槛。你也可以根据本文档提供的方法,在拥有更强算力的本地环境中快速启动更大参数的模型。

第一步:安装Agently和Xinference

pip install -U -q typing_extensions xinference[transformers] Agently

第二步:启动Xinference的本地服务

要启动一个本地运行的Xinference服务实例,只需要通过nohup后台运行xinferencce即可:

nohup xinference-local > xinference.log 2>&1 &

本行指令将会启动一个运行在Colab机器上的Xinference服务,该服务的默认地址是127.0.0.1,默认端口为9997。

第三步:下载并运行一个开源模型

Xinference内置支持了一系列语言模型,可以通过点击这里查看详细列表。

接下来让我们尝试一下通义千问1.5的1.8B参数小模型Qwen1.5-chat-1_8B(适合在Colab环境中使用的小模型)。

xinference launch --model-name qwen1.5-chat --size-in-billions 1_8 --model-format pytorch -u my-llm

下载模型需要花一些时间,请放松等待☕️。

当下载和启动步骤都完成之后,你将会看到一个关于model uid控制台提示信息,如Model uid: my-llm,这个uid与你在命令中的-u参数设置一致,接下来我们会在Agently框架的设置中使用到它。

第四步:使用Agently框架请求本地模型服务,开始你的开发吧

然后你就可以开始使用Agently开发框架进行你的LLMs应用开发了~

因为我们使用的是1.8B参数的小模型,模型对于结构化输出的遵从能力不足,下面的例子主要体现模型的常规问答能力。在学会本案例之后,你可以根据自己的需要选择适合当前场景的模型来驱动你的Agent实例,以完成不同复杂度的任务。

import Agently

agent_factory = (
    Agently.AgentFactory()
        .set_settings("model.OpenAI.auth.api_key", "nothing")
        .set_settings("model.OpenAI.options", {"model": "my-llm"})
        .set_settings("model.OpenAI.url", "http://127.0.0.1:9997/v1")
)

agent = agent_factory.create_agent()

@agent.on_event("delta")
def handler(data):
    print(data, end="")

result = (
    agent
        .input("How to wake up Siri in Apple iOS?")
        .start()
)

输出结果样例:

To wake up Siri in Apple iOS, follow these steps:

1. Tap on the multitasking bar at the bottom of your screen.
2. Scroll down and tap on the "Siri" icon or press and hold it until you see a list of options.
3. Choose the option that says "Wake Me Up."
4. If Siri is already active, you may need to swipe left from the multitasking bar to dismiss Siri's display.

Alternatively, you can also use voice commands to wake up Siri by saying "Hey Siri" when you turn on your device, or by using the Wake Up function on the Siri app on your iPhone. Here's how:

- When you turn on your phone, say "Hey Siri" loudly from within the same app (e.g., Settings > Siri).
- If Siri is already active, say "Wake Up" again to bring up her interface.

Alternatively, if you're using an older version of iOS, you can also use the "Hey Siri" feature to wake up Siri directly from the lock screen. Follow these steps:

- On your iPhone, open the lock screen.
- Say "Hey Siri" aloud to wake up Siri.
- Once Siri is activated, she will appear on your screen as usual.

Keep in mind that Siri's wake-up functionality may vary depending on your model of iPhone and iOS version. For example, newer devices may have a dedicated Wake Up button or gesture recognition to activate Siri more easily. It's always a good idea to check the documentation for your specific iPhone model to ensure that you know how to wake it up using Siri's built-in features.