From 9b4e65c121543a76831ad706fdfd47a7e483549c Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 22 Jun 2023 19:59:33 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(vector=5Fstore.py):=20add?= =?UTF-8?q?=20support=20for=20Weaviate=20and=20FAISS=20vector=20stores=20T?= =?UTF-8?q?his=20commit=20adds=20support=20for=20Weaviate=20and=20FAISS=20?= =?UTF-8?q?vector=20stores=20to=20the=20existing=20Pinecone,=20Qdrant,=20a?= =?UTF-8?q?nd=20Chroma=20vector=20stores.=20The=20`initialize=5Fweaviate`?= =?UTF-8?q?=20function=20initializes=20Weaviate=20and=20returns=20the=20cl?= =?UTF-8?q?ass=20object.=20The=20`initialize=5Ffaiss`=20function=20initial?= =?UTF-8?q?izes=20FAISS=20and=20returns=20the=20class=20object.=20These=20?= =?UTF-8?q?functions=20are=20used=20to=20initialize=20the=20respective=20v?= =?UTF-8?q?ector=20stores.=20The=20`docs=5Fin=5Fparams`=20function=20is=20?= =?UTF-8?q?used=20to=20check=20if=20there=20are=20documents=20in=20the=20p?= =?UTF-8?q?arameters.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interface/initialize/vector_store.py | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/backend/langflow/interface/initialize/vector_store.py b/src/backend/langflow/interface/initialize/vector_store.py index c1ce5c227..dfdf3a28f 100644 --- a/src/backend/langflow/interface/initialize/vector_store.py +++ b/src/backend/langflow/interface/initialize/vector_store.py @@ -1,5 +1,6 @@ +import json from typing import Type -from langchain.vectorstores import Pinecone, Qdrant, Chroma +from langchain.vectorstores import Pinecone, Qdrant, Chroma, FAISS, Weaviate def docs_in_params(params: dict) -> bool: @@ -10,6 +11,45 @@ def docs_in_params(params: dict) -> bool: ) +def initialize_weaviate(class_object: Type[Weaviate], params: dict): + """Initialize weaviate and return the class object""" + if not docs_in_params(params): + import weaviate + + client_kwargs_json = params.get("client_kwargs", "{}") + client_kwargs = json.loads(client_kwargs_json) + client_params = { + "url": params.get("weaviate_url"), + } + client_params.update(client_kwargs) + weaviate_client = weaviate.Client(**client_params) + + new_params = { + "client": weaviate_client, + "index_name": params.get("index_name"), + "text_key": params.get("text_key"), + } + weaviate = class_object(**new_params) + # If there are docs in the params, create a new index + if "texts" in params: + params["documents"] = params.pop("texts") + + return class_object.from_documents(**params) + + +def initialize_faiss(class_object: Type[FAISS], params: dict): + """Initialize faiss and return the class object""" + + if not docs_in_params(params): + return class_object.load_local + + save_local = params.get("save_local") + faiss_index = class_object(**params) + if save_local: + faiss_index.save_local(folder_path=save_local) + return faiss_index + + def initialize_pinecone(class_object: Type[Pinecone], params: dict): """Initialize pinecone and return the class object"""