Skip to content

Commit 23c05fc

Browse files
rongfengliangpre-commit-ci[bot]aniketmauryaBordabhimrazy
authored
fix: OpenAIEmbeddingSpec setup check for multi endpoint (#568)
* fix: OpenAIEmbeddingSpec setup check for multi endpoint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * format remve type * test: add multi endpoint with test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: add add test class && fix _check_lit_api should for OpenAIEmbeddingSpec not all * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review * Update tests/unit/test_openai_embedding.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> * Update tests/unit/test_openai_embedding.py * Update tests/unit/test_openai_embedding.py Co-authored-by: Bhimraj Yadav <bhimrajyadav977@gmail.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aniket Maurya <theaniketmaurya@gmail.com> Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Bhimraj Yadav <bhimrajyadav977@gmail.com>
1 parent c4abf01 commit 23c05fc

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

src/litserve/specs/openai_embedding.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,38 @@ def setup(self, server: "LitServer"):
135135
super().setup(server)
136136

137137
lit_api = server.lit_api
138-
if inspect.isgeneratorfunction(lit_api.predict):
139-
raise ValueError(
140-
"You are using yield in your predict method, which is used for streaming.",
141-
"OpenAIEmbeddingSpec doesn't support streaming because producing embeddings ",
142-
"is not a sequential operation.",
143-
"Please consider replacing yield with return in predict.\n",
144-
EMBEDDING_API_EXAMPLE,
145-
)
146138

147-
is_encode_response_original = lit_api.encode_response.__code__ is LitAPI.encode_response.__code__
148-
if not is_encode_response_original and inspect.isgeneratorfunction(lit_api.encode_response):
149-
raise ValueError(
150-
"You are using yield in your encode_response method, which is used for streaming.",
151-
"OpenAIEmbeddingSpec doesn't support streaming because producing embeddings ",
152-
"is not a sequential operation.",
153-
"Please consider replacing yield with return in encode_response.\n",
154-
EMBEDDING_API_EXAMPLE,
155-
)
139+
if isinstance(lit_api, LitAPI):
140+
self._check_lit_api(lit_api)
141+
elif isinstance(lit_api, list):
142+
for api in lit_api:
143+
self._check_lit_api(api)
156144

157145
print("OpenAI Embedding Spec is ready.")
158146

147+
def _check_lit_api(self, api):
148+
from litserve import LitAPI
149+
150+
if isinstance(api.spec, OpenAIEmbeddingSpec):
151+
if inspect.isgeneratorfunction(api.predict):
152+
raise ValueError(
153+
"You are using yield in your predict method, which is used for streaming.",
154+
"OpenAIEmbeddingSpec doesn't support streaming because producing embeddings ",
155+
"is not a sequential operation.",
156+
"Please consider replacing yield with return in predict.\n",
157+
EMBEDDING_API_EXAMPLE,
158+
)
159+
160+
is_encode_response_original = api.encode_response.__code__ is LitAPI.encode_response.__code__
161+
if not is_encode_response_original and inspect.isgeneratorfunction(api.encode_response):
162+
raise ValueError(
163+
"You are using yield in your encode_response method, which is used for streaming.",
164+
"OpenAIEmbeddingSpec doesn't support streaming because producing embeddings ",
165+
"is not a sequential operation.",
166+
"Please consider replacing yield with return in encode_response.\n",
167+
EMBEDDING_API_EXAMPLE,
168+
)
169+
159170
def decode_request(self, request: EmbeddingRequest, context_kwargs: Optional[dict] = None) -> List[str]:
160171
return request.input
161172

tests/unit/test_openai_embedding.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ async def test_openai_embedding_spec_with_single_input(openai_embedding_request_
5151
assert len(resp.json()["data"][0]["embedding"]) == 768, "Embedding length should be 768"
5252

5353

54+
@pytest.mark.asyncio
55+
async def test_openai_embedding_spec_with_multi_endpoint(openai_embedding_request_data):
56+
server = ls.LitServer([
57+
TestEmbedAPI(spec=OpenAIEmbeddingSpec()),
58+
])
59+
with wrap_litserve_start(server) as server:
60+
async with LifespanManager(server.app) as manager, AsyncClient(
61+
transport=ASGITransport(app=manager.app), base_url="http://test"
62+
) as ac:
63+
resp = await ac.post("/v1/embeddings", json=openai_embedding_request_data, timeout=10)
64+
assert resp.status_code == 200, "Status code should be 200"
65+
assert resp.json()["object"] == "list", "Object should be list"
66+
assert resp.json()["data"][0]["index"] == 0, "Index should be 0"
67+
assert len(resp.json()["data"]) == 1, "Length of data should be 1"
68+
assert len(resp.json()["data"][0]["embedding"]) == 768, "Embedding length should be 768"
69+
70+
5471
@pytest.mark.asyncio
5572
async def test_openai_embedding_spec_with_multiple_inputs(openai_embedding_request_data_array):
5673
spec = OpenAIEmbeddingSpec()

0 commit comments

Comments
 (0)