mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-16 06:53:16 +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:
@@ -5,32 +5,17 @@ from langchain_core.agents import AgentAction, AgentFinish
|
||||
from langchain_core.callbacks import BaseCallbackHandler
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_core.outputs import ChatGeneration, LLMResult
|
||||
from langchain_core.utils import guard_import
|
||||
|
||||
|
||||
def import_infino() -> Any:
|
||||
"""Import the infino client."""
|
||||
try:
|
||||
from infinopy import InfinoClient
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"To use the Infino callbacks manager you need to have the"
|
||||
" `infinopy` python package installed."
|
||||
"Please install it with `pip install infinopy`"
|
||||
)
|
||||
return InfinoClient()
|
||||
return guard_import("infinopy").InfinoClient()
|
||||
|
||||
|
||||
def import_tiktoken() -> Any:
|
||||
"""Import tiktoken for counting tokens for OpenAI models."""
|
||||
try:
|
||||
import tiktoken
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"To use the ChatOpenAI model with Infino callback manager, you need to "
|
||||
"have the `tiktoken` python package installed."
|
||||
"Please install it with `pip install tiktoken`"
|
||||
)
|
||||
return tiktoken
|
||||
return guard_import("tiktoken")
|
||||
|
||||
|
||||
def get_num_tokens(string: str, openai_model_name: str) -> int:
|
||||
|
Reference in New Issue
Block a user