|
| 1 | +# OpenVINO™ LangChain.js adapter |
| 2 | + |
| 3 | +This package contains the LangChain.js integrations for OpenVINO™ |
| 4 | + |
| 5 | +> **Disclaimer** |
| 6 | +> It's preview version, do not use it on production! |
| 7 | +
|
| 8 | +## Introduction |
| 9 | + |
| 10 | +OpenVINO is an open-source toolkit for deploying performant AI solutions. Convert, optimize, and run inference on local hardware utilizing the full potential of Intel® hardware. |
| 11 | + |
| 12 | +## Installation and Setup |
| 13 | + |
| 14 | +See [this section](https://js.langchain.com/docs/how_to/installation#installing-integration-packages) for general instructions on installing integration packages. |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install openvino-langchain |
| 18 | +``` |
| 19 | + |
| 20 | +### Export your model to the OpenVINO™ IR |
| 21 | + |
| 22 | +In order to use OpenVINO, you need to convert and compress the text generation model into the [OpenVINO IR format](https://docs.openvino.ai/2025/documentation/openvino-ir-format.html). |
| 23 | + |
| 24 | +Tested compatibility with: |
| 25 | + - BAAI/bge-small-en-v1.5 (Embeddings model) |
| 26 | + - openlm-research/open_llama_7b_v2 |
| 27 | + - meta-llama/Llama-2-13b-chat-hf |
| 28 | + - microsoft/Phi-3.5-mini-instruct |
| 29 | + |
| 30 | +#### Use HuggingFace Hub |
| 31 | + |
| 32 | +Pre-converted and pre-optimized models are available under the [LLM collections](https://huggingface.co/collections/OpenVINO/llm-6687aaa2abca3bbcec71a9bd) in the [OpenVINO Toolkit](https://huggingface.co/OpenVINO) organization. |
| 33 | + |
| 34 | +To export another [model](https://huggingface.co/docs/optimum/main/en/intel/openvino/models) hosted on the [HuggingFace Hub](https://huggingface.co/models) you can use [OpenVINO space](https://huggingface.co/spaces/echarlaix/openvino-export). After conversion, a repository will be pushed under your namespace, this repository can be either public or private. |
| 35 | + |
| 36 | +#### Use the Optimum Intel |
| 37 | + |
| 38 | +[Optimum Intel](https://github.com/huggingface/optimum-intel) provides a simple interface to optimize your Transformers and Diffusers models and convert them to the OpenVINO Intermediate Representation (IR) format. |
| 39 | + |
| 40 | +Firstly install Optimum Intel for OpenVINO: |
| 41 | + |
| 42 | +```bash |
| 43 | +pip install --upgrade --upgrade-strategy eager "optimum[openvino]" |
| 44 | +``` |
| 45 | + |
| 46 | +Then you download and convert a model to OpenVINO: |
| 47 | + |
| 48 | +```bash |
| 49 | +optimum-cli export openvino --model <model_id> --trust-remote-code <exported_model_name> |
| 50 | +``` |
| 51 | +> **Note:** Any model_id, for example "TinyLlama/TinyLlama-1.1B-Chat-v1.0", or the path to a local model file can be used. |
| 52 | +
|
| 53 | +Optimum-Intel API also provides out-of-the-box model optimization through weight compression using NNCF which substantially reduces the model footprint and inference latency: |
| 54 | + |
| 55 | +```bash |
| 56 | +optimum-cli export openvino --model "TinyLlama/TinyLlama-1.1B-Chat-v1.0" --weight-format int4 --trust-remote-code "TinyLlama-1.1B-Chat-v1.0" |
| 57 | +``` |
| 58 | + |
| 59 | +## LLM |
| 60 | + |
| 61 | +This package contains the `GenAI` class, which is the recommended way to interact with models optimized for the OpenVINO toolkit. |
| 62 | + |
| 63 | +**GenAI Parameters** |
| 64 | + |
| 65 | +| Name | Type | Required | Description | |
| 66 | +| ----- | ---- |--------- | ----------- | |
| 67 | +| modelPath | string | ✅ | Path to the directory containing model xml/bin files and tokenizer | |
| 68 | +| device | string | ❌ | Device to run the model on (e.g., CPU, GPU). | |
| 69 | +| generationConfig | [GenerationConfig](https://github.com/openvinotoolkit/openvino.genai/blob/master/src/js/lib/utils.ts#L107-L110) | ❌ | Structure to keep generation config parameters. | |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { GenAI } from "openvino-langchain"; |
| 73 | + |
| 74 | +const model = new GenAI({ |
| 75 | + modelPath: "path-to-model", |
| 76 | + device: "CPU", |
| 77 | + generationConfig: { |
| 78 | + "max_new_tokens": 100, |
| 79 | + }, |
| 80 | + }); |
| 81 | +const response = await model.invoke("Hello, world!"); |
| 82 | +``` |
| 83 | + |
| 84 | +## Text Embedding Model |
| 85 | + |
| 86 | +This package also adds support for OpenVINO's embeddings model. |
| 87 | + |
| 88 | +| Name | Type | Required | Description | |
| 89 | +| ----- | ---- |--------- | ----------- | |
| 90 | +| modelPath | string | ✅ | Path to the directory containing embeddings model | |
| 91 | +| device | string | ❌ | Device to run the embeddings model on (e.g., CPU, GPU). | |
| 92 | + |
| 93 | +```typescript |
| 94 | +import { OvEmbeddings } from "openvino-langchain"; |
| 95 | + |
| 96 | +const embeddings = new OvEmbeddings({ |
| 97 | + modelPath: "path-to-model", |
| 98 | + device: "CPU", |
| 99 | +}); |
| 100 | +const res = await embeddings.embedQuery("Hello world"); |
| 101 | +``` |
0 commit comments