Skip to content

Commit 2c6ea38

Browse files
[openvino-langchain] Fix embeddings (#977)
Details: Fixed OpenVINOEmbeddings work with the embedding models. Expanded list of tested embedding models Updated env variable for embedding test --------- Signed-off-by: Kirill Suvorov <[email protected]> Co-authored-by: Alicja Miloszewska <[email protected]>
1 parent 42c2d17 commit 2c6ea38

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

modules/openvino-langchain/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ npm install openvino-langchain
2121

2222
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).
2323

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
24+
The following models are tested:
25+
* Embeddings models:
26+
- BAAI/bge-small-en-v1.5
27+
- intfloat/multilingual-e5-large
28+
- sentence-transformers/all-MiniLM-L12-v2
29+
- sentence-transformers/all-mpnet-base-v2
30+
* Large language models:
31+
- openlm-research/open_llama_7b_v2
32+
- meta-llama/Llama-2-13b-chat-hf
33+
- microsoft/Phi-3.5-mini-instruct
34+
- deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
2935

3036
#### Use HuggingFace Hub
3137

modules/openvino-langchain/sample/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@ cd sample/
1919
npm install
2020
node index.js *path_to_llm_model_dir* *path_to_embeddings_model_dir*
2121
```
22-
23-
The sample model `Phi-3.5-mini-instruct` should include both LLM and embeddings models.

modules/openvino-langchain/src/embeddings.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { addon as ov } from 'openvino-node';
33
// CVS-146344
44
type CompiledModel = {
55
createInferRequest: () => InferRequest;
6+
output: (index: number) => any;
7+
outputs: any[];
8+
inputs: any[];
69
};
710
type InferRequest = {
811
inferAsync: (inputs: any) => Promise<{ [outputName: string]: Tensor }>;
@@ -100,30 +103,25 @@ export class OpenVINOEmbeddings extends Embeddings {
100103

101104
const inputTensor = new ov.Tensor([text]);
102105
const tokenizedInput = await irTokenizer.inferAsync([inputTensor]);
103-
const inputShape = tokenizedInput['input_ids'].getShape();
104-
105-
// The current openvino-node version does not officially support Int64Array
106-
// TODO: Remove any after adding official Int64Array support
107-
const positionArray : any = BigInt64Array.from(
108-
{length: inputShape[1]},
109-
(_x, i) => BigInt(i),
110-
);
111-
const positionIds = new ov.Tensor(
112-
ov.element.i64,
113-
[1, inputShape[1]],
114-
positionArray,
115-
);
116-
const beamIdx = new ov.Tensor(ov.element.i32, [1], new Int32Array([0]));
117106

118107
const modelCompiled = await this.modelCompiled;
108+
109+
const tokenizerOutputs = tokenizerModelCompiled.outputs
110+
.map(o => o.getAnyName())
111+
.sort();
112+
const embeddingInputs = modelCompiled.inputs
113+
.map(i => i.getAnyName())
114+
.sort();
115+
if (!tokenizerOutputs.every(
116+
(value, index) => value === embeddingInputs[index],
117+
)) {
118+
throw Error('Embedding model is incorrect. Tokenizer outputs should be the same as embedding model inputs.');
119+
}
119120
const ir = modelCompiled.createInferRequest();
120-
const embeddings = await ir.inferAsync({
121-
'input_ids': tokenizedInput['input_ids'],
122-
'attention_mask': tokenizedInput['attention_mask'],
123-
'position_ids': positionIds,
124-
'beam_idx': beamIdx,
125-
});
126-
127-
return embeddings.logits.data;
121+
const embeddings = await ir.inferAsync(tokenizedInput);
122+
123+
const outputName = modelCompiled.output(0);
124+
125+
return embeddings[outputName].data;
128126
}
129127
}

modules/openvino-langchain/src/tests/embeddings.int.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { test, expect } from '@jest/globals';
22
import { OpenVINOEmbeddings } from '../embeddings.js';
33

4-
const modelPath = process.env.MODEL_PATH;
5-
if (modelPath === undefined) throw new Error('MODEL_PATH doest not defined');
4+
const modelPath = process.env.EMBEDDING_MODEL_PATH;
5+
if (modelPath === undefined) throw new Error('EMBEDDING_MODEL_PATH doest not defined');
66

77
test('Test embedQuery', async () => {
88
const embeddings = new OpenVINOEmbeddings({

0 commit comments

Comments
 (0)