🚀 feat(validate.py): remove unused imports and endpoint for node validation

🚀 feat(base.py): update import statement for loading module
🚀 feat(vector_store.py): add initialization functions for pinecone, chroma, and qdrant
The unused imports and endpoint for node validation were removed from validate.py. The import statement for the loading module was updated in base.py. Initialization functions for pinecone, chroma, and qdrant were added to vector_store.py to allow for the creation of objects from the respective services.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-22 13:51:18 -03:00
commit 1a0d448e82
3 changed files with 76 additions and 28 deletions

View file

@ -1,5 +1,3 @@
import json
from fastapi import APIRouter, HTTPException
from langflow.api.v1.base import (
@ -9,8 +7,6 @@ from langflow.api.v1.base import (
PromptValidationResponse,
validate_prompt,
)
from langflow.graph.vertex.types import VectorStoreVertex
from langflow.graph import Graph
from langflow.utils.logger import logger
from langflow.utils.validate import validate_code
@ -37,26 +33,3 @@ def post_validate_prompt(prompt: Prompt):
except Exception as e:
logger.exception(e)
raise HTTPException(status_code=500, detail=str(e)) from e
# validate node
@router.post("/node/{node_id}", status_code=200)
def post_validate_node(node_id: str, data: dict):
try:
# build graph
graph = Graph.from_payload(data)
# validate node
node = graph.get_node(node_id)
if node is None:
raise ValueError(f"Node {node_id} not found")
if not isinstance(node, VectorStoreVertex):
node.build()
return json.dumps(
{
"valid": True,
"params": f"{str(node._built_object_repr())[:300]}...",
}
)
except Exception as e:
logger.exception(e)
return json.dumps({"valid": False, "params": str(e)})

View file

@ -1,5 +1,5 @@
from langflow.graph.vertex.constants import DIRECT_TYPES
from langflow.interface import loading
from langflow.interface.initialize import loading
from langflow.interface.listing import ALL_TYPES_DICT
from langflow.utils.logger import logger
from langflow.utils.util import sync_to_async

View file

@ -0,0 +1,75 @@
from typing import Any
def docs_in_params(params: dict) -> bool:
"""Check if params has documents OR texts and one of them is not an empty list"""
return ("documents" in params and params["documents"] != []) or (
"texts" in params and params["texts"] != []
)
def initialize_pinecone(class_object: Any, params: dict):
"""Initialize pinecone and return the class object"""
PINECONE_API_KEY = params.get("pinecone_api_key")
PINECONE_ENV = params.get("pinecone_env")
if PINECONE_API_KEY is None or PINECONE_ENV is None:
raise ValueError(
"Pinecone API key and environment must be provided in the params"
)
if not docs_in_params(params):
import pinecone
# initialize pinecone
pinecone.init(
api_key=PINECONE_API_KEY, # find at app.pinecone.io
environment=PINECONE_ENV, # next to api key in console
)
# If there are docs in the index, delete them
return class_object.from_existing_index(**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_chroma(class_object, params):
"""Initialize a ChromaDB object from the params"""
persist = params.pop("persist", False)
if not docs_in_params(params):
if "texts" in params:
params["documents"] = params.pop("texts")
for doc in params["documents"]:
if doc.metadata is None:
doc.metadata = {}
for key, value in doc.metadata.items():
if value is None:
doc.metadata[key] = ""
chromadb = class_object.from_documents(**params)
else:
chromadb = class_object(**params)
if persist:
chromadb.persist()
return chromadb
def initialize_qdrant(class_object, params):
if not docs_in_params(params):
if "location" not in params and "api_key" not in params:
raise ValueError("Location and API key must be provided in the params")
from qdrant_client import QdrantClient
client_params = {
"location": params.pop("location"),
"api_key": params.pop("api_key"),
}
lc_params = {
"collection_name": params.pop("collection_name"),
"embeddings": params.pop("embedding"),
}
client = QdrantClient(**client_params)
return class_object(client=client, **lc_params)
return class_object.from_documents(**params)