1313from ._utils import compute_args_hash , wrap_embedding_func_with_attrs
1414from .base import BaseKVStorage
1515
16+ global_openai_async_client = None
17+ global_azure_openai_async_client = None
18+
19+
20+ def get_openai_async_client_instance ():
21+ global global_openai_async_client
22+ if global_openai_async_client is None :
23+ global_openai_async_client = AsyncOpenAI ()
24+ return global_openai_async_client
25+
26+
27+ def get_azure_openai_async_client_instance ():
28+ global global_azure_openai_async_client
29+ if global_azure_openai_async_client is None :
30+ global_azure_openai_async_client = AsyncAzureOpenAI ()
31+ return global_azure_openai_async_client
32+
1633
1734@retry (
1835 stop = stop_after_attempt (5 ),
2239async def openai_complete_if_cache (
2340 model , prompt , system_prompt = None , history_messages = [], ** kwargs
2441) -> str :
25- openai_async_client = AsyncOpenAI ()
42+ openai_async_client = get_openai_async_client_instance ()
2643 hashing_kv : BaseKVStorage = kwargs .pop ("hashing_kv" , None )
2744 messages = []
2845 if system_prompt :
@@ -78,7 +95,7 @@ async def gpt_4o_mini_complete(
7895 retry = retry_if_exception_type ((RateLimitError , APIConnectionError )),
7996)
8097async def openai_embedding (texts : list [str ]) -> np .ndarray :
81- openai_async_client = AsyncOpenAI ()
98+ openai_async_client = get_openai_async_client_instance ()
8299 response = await openai_async_client .embeddings .create (
83100 model = "text-embedding-3-small" , input = texts , encoding_format = "float"
84101 )
@@ -93,7 +110,7 @@ async def openai_embedding(texts: list[str]) -> np.ndarray:
93110async def azure_openai_complete_if_cache (
94111 deployment_name , prompt , system_prompt = None , history_messages = [], ** kwargs
95112) -> str :
96- azure_openai_client = AsyncAzureOpenAI ()
113+ azure_openai_client = get_azure_openai_async_client_instance ()
97114 hashing_kv : BaseKVStorage = kwargs .pop ("hashing_kv" , None )
98115 messages = []
99116 if system_prompt :
@@ -154,11 +171,7 @@ async def azure_gpt_4o_mini_complete(
154171 retry = retry_if_exception_type ((RateLimitError , APIConnectionError )),
155172)
156173async def azure_openai_embedding (texts : list [str ]) -> np .ndarray :
157- azure_openai_client = AsyncAzureOpenAI (
158- api_key = os .environ .get ("API_KEY_EMB" ),
159- api_version = os .environ .get ("API_VERSION_EMB" ),
160- azure_endpoint = os .environ .get ("AZURE_ENDPOINT_EMB" ),
161- )
174+ azure_openai_client = get_azure_openai_async_client_instance ()
162175 response = await azure_openai_client .embeddings .create (
163176 model = "text-embedding-3-small" , input = texts , encoding_format = "float"
164177 )
0 commit comments