mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-09-06 08:16:00 +00:00
refactor(core): implement lazy initialization for OpenAI clients
This commit is contained in:
parent
eae723b5d4
commit
41876bdbfb
5 changed files with 35 additions and 13 deletions
|
|
@ -20,7 +20,7 @@ __all__ = [
|
|||
"ReMeFs",
|
||||
]
|
||||
|
||||
__version__ = "0.3.0.0a8"
|
||||
__version__ = "0.3.0.0a9"
|
||||
|
||||
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -15,16 +15,23 @@ class OpenAIEmbeddingModel(BaseEmbeddingModel):
|
|||
super().__init__(**kwargs)
|
||||
self.encoding_format: Literal["float", "base64"] = encoding_format
|
||||
|
||||
# Create client using factory method
|
||||
self._client = self._create_client()
|
||||
# Lazy client initialization
|
||||
self._client = None
|
||||
|
||||
def _create_client(self):
|
||||
"""Create and return an internal AsyncOpenAI client instance."""
|
||||
return AsyncOpenAI(api_key=self.api_key, base_url=self.base_url)
|
||||
|
||||
@property
|
||||
def client(self):
|
||||
"""Lazily create and return the AsyncOpenAI client."""
|
||||
if self._client is None:
|
||||
self._client = self._create_client()
|
||||
return self._client
|
||||
|
||||
async def _get_embeddings(self, input_text: list[str], **kwargs) -> list[list[float]]:
|
||||
"""Fetch embeddings from the API for a batch of strings."""
|
||||
completion = await self._client.embeddings.create(
|
||||
completion = await self.client.embeddings.create(
|
||||
model=self.model_name,
|
||||
input=input_text,
|
||||
dimensions=self.dimensions,
|
||||
|
|
@ -40,4 +47,6 @@ class OpenAIEmbeddingModel(BaseEmbeddingModel):
|
|||
|
||||
async def close(self):
|
||||
"""Close the asynchronous OpenAI client and release network resources."""
|
||||
await self._client.close()
|
||||
if self._client is not None:
|
||||
await self._client.close()
|
||||
self._client = None
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class OpenAIEmbeddingModelSync(OpenAIEmbeddingModel):
|
|||
|
||||
def _get_embeddings_sync(self, input_text: list[str], **kwargs) -> list[list[float]]:
|
||||
"""Fetch embeddings synchronously from the API for a batch of strings."""
|
||||
completion = self._client.embeddings.create(
|
||||
completion = self.client.embeddings.create(
|
||||
model=self.model_name,
|
||||
input=input_text,
|
||||
dimensions=self.dimensions,
|
||||
|
|
@ -30,4 +30,6 @@ class OpenAIEmbeddingModelSync(OpenAIEmbeddingModel):
|
|||
|
||||
def close_sync(self):
|
||||
"""Close the synchronous OpenAI client and release network resources."""
|
||||
self._client.close()
|
||||
if self._client is not None:
|
||||
self._client.close()
|
||||
self._client = None
|
||||
|
|
|
|||
|
|
@ -19,13 +19,20 @@ class OpenAILLM(BaseLLM):
|
|||
"""Initialize the OpenAI async client with API credentials and model configuration."""
|
||||
super().__init__(**kwargs)
|
||||
|
||||
# Create client using factory method
|
||||
self._client = self._create_client()
|
||||
# Lazy client initialization
|
||||
self._client = None
|
||||
|
||||
def _create_client(self):
|
||||
"""Create and return an instance of the AsyncOpenAI client."""
|
||||
return AsyncOpenAI(api_key=self.api_key, base_url=self.base_url)
|
||||
|
||||
@property
|
||||
def client(self):
|
||||
"""Lazily create and return the AsyncOpenAI client."""
|
||||
if self._client is None:
|
||||
self._client = self._create_client()
|
||||
return self._client
|
||||
|
||||
def _build_stream_kwargs(
|
||||
self,
|
||||
messages: list[Message],
|
||||
|
|
@ -76,7 +83,7 @@ class OpenAILLM(BaseLLM):
|
|||
) -> AsyncGenerator[StreamChunk, None]:
|
||||
"""Generate a stream of chat completion chunks including text, reasoning content, and tool calls."""
|
||||
stream_kwargs = stream_kwargs or {}
|
||||
completion = await self._client.chat.completions.create(**stream_kwargs)
|
||||
completion = await self.client.chat.completions.create(**stream_kwargs)
|
||||
ret_tool_calls: list[ToolCall] = []
|
||||
|
||||
async for chunk in completion:
|
||||
|
|
@ -102,4 +109,6 @@ class OpenAILLM(BaseLLM):
|
|||
|
||||
async def close(self):
|
||||
"""Asynchronously close the OpenAI client and release network resources."""
|
||||
await self._client.close()
|
||||
if self._client is not None:
|
||||
await self._client.close()
|
||||
self._client = None
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ class OpenAILLMSync(OpenAILLM):
|
|||
) -> Generator[StreamChunk, None, None]:
|
||||
"""Synchronously generate a stream of chat completion chunks including text, reasoning, and tool calls."""
|
||||
stream_kwargs = stream_kwargs or {}
|
||||
completion = self._client.chat.completions.create(**stream_kwargs)
|
||||
completion = self.client.chat.completions.create(**stream_kwargs)
|
||||
ret_tool_calls: list[ToolCall] = []
|
||||
|
||||
for chunk in completion:
|
||||
|
|
@ -52,4 +52,6 @@ class OpenAILLMSync(OpenAILLM):
|
|||
|
||||
def close_sync(self):
|
||||
"""Close the synchronous OpenAI client and release network resources."""
|
||||
self._client.close()
|
||||
if self._client is not None:
|
||||
self._client.close()
|
||||
self._client = None
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue