Refactor async pipe execution for improved readability

This commit is contained in:
Dominik Peter 2025-03-18 19:18:57 +01:00
parent dcb74a7ba8
commit 8cc3d6d76c

View File

@ -667,13 +667,13 @@ async def generate_openai_chat_completion(form_data: OpenAIChatCompletionForm):
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
detail=f"Pipeline {form_data.model} not found", detail=f"Pipeline {form_data.model} not found",
) )
async def execute_pipe(pipe, *args, **kwargs): async def execute_pipe(pipe, *args, **kwargs):
if inspect.isasyncgenfunction(pipe): if inspect.isasyncgenfunction(pipe):
async for res in pipe(*args, **kwargs): async for res in pipe(*args, **kwargs):
yield res yield res
elif inspect.iscoroutinefunction(pipe): elif inspect.iscoroutinefunction(pipe):
ls = await pipe(*args, **kwargs) for item in await pipe(*args, **kwargs):
for item in ls:
yield item yield item
else: else:
for item in await run_in_threadpool(pipe, *args, **kwargs): for item in await run_in_threadpool(pipe, *args, **kwargs):