forked from codefuse-ai/ModelCache
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathllmEmb.py
More file actions
38 lines (31 loc) · 1.21 KB
/
llmEmb.py
File metadata and controls
38 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
import numpy as np
from modelcache.embedding.base import BaseEmbedding
from transformers import AutoTokenizer
from transformers import AutoConfig
class LlmEmb2Vec(BaseEmbedding):
def __init__(self):
self.model_name = '' # 13b-mft-embedding.npy
model_path = '' # .npy file storage path
model_file = model_path + self.model_name # .npy file
config = AutoConfig.from_pretrained(model_path)
dimension = config.hidden_size
self.__dimension = dimension
self.model = np.load(model_file)
self.tokenizer = AutoTokenizer.from_pretrained(model_path, local_files_only=True)
def to_embeddings(self, data, **_):
"""Generate embedding given text input
:param data: text in string.
:return: a text embedding in shape of (dim,).
"""
input_ids = self.tokenizer.encode(data, add_special_tokens=True)
embedding_array = self.model[input_ids].mean(axis=0)
return embedding_array
def post_proc(self, token_embeddings, inputs):
pass
@property
def dimension(self):
"""Embedding dimension.
:return: embedding dimension
"""
return self.__dimension