Upload model class and mteb evaluation codes
Browse files- model_api.py +42 -0
- mteb_evaluate.py +54 -0
model_api.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Optional, Union
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import requests
|
5 |
+
from mteb import DRESModel
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
|
9 |
+
class SionicEmbeddingModel(DRESModel):
|
10 |
+
def __init__(self, url: str, instruction: Optional[str] = None, batch_size: int = 128, dimension: int = 2048, **kwargs) -> None:
|
11 |
+
self.url = url
|
12 |
+
self.instruction = instruction
|
13 |
+
self.batch_size = batch_size
|
14 |
+
self.dimension = dimension
|
15 |
+
|
16 |
+
def get_embeddings(self, queries: List[str]) -> np.ndarray:
|
17 |
+
return np.asarray(
|
18 |
+
requests.post(self.url, json={'inputs': queries}).json()['embedding'],
|
19 |
+
dtype=np.float32,
|
20 |
+
).reshape(len(queries), self.dimension)
|
21 |
+
|
22 |
+
def encode_queries(self, queries: List[str], **kwargs) -> np.ndarray:
|
23 |
+
return self.encode([f'{self.instruction}{query}' for query in queries])
|
24 |
+
|
25 |
+
def encode_corpus(self, corpus: List[Union[Dict[str, str], str]], **kwargs) -> np.ndarray:
|
26 |
+
sentences: List[str] = (
|
27 |
+
[f"{doc.get('title', '')} {doc['text']}".strip() for doc in corpus]
|
28 |
+
if isinstance(corpus[0], dict)
|
29 |
+
else corpus
|
30 |
+
)
|
31 |
+
|
32 |
+
return self.encode(sentences)
|
33 |
+
|
34 |
+
def encode(self, sentences: List[str], **kwargs) -> np.ndarray:
|
35 |
+
return np.concatenate(
|
36 |
+
[
|
37 |
+
self.get_embeddings(sentences[idx:idx + self.batch_size])
|
38 |
+
for idx in tqdm(range(0, len(sentences), self.batch_size), desc='encode')
|
39 |
+
],
|
40 |
+
axis=0,
|
41 |
+
)
|
42 |
+
|
mteb_evaluate.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from argparse import ArgumentParser, Namespace
|
2 |
+
from typing import List, Optional
|
3 |
+
|
4 |
+
from model_api import SionicEmbeddingModel
|
5 |
+
from mteb import MTEB
|
6 |
+
|
7 |
+
RETRIEVAL_TASKS: List[str] = [
|
8 |
+
'ArguAna',
|
9 |
+
'ClimateFEVER',
|
10 |
+
'DBPedia',
|
11 |
+
'FEVER',
|
12 |
+
'FiQA2018',
|
13 |
+
'HotpotQA',
|
14 |
+
'MSMARCO',
|
15 |
+
'NFCorpus',
|
16 |
+
'NQ',
|
17 |
+
'QuoraRetrieval',
|
18 |
+
'SCIDOCS',
|
19 |
+
'SciFact',
|
20 |
+
'Touche2020',
|
21 |
+
'TRECCOVID',
|
22 |
+
]
|
23 |
+
|
24 |
+
|
25 |
+
def get_arguments() -> Namespace:
|
26 |
+
parser = ArgumentParser()
|
27 |
+
parser.add_argument('--url', type=str, default='https://api.sionic.ai/v2/embedding', help='api server url')
|
28 |
+
parser.add_argument('--instruction', type=str, default='query: ', help='query instruction')
|
29 |
+
parser.add_argument('--batch_size', type=int, default=128)
|
30 |
+
parser.add_argument('--dimension', type=int, default=3072)
|
31 |
+
parser.add_argument('--output_dir', type=str, default='./result/v2')
|
32 |
+
return parser.parse_args()
|
33 |
+
|
34 |
+
|
35 |
+
if __name__ == '__main__':
|
36 |
+
args = get_arguments()
|
37 |
+
|
38 |
+
model = SionicEmbeddingModel(url=args.url, instruction=args.instruction, batch_size=args.batch_size, dimension=args.dimension)
|
39 |
+
|
40 |
+
task_names: List[str] = [t.description['name'] for t in MTEB(task_types=None, task_langs=['en']).tasks]
|
41 |
+
|
42 |
+
for task in task_names:
|
43 |
+
if task in ['MSMARCOv2']:
|
44 |
+
continue
|
45 |
+
|
46 |
+
instruction: Optional[str] = args.instruction if ('CQADupstack' in task) or (task in RETRIEVAL_TASKS) else None
|
47 |
+
model.instruction = instruction
|
48 |
+
|
49 |
+
evaluation = MTEB(
|
50 |
+
tasks=[task],
|
51 |
+
task_langs=['en'],
|
52 |
+
eval_splits=['test' if task not in ['MSMARCO'] else 'dev'],
|
53 |
+
)
|
54 |
+
evaluation.run(model, output_folder=args.output_dir)
|