Morgan Funtowicz
commited on
Commit
·
5d3967e
1
Parent(s):
cb9be17
test(embeddings): add unittests
Browse files- .hfjobs/Dockerfile +3 -0
- .hfjobs/deploy.json +7 -0
- .hfjobs/requirements.txt +2 -0
- tests/test_openai.py +55 -0
.hfjobs/Dockerfile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python/3.12
|
| 2 |
+
|
| 3 |
+
RUN --mount=bind,source=requirements.txt,target=/opt/jobs/requirements.txt
|
.hfjobs/deploy.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"id": "deploy-embeddings-intel-cpu",
|
| 3 |
+
"description": "Build and deploy Embeddings endpoint",
|
| 4 |
+
"instance_type": "intel-spr",
|
| 5 |
+
"instance_size": "x4",
|
| 6 |
+
"model": "sentence-transformers/all-MiniLM-L6-v2"
|
| 7 |
+
}
|
.hfjobs/requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai>=1.75
|
| 2 |
+
pytest>=8.3.0
|
tests/test_openai.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Literal
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
from openai.types import CreateEmbeddingResponse
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def client():
|
| 12 |
+
return OpenAI(
|
| 13 |
+
base_url=os.environ.get("HFENDPOINTS_BASE_URL", "http://localhost:8000/api/v1/"),
|
| 14 |
+
api_key=os.environ.get("HFENDPOINTS_API_KEY", "hf_notdefined"),
|
| 15 |
+
organization="hfendpoints-images",
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize("encoding_format", ["float", "base64"])
|
| 20 |
+
def test_openai_embedding_single(client, encoding_format: Literal["float", "base64"]):
|
| 21 |
+
response: CreateEmbeddingResponse = client.embeddings.create(
|
| 22 |
+
input="Hello, how are you?",
|
| 23 |
+
model="all-MiniLM-L6-v2",
|
| 24 |
+
encoding_format=encoding_format,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
print(response)
|
| 28 |
+
assert response.model == "all-MiniLM-L6-v2"
|
| 29 |
+
|
| 30 |
+
assert response.object == "list"
|
| 31 |
+
assert len(response.data) == 1
|
| 32 |
+
|
| 33 |
+
assert response.data[0].object == "embedding"
|
| 34 |
+
assert len(response.data[0].embedding) == 384
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@pytest.mark.parametrize("encoding_format", ["float", "base64"])
|
| 38 |
+
def test_openai_embedding_batch(client, encoding_format: Literal["float", "base64"]):
|
| 39 |
+
response: CreateEmbeddingResponse = client.embeddings.create(
|
| 40 |
+
input=["Hello, how are you?", "How are you?"],
|
| 41 |
+
model="all-MiniLM-L6-v2",
|
| 42 |
+
encoding_format=encoding_format,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
print(response)
|
| 46 |
+
assert response.model == "all-MiniLM-L6-v2"
|
| 47 |
+
|
| 48 |
+
assert response.object == "list"
|
| 49 |
+
assert len(response.data) == 2
|
| 50 |
+
|
| 51 |
+
assert response.data[0].object == "embedding"
|
| 52 |
+
assert len(response.data[0].embedding) == 384
|
| 53 |
+
|
| 54 |
+
assert response.data[1].object == "embedding"
|
| 55 |
+
assert len(response.data[1].embedding) == 384
|