### Advanced usage with sequence length and similarity calculation
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/08067cd21f562700e8c44294f9799bcbaca96a6a/README.md
Example demonstrating setting the maximum sequence length and performing matrix multiplication for similarity.
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Lợi ích của giấc ngủ"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
print(similarity = query_embedding @ doc_embeddings.T)
'''
array([[0.66212064, 0.33066642],
[0.25866613, 0.5865289 ]], dtype=float32)
'''
```
--------------------------------
### Calculate sentence similarity with custom sequences
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/a5fa2bb3e8d42d7bc2ed4f41c042a87cc4dff394
Example demonstrating how to set the maximum sequence length and compute similarity between query and document embeddings.
```python
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Lợi ích của giấc ngủ"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
print(similarity = query_embedding @ doc_embeddings.T)
'''
array([[0.66212064, 0.33066642],
[0.25866613, 0.5865289 ]], dtype=float32)
'''
```
--------------------------------
### Advanced Inference with Custom Sequence Length
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/17a88ad3f6ea946f7545dd55501ac68f3ce0c325/README.md
Usage example demonstrating how to set the maximum sequence length to 2048 and perform dot product similarity calculation.
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Lợi ích của giấc ngủ"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
print(similarity)
'''
array([[0.66212064, 0.33066642],
[0.25866613, 0.5865289 ]], dtype=float32)
'''
```
--------------------------------
### Vector Database Wrapper
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
Manages vector database initialization, local loading, and retrieval configuration.
```python
class VectorDB:
"""Vector database for document retrieval."""
def __init__(
self,
documents: List[Document],
embedding: Embeddings,
vector_db=FAISS,
index_path: Optional[str] = None
) -> None:
self.vector_db = vector_db
self.embedding = embedding
self.index_path = index_path
self.db = self._build_db(documents)
def _build_db(self, documents: List[Document]):
if self.index_path:
db = self.vector_db.load_local(
self.index_path,
self.embedding,
allow_dangerous_deserialization=True
)
else:
db = self.vector_db.from_documents(
documents=documents,
embedding=self.embedding,
distance_strategy=DistanceStrategy.DOT_PRODUCT
)
return db
def get_retriever(self, search_type: str = "similarity", search_kwargs: dict = {"k": 10}):
retriever = self.db.as_retriever(search_type=search_type, search_kwargs=search_kwargs)
return retriever
def save_local(self, folder_path: str) -> None:
self.db.save_local(folder_path)
```
--------------------------------
### Model configuration file
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/config_sentence_transformers.json
Displays the versioning information for the model's dependencies.
```json
{
"__version__": {
"sentence_transformers": "2.6.1",
"transformers": "4.49.0",
"pytorch": "2.6.0+cu124"
},
"prompts": {},
"default_prompt_name": null
}
```
--------------------------------
### Initialize and compute similarity with sentence-transformers
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/1d3220cbafef127899a8dd35c47faa437b632a03/README.md
Demonstrates loading the model and calculating similarity between sentences using the sentence-transformers library.
```python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
```
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Lợi ích của giấc ngủ"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
print(similarity)
'''
array([[0.66212064, 0.33066642],
[0.25866613, 0.5865289 ]], dtype=float32)
'''
```
--------------------------------
### Model configuration metadata
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/config_sentence_transformers.json
Displays the versioning and prompt configuration for the model.
```json
{
"__version__": {
"sentence_transformers": "2.6.1",
"transformers": "4.49.0",
"pytorch": "2.6.0+cu124"
},
"prompts": {},
"default_prompt_name": null
}
```
--------------------------------
### Run evaluation script
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/32ac494aa1de0f47f0db3ede2dca49cb0b683d9a/README.md
Execute the evaluation process using the provided Python script after downloading the necessary data from Kaggle.
```bash
python evaluation_model.py
```
--------------------------------
### Model configuration parameters
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/config.json
JSON configuration file defining the architecture and hyperparameters for the XLM-RoBERTa based model.
```json
{
"_name_or_path": "/AITeamVN/bge_vi_2048",
"architectures": [
"XLMRobertaModel"
],
"attention_probs_dropout_prob": 0.1,
"bos_token_id": 0,
"classifier_dropout": null,
"eos_token_id": 2,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 1024,
"initializer_range": 0.02,
"intermediate_size": 4096,
"layer_norm_eps": 1e-05,
"max_position_embeddings": 8194,
"model_type": "xlm-roberta",
"num_attention_heads": 16,
"num_hidden_layers": 24,
"output_past": true,
"pad_token_id": 1,
"position_embedding_type": "absolute",
"torch_dtype": "float32",
"transformers_version": "4.49.0",
"type_vocab_size": 1,
"use_cache": true,
"vocab_size": 250002
}
```
--------------------------------
### Model Configuration Parameters
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/config.json
The JSON configuration file defining the architecture and hyperparameters for the model.
```json
{
"_name_or_path": "/AITeamVN/bge_vi_2048",
"architectures": [
"XLMRobertaModel"
],
"attention_probs_dropout_prob": 0.1,
"bos_token_id": 0,
"classifier_dropout": null,
"eos_token_id": 2,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 1024,
"initializer_range": 0.02,
"intermediate_size": 4096,
"layer_norm_eps": 1e-05,
"max_position_embeddings": 8194,
"model_type": "xlm-roberta",
"num_attention_heads": 16,
"num_hidden_layers": 24,
"output_past": true,
"pad_token_id": 1,
"position_embedding_type": "absolute",
"torch_dtype": "float32",
"transformers_version": "4.49.0",
"type_vocab_size": 1,
"use_cache": true,
"vocab_size": 250002
}
```
--------------------------------
### Process Sample and Calculate Metrics
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
Functions to evaluate retrieval performance by comparing retrieved documents against relevant articles.
```python
def process_sample(sample: dict, retriever) -> List[int]:
question = sample['question']
docs = retriever.invoke(question)
retrieved_article_full_ids = [
docs[i].metadata['law_id'] + "#" + docs[i].metadata['article_id']
for i in range(len(docs))
]
indexes = []
for article in sample['relevant_articles']:
article_full_id = article['law_id'] + "#" + article['article_id']
if article_full_id in retrieved_article_full_ids:
idx = retrieved_article_full_ids.index(article_full_id) + 1
indexes.append(idx)
else:
indexes.append(0)
return indexes
def calculate_metrics(all_indexes: List[List[int]], num_samples: int, selected_keys: Set[str]) -> Dict[str, float]:
count = [len(indexes) for indexes in all_indexes]
result = {}
for thres in [1, 3, 5, 10, 100]:
found = [[y for y in x if 0 < y <= thres] for x in all_indexes]
found_count = [len(x) for x in found]
acc = sum(1 for i in range(num_samples) if found_count[i] > 0) / num_samples
rec = sum(found_count[i] / count[i] for i in range(num_samples)) / num_samples
pre = sum(found_count[i] / thres for i in range(num_samples)) / num_samples
mrr = sum(1 / min(x) if x else 0 for x in found) / num_samples
if f"Accuracy@{thres}" in selected_keys:
result[f"Accuracy@{thres}"] = acc
if f"MRR@{thres}" in selected_keys:
result[f"MRR@{thres}"] = mrr
return result
def save_results(result: Dict[str, float], output_path: str) -> None:
```
--------------------------------
### Reference Git LFS pointers for ONNX files
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/discussions/1/files
Git LFS pointer files for the model and its data.
```text
version https://git-lfs.github.com/spec/v1
oid sha256:8abc48c79bda16d715a7ea838f91b21a97bf22c8f80ad1d11d7701f78ecbad1d
size 745085
```
```text
version https://git-lfs.github.com/spec/v1
oid sha256:f4de2a8bc780de9b4f6dda97e2f403ea93a40b29db05ebee065a92ccf69884bf
size 2266886160
```
--------------------------------
### Model configuration file
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/d4a60ad49d14ebb0da322b2dff0a46ed04cb7cb2/config.json
The JSON configuration file defining the architecture and parameters for the XLM-RoBERTa model.
```json
{
"_name_or_path": "/AITeamVN/bge_vi_2048",
"architectures": [
"XLMRobertaModel"
],
"attention_probs_dropout_prob": 0.1,
"bos_token_id": 0,
"classifier_dropout": null,
"eos_token_id": 2,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 1024,
"initializer_range": 0.02,
"intermediate_size": 4096,
"layer_norm_eps": 1e-05,
"max_position_embeddings": 8194,
"model_type": "xlm-roberta",
"num_attention_heads": 16,
"num_hidden_layers": 24,
"output_past": true,
"pad_token_id": 1,
"position_embedding_type": "absolute",
"torch_dtype": "float32",
"transformers_version": "4.49.0",
"type_vocab_size": 1,
"use_cache": true,
"vocab_size": 250002
}
```
--------------------------------
### Model license configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/6edc484db954937df077dbdc29d76c7bed9ad85a/README.md
YAML frontmatter defining the model license.
```yaml
---
license: cc-by-nc-4.0
---
```
--------------------------------
### Evaluation Model Utilities
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
Utility functions for GPU monitoring, model loading, and processing JSON/JSONL data for evaluation.
```python
import numpy as np
import torch
import json
import pandas as pd
from tqdm import tqdm
from typing import List, Dict, Tuple, Set, Union, Optional
from langchain.docstore.document import Document
from langchain_community.vectorstores import FAISS
from langchain_community.vectorstores.faiss import DistanceStrategy
from langchain_core.embeddings.embeddings import Embeddings
from FlagEmbedding import BGEM3FlagModel
def setup_gpu_info() -> None:
print(f"Số lượng GPU khả dụng: {torch.cuda.device_count()}")
print(f"GPU hiện tại: {torch.cuda.current_device()}")
print(f"Tên GPU: {torch.cuda.get_device_name(0)}")
def load_model(model_name: str, use_fp16: bool = False) -> BGEM3FlagModel:
return BGEM3FlagModel(model_name, use_fp16=use_fp16)
def load_json_file(file_path: str) -> dict:
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
def load_jsonl_file(file_path: str) -> List[Dict]:
corpus = []
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
data = json.loads(line.strip())
corpus.append(data)
return corpus
def extract_corpus_from_legal_documents(legal_data: dict) -> List[Dict]:
corpus = []
for document in legal_data:
for article in document['articles']:
chunk = {
"law_id": document['law_id'],
"article_id": article['article_id'],
"title": article['title'],
"text": article['title'] + '\n' + article['text']
}
corpus.append(chunk)
return corpus
def convert_corpus_to_documents(corpus: List[Dict[str, str]]) -> List[Document]:
```
--------------------------------
### Model metadata configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/1ffdaf02df0ed8eac7eca01dfbf5d4cca913ac4a/README.md
YAML configuration block defining model metadata, license, and library tags.
```yaml
---
license: cc-by-nc-4.0
language:
- vi
base_model:
- BAAI/bge-m3
pipeline_tag: sentence-similarity
library_name: sentence-transformers
tags:
- Embedding
---
```
--------------------------------
### Execute model evaluation pipeline
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
Main function to load the model, process legal documents, build a FAISS vector database, and calculate retrieval metrics.
```python
def main():
setup_gpu_info()
model = load_model('AITeamVN/Vietnamese_Embedding', use_fp16=False)
samples = load_json_file('zalo_kaggle/train_question_answer.json')['items']
legal_data = load_json_file('zalo_kaggle/legal_corpus.json')
corpus = extract_corpus_from_legal_documents(legal_data)
documents = convert_corpus_to_documents(corpus)
embedding = CustomEmbedding(model, batch_size=1) # Increased batch size for efficiency time
vectordb = VectorDB(
documents=documents,
embedding=embedding,
vector_db=FAISS,
index_path=None
)
retriever = vectordb.get_retriever(search_type="similarity", search_kwargs={"k": 100})
all_indexes = []
for sample in tqdm(samples, desc="Processing samples"):
all_indexes.append(process_sample(sample, retriever))
selected_keys = {"Accuracy@1", "Accuracy@3", "Accuracy@5", "Accuracy@10", "MRR@10", "Accuracy@100"}
result = calculate_metrics(all_indexes, len(samples), selected_keys)
print(result)
save_results(result, "zalo_kaggle/Vietnamese_Embedding.json")
if __name__ == "__main__":
main()
```
--------------------------------
### Model configuration parameters
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/sentence_bert_config.json
Defines the maximum sequence length and case sensitivity settings for the model.
```json
{
"max_seq_length": 8192,
"do_lower_case": false
}
```
--------------------------------
### Model metadata configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/6edc484db954937df077dbdc29d76c7bed9ad85a/README.md
YAML configuration block defining the model license.
```yaml
license: cc-by-nc-4.0
```
--------------------------------
### Load and use Vietnamese_Embedding with sentence-transformers
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/sentence_bert_config.json
Initializes the model and computes similarity scores for a list of sentences.
```python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
```
--------------------------------
### Sentence Similarity with sentence-transformers
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commits/1ffdaf02df0ed8eac7eca01dfbf5d4cca913ac4a/.gitattributes
Demonstrates how to load the AITeamVN/Vietnamese_Embedding model and compute sentence embeddings and similarities.
```APIDOC
## Sentence Similarity with sentence-transformers
### Description
This code snippet demonstrates how to initialize the AITeamVN/Vietnamese_Embedding model using the `sentence-transformers` library and perform sentence encoding and similarity calculations.
### Usage
```python
from sentence_transformers import SentenceTransformer
# Load the model
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
# Define sentences
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium."
]
# Encode sentences to embeddings
embeddings = model.encode(sentences)
# Calculate similarities
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
```
```
--------------------------------
### Git LFS pointer file content
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/sentencepiece.bpe.model
Displays the content of a Git LFS pointer file for the model.
```text
version https://git-lfs.github.com/spec/v1
oid sha256:cfc8146abe2a0488e9e2a0c56de7952f7c11ab059eca145a0a727afce0db2865
size 5069051
```
--------------------------------
### Git LFS pointer file content
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/tokenizer.json
Displays the Git LFS pointer file structure for the model repository.
```text
version https://git-lfs.github.com/spec/v1
oid sha256:b74659c780d49afad7a7b9799868f75cbd3014fb6c34956e85a793028d38094a
size 17098251
```
--------------------------------
### Git LFS pointer file content
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/model.safetensors
Displays the Git LFS pointer file metadata for the model.
```text
version https://git-lfs.github.com/spec/v1
oid sha256:1f2debafdf03659e8273022a3e902b94deec73cd20c2b7262ab7e21630163f6d
size 2271064456
```
--------------------------------
### Model configuration structure
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/modules.json
The internal architecture definition of the model components.
```json
[
{
"idx": 0,
"name": "0",
"path": "",
"type": "sentence_transformers.models.Transformer"
},
{
"idx": 1,
"name": "1",
"path": "1_Pooling",
"type": "sentence_transformers.models.Pooling"
},
{
"idx": 2,
"name": "2",
"path": "2_Normalize",
"type": "sentence_transformers.models.Normalize"
}
]
```
--------------------------------
### Tokenizer configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/tokenizer_config.json
The JSON configuration for the XLMRobertaTokenizer used by the model.
```json
{
"added_tokens_decoder": {
"0": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"1": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"2": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"3": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"250001": {
"content": "",
"lstrip": true,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
}
},
"bos_token": "",
"clean_up_tokenization_spaces": true,
"cls_token": "",
"eos_token": "",
"extra_special_tokens": {},
"mask_token": "",
"model_max_length": 8192,
"pad_token": "",
"sep_token": "",
"sp_model_kwargs": {},
"tokenizer_class": "XLMRobertaTokenizer",
"unk_token": ""
}
```
--------------------------------
### Configure Git LFS attributes
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/08067cd21f562700e8c44294f9799bcbaca96a6a/.gitattributes
Define file patterns to be tracked by Git LFS to handle large binary files in the repository.
```gitattributes
*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text
tokenizer.json filter=lfs diff=lfs merge=lfs -text
```
--------------------------------
### Model module configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/modules.json
JSON representation of the model's internal architecture modules.
```json
[
{
"idx": 0,
"name": "0",
"path": "",
"type": "sentence_transformers.models.Transformer"
},
{
"idx": 1,
"name": "1",
"path": "1_Pooling",
"type": "sentence_transformers.models.Pooling"
},
{
"idx": 2,
"name": "2",
"path": "2_Normalize",
"type": "sentence_transformers.models.Normalize"
}
]
```
--------------------------------
### Advanced Usage with Sequence Length Configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/8df5d5bb1dba0cb896f07946763bc4a403d2cecb
Configures the maximum sequence length and computes similarity between query and document embeddings.
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Tại sao giấc ngủ quan trọng?"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
'''
array([[0.6621206 , 0.33066636],
[0.18678051, 0.4875508 ]], dtype=float32)'''
```
--------------------------------
### Remote Inference via InferenceClient
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/colab
Executes sentence similarity tasks remotely using the Hugging Face InferenceClient with automatic provider selection.
```python
import os
from huggingface_hub import InferenceClient
client = InferenceClient(
provider="auto",
api_key=os.environ["HF_TOKEN"],
)
result = client.sentence_similarity(
{
"source_sentence": "That is a happy person",
"sentences": [
"That is a happy dog",
"That is a very happy person",
"Today is a sunny day"
]
},
model="AITeamVN/Vietnamese_Embedding",
)
```
--------------------------------
### Query and Document Embedding
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/fcbbb905e6c3757d421aaa5db6fd7c53d038f6fb/README.md?code=true
Demonstrates setting the maximum sequence length and calculating similarity between query and document embeddings using matrix multiplication.
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Lợi ích của giấc ngủ"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
print(similarity)
```
--------------------------------
### Custom Embedding Class
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
A custom embedding implementation wrapping BGEM3FlagModel for document and query embedding.
```python
class CustomEmbedding(Embeddings):
"""Custom embedding class that uses the BGEM3FlagModel."""
def __init__(self, model: BGEM3FlagModel, batch_size: int = 1):
self.model = model
self.batch_size = batch_size
def embed_documents(self, texts: List[str]) -> List[List[float]]:
embeddings = []
for i in tqdm(range(0, len(texts), self.batch_size), desc="Embedding documents"):
batch_texts = texts[i:i+self.batch_size]
batch_embeddings = self._get_batch_embeddings(batch_texts)
embeddings.extend(batch_embeddings)
torch.cuda.empty_cache()
return np.vstack(embeddings)
def embed_query(self, text: str) -> List[float]:
embedding = self.model.encode(text, max_length=256)['dense_vecs']
return embedding
def _get_batch_embeddings(self, texts: List[str]) -> List[List[float]]:
with torch.no_grad():
outputs = self.model.encode(texts, batch_size=self.batch_size, max_length=2048)['dense_vecs']
batch_embeddings = outputs
del outputs
return batch_embeddings
```
--------------------------------
### Configure Git LFS attributes
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/2b4f6e9376e33b3ccfd7dd0f7270fd4a05b887e9/.gitattributes
Defines Git LFS tracking rules for ONNX model files to prevent text-based diffing.
```gitattributes
onnx/tokenizer.json filter=lfs diff=lfs merge=lfs -text
onnx/model.onnx filter=lfs diff=lfs merge=lfs -text
onnx/model.onnx_data filter=lfs diff=lfs merge=lfs -text
```
--------------------------------
### SentenceTransformer Usage
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/modules.json
How to load the model and generate embeddings for a list of sentences using the sentence-transformers library.
```APIDOC
## SentenceTransformer.encode
### Description
Initializes the model and encodes a list of sentences into vector embeddings.
### Method
Python SDK Method
### Usage
```python
from sentence_transformers import SentenceTransformer
# Initialize the model
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
# Define sentences
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium."
]
# Generate embeddings
embeddings = model.encode(sentences)
# Calculate similarities
similarities = model.similarity(embeddings, embeddings)
```
```
--------------------------------
### Model pooling configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/1_Pooling/config.json
Defines the pooling strategy and dimension settings for the model.
```json
{
"word_embedding_dimension": 1024,
"pooling_mode_cls_token": true,
"pooling_mode_mean_tokens": false,
"pooling_mode_max_tokens": false,
"pooling_mode_mean_sqrt_len_tokens": false,
"pooling_mode_weightedmean_tokens": false,
"pooling_mode_lasttoken": false,
"include_prompt": true
}
```
--------------------------------
### Convert Corpus to Documents
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
Converts a raw corpus into a list of Document objects with associated metadata.
```python
documents = []
for i in tqdm(range(len(corpus)), desc="Converting corpus to documents"):
context = corpus[i]['text']
metadata = {
'law_id': corpus[i]['law_id'],
'article_id': corpus[i]['article_id'],
'title': corpus[i]['title']
}
documents.append(Document(page_content=context, metadata=metadata))
return documents
```
--------------------------------
### Display commit hashes
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/2586bc84d1e971c7e51263dc28cedff26c491a1c/README.md
Displays specific commit identifiers associated with the repository.
```text
3fc433e
2586bc8
f2d85b2
```
--------------------------------
### Configure Environment Token
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/colab
Sets the HF_TOKEN environment variable required for authenticated remote API requests.
```python
import os
os.environ['HF_TOKEN'] = 'YOUR_TOKEN_HERE'
```
--------------------------------
### Special tokens configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/special_tokens_map.json
The JSON configuration defining special tokens for the model, including bos, cls, eos, mask, pad, sep, and unk tokens.
```json
{
"bos_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"cls_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"mask_token": {
"content": "",
"lstrip": true,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"sep_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"unk_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}
```
--------------------------------
### ONNX configuration for Vietnamese_Embedding
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/2b4f6e9376e33b3ccfd7dd0f7270fd4a05b887e9/onnx/config.json
The JSON configuration file defining the model architecture and hyperparameters for ONNX export.
```json
{
"_attn_implementation_autoset": true,
"architectures": [
"XLMRobertaModel"
],
"attention_probs_dropout_prob": 0.1,
"bos_token_id": 0,
"classifier_dropout": null,
"eos_token_id": 2,
"export_model_type": "transformer",
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 1024,
"initializer_range": 0.02,
"intermediate_size": 4096,
"layer_norm_eps": 1e-05,
"max_position_embeddings": 8194,
"model_type": "xlm-roberta",
"num_attention_heads": 16,
"num_hidden_layers": 24,
"output_past": true,
"pad_token_id": 1,
"position_embedding_type": "absolute",
"torch_dtype": "float32",
"transformers_version": "4.51.3",
"type_vocab_size": 1,
"use_cache": true,
"vocab_size": 250002
}
```
--------------------------------
### Advanced Usage with Custom Sequence Length
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/README.md
Configures the model with a maximum sequence length of 2048 and computes similarity between queries and documents.
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Tại sao giấc ngủ quan trọng?"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
'''
array([[0.6621206 , 0.33066636],
[0.18678051, 0.4875508 ]], dtype=float32)'''
```
--------------------------------
### SentenceTransformer.encode
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/bbd3f721a490466005724d68c01e3e007bc0d42b/.gitattributes
Initializes the model and generates embeddings for a list of input sentences.
```APIDOC
## SentenceTransformer.encode
### Description
Loads the AITeamVN/Vietnamese_Embedding model and encodes a list of strings into numerical embeddings.
### Usage
```python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
sentences = ["The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium."]
embeddings = model.encode(sentences)
```
```
--------------------------------
### Special tokens configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/0386e447c53eeb1534b965348b1cfdaa5ca2bfee/special_tokens_map.json
The JSON configuration defining special tokens for the model, such as BOS, EOS, and padding tokens.
```json
{
"bos_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"cls_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"mask_token": {
"content": "",
"lstrip": true,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"sep_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"unk_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}
```
--------------------------------
### Display sequence numbers
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/6edc484db954937df077dbdc29d76c7bed9ad85a/README.md
A sequence of numbers from 1 to 8.
```text
1
```
```text
2
```
```text
3
```
```text
4
```
```text
5
```
```text
6
```
```text
7
```
```text
8
```
--------------------------------
### Display numeric sequence
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/2586bc84d1e971c7e51263dc28cedff26c491a1c/README.md
Displays a sequence of numbers from 1 to 6.
```text
1
```
```text
2
```
```text
3
```
```text
4
```
```text
5
```
```text
6
```
--------------------------------
### Local Inference with Sentence Transformers
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/colab
Performs local embedding generation and similarity calculation using the sentence-transformers library.
```python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
```
--------------------------------
### Special tokens configuration
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blob/316b0e87d7322dcdf078b2f796fd058faa5b0356/special_tokens_map.json
Defines the special tokens mapping for the model, including BOS, EOS, mask, and padding tokens.
```json
{
"bos_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"cls_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"mask_token": {
"content": "",
"lstrip": true,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"sep_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"unk_token": {
"content": "",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}
```
--------------------------------
### Generate Embeddings with sentence-transformers
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/blame/58576c19f18a422b390e17c21cdd984bbc58ebf6/README.md
Basic usage of the model to encode sentences and calculate similarity scores.
```python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
```
```python
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
model.max_seq_length = 2048
sentences_1 = ["Trí tuệ nhân tạo là gì", "Tại sao giấc ngủ quan trọng?"]
sentences_2 = ["Trí tuệ nhân tạo là công nghệ giúp máy móc suy nghĩ và học hỏi như con người. Nó hoạt động bằng cách thu thập dữ liệu, nhận diện mẫu và đưa ra quyết định.",
"Giấc ngủ giúp cơ thể và não bộ nghỉ ngơi, hồi phục năng lượng và cải thiện trí nhớ. Ngủ đủ giấc giúp tinh thần tỉnh táo và làm việc hiệu quả hơn."]
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
'''
array([[0.6621206 , 0.33066636],
[0.18678051, 0.4875508 ]], dtype=float32)'''
```
--------------------------------
### Save evaluation results to JSON
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/c2e7c2a8e8d1b4a9dbe44b8f39014d7103dfeec6
Writes the evaluation results to a specified file path in JSON format.
```python
with open(output_path, "w", encoding="utf-8") as f:
json.dump(result, f, indent=4, ensure_ascii=False)
print(f"Results saved to {output_path}")
```
--------------------------------
### Update model tags in README.md
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/discussions/1/files
Adds the onnx tag to the model metadata.
```yaml
library_name: sentence-transformers
tags:
- Embedding
- onnx
---
```
--------------------------------
### Calculate Similarity with Matrix Multiplication
Source: https://huggingface.co/AITeamVN/Vietnamese_Embedding/commit/f97c74a53a3b31f248396c8febee3fb9e4c1a2fc
Computes similarity between query and document embeddings using matrix multiplication.
```python
query_embedding = model.encode(sentences_1)
doc_embeddings = model.encode(sentences_2)
similarity = query_embedding @ doc_embeddings.T
print(similarity)
```