Commit ae5a317d authored by Rayyyyy's avatar Rayyyyy
Browse files

Delete test.py and update README

parent f75058c7
Pipeline #1412 failed with stages
in 0 seconds
# Contributors
None
\ No newline at end of file
# FlagEmbedding # FlagEmbedding
智源开源库FlagEmbedding 智源开源库FlagEmbedding,专注于检索增强llm领域。
## 已适配模型 ## 已适配模型
- **Embedding Model**: [BGE Embedding](./FlagEmbedding/baai_general_embedding) - **Embedding Model**: [BGE Embedding](./FlagEmbedding/baai_general_embedding)
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk24.04-py310 docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk24.04-py310
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
cd /your_code_path/FlagEmbedding cd /your_code_path/FlagEmbedding_pytorch
pip install -e . pip install -e .
pip install faiss-1.7.2_dtk24.04_gitb7348e7df780-py3-none-any.whl pip install faiss-1.7.2_dtk24.04_gitb7348e7df780-py3-none-any.whl
``` ```
...@@ -25,7 +25,7 @@ cd docker ...@@ -25,7 +25,7 @@ cd docker
docker build --no-cache -t FlagEmbedding:latest . docker build --no-cache -t FlagEmbedding:latest .
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
cd /your_code_path/FlagEmbedding cd /your_code_path/FlagEmbedding_pytorch
pip install -e . pip install -e .
pip install faiss-1.7.2_dtk24.04_gitb7348e7df780-py3-none-any.whl pip install faiss-1.7.2_dtk24.04_gitb7348e7df780-py3-none-any.whl
``` ```
...@@ -41,7 +41,7 @@ torch: 2.1.0 ...@@ -41,7 +41,7 @@ torch: 2.1.0
其它非深度学习库安装方式如下: 其它非深度学习库安装方式如下:
```bash ```bash
cd /your_code_path/FlagEmbedding cd /your_code_path/FlagEmbedding_pytorch
pip install -e . pip install -e .
pip install faiss-1.7.2_dtk24.04_gitb7348e7df780-py3-none-any.whl pip install faiss-1.7.2_dtk24.04_gitb7348e7df780-py3-none-any.whl
``` ```
...@@ -70,13 +70,14 @@ BGE Embedding是一个通用向量模型。 我们使用[retromae](https://githu ...@@ -70,13 +70,14 @@ BGE Embedding是一个通用向量模型。 我们使用[retromae](https://githu
## 应用场景 ## 应用场景
### 算法类别 ### 算法类别
检索增强
### 热点应用行业 ### 热点应用行业
制造,广媒,家居,教育 广媒,教育,政府
## 预训练权重 ## 预训练权重
通过[SCNet AIModels](http://113.200.138.88:18080/aimodels)下载预训练模型: 通过[SCNet AIModels](http://113.200.138.88:18080/aimodels)下载预训练模型:
### BGE Embedding ### BGE Embedding
- [bge-large-zh-v1.5](http://113.200.138.88:18080/aimodels/bge-large-zh-v1.5) - [bge-large-zh-v1.5](http://113.200.138.88:18080/aimodels/bge-large-zh-v1.5)
- [bge-base-zh-v1.5](http://113.200.138.88:18080/aimodels/baai/bge-base-zh-v1.5) - [bge-base-zh-v1.5](http://113.200.138.88:18080/aimodels/baai/bge-base-zh-v1.5)
...@@ -90,7 +91,7 @@ BGE Embedding是一个通用向量模型。 我们使用[retromae](https://githu ...@@ -90,7 +91,7 @@ BGE Embedding是一个通用向量模型。 我们使用[retromae](https://githu
- [bge-reranker-large](http://113.200.138.88:18080/aimodels/bge-reranker-large) - [bge-reranker-large](http://113.200.138.88:18080/aimodels/bge-reranker-large)
## 源码仓库及问题反馈 ## 源码仓库及问题反馈
- https://developer.hpccube.com/codes/modelzoo/ - https://developer.hpccube.com/codes/modelzoo/flagembedding_pytorch
## 参考资料 ## 参考资料
- https://github.com/FlagOpen/FlagEmbedding - https://github.com/FlagOpen/FlagEmbedding
# 模型唯一标识
modelCode=822
# 模型名称
modelName=FlagEmbedding_pytorch
# 模型描述
modelDescription=智源开源仓,FlagEmbedding专注于检索增强llm领域。
# 应用场景
appScenario=推理,训练,检索增强,广媒,教育,政府
# 框架类型
frameType=pytorch
import argparse
def use_hf(model_path):
from transformers import AutoTokenizer, AutoModel
import torch
# Sentences we want sentence embeddings for
sentences = ["怎么优化网卡性能及丢包问题", "按电源键启动几秒后机器自动掉电"]
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModel.from_pretrained(model_path)
model.eval()
# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# for s2p(short query to long passage) retrieval task, add an instruction to query (not add instruction for passages)
# encoded_input = tokenizer([instruction + q for q in queries], padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling. In this case, cls pooling.
sentence_embeddings = model_output[0][:, 0]
# normalize embeddings
sentence_embeddings = torch.nn.functional.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:", sentence_embeddings)
def use_stf(model_path):
from sentence_transformers import SentenceTransformer
sentences_1 = ["怎么优化网卡性能及丢包问题", "按电源键启动几秒后机器自动掉电"]
sentences_2 = ["存储设备开机关机的顺序有哪些要求", "按电源键启动几秒自动掉电"]
model = SentenceTransformer(model_path)
embeddings_1 = model.encode(sentences_1, normalize_embeddings=True)
embeddings_2 = model.encode(sentences_2, normalize_embeddings=True)
similarity = embeddings_1 @ embeddings_2.T
print(similarity)
queries = ['售前咨询', '存储设备开机关机的顺序有哪些要求']
passages = ["售前咨询需要联系您所在地平台,平台联系方式可以通过以下途径进行获取:\n<平台联系方式>", "存储设备开机关机顺序:\n开机流程:启动交换机和存储扩展柜--启动存储控制器--启动物理服务器--启动物理服务器上的业务\n关机流程:停止物理服务器上的业务--关闭物理服务器--关闭存储控制器--关闭交换机和存储扩展柜"]
instruction = "为这个句子生成表示以用于检索相关文章:"
model = SentenceTransformer(model_path)
q_embeddings = model.encode([instruction+q for q in queries], normalize_embeddings=True)
p_embeddings = model.encode(passages, normalize_embeddings=True)
scores = q_embeddings @ p_embeddings.T
print(scores)
def use_flagE(model_path):
# from FlagEmbedding
from FlagEmbedding import FlagModel
sentences_1 = ["怎么优化网卡性能及丢包问题", "按电源键启动几秒后机器自动掉电"]
sentences_2 = ["存储设备开机关机的顺序有哪些要求", "按电源键启动几秒自动掉电"]
model = FlagModel(model_path,
query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
embeddings_1 = model.encode(sentences_1)
embeddings_2 = model.encode(sentences_2)
similarity = embeddings_1 @ embeddings_2.T
print(similarity)
# for s2p(short query to long passage) retrieval task, suggest to use encode_queries() which will automatically add the instruction to each query
# corpus in retrieval task can still use encode() or encode_corpus(), since they don't need instruction
queries = ['售前咨询', '存储设备开机关机的顺序有哪些要求']
passages = ["售前咨询需要联系您所在地平台,平台联系方式可以通过以下途径进行获取:\n<平台联系方式>", "存储设备开机关机顺序:\n开机流程:启动交换机和存储扩展柜--启动存储控制器--启动物理服务器--启动物理服务器上的业务\n关机流程:停止物理服务器上的业务--关闭物理服务器--关闭存储控制器--关闭交换机和存储扩展柜"]
q_embeddings = model.encode_queries(queries)
p_embeddings = model.encode(passages)
scores = q_embeddings @ p_embeddings.T
print(scores)
def get_args():
parse = argparse.ArgumentParser('Testing reranker in FlagEmbedding or Transformers.')
parse.add_argument('--model_path', default='BAAI/bge-large-zh-v1.5')
parse.add_argument('--use_hf', action='store_true')
parse.add_argument('--use_stf', action='store_true')
args = parse.parse_args()
return args
if __name__ == '__main__':
args = get_args()
model_path = args.model_path
if args.use_hf:
use_hf(model_path)
elif args.use_stf:
use_stf(model_path)
else:
use_flagE(model_path)
import argparse
def use_hf(model_path):
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
# from transformers
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
model.eval()
pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
with torch.no_grad():
inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt', max_length=512)
scores = model(**inputs, return_dict=True).logits.view(-1, ).float()
print(scores)
def use_flagE(model_path):
# from FlagEmbedding
from FlagEmbedding import FlagReranker
reranker = FlagReranker(model_path, use_fp16=True) #use fp16 can speed up computing
score = reranker.compute_score(['售前咨询', '售前咨询需要联系您所在地平台,平台联系方式可以通过以下途径进行获取:\n<平台联系方式>'])
print(score)
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']])
print(scores)
def get_args():
parse = argparse.ArgumentParser('Testing reranker in FlagEmbedding or Transformers.')
parse.add_argument('--model_path', default='BAAI/bge-reranker-base')
parse.add_argument('--use_hf', action='store_true')
args = parse.parse_args()
return args
if __name__ == '__main__':
args = get_args()
model_path = args.model_path
if args.use_hf:
use_hf(model_path)
else:
use_flagE(model_path)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment