-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add sentence trimming to OpenAIWrapper #1526
Conversation
tiktoken library needs to be installed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide the results of the model run to ensure that the parameters were passed correctly?
Doing some tests right now. |
My test code for evaluating Ko-StrategyQA (Korean retrieval task) is: """Example script for benchmarking all datasets constituting the MTEB Korean leaderboard & average scores"""
from __future__ import annotations
import os
import logging
from multiprocessing import Process, current_process
import torch
from sentence_transformers import SentenceTransformer
from sentence_transformers.models import StaticEmbedding
import mteb
from mteb import MTEB, get_tasks
from mteb.encoder_interface import PromptType
from mteb.models.sentence_transformer_wrapper import SentenceTransformerWrapper
from mteb.models.instruct_wrapper import instruct_wrapper
import argparse
from dotenv import load_dotenv
# from setproctitle import setproctitle
import traceback
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("main")
TASK_LIST_CLASSIFICATION = []
TASK_LIST_CLUSTERING = []
TASK_LIST_PAIR_CLASSIFICATION = []
TASK_LIST_RERANKING = []
TASK_LIST_RETRIEVAL = [
"Ko-StrategyQA",
# "AutoRAGRetrieval",
# "MIRACLRetrieval",
# "PublicHealthQA",
# "BelebeleRetrieval",
# "MrTidyRetrieval",
# "MultiLongDocRetrieval",
# "XPQARetrieval"
]
TASK_LIST_STS = []
TASK_LIST = (
TASK_LIST_CLASSIFICATION
+ TASK_LIST_CLUSTERING
+ TASK_LIST_PAIR_CLASSIFICATION
+ TASK_LIST_RERANKING
+ TASK_LIST_RETRIEVAL
+ TASK_LIST_STS
)
model_names = [
"openai/text-embedding-3-large", # 8191
]
def evaluate_model(model_name, gpu_id):
try:
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
model = None
if not os.path.exists(model_name):
if "m2v" in model_name:
static_embedding = StaticEmbedding.from_model2vec(model_name)
model = SentenceTransformer(modules=[static_embedding])
else:
model = mteb.get_model(model_name)
else:
file_name = os.path.join(model_name, "model.safetensors")
if os.path.exists(file_name):
if "m2v" in model_name:
static_embedding = StaticEmbedding.from_model2vec(model_name)
model = SentenceTransformer(modules=[static_embedding])
else:
model = mteb.get_model(model_name)
if model:
# setproctitle(f"{model_name}-{gpu_id}")
print(f"Running task: {TASK_LIST} / {model_name} on GPU {gpu_id} in process {current_process().name}")
evaluation = MTEB(
tasks=get_tasks(tasks=TASK_LIST, languages=["kor-Kore", "kor-Hang", "kor_Hang"])
)
# 48GB VRAM 기준 적합한 batch sizes
if "multilingual-e5" in model_name:
batch_size = 256
elif "jina" in model_name:
batch_size = 8
elif "bge-m3" in model_name:
batch_size = 32
elif "gemma2" in model_name:
batch_size = 256
else:
batch_size = 64
if args.quantize: # quantized model의 경우
evaluation.run(
model,
output_folder=f"/data_x/EMBEDDING/RESULTS/{model_name}-quantized",
encode_kwargs={"batch_size": batch_size, "precision": "binary"},
)
else:
evaluation.run(
model,
output_folder=f"results/{model_name}",
encode_kwargs={"batch_size": batch_size},
)
except Exception as ex:
print(ex)
traceback.print_exc()
if __name__ == "__main__":
gpu_id = 0
evaluate_model(model_names[0], gpu_id) and got these results: {
"precision_at_1": 0.68243,
"precision_at_10": 0.15,
"precision_at_100": 0.01711,
"ndcg_at_1": 0.68243,
"ndcg_at_10": 0.73607,
"ndcg_at_100": 0.76238,
"recall_at_1": 0.44298,
"recall_at_10": 0.80961,
"recall_at_100": 0.91126,
} (Others excluded) |
After deleting changes for ModelMetadata, I tested on AutoRAGRetrieval, which is also a Korean retrieval task. {
"precision_at_1": 0.58772,
"precision_at_10": 0.09386,
"precision_at_100": 0.00991,
"ndcg_at_1": 0.58772,
"ndcg_at_10": 0.76549,
"ndcg_at_100": 0.77714,
"recall_at_1": 0.58772,
"recall_at_10": 0.9386,
"recall_at_100": 0.99123,
} (Others excluded) The results shows that the parameters are passed well. |
Great! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. A few minor changes.
Might be worth rerunning openai given these new changes (@Muennighoff I imagine that we could use the key here)
I forgot to push this but I actually also implemented this locally. IIUC the things that'd be nice to add here such that I can discard my implementation would be:
class OpenAIWrapper(Wrapper):
def __init__(self, model_name: str, embed_dim: int | None = None, **kwargs) -> None:
requires_package(self, "openai", "Openai text embedding")
from openai import OpenAI
if model_name in ["text-embedding-3-small", "text-embedding-3-large"]:
requires_package(self, "tiktoken", "Openai text embedding")
import tiktoken
self.max_tokens = 8191
self.encoding = tiktoken.get_encoding("cl100k_base")
self._client = OpenAI()
self._model_name = model_name
self._embed_dim = embed_dim
def truncate_text_tokens(self, text):
"""Truncate a string to have `max_tokens` according to the given encoding."""
return self.encoding.encode(text)[:self.max_tokens]
def encode(self, sentences: list[str], **kwargs: Any) -> np.ndarray:
from openai import NotGiven
if self._model_name == "text-embedding-ada-002" and self._embed_dim is not None:
logger.warning(
"Reducing embedding size available only for text-embedding-3-* models"
)
max_batch_size = kwargs.get("batch_size", 256)
sublists = [
sentences[i : i + max_batch_size]
for i in range(0, len(sentences), max_batch_size)
]
all_embeddings = []
for sublist in sublists:
if self._model_name in ["text-embedding-3-small", "text-embedding-3-large"]:
sublist = [self.truncate_text_tokens(s) for s in sublist]
try:
# Takes either strings or tokenized inputs
response = self._client.embeddings.create(
input=sublist,
model=self._model_name,
encoding_format="float",
dimensions=self._embed_dim or NotGiven(),
)
except Exception as e:
# import pdb; pdb.set_trace()
# Sleep
print("Sleeping for 10 seconds due to error", e)
import time
time.sleep(10)
try:
response = self._client.embeddings.create(
input=sublist,
model=self._model_name,
encoding_format="float",
dimensions=self._embed_dim or NotGiven(),
)
except Exception as e:
print("Sleeping for 60 seconds due to error", e)
time.sleep(60)
response = self._client.embeddings.create(
input=sublist,
model=self._model_name,
encoding_format="float",
dimensions=self._embed_dim or NotGiven(),
)
all_embeddings.extend(self._to_numpy(response))
return np.array(all_embeddings)
def _to_numpy(self, embedding_response) -> np.ndarray:
return np.array([e.embedding for e in embedding_response.data]) |
@KennethEnevoldsen I don't think we need to rerun things as I already ran them with the truncation per my implementation above; Makes more sense to use for new tasks I think. Another thing we could run is using the Matryoshka parameter to change the embedding size and benchmark the large model but with a smaller emb size |
Woah, sorry I saw this comment too late. I think this code is better because I’ve also experienced an error related to Can I implement this in my code, or could you revise the code I wrote? Let me know if there are any issues with authorization. @Muennighoff |
Ofc feel free to implement it in your code and take the bits from my code that are useful to you; then after you commit it hopefully we can merge this PR! 🚀 |
I changed my codes thanks to great reviews and comments. "ndcg_at_1": 0.57018,
"ndcg_at_3": 0.71157,
"ndcg_at_5": 0.74102,
"ndcg_at_10": 0.76131, and "ndcg_at_1": 0.47368,
"ndcg_at_3": 0.61831,
"ndcg_at_5": 0.63605,
"ndcg_at_10": 0.6677, |
I think everything looks good here. Will merge it in - thanks for the contribution! |
* fix: Count unique texts, data leaks in calculate metrics (#1438) * add more stat * add more stat * update statistics * fix: update task metadata to allow for null (#1448) * Update tasks table * 1.19.5 Automatically generated by python-semantic-release * Fix: Made data parsing in the leaderboard figure more robust (#1450) Bugfixes with data parsing in main figure * Fixed task loading (#1451) * Fixed task result loading from disk * Fixed task result loading from disk * fix: publish (#1452) * 1.19.6 Automatically generated by python-semantic-release * fix: Fix load external results with `None` mteb_version (#1453) * fix * lint * 1.19.7 Automatically generated by python-semantic-release * WIP: Polishing up leaderboard UI (#1461) * fix: Removed column wrapping on the table, so that it remains readable * Added disclaimer to figure * fix: Added links to task info table, switched out license with metric * fix: loading pre 1.11.0 (#1460) * small fix * fix: fix * 1.19.8 Automatically generated by python-semantic-release * fix: swap touche2020 to maintain compatibility (#1469) swap touche2020 for parity * 1.19.9 Automatically generated by python-semantic-release * docs: Add sum per language for task counts (#1468) * add sum per lang * add sort by sum option * make lint * fix: pinned datasets to <3.0.0 (#1470) * 1.19.10 Automatically generated by python-semantic-release * feat: add CUREv1 retrieval dataset (#1459) * feat: add CUREv1 dataset --------- Co-authored-by: nadshe <[email protected]> Co-authored-by: olivierr42 <[email protected]> Co-authored-by: Daniel Buades Marcos <[email protected]> * feat: add missing domains to medical tasks * feat: modify benchmark tasks * chore: benchmark naming --------- Co-authored-by: nadshe <[email protected]> Co-authored-by: olivierr42 <[email protected]> * Update tasks table * 1.20.0 Automatically generated by python-semantic-release * fix: check if `model` attr of model exists (#1499) * check if model attr of model exists * lint * Fix retrieval evaluator * 1.20.1 Automatically generated by python-semantic-release * fix: Leaderboard demo data loading (#1507) * Made get_scores error tolerant * Added join_revisions, made get_scores failsafe * Fetching metadata fixed fr HF models * Added failsafe metadata fetching to leaderboard code * Added revision joining to leaderboard app * fix * Only show models that have metadata, when filter_models is called * Ran linting * 1.20.2 Automatically generated by python-semantic-release * fix: leaderboard only shows models that have ModelMeta (#1508) Filtering for models that have metadata * 1.20.3 Automatically generated by python-semantic-release * fix: align readme with current mteb (#1493) * align readme with current mteb * align with mieb branch * fix test * 1.20.4 Automatically generated by python-semantic-release * docs: Add lang family mapping and map to task table (#1486) * add lang family mapping and map to task table * make lint * add back some unclassified lang codes * Update tasks table * fix: Ensure that models match the names on embedding-benchmarks/results (#1519) * 1.20.5 Automatically generated by python-semantic-release * fix: Adding missing metadata on models and mathcing names up with the results repo (#1528) * Added Voyage 3 models * Added correct metadata to Cohere models and matched names with the results repo * 1.20.6 Automatically generated by python-semantic-release * feat: Evaluate missing splits (#1525) * fix: evaluate missing splits (#1268) * implement partial evaluation for missing splits * lint * requested changes done from scratch * test for missing split evaluation added * uncomment test * lint * avoid circular import * use TaskResult * skip tests for now --------- Co-authored-by: Isaac Chung <[email protected]> * got test_all_splits_evaluated passing * tests passing * address review comments * make lint * handle None cases for kg_co2_emissions * use new results info --------- Co-authored-by: Thivyanth <[email protected]> * 1.21.0 Automatically generated by python-semantic-release * fix: Correct typos superseeded -> superseded (#1532) fix typo -> superseded * 1.21.1 Automatically generated by python-semantic-release * fix: Task load data error for SICK-BR-STS and XStance (#1534) * fix task load data for two tasks * correct dataset keys * 1.21.2 Automatically generated by python-semantic-release * fix: Proprietary models now get correctly shown in leaderboard (#1530) * Fixed showing proprietary models in leaderboard * Added links to all OpenAI models * Fixed table formatting issues * Bumped Gradio version * 1.21.3 Automatically generated by python-semantic-release * docs: Add Model Meta parameters and metadata (#1536) * add multi_qa_MiniLM_L6_cos_v1 model meta * add all_mpnet_base_v2 * add parameters to model meta * make lint * add extra params to meta * fix: add more model meta (jina, e5) (#1537) * add e5 model meta * address review comments * 1.21.4 Automatically generated by python-semantic-release * Add cohere models (#1538) * fix: bug cohere names * format * fix: add nomic models (#1543) #1515 * fix: Added all-minilm-l12-v2 (#1542) #1515 * fix: Added arctic models (#1541) #1515 * fix: add sentence trimming to OpenAIWrapper (#1526) * fix: add sentence trimming to OpenAIWrapper * fix: import tiktoken library inside encode function * fix: check tokenizer library installed and update ModelMeta to pass tokenizer_name * fix: pass tokenizer_name, max_tokens to loader * fix: make tokenizer_name None for default * fix: delete changes for ModelMeta * fix: fix revision to 2 for OpenAI models * fix: add docstring for OpenAIWrapper * fix: lint * feat: add openai optional dependency set * fix: add sleep for too many requests * fix: add lint * fix: delete evaluate file * 1.21.5 Automatically generated by python-semantic-release * fix: Fixed metadata errors (#1547) * 1.21.6 Automatically generated by python-semantic-release * fix: remove curev1 from multlingual (#1552) Seems like it was added here: 1cc6c9e * 1.21.7 Automatically generated by python-semantic-release * fix: Add Model2vec (#1546) * Added Model2Vec wrapper * Added Model2vec models * Added model2vec models to registry * Added model2vec as a dependency * Ran linting * Update mteb/models/model2vec_models.py Co-authored-by: Kenneth Enevoldsen <[email protected]> * Update mteb/models/model2vec_models.py Co-authored-by: Kenneth Enevoldsen <[email protected]> * Added adapted_from and superseeded_by to model2vec models. * Added missing import * Moved pyproject.toml to optional dependencies * Fixed typos * Added import error and changed model to model_name * Added Numpy to frameworks * Added Numpy to frameworks * Corrected false info on model2vec models * Replaced np.inf with maxint * Update mteb/models/model2vec_models.py Co-authored-by: Isaac Chung <[email protected]> * Added option to have infinite max tokens, added it to Model2vec --------- Co-authored-by: Kenneth Enevoldsen <[email protected]> Co-authored-by: Isaac Chung <[email protected]> * Made result loading more permissive, changed eval splits for HotPotQA and DBPedia (#1554) * Removed train and dev from eval splits on HotpotQA * Removed dev from eval splits on DBPedia * Made task_results validation more permissive * Readded exception in get_score * Ran linting * 1.21.8 Automatically generated by python-semantic-release * docs: Correction of SICK-R metadata (#1558) * Correction of SICK-R metadata * Correction of SICK-R metadata --------- Co-authored-by: rposwiata <[email protected]> * feat(google_models): fix issues and add support for `text-embedding-005` and `text-multilingual-embedding-002` (#1562) * fix: google_models batching and prompt * feat: add text-embedding-005 and text-multilingual-embedding-002 * chore: `make lint` errors * fix: address PR comments * 1.22.0 Automatically generated by python-semantic-release * fix(bm25s): search implementation (#1566) fix: bm25s implementation * 1.22.1 Automatically generated by python-semantic-release * docs: Fix dependency library name for bm25s (#1568) * fix: bm25s implementation * correct library name --------- Co-authored-by: Daniel Buades Marcos <[email protected]> * fix: Add training dataset to model meta (#1561) * fix: Add training dataset to model meta Adresses #1556 * Added docs * format * feat: (cohere_models) cohere_task_type issue, batch requests and tqdm for visualization (#1564) * feat: batch requests to cohere models * fix: use correct task_type * feat: use tqdm with openai * fix: explicitely set `show_progress_bar` to False * fix(publichealth-qa): ignore rows with `None` values in `question` or `answer` (#1565) * 1.23.0 Automatically generated by python-semantic-release * fix wongnai * update inits * fix tests * lint * update imports * fix tests * lint --------- Co-authored-by: Kenneth Enevoldsen <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions <[email protected]> Co-authored-by: Márton Kardos <[email protected]> Co-authored-by: Isaac Chung <[email protected]> Co-authored-by: Napuh <[email protected]> Co-authored-by: Daniel Buades Marcos <[email protected]> Co-authored-by: nadshe <[email protected]> Co-authored-by: olivierr42 <[email protected]> Co-authored-by: Thivyanth <[email protected]> Co-authored-by: Youngjoon Jang <[email protected]> Co-authored-by: Rafał Poświata <[email protected]>
If OpenAI embedding credit for MTEB-related benchmarking would be useful to anyone here in this thread, let me know! We have 1K+ USD in credits expiring Jan 31st :) |
#1494
@Samoed