Skip to content

Commit

Permalink
Various improvements to data_layer acquisition logic. (#1534)
Browse files Browse the repository at this point in the history
* Initialise data layer only once.
* Give DeprecationWarning when users set _data_layer directly.
* Remove debug print statement.
  • Loading branch information
dokterbob authored Nov 21, 2024
1 parent dde96bc commit eab9533
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions backend/chainlit/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,38 @@
)

_data_layer: Optional[BaseDataLayer] = None
_data_layer_initialized = False


def get_data_layer():
global _data_layer
print("Getting data layer", _data_layer)

if not _data_layer:
from chainlit.config import config

if config.code.data_layer:
# When @data_layer is configured, call it to get data layer.
_data_layer = config.code.data_layer()
elif api_key := os.environ.get("LITERAL_API_KEY"):
# When LITERAL_API_KEY is defined, use LiteralAI data layer
from .literalai import LiteralDataLayer

# support legacy LITERAL_SERVER variable as fallback
server = os.environ.get("LITERAL_API_URL") or os.environ.get(
"LITERAL_SERVER"
global _data_layer, _data_layer_initialized

if not _data_layer_initialized:
if _data_layer:
# Data layer manually set, warn user that this is deprecated.
import warnings

warnings.warn(
"Setting data layer manually is deprecated. Use @data_layer instead.",
DeprecationWarning,
)
_data_layer = LiteralDataLayer(api_key=api_key, server=server)

else:
from chainlit.config import config

if config.code.data_layer:
# When @data_layer is configured, call it to get data layer.
_data_layer = config.code.data_layer()
elif api_key := os.environ.get("LITERAL_API_KEY"):
# When LITERAL_API_KEY is defined, use LiteralAI data layer
from .literalai import LiteralDataLayer

# support legacy LITERAL_SERVER variable as fallback
server = os.environ.get("LITERAL_API_URL") or os.environ.get(
"LITERAL_SERVER"
)
_data_layer = LiteralDataLayer(api_key=api_key, server=server)

_data_layer_initialized = True

return _data_layer

0 comments on commit eab9533

Please sign in to comment.