-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddingsLocalHuggingFace.py
More file actions
41 lines (36 loc) · 1.61 KB
/
EmbeddingsLocalHuggingFace.py
File metadata and controls
41 lines (36 loc) · 1.61 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
39
40
41
# Copyright 2024-, RGBYang.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from transformers import AutoTokenizer, AutoModel
import torch
from langchain.embeddings.base import Embeddings
# 使用本地模型初始化Embeddings
class EmbeddingsLocalHuggingFace(Embeddings):
def __init__(self, model_name: str):
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModel.from_pretrained(model_name)
self.model.eval() # 确保模型处于评估模式
def embed_documents(self, texts):
return self._embed(texts)
def embed_query(self, text):
return self._embed([text])[0]
def _embed(self, texts):
embeddings = []
with torch.no_grad():
for text in texts:
inputs = self.tokenizer(text, return_tensors='pt', padding=True, truncation=True)
outputs = self.model(**inputs)
# 获取最后一层隐藏状态的均值作为嵌入
embedding = outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
embeddings.append(embedding.tolist())
return embeddings