Add imports and fix duplicate key in dictionary

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-22 17:05:13 -03:00
commit 3a07ad38b0
3 changed files with 21 additions and 34 deletions

View file

@ -4,13 +4,15 @@ from typing import TYPE_CHECKING, List, Optional
from fastapi import HTTPException from fastapi import HTTPException
from langchain_core.documents import Document 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.graph.graph.base import Graph
from langflow.services.chat.service import ChatService from langflow.services.chat.service import ChatService
from langflow.services.database.models.flow import Flow from langflow.services.database.models.flow import Flow
from langflow.services.store.schema import StoreComponentCreate from langflow.services.store.schema import StoreComponentCreate
from platformdirs import user_cache_dir from langflow.services.store.utils import get_lf_version_from_pypi
from pydantic import BaseModel
from sqlmodel import Session
if TYPE_CHECKING: if TYPE_CHECKING:
from langflow.services.database.models.flow.model import Flow from langflow.services.database.models.flow.model import Flow
@ -20,9 +22,7 @@ API_WORDS = ["api", "key", "token"]
def has_api_terms(word: str): def has_api_terms(word: str):
return "api" in word and ( return "api" in word and ("key" in word or ("token" in word and "tokens" not in word))
"key" in word or ("token" in word and "tokens" not in word)
)
def remove_api_keys(flow: dict): def remove_api_keys(flow: dict):
@ -32,11 +32,7 @@ def remove_api_keys(flow: dict):
node_data = node.get("data").get("node") node_data = node.get("data").get("node")
template = node_data.get("template") template = node_data.get("template")
for value in template.values(): for value in template.values():
if ( if isinstance(value, dict) and has_api_terms(value["name"]) and value.get("password"):
isinstance(value, dict)
and has_api_terms(value["name"])
and value.get("password")
):
value["value"] = None value["value"] = None
return flow return flow
@ -57,9 +53,7 @@ def build_input_keys_response(langchain_object, artifacts):
input_keys_response["input_keys"][key] = value input_keys_response["input_keys"][key] = value
# If the object has memory, that memory will have a memory_variables attribute # If the object has memory, that memory will have a memory_variables attribute
# memory variables should be removed from the input keys # memory variables should be removed from the input keys
if hasattr(langchain_object, "memory") and hasattr( if hasattr(langchain_object, "memory") and hasattr(langchain_object.memory, "memory_variables"):
langchain_object.memory, "memory_variables"
):
# Remove memory variables from input keys # Remove memory variables from input keys
input_keys_response["input_keys"] = { input_keys_response["input_keys"] = {
key: value key: value
@ -69,9 +63,7 @@ def build_input_keys_response(langchain_object, artifacts):
# Add memory variables to memory_keys # Add memory variables to memory_keys
input_keys_response["memory_keys"] = langchain_object.memory.memory_variables input_keys_response["memory_keys"] = langchain_object.memory.memory_variables
if hasattr(langchain_object, "prompt") and hasattr( if hasattr(langchain_object, "prompt") and hasattr(langchain_object.prompt, "template"):
langchain_object.prompt, "template"
):
input_keys_response["template"] = langchain_object.prompt.template input_keys_response["template"] = langchain_object.prompt.template
return input_keys_response 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): def is_valid_data(frontend_node, raw_frontend_data):
"""Check if the data is valid for processing.""" """Check if the data is valid for processing."""
return ( return frontend_node and "template" in frontend_node and raw_frontend_data_is_valid(raw_frontend_data)
frontend_node
and "template" in frontend_node
and raw_frontend_data_is_valid(raw_frontend_data)
)
def update_template_values(frontend_template, raw_template): 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 # If the path is not in the cache dir, return empty string
# This is to prevent access to files outside the cache dir # This is to prevent access to files outside the cache dir
# If the path is not a file, return empty string # If the path is not a file, return empty string
if not path.exists() or not str(path).startswith( if not path.exists() or not str(path).startswith(user_cache_dir("langflow", "langflow")):
user_cache_dir("langflow", "langflow")
):
return "" return ""
return file_path return file_path
@ -183,9 +169,7 @@ async def check_langflow_version(component: StoreComponentCreate):
langflow_version = get_lf_version_from_pypi() langflow_version = get_lf_version_from_pypi()
if langflow_version is None: if langflow_version is None:
raise HTTPException( raise HTTPException(status_code=500, detail="Unable to verify the latest version of Langflow")
status_code=500, detail="Unable to verify the latest version of Langflow"
)
elif langflow_version != component.last_tested_version: elif langflow_version != component.last_tested_version:
warnings.warn( warnings.warn(
f"Your version of Langflow ({component.last_tested_version}) is outdated. " f"Your version of Langflow ({component.last_tested_version}) is outdated. "

View file

@ -1,15 +1,15 @@
from typing import Optional from typing import Optional
from langflow import CustomComponent
from langchain.llms.base import BaseLanguageModel from langchain.llms.base import BaseLanguageModel
from langchain_openai import AzureChatOpenAI from langchain_openai import AzureChatOpenAI
from langflow import CustomComponent
class AzureChatOpenAIComponent(CustomComponent): class AzureChatOpenAIComponent(CustomComponent):
display_name: str = "AzureOpenAI model" display_name: str = "AzureOpenAI model"
description: str = "Generate text using LLM model from Azure OpenAI." description: str = "Generate text using LLM model from Azure OpenAI."
documentation: str = ( documentation: str = "https://python.langchain.com/docs/integrations/llms/azure_openai"
"https://python.langchain.com/docs/integrations/llms/azure_openai"
)
beta = False beta = False
AZURE_OPENAI_MODELS = [ AZURE_OPENAI_MODELS = [
@ -72,7 +72,6 @@ class AzureChatOpenAIComponent(CustomComponent):
}, },
"code": {"show": False}, "code": {"show": False},
"inputs": {"display_name": "Input"}, "inputs": {"display_name": "Input"},
"inputs": {"display_name": "Input"},
} }
def build( def build(

View file

@ -1,3 +1,8 @@
import warnings
import emoji
def validate_icon(value: str, *args, **kwargs): def validate_icon(value: str, *args, **kwargs):
# we are going to use the emoji library to validate the emoji # we are going to use the emoji library to validate the emoji
# emojis can be defined using the :emoji_name: syntax # 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): def getattr_return_str(value):
return str(value) if value else "" return str(value) if value else ""