实战精选 | 如何用 C++ 构建 OpenVINO™ 生成式 AI 应用

点击蓝字

关注我们,让开发变得更有趣

作者 | 武卓  英特尔 AI 软件布道师

排版 | 吴紫琴

OpenVINO™

介绍

许多桌面应用程序是使用 C++ 开发的,而将生成式AI(GenAI)功能集成到这些应用程序中可能会很具有挑战性,尤其是因为使用像 Hugging Face 这样的 Python 库的复杂性。C++ 结合 OpenVINO™ Runtime 提供了一个更加简化、轻量且内存高效的解决方案,尤其适用于 Windows 环境。你可以在此找到详细的依赖项对比。

OpenVINO™ GenAI API 提供了一个原生的 C++ 接口,消除了对 Python 依赖的需求,使开发者可以用更高效的方式创建 AI 应用程序。下面是一个基于 Windows 操作系统的分步骤指南,手把手教你如何在 C++ 中构建 OpenVINO™ GenAI 应用程序。

OpenVINO™

扫码申请(或阅读原文报名)

下载 OpenVINO™ 压缩包并安装依赖

构建项目

下载和转换LLM及分词器

运行模型

总结

第一步:下载并解压OpenVINO压缩包

访问 OpenVINO™ 下载页面,

https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/download.html?PACKAGE=OPENVINO_BASE&VERSION=v_2024_4_0&OP_SYSTEM=WINDOWS&DISTRIBUTION=PIP

并点击“Download Archives with GenAI”选择最新版本。

11b2057acd5ee77ce524f94c432e4ed4.png

下载完成后,解压该zip文件并将内容提取到

<your path >\openvino_genai_windows_2024.3.0.0_x86_64

第二步:构建项目

确保你的电脑上已安装以下软件组件:

  • CMake 3.23 或更高版本

  • Microsoft Visual Studio 2019 或更高版本,16.3或更新

  • Python 3.8 或更高版本

打开命令窗口,并在下载并解压后的 OpenVINO™ GenAI 文件夹中运行 setupvars.bat 文件:

<your_path>\openvino_genai_windows_2024.3.0.0_x86_64\setupvars.bat

397a71db5bf674165869d3a7b7ee7987.png

在同一个命令窗口中,OpenVINO™ 环境初始化后,导航到 samples/cpp/ 文件夹,然后运行 build_samples_msvc.bat。

当所有构建过程完成后,你可以在构建输出中指示的路径找到 chat_sample.exe 文件。

c22d9f37be34d723146cfd26bf73c480.png

71505b66b01a568dce0e5cb806ce6b72.png

第三步:下载和转换LLM及分词器

你有两种选择来为AI推理准备模型:

1. 下载已转换的模型:从 Hugging      Face 上的 OpenVINO™ LLMs 集合中直接下载模型:

pip install huggingface_hub
huggingface-cli download OpenVINO/TinyLlama-1.1B-Chat-v1.0-int4-ov --local-dir TinyLlama-1.1B-Chat-v1

请参阅此处的说明:

https://huggingface.co/OpenVINO/TinyLlama-1.1B-Chat-v1.0-int4-ov

在 Hugging Face 上的 OpenVINO™ LLMs 集合中还有其他可用模型,欢迎探索更多模型。

2. 本地转换模型:使用 optimum Intel 在你的设备上转换模型。请确保根据此处的详细要求安装所需的依赖项:

optimum-cli export openvino --model "TinyLlama/TinyLlama-1.1B-Chat-v1.0" --trust-remote-code "TinyLlama-1.1B-Chat-v1.0"

然后,你可以使用已构建的文件和LLM模型的路径开始对话,例如:

chat_sample TinyLlama-1.1B-Chat-v1.0

第四步:运行模型

现在开始享受与基于LLM的聊天机器人进行对话吧!

注意,上述展示的是在CPU上运行LLM推理的示例,然而,只需在

 \openvino_genai_windows_2024.3.0.0_x86_64\samples\cpp\chat_sample.cpp

文件中的以下两行代码中将“CPU”替换为“GPU”,就可以轻松切换为在GPU上运行推理,并再次构建:

std::string device = "GPU";  // GPU can be used as well
ov::genai::LLMPipeline pipe(model_path, device);

结论:

使用 OpenVINO™ GenAI API 在 C++ 中构建 AI 应用程序只需几个步骤。探索并尝试这个 API,充分释放其潜力吧!

阅读更多:

更多关于生成式 AI 与 OpenVINO™ 的相关指南:

如何使用 OpenVINO™ 生成式AI API (GenAI API) 以更少的代码构建更快的生成式AI 应用

Additional Resources

OpenVINO™ Documentation

https://docs.openvino.ai/2024/index.html

OpenVINO™ Notebooks

https://github.com/openvinotoolkit/openvino_notebooks

Provide Feedback & Report Issues

https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fopenvinotoolkit%2Fopenvino%2Fissues%2Fnew%2Fchoose

About the Authors & Editors:

Notices & Disclaimers

Performance varies by use, configuration, and other factors. Learn more on the Performance Index site.

Performance results are based on testing as of dates shown in configurations and may not reflect all publicly available updates. See backup for configuration details. No product or component can be absolutely secure. Your costs and results may vary. Intel technologies may require enabled hardware, software or service activation.

© Intel Corporation. Intel, the Intel logo, and other Intel marks are trademarks of Intel Corporation or its subsidiaries.

OpenVINO™

---------------------------------------

*OpenVINO and the OpenVINO logo are trademarks of Intel Corporation or its subsidiaries.

-----------------------------

 OpenVINO 中文社区 

微信号 : openvinodev

B站:OpenVINO中文社区

“开放、开源、共创”

致力于通过定期举办线上与线下的沙龙、动手实践及开发者交流大会等活动,促进人工智能开发者之间的交流学习。

○ 点击 “ 在看 ”,让更多人看见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值