mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-12 21:11:43 +00:00
community: callbacks
guard_imports (#21173)
Issue: we have several helper functions to import third-party libraries like import_uptrain in [community.callbacks](https://api.python.langchain.com/en/latest/callbacks/langchain_community.callbacks.uptrain_callback.import_uptrain.html#langchain_community.callbacks.uptrain_callback.import_uptrain). And we have core.utils.utils.guard_import that works exactly for this purpose. The import_<package> functions work inconsistently and rather be private functions. Change: replaced these functions with the guard_import function. Related to #21133
This commit is contained in:
@@ -4,7 +4,7 @@ import logging
|
||||
from typing import TYPE_CHECKING, Any, Optional
|
||||
|
||||
from langchain_core.callbacks import BaseCallbackHandler
|
||||
from langchain_core.utils import get_from_env
|
||||
from langchain_core.utils import get_from_env, guard_import
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from whylogs.api.logger.logger import Logger
|
||||
@@ -27,22 +27,15 @@ def import_langkit(
|
||||
Returns:
|
||||
The imported langkit module.
|
||||
"""
|
||||
try:
|
||||
import langkit
|
||||
import langkit.regexes
|
||||
import langkit.textstat
|
||||
|
||||
if sentiment:
|
||||
import langkit.sentiment
|
||||
if toxicity:
|
||||
import langkit.toxicity
|
||||
if themes:
|
||||
import langkit.themes
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"To use the whylabs callback manager you need to have the `langkit` python "
|
||||
"package installed. Please install it with `pip install langkit`."
|
||||
)
|
||||
langkit = guard_import("langkit")
|
||||
guard_import("langkit.regexes")
|
||||
guard_import("langkit.textstat")
|
||||
if sentiment:
|
||||
guard_import("langkit.sentiment")
|
||||
if toxicity:
|
||||
guard_import("langkit.toxicity")
|
||||
if themes:
|
||||
guard_import("langkit.themes")
|
||||
return langkit
|
||||
|
||||
|
||||
@@ -161,10 +154,12 @@ class WhyLabsCallbackHandler(BaseCallbackHandler):
|
||||
# langkit library will import necessary whylogs libraries
|
||||
import_langkit(sentiment=sentiment, toxicity=toxicity, themes=themes)
|
||||
|
||||
import whylogs as why
|
||||
from langkit.callback_handler import get_callback_instance
|
||||
from whylogs.api.writer.whylabs import WhyLabsWriter
|
||||
from whylogs.experimental.core.udf_schema import udf_schema
|
||||
why = guard_import("whylogs")
|
||||
get_callback_instance = guard_import(
|
||||
"langkit.callback_handler"
|
||||
).get_callback_instance
|
||||
WhyLabsWriter = guard_import("whylogs.api.writer.whylabs").WhyLabsWriter
|
||||
udf_schema = guard_import("whylogs.experimental.core.udf_schema").udf_schema
|
||||
|
||||
if logger is None:
|
||||
api_key = api_key or get_from_env("api_key", "WHYLABS_API_KEY")
|
||||
|
Reference in New Issue
Block a user