> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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™ Runtime 可以支持在多种硬件 [设备](https://github.com/openvinotoolkit/openvino?tab=readme-ov-file#supported-hardware-matrix) 上运行经过优化的同一模型。加速您的深度学习性能，适用于语言 + LLM、计算机视觉、自动语音识别等多种用例。

可以通过 `HuggingFacePipeline` [类](https://python.langchain.com/docs/integrations/llms/huggingface_pipeline) 在本地运行 OpenVINO 模型。要使用 OpenVINO 部署模型，您可以指定 `backend="openvino"` 参数以将 OpenVINO 触发为后端推理框架。

要使用它，您应该已安装带有 OpenVINO Accelerator 的 `optimum-intel` Python [包已安装](https://github.com/huggingface/optimum-intel?tab=readme-ov-file#installation)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -U-strategy eager "optimum[openvino,nncf]" langchain-huggingface --quiet
```

### 模型加载

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

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
!optimum-cli export openvino --model gpt2 ov_model_dir
```

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
!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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
ov_config = {
    "KV_CACHE_PRECISION": "u8",
    "DYNAMIC_QUANTIZATION_GROUP_SIZE": "32",
    "PERFORMANCE_HINT": "LATENCY",
    "NUM_STREAMS": "1",
    "CACHE_DIR": "",
}
```

### 流式传输

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\llms\openvino.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
