From 3a5320a0986e680b89612fd632530cfa4803729b Mon Sep 17 00:00:00 2001 From: Yassine Selmi Date: Tue, 19 Dec 2023 18:36:29 +0000 Subject: [PATCH] Migrated Weaviate to custom component --- .../components/vectorstores/Weaviate.py | 79 +++++++++++++++++++ src/frontend/src/utils/styleUtils.ts | 2 + 2 files changed, 81 insertions(+) create mode 100644 src/backend/langflow/components/vectorstores/Weaviate.py diff --git a/src/backend/langflow/components/vectorstores/Weaviate.py b/src/backend/langflow/components/vectorstores/Weaviate.py new file mode 100644 index 000000000..2e03ca6d4 --- /dev/null +++ b/src/backend/langflow/components/vectorstores/Weaviate.py @@ -0,0 +1,79 @@ +import weaviate # type: ignore +from typing import Optional, Union +from langflow import CustomComponent + +from langchain.vectorstores import Weaviate +from langchain.schema import Document +from langchain.vectorstores.base import VectorStore +from langchain.schema import BaseRetriever +from langchain.embeddings.base import Embeddings + + +class WeaviateVectorStore(CustomComponent): + display_name: str = "Weaviate" + description: str = "Implementation of Vector Store using Weaviate" + documentation = ( + "https://python.langchain.com/docs/integrations/vectorstores/weaviate" + ) + beta = True + field_config = { + "url": {"display_name": "Weaviate URL", "value": "http://localhost:8080"}, + "api_key": { "display_name": "API Key", "password": True,"required": False, }, + "index_name": {"display_name": "Index name","required": False,}, + "text_key": {"display_name": "Text Key","required": False, "advanced": True, "value": "text"}, + "documents": {"display_name": "Documents", "is_list": True}, + "embedding": {"display_name": "Embedding"}, + "attributes": {"display_name": "Attributes", "required": False, "is_list": True, "field_type": "str", "advanced": True}, + "search_by_text": {"display_name": "Search By Text", "field_type": "bool", "advanced": True}, + "code": {"show": False}, + } + + def build( + self, + url: str, + search_by_text: bool = False, + api_key: Optional[str] = None, + index_name: Optional[str] = None, + text_key: Optional[str] = "text", + embedding: Optional[Embeddings] = None, + documents: Optional[Document] = None, + attributes: Optional[list] = None, + ) -> Union[VectorStore, BaseRetriever]: + if api_key: + auth_config = weaviate.AuthApiKey(api_key=api_key) + client = weaviate.Client(url=url, auth_client_secret=auth_config) + else: + client = weaviate.Client(url=url) + + def _to_pascal_case(word: str): + if word and not word[0].isupper(): + word = word.capitalize() + + if word.isidentifier(): + return word + + word = word.replace("-", " ").replace("_", " ") + parts = word.split() + pascal_case_word = "".join([part.capitalize() for part in parts]) + + return pascal_case_word + + index_name = _to_pascal_case(index_name) if index_name else None + + if documents is not None and embedding is not None: + return Weaviate.from_documents( + client=client, + index_name=index_name, + documents=documents, + embedding=embedding, + by_text=search_by_text, + ) + + return Weaviate( + client=client, + index_name=index_name, + text_key=text_key, + embedding=embedding, + by_text=search_by_text, + attributes=attributes if attributes is not None else [], + ) diff --git a/src/frontend/src/utils/styleUtils.ts b/src/frontend/src/utils/styleUtils.ts index d8e96207d..4c1151cc3 100644 --- a/src/frontend/src/utils/styleUtils.ts +++ b/src/frontend/src/utils/styleUtils.ts @@ -129,6 +129,7 @@ import { NotionIcon } from "../icons/Notion"; import { OpenAiIcon } from "../icons/OpenAi"; import { PineconeIcon } from "../icons/Pinecone"; import { QDrantIcon } from "../icons/QDrant"; +import { WeaviateIcon } from "../icons/Weaviate"; import { SearxIcon } from "../icons/Searx"; import { ShareIcon } from "../icons/Share"; import { Share2Icon } from "../icons/Share2"; @@ -254,6 +255,7 @@ export const nodeIconsLucide: iconsType = { OpenAIEmbeddings: OpenAiIcon, Pinecone: PineconeIcon, Qdrant: QDrantIcon, + Weaviate: WeaviateIcon, Searx: SearxIcon, SlackDirectoryLoader: SvgSlackIcon, SupabaseVectorStore: SupabaseIcon,