> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Openvino 集成

> 使用 LangChain Python 集成 Openvino LLM。

[OpenVINO™](https://github.com/openvinotoolkit/openvino) 是一个开源工具包，用于优化和部署 AI 推理。OpenVINO™ 运行时可以启用在多种硬件 [设备](https://github.com/openvinotoolkit/openvino?tab=readme-ov-file#supported-hardware-matrix) 上运行经过优化的相同模型。加速您的深度学习性能，适用于语言 + LLMs、计算机视觉、自动语音识别等用例。

OpenVINO 模型可以通过 `HuggingFacePipeline` [类](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/llms/huggingface_pipelines.md) 本地运行。要在 OpenVINO 中部署模型，您可以指定 `backend="openvino"` 参数以触发 OpenVINO 作为后端推理框架。

要使用，您应该安装 `optimum-intel` 与 OpenVINO 加速器 python [包](https://github.com/huggingface/optimum-intel?tab=readme-ov-file#installation)。

```python theme={null}
pip install -U-strategy eager "optimum[openvino,nncf]" langchain-huggingface --quiet
```

### 模型加载

可以通过使用 `from_model_id` 方法指定模型参数来加载模型。

如果您有 Intel GPU，您可以指定 `model_kwargs={"device": "GPU"}` 以在其上运行推理。

```python theme={null}
from langchain_huggingface import HuggingFacePipeline

ov_config = {"PERFORMANCE_HINT": "LATENCY", "NUM_STREAMS": "1", "CACHE_DIR": ""}

ov_llm = HuggingFacePipeline.from_model_id(
    model_id="gpt2",
    task="text-generation",
    backend="openvino",
    model_kwargs={"device": "CPU", "ov_config": ov_config},
    pipeline_kwargs={"max_new_tokens": 10},
)
```

它们也可以通过直接传递现有的 [`optimum-intel`](https://huggingface.co/docs/optimum/main/en/intel/inference) 管道来加载

```python theme={null}
from optimum.intel.openvino import OVModelForCausalLM
from transformers import AutoTokenizer, pipeline

model_id = "gpt2"
device = "CPU"
tokenizer = AutoTokenizer.from_pretrained(model_id)
ov_model = OVModelForCausalLM.from_pretrained(
    model_id, export=True, device=device, ov_config=ov_config
)
ov_pipe = pipeline(
    "text-generation", model=ov_model, tokenizer=tokenizer, max_new_tokens=10
)
ov_llm = HuggingFacePipeline(pipeline=ov_pipe)
```

### 创建链

将模型加载到内存后，您可以将其与提示词组合形成链。

```python theme={null}
from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)

chain = prompt | ov_llm

question = "What is electroencephalography?"

print(chain.invoke({"question": question}))
```

要获取无需提示词的响应，您可以绑定 `skip_prompt=True` 与 LLM。

```python theme={null}
chain = prompt | ov_llm.bind(skip_prompt=True)

question = "What is electroencephalography?"

print(chain.invoke({"question": question}))
```

### 使用本地 OpenVINO 模型进行推理

可以使用 CLI 将您的模型 [导出](https://github.com/huggingface/optimum-intel?tab=readme-ov-file#export) 到 OpenVINO IR 格式，并从本地文件夹加载模型。

```python theme={null}
!optimum-cli export openvino --model gpt2 ov_model_dir
```

建议使用 `--weight-format` 应用 8 位或 4 位权重量化，以减少推理延迟和模型占用空间：

```python theme={null}
!optimum-cli export openvino --model gpt2  --weight-format int8 ov_model_dir # for 8-bit quantization

!optimum-cli export openvino --model gpt2  --weight-format int4 ov_model_dir # for 4-bit quantization
```

```python theme={null}
ov_llm = HuggingFacePipeline.from_model_id(
    model_id="ov_model_dir",
    task="text-generation",
    backend="openvino",
    model_kwargs={"device": "CPU", "ov_config": ov_config},
    pipeline_kwargs={"max_new_tokens": 10},
)

chain = prompt | ov_llm

question = "What is electroencephalography?"

print(chain.invoke({"question": question}))
```

您可以通过动态量化激活和 KV 缓存量化获得额外的推理速度提升。这些选项可以使用 `ov_config` 启用，如下所示：

```python theme={null}
ov_config = {
    "KV_CACHE_PRECISION": "u8",
    "DYNAMIC_QUANTIZATION_GROUP_SIZE": "32",
    "PERFORMANCE_HINT": "LATENCY",
    "NUM_STREAMS": "1",
    "CACHE_DIR": "",
}
```

### 流式输出

您可以使用 `stream` 方法获取 LLM 输出的流，

```python theme={null}
generation_config = {"skip_prompt": True, "pipeline_kwargs": {"max_new_tokens": 100}}
chain = prompt | ov_llm.bind(**generation_config)

for chunk in chain.stream(question):
    print(chunk, end="", flush=True)
```

更多信息请参考：

* [OpenVINO LLM 指南](https://docs.openvino.ai/2024/learn-openvino/llm_inference_guide.html)。

* [OpenVINO 文档](https://docs.openvino.ai/2024/home.html)。

* [OpenVINO 入门指南](https://www.intel.com/content/www/us/en/content-details/819067/openvino-get-started-guide.html)。

* [使用 LangChain 的 RAG 笔记本](https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-rag-langchain)。

***

> 官方原文：[src/oss/python/integrations/llms/openvino.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/llms/openvino.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.