refactor: move import statement inside function to avoid unnecessary imports (#3505)

* fix: Add exception handling for missing langchain_anthropic package

* refactor: Move import statement for get_starter_projects_dump in get_starter_projects

* chore: Modify logger to use error level for exception in directory_reader
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-08-22 11:56:36 -03:00 • committed by GitHub
commit 2eca4d9e32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 5 deletions

View file

@ -4,10 +4,8 @@ from fastapi import APIRouter, Depends, HTTPException
from loguru import logger from loguru import logger
from langflow.graph.graph.schema import GraphDump from langflow.graph.graph.schema import GraphDump
from langflow.services.database.models.user.model import User
from langflow.services.auth.utils import get_current_active_user from langflow.services.auth.utils import get_current_active_user
from langflow.initial_setup.load import get_starter_projects_dump from langflow.services.database.models.user.model import User
router = APIRouter(prefix="/starter-projects", tags=["Flows"]) router = APIRouter(prefix="/starter-projects", tags=["Flows"])
@ -18,6 +16,8 @@ def get_starter_projects(
current_user: User = Depends(get_current_active_user), current_user: User = Depends(get_current_active_user),
): ):
"""Get a list of starter projects.""" """Get a list of starter projects."""
from langflow.initial_setup.load import get_starter_projects_dump
try: try:
flows = get_starter_projects_dump() flows = get_starter_projects_dump()
return flows return flows

View file

@ -1,4 +1,3 @@
from langchain_anthropic.chat_models import ChatAnthropic
from pydantic.v1 import SecretStr from pydantic.v1 import SecretStr
from langflow.base.models.model import LCModelComponent from langflow.base.models.model import LCModelComponent
@ -53,6 +52,12 @@ class AnthropicModelComponent(LCModelComponent):
] ]
def build_model(self) -> LanguageModel: # type: ignore[type-var] def build_model(self) -> LanguageModel: # type: ignore[type-var]
try:
from langchain_anthropic.chat_models import ChatAnthropic
except ImportError:
raise ImportError(
"langchain_anthropic is not installed. Please install it with `pip install langchain_anthropic`."
)
model = self.model model = self.model
anthropic_api_key = self.anthropic_api_key anthropic_api_key = self.anthropic_api_key
max_tokens = self.max_tokens max_tokens = self.max_tokens

View file

@ -348,7 +348,7 @@ class DirectoryReader:
try: try:
output_types = await self.get_output_types_from_code_async(result_content) output_types = await self.get_output_types_from_code_async(result_content)
except Exception as exc: except Exception as exc:
logger.exception(f"Error while getting output types from code: {str(exc)}") logger.error(f"Error while getting output types from code: {str(exc)}")
output_types = [component_name_camelcase] output_types = [component_name_camelcase]
else: else:
output_types = [component_name_camelcase] output_types = [component_name_camelcase]