Qwen2 是阿里巴巴集团 Qwen 团队研发的大语言模型和大型多模态模型系列。Qwen2 具备自然语言理解、文本生成、视觉理解、音频理解、工具使用、角色扮演、作为 AI Agent 进行互动等多种能力。

图:智能体中工具使用执行逻辑

Qwen-Agent 是一个 AI 智能体的开发框架。开发者可基于本框架开发 Agent 应用,充分利用基于通义千问模型(Qwen)的指令遵循、工具使用、规划、记忆能力。同时该框架也提供了浏览器助手、代码解释器、自定义助手等示例应用。

近期 OpenVINO™ 工具套件也作为 Qwen-Agent 的大语言模型推理后端,被集成到 llm 组件中,接下来就让我们一起看下如何在 Intel 硬件平台上通过 OpenVINO™ 和 Qwen2 构建一个纯本地运行的 AI 智能体。

转化压缩 Qwen2 模型

第一步我们需要安装 Optimum-intel 组件,以此来导出并量化原始的 Qwen2 模型,使用方法可以参考以下示例。

pip install optimum[openvino]
pip install modelscope
modelscope download --model=qwen/Qwen2-7B-Instruct --local_dir ./Qwen2-7B-Instruct
optimum-cli export openvino --model ./Qwen2-7B-Instruct --task text-generation-with-past --trust-remote-code --weight-format int4 ./Qwen2-7B-Instruct-int4

构建工具

Qwen-Agent 提供了注册工具的机制,例如,下面我们注册一个自己的图片生成工具:

  • 指定工具的name、description、和parameters,注意 @register_tool('my_image_gen') 中的 'my_image_gen' 会被自动添加为这个类的 .name 属性,将作为工具的唯一标识。

  • 实现 call(...) 函数

在这个例子中,我们定义了一个调用云端 API 工具,用来根据输入请求,生成图片。

@register_tool("image_generation")
class ImageGeneration(BaseTool):
    description = "AI painting (image generation) service, input text description, and return the image URL drawn based on text information."
    parameters = [{"name": "prompt", "type": "string", "description": "Detailed description of the desired image content, in English", "required": True}]

    def call(self, params: str, **kwargs) -> str:
        prompt = json5.loads(params)["prompt"]
        prompt = urllib.parse.quote(prompt)
        return json5.dumps({"image_url": f"https://image.pollinations.ai/prompt/{prompt}"}, ensure_ascii=False)

创建基于 OpenVINO™的 AI 智能体

Qwen-Agent 中的 LLM 统一使用 get_chat_model(cfg: Optional[Dict] = None) -> BaseChatModel 接口来调用,参数传入 LLM 的配置文件,目前 OpenVINO™ 的 LLM  配置文件格式如下:

llm_cfg = {
    "ov_model_dir": model_path,
    "model_type": "openvino",
    "device": device.value,
    "ov_config": ov_config,
    "generate_cfg": {"top_p": 0.8},
}

其中各类参数的要求为:

  • ov_model_dir: 在第一步中得到的 OpenVINO™ 模型路径

  • model_type: 对应某个具体的llm类,这里需要指定为 “openvino”

  • device: Intel设备名称,目前支持”cpu”及“gpu”

  • ov_config: OpenVINO infer request中的可配置项

  • generate_cfg:模型生成时候的参数

Qwen-Agent 框架为我们提供了自带的智能体实现(如 class Assistant ),开发者可以直接将定义好的 OpenVINO™ LLM 配置文件传入该对象中,快速构建智能体应用,为此 Qwen-Agent 也提供了丰富的[代码示例]

( https://github.com/QwenLM/Qwen-Agent/tree/main/examples)

bot = Assistant(llm=llm_cfg, function_list=tools, name="OpenVINO Agent")

完整示例和实现效果

同时基于以上流程,我们也在OpenVINO Notebook 仓库中准备了完整示例供大家测试:

https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-agent-functioncall

该示例会理解用户意图,并调用多种预先定义好的工具来完成任务,包括, wikipedia 查询工具,天气查询工具和绘图工具。以下截图便是该 notebook 示例在 Intel AIPC 上所呈现的本地部署效果:

图:Qwen-Agent与 OpenVINOTM智能体示例

可以看到在这个例子中,智能体首先会将用户的请求按任务进行拆解,并分别调用不同的工具获得对应的输出结果,并将这些输出结果合并后,作为最终答案反馈给用户。

总结

AI智能体作为通用人工智能的核心载体,可以模仿人类的思维逻辑,将复杂任务进行拆解,并借助外部工具解决任务。通过利用 OpenVINO™ 和 Qwen-Agent 这样的工具,我们可以非常快捷地在本地构建一个 AI 智能体应用,在保护用户数据隐私的同时,更快速地响应任务需求,简单任务调用本地工具处理,复杂任务调用云端资源处理。

参考资料:

  • Qwen model: https://modelscope.cn/organization/qwen

  • Qwen-Agent:https://github.com/QwenLM/Qwen-Agent

  • OpenVINO notebook:https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-agent-functioncall

 

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐