feat: Use Async OpenAI client to prevent blocking server thread (#811)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import copy
|
||||
import difflib
|
||||
import hashlib
|
||||
@@ -15,7 +16,7 @@ import uuid
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from functools import wraps
|
||||
from typing import List, Union, _GenericAlias, get_args, get_origin, get_type_hints
|
||||
from typing import Any, Coroutine, List, Union, _GenericAlias, get_args, get_origin, get_type_hints
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
import demjson3 as demjson
|
||||
@@ -1127,3 +1128,25 @@ def get_friendly_error_msg(function_name: str, exception_name: str, exception_me
|
||||
if len(error_msg) > MAX_ERROR_MESSAGE_CHAR_LIMIT:
|
||||
error_msg = error_msg[:MAX_ERROR_MESSAGE_CHAR_LIMIT]
|
||||
return error_msg
|
||||
|
||||
|
||||
def run_async_task(coro: Coroutine[Any, Any, Any]) -> Any:
|
||||
"""
|
||||
Safely runs an asynchronous coroutine in a synchronous context.
|
||||
|
||||
If an event loop is already running, it uses `asyncio.ensure_future`.
|
||||
Otherwise, it creates a new event loop and runs the coroutine.
|
||||
|
||||
Args:
|
||||
coro: The coroutine to execute.
|
||||
|
||||
Returns:
|
||||
The result of the coroutine.
|
||||
"""
|
||||
try:
|
||||
# If there's already a running event loop, schedule the coroutine
|
||||
loop = asyncio.get_running_loop()
|
||||
return asyncio.run_until_complete(coro) if loop.is_closed() else asyncio.ensure_future(coro)
|
||||
except RuntimeError:
|
||||
# If no event loop is running, create a new one
|
||||
return asyncio.run(coro)
|
||||
|
||||
Reference in New Issue
Block a user