Skip to content
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

Local OpenAI Evaluation #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions run-e5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import asyncio

import numpy as np
from typing import Any
from openai import AsyncOpenAI

import mteb
from mteb.model_meta import ModelMeta
from mteb.models.text_formatting_utils import corpus_to_texts
from mteb.models.instructions import task_to_instruction

MODEL = "intfloat/e5-mistral-7b-instruct"
TASKS = ["STS12"]

class AsyncOpenAIWrapper:
def __init__(self,
model_name: str,
base_url: str = "http://localhost:8000/v1",
num_concurrent: int = 100) -> None:

self._client = AsyncOpenAI(base_url=base_url)
self._model_name = model_name
self._num_concurrent = num_concurrent

async def async_encode(self, sentences: list[str]) -> np.ndarray:
sublists = [
sentences[i : i + self._num_concurrent]
for i in range(0, len(sentences), self._num_concurrent)
]

all_embeddings = []

for sublist in sublists:
outputs = [
self._client.embeddings.create(
input=sentence,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not sending sublist here instead of doing a loop? Is it because you want to verify the correctness of passing one vs. multiple strings?

model=self._model_name
) for sentence in sublist
]
for output in await asyncio.gather(*outputs):
all_embeddings.extend(self._to_numpy(output))

return np.array(all_embeddings)

def encode(self, sentences: list[str], **kwargs: Any) -> np.ndarray:
def e5_instruction(instruction: str) -> str:
return f"Instruct: {instruction}\nQuery: "

if "prompt_name" in kwargs:
if "instruction" in kwargs:
raise ValueError(
"Cannot specify both `prompt_name` and `instruction`."
)
instruction = task_to_instruction(
kwargs.pop("prompt_name"), kwargs.pop("is_query", True)
)
else:
instruction = kwargs.pop("instruction", "")
if instruction:
formatted_instruction = e5_instruction(instruction)
sentences = [
f"{formatted_instruction}{sentence}" for sentence in sentences]

return asyncio.run(self.async_encode(sentences))

def encode_queries(self, queries: list[str], **kwargs: Any) -> np.ndarray:
return self.encode(queries, **kwargs)

def encode_corpus(
self, corpus: list[dict[str, str]] | dict[str, list[str]], **kwargs: Any
) -> np.ndarray:
sentences = corpus_to_texts(corpus)
return self.encode(sentences, **kwargs)

def _to_numpy(self, embedding_response) -> np.ndarray:
return np.array([e.embedding for e in embedding_response.data])



model = AsyncOpenAIWrapper(model_name=MODEL)
model.mteb_model_meta = ModelMeta(
name=MODEL,
revision=None,
release_date=None,
languages=None,
similarity_fn_name=None,)

tasks = mteb.get_tasks(tasks=TASKS)
evaluation = mteb.MTEB(tasks=tasks)
results = evaluation.run(model, output_folder=f"results/")