mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-06 13:33:37 +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:
@@ -1,4 +1,5 @@
|
||||
"""Callback handler for Context AI"""
|
||||
|
||||
import os
|
||||
from typing import Any, Dict, List
|
||||
from uuid import UUID
|
||||
@@ -6,26 +7,23 @@ from uuid import UUID
|
||||
from langchain_core.callbacks import BaseCallbackHandler
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_core.outputs import LLMResult
|
||||
from langchain_core.utils import guard_import
|
||||
|
||||
|
||||
def import_context() -> Any:
|
||||
"""Import the `getcontext` package."""
|
||||
try:
|
||||
import getcontext
|
||||
from getcontext.generated.models import (
|
||||
Conversation,
|
||||
Message,
|
||||
MessageRole,
|
||||
Rating,
|
||||
)
|
||||
from getcontext.token import Credential
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"To use the context callback manager you need to have the "
|
||||
"`getcontext` python package installed (version >=0.3.0). "
|
||||
"Please install it with `pip install --upgrade python-context`"
|
||||
)
|
||||
return getcontext, Credential, Conversation, Message, MessageRole, Rating
|
||||
return (
|
||||
guard_import("getcontext", pip_name="python-context"),
|
||||
guard_import("getcontext.token", pip_name="python-context").Credential,
|
||||
guard_import(
|
||||
"getcontext.generated.models", pip_name="python-context"
|
||||
).Conversation,
|
||||
guard_import("getcontext.generated.models", pip_name="python-context").Message,
|
||||
guard_import(
|
||||
"getcontext.generated.models", pip_name="python-context"
|
||||
).MessageRole,
|
||||
guard_import("getcontext.generated.models", pip_name="python-context").Rating,
|
||||
)
|
||||
|
||||
|
||||
class ContextCallbackHandler(BaseCallbackHandler):
|
||||
|
Reference in New Issue
Block a user