Skip to content

Commit

Permalink
fix(api): type-checking embedding list[str] (#773)
Browse files Browse the repository at this point in the history
* fixes a bug with type checking list[str] in embeddings route

---------

Co-authored-by: Jonathan Perry <YrrepNoj@gmail.com>
  • Loading branch information
gphorvath and YrrepNoj committed Jul 12, 2024
1 parent 19ce48a commit a7030ad
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/leapfrogai_api/routers/openai/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,25 @@ async def embeddings(

if isinstance(req.input, str):
request = lfai.EmbeddingRequest(inputs=[req.input])
elif isinstance(req.input, list[str]):
request = lfai.EmbeddingRequest(inputs=req.input)
elif list_str := _to_list_of_strs(req.input):
request = lfai.EmbeddingRequest(inputs=list_str)
else:
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail=f"Invalid input type {type(req.input)}. Currently supported types are str and list[str]",
)

return await create_embeddings(model, request)


def _to_list_of_strs(v: list) -> list[str]:
new_list: list[str] = []
for item in v:
if not isinstance(item, str):
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail=f"Invalid input type {type(item)}. Currently supported types are str and list[str]",
)

new_list.append(item)
return new_list

0 comments on commit a7030ad

Please sign in to comment.