From 3a07ad38b01c123296ad9a6a106c344cdcbad011 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 22 Feb 2024 17:05:13 -0300 Subject: [PATCH] Add imports and fix duplicate key in dictionary --- src/backend/langflow/api/utils.py | 40 ++++++------------- .../langflow/components/models/AzureOpenAI.py | 9 ++--- .../langflow/interface/custom/attributes.py | 6 ++- 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py index f419ea269..f92fb2986 100644 --- a/src/backend/langflow/api/utils.py +++ b/src/backend/langflow/api/utils.py @@ -4,13 +4,15 @@ from typing import TYPE_CHECKING, List, Optional from fastapi import HTTPException from langchain_core.documents import Document +from platformdirs import user_cache_dir +from pydantic import BaseModel +from sqlmodel import Session + from langflow.graph.graph.base import Graph from langflow.services.chat.service import ChatService from langflow.services.database.models.flow import Flow from langflow.services.store.schema import StoreComponentCreate -from platformdirs import user_cache_dir -from pydantic import BaseModel -from sqlmodel import Session +from langflow.services.store.utils import get_lf_version_from_pypi if TYPE_CHECKING: from langflow.services.database.models.flow.model import Flow @@ -20,9 +22,7 @@ API_WORDS = ["api", "key", "token"] def has_api_terms(word: str): - return "api" in word and ( - "key" in word or ("token" in word and "tokens" not in word) - ) + return "api" in word and ("key" in word or ("token" in word and "tokens" not in word)) def remove_api_keys(flow: dict): @@ -32,11 +32,7 @@ def remove_api_keys(flow: dict): node_data = node.get("data").get("node") template = node_data.get("template") for value in template.values(): - if ( - isinstance(value, dict) - and has_api_terms(value["name"]) - and value.get("password") - ): + if isinstance(value, dict) and has_api_terms(value["name"]) and value.get("password"): value["value"] = None return flow @@ -57,9 +53,7 @@ def build_input_keys_response(langchain_object, artifacts): input_keys_response["input_keys"][key] = value # If the object has memory, that memory will have a memory_variables attribute # memory variables should be removed from the input keys - if hasattr(langchain_object, "memory") and hasattr( - langchain_object.memory, "memory_variables" - ): + if hasattr(langchain_object, "memory") and hasattr(langchain_object.memory, "memory_variables"): # Remove memory variables from input keys input_keys_response["input_keys"] = { key: value @@ -69,9 +63,7 @@ def build_input_keys_response(langchain_object, artifacts): # Add memory variables to memory_keys input_keys_response["memory_keys"] = langchain_object.memory.memory_variables - if hasattr(langchain_object, "prompt") and hasattr( - langchain_object.prompt, "template" - ): + if hasattr(langchain_object, "prompt") and hasattr(langchain_object.prompt, "template"): input_keys_response["template"] = langchain_object.prompt.template return input_keys_response @@ -106,11 +98,7 @@ def raw_frontend_data_is_valid(raw_frontend_data): def is_valid_data(frontend_node, raw_frontend_data): """Check if the data is valid for processing.""" - return ( - frontend_node - and "template" in frontend_node - and raw_frontend_data_is_valid(raw_frontend_data) - ) + return frontend_node and "template" in frontend_node and raw_frontend_data_is_valid(raw_frontend_data) def update_template_values(frontend_template, raw_template): @@ -150,9 +138,7 @@ def get_file_path_value(file_path): # If the path is not in the cache dir, return empty string # This is to prevent access to files outside the cache dir # If the path is not a file, return empty string - if not path.exists() or not str(path).startswith( - user_cache_dir("langflow", "langflow") - ): + if not path.exists() or not str(path).startswith(user_cache_dir("langflow", "langflow")): return "" return file_path @@ -183,9 +169,7 @@ async def check_langflow_version(component: StoreComponentCreate): langflow_version = get_lf_version_from_pypi() if langflow_version is None: - raise HTTPException( - status_code=500, detail="Unable to verify the latest version of Langflow" - ) + raise HTTPException(status_code=500, detail="Unable to verify the latest version of Langflow") elif langflow_version != component.last_tested_version: warnings.warn( f"Your version of Langflow ({component.last_tested_version}) is outdated. " diff --git a/src/backend/langflow/components/models/AzureOpenAI.py b/src/backend/langflow/components/models/AzureOpenAI.py index f8c13d8ed..f640be109 100644 --- a/src/backend/langflow/components/models/AzureOpenAI.py +++ b/src/backend/langflow/components/models/AzureOpenAI.py @@ -1,15 +1,15 @@ from typing import Optional -from langflow import CustomComponent + from langchain.llms.base import BaseLanguageModel from langchain_openai import AzureChatOpenAI +from langflow import CustomComponent + class AzureChatOpenAIComponent(CustomComponent): display_name: str = "AzureOpenAI model" description: str = "Generate text using LLM model from Azure OpenAI." - documentation: str = ( - "https://python.langchain.com/docs/integrations/llms/azure_openai" - ) + documentation: str = "https://python.langchain.com/docs/integrations/llms/azure_openai" beta = False AZURE_OPENAI_MODELS = [ @@ -72,7 +72,6 @@ class AzureChatOpenAIComponent(CustomComponent): }, "code": {"show": False}, "inputs": {"display_name": "Input"}, - "inputs": {"display_name": "Input"}, } def build( diff --git a/src/backend/langflow/interface/custom/attributes.py b/src/backend/langflow/interface/custom/attributes.py index 7aa2bc184..5d8a17bc4 100644 --- a/src/backend/langflow/interface/custom/attributes.py +++ b/src/backend/langflow/interface/custom/attributes.py @@ -1,3 +1,8 @@ +import warnings + +import emoji + + def validate_icon(value: str, *args, **kwargs): # we are going to use the emoji library to validate the emoji # emojis can be defined using the :emoji_name: syntax @@ -12,7 +17,6 @@ def validate_icon(value: str, *args, **kwargs): def getattr_return_str(value): - return str(value) if value else ""