Skip to content

Commit 42c2d17

Browse files
Add LangChain.js adapter for OpenVINO (#973)
* Add LangChain.js adapter for OpenVINO Signed-off-by: Kirill Suvorov <[email protected]> * Move into modules directory * Update dependency * npm pkg fix Signed-off-by: Kirill Suvorov <[email protected]> * Remove outdated docs * Update licenses --------- Signed-off-by: Kirill Suvorov <[email protected]>
1 parent b4230d4 commit 42c2d17

21 files changed

+8886
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Additional build instructions are available for the following modules:
3838
* [**nvidia_plugin**](./modules/nvidia_plugin/README.md)
3939
* [**custom_operations**](./modules/custom_operations/README.md)
4040
* [**ollama_OpenVINO**](./modules/ollama_openvino)
41+
* [**openvino-langchain**](./modules/openvino-langchain): LangChain.js integrations for OpenVINO™
42+
4143
## Update the repository documentation
4244
In order to keep a clean overview containing all contributed modules, the following files need to be created/adapted:
4345

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
node_modules
3+
sample/models
4+
types
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import globals from 'globals';
2+
import pluginJs from '@eslint/js';
3+
import { parser, ConfigArray } from 'typescript-eslint';
4+
5+
export default [
6+
pluginJs.configs.recommended,
7+
{
8+
ignores: [ 'types/', 'dist/' ],
9+
},
10+
{
11+
files: ['**/*.{js,mjs,cjs,ts}'],
12+
languageOptions: {
13+
globals: globals.node,
14+
parser: parser,
15+
},
16+
rules: {
17+
'semi': ['error'],
18+
'no-var': ['error'],
19+
'max-len': ['error', { 'ignoreUrls': true }],
20+
'eol-last': ['error'],
21+
'indent': ['error', 2],
22+
'camelcase': ['error'],
23+
'semi-spacing': ['error'],
24+
'arrow-spacing': ['error'],
25+
'comma-spacing': ['error'],
26+
'no-multi-spaces': ['error'],
27+
'quotes': ['error', 'single'],
28+
'no-trailing-spaces': ['error'],
29+
'space-before-blocks': ['error'],
30+
'newline-before-return': ['error'],
31+
'comma-dangle': ['error', 'always-multiline'],
32+
'space-before-function-paren': ['error', {
33+
named: 'never',
34+
anonymous: 'never',
35+
asyncArrow: 'always',
36+
}],
37+
'key-spacing': ['error', { beforeColon: false }],
38+
'no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0, maxEOF: 0 }],
39+
'keyword-spacing': ['error', { overrides: { catch: { after: false } } }],
40+
'prefer-destructuring': ['error', { object: true, array: false }],
41+
'no-explicit-any': 0,
42+
'no-unused-vars': ['error', { args: 'none' } ],
43+
},
44+
},
45+
] satisfies ConfigArray;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
3+
module.exports = {
4+
preset: 'ts-jest/presets/default-esm',
5+
testEnvironment: './jest.env.cjs',
6+
modulePathIgnorePatterns: ['dist/', 'docs/'],
7+
moduleNameMapper: {
8+
'^(\\.{1,2}/.*)\\.(c)*js$': '$1',
9+
},
10+
transform: {
11+
'^.+\\.tsx?$': ['@swc/jest'],
12+
},
13+
transformIgnorePatterns: [
14+
'/node_modules/',
15+
'\\.pnp\\.[^\\/]+$',
16+
'./scripts/jest-setup-after-env.js',
17+
],
18+
setupFiles: ['dotenv/config'],
19+
setupFilesAfterEnv: ['./scripts/jest-setup-after-env.js'],
20+
testTimeout: 20_000,
21+
collectCoverageFrom: ['src/**/*.ts'],
22+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { TestEnvironment } = require('jest-environment-node');
2+
3+
class AdjustedTestEnvironmentToSupportFloat32Array extends TestEnvironment {
4+
constructor(config, context) {
5+
// Make `instanceof Float32Array` return true in tests
6+
// to avoid https://github.com/xenova/transformers.js/issues/57 and https://github.com/jestjs/jest/issues/2549
7+
super(config, context);
8+
this.global.Float32Array = Float32Array;
9+
}
10+
}
11+
12+
module.exports = AdjustedTestEnvironmentToSupportFloat32Array;

0 commit comments

Comments
 (0)