如何用 C++ 构建 OpenVINO™ 生成式 AI 应用

英特尔开发人员专区 2024-10-11 08:31:01 阅读 81

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

介绍

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

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

目录

        下载 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”选择最新版本。

Image

下载完成后,解压该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 文件:

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

Image

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

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

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

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

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

<code>pip install huggingface_hubhuggingface-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 wellov::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.



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。