Skip to content

Commit

Permalink
Memoize client
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin committed Nov 11, 2023
1 parent 3f3a963 commit 4d44f60
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/marvin/utilities/openai.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import cache
from typing import Optional

from openai import AsyncClient
Expand All @@ -10,7 +11,18 @@ def get_client() -> AsyncClient:
settings.openai.api_key.get_secret_value() if settings.openai.api_key else None
)
organization: Optional[str] = settings.openai.organization
return _get_client_memoized(api_key=api_key, organization=organization)


@cache
def _get_client_memoized(
api_key: Optional[str],
organization: Optional[str],
) -> AsyncClient:
"""
This function is memoized to ensure that only one instance of the client is
created for a given api key / organization pair.
"""
return AsyncClient(
api_key=api_key,
organization=organization,
Expand Down

0 comments on commit 4d44f60

Please sign in to comment.