Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QDrant Search , error for connect distance_func #2517

Closed
devalexandre opened this issue Jul 3, 2024 · 5 comments
Closed

QDrant Search , error for connect distance_func #2517

devalexandre opened this issue Jul 3, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@devalexandre
Copy link
Contributor

Describe the bug
I try use Qrant Search and broken

To Reproduce
Steps to reproduce the behavior:

  1. Add qdrant vectorstore
  2. add chat iput
  3. add openai embedding
  4. use localhost or cloud qdrant display some error

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context

image

log:

image

@dosubot dosubot bot added the bug Something isn't working label Jul 3, 2024
Copy link

dosubot bot commented Jul 3, 2024

Hey @devalexandre! I'm here to help you with any bugs, questions, or contributions you have regarding the repository. Let's work together to get this sorted out!

To resolve the issue with the distance_func argument in Qdrant when trying to use Qdrant Search, ensure that you are passing the distance_func parameter correctly when creating the Qdrant instance. The distance_func parameter should be included in the Qdrant.from_documents method call as shown in the provided code snippet.

Here is the relevant part of the code that handles the distance_func argument:

vs = Qdrant.from_documents(
    documents=documents,  # type: ignore
    embedding=embedding,
    api_key=api_key,
    collection_name=collection_name,
    content_payload_key=content_payload_key,
    distance_func=distance_func,  # Ensure this parameter is correctly passed
    grpc_port=grpc_port,
    host=host,
    https=https,
    location=location,
    metadata_payload_key=metadata_payload_key,
    path=path,
    port=port,
    prefer_grpc=prefer_grpc,
    prefix=prefix,
    timeout=timeout,
    url=url,
)

Make sure that the distance_func parameter is set to a valid distance function supported by Qdrant, such as "Cosine", "Euclidean", or "Dot". Additionally, ensure that other parameters like host, port, and api_key are correctly configured as per your Qdrant setup.

In the QdrantSearchComponent class, the distance_func parameter is also defined and used in the build method. Here is the relevant part of the class:

class QdrantSearchComponent(QdrantVectorStoreComponent, LCVectorStoreComponent):
    display_name = "Qdrant Search"
    description = "Construct Qdrant wrapper from a list of texts."
    icon = "Qdrant"

    def build_config(self):
        return {
            "search_type": {
                "display_name": "Search Type",
                "options": ["Similarity", "MMR"],
            },
            "input_value": {"display_name": "Input"},
            "embedding": {"display_name": "Embedding"},
            "api_key": {"display_name": "API Key", "password": True, "advanced": True},
            "collection_name": {"display_name": "Collection Name"},
            "content_payload_key": {
                "display_name": "Content Payload Key",
                "advanced": True,
            },
            "distance_func": {"display_name": "Distance Function", "advanced": True},
            "grpc_port": {"display_name": "gRPC Port", "advanced": True},
            "host": {"display_name": "Host", "advanced": True},
            "https": {"display_name": "HTTPS", "advanced": True},
            "location": {"display_name": "Location", "advanced": True},
            "metadata_payload_key": {
                "display_name": "Metadata Payload Key",
                "advanced": True,
            },
            "path": {"display_name": "Path", "advanced": True},
            "port": {"display_name": "Port", "advanced": True},
            "prefer_grpc": {"display_name": "Prefer gRPC", "advanced": True},
            "prefix": {"display_name": "Prefix", "advanced": True},
            "search_kwargs": {"display_name": "Search Kwargs", "advanced": True},
            "timeout": {"display_name": "Timeout", "advanced": True},
            "url": {"display_name": "URL", "advanced": True},
            "number_of_results": {
                "display_name": "Number of Results",
                "info": "Number of results to return.",
                "advanced": True,
            },
        }

    def build(  # type: ignore[override]
        self,
        input_value: Text,
        embedding: Embeddings,
        collection_name: str,
        number_of_results: int = 4,
        search_type: str = "similarity",
        api_key: Optional[str] = None,
        content_payload_key: str = "page_content",
        distance_func: str = "Cosine",
        grpc_port: int = 6334,
        https: bool = False,
        host: Optional[str] = None,
        location: Optional[str] = None,
        metadata_payload_key: str = "metadata",
        path: Optional[str] = None,
        port: Optional[int] = 6333,
        prefer_grpc: bool = False,
        prefix: Optional[str] = None,
        search_kwargs: Optional[NestedDict] = None,
        timeout: Optional[int] = None,
        url: Optional[str] = None,
    ) -> List[Data]:  # type: ignore[override]
        vector_store = super().build(
            embedding=embedding,
            collection_name=collection_name,
            api_key=api_key,
            content_payload_key=content_payload_key,
            distance_func=distance_func,
            grpc_port=grpc_port,
            https=https,
            host=host,
            location=location,
            metadata_payload_key=metadata_payload_key,
            path=path,
            port=port,
            prefer_grpc=prefer_grpc,
            prefix=prefix,
            timeout=timeout,
            url=url,
        )
        if not vector_store:
            raise ValueError("Failed to load the Qdrant index.")
        if search_kwargs is None:
            search_kwargs = {}

        return self.search_with_vector_store(
            vector_store=vector_store,
            input_value=input_value,
            search_type=search_type,
            k=number_of_results,
            **search_kwargs,
        )

Ensure that the distance_func parameter is correctly set in both the Qdrant.from_documents method and the QdrantSearchComponent class [1][2][3].

To continue talking to Dosu, mention @dosu.

@devalexandre
Copy link
Contributor Author

Hey @devalexandre! I'm here to help you with any bugs, questions, or contributions you have regarding the repository. Let's work together to get this sorted out!

To resolve the issue with the distance_func argument in Qdrant when trying to use Qdrant Search, ensure that you are passing the distance_func parameter correctly when creating the Qdrant instance. The distance_func parameter should be included in the Qdrant.from_documents method call as shown in the provided code snippet.

Here is the relevant part of the code that handles the distance_func argument:

vs = Qdrant.from_documents(
    documents=documents,  # type: ignore
    embedding=embedding,
    api_key=api_key,
    collection_name=collection_name,
    content_payload_key=content_payload_key,
    distance_func=distance_func,  # Ensure this parameter is correctly passed
    grpc_port=grpc_port,
    host=host,
    https=https,
    location=location,
    metadata_payload_key=metadata_payload_key,
    path=path,
    port=port,
    prefer_grpc=prefer_grpc,
    prefix=prefix,
    timeout=timeout,
    url=url,
)

Make sure that the distance_func parameter is set to a valid distance function supported by Qdrant, such as "Cosine", "Euclidean", or "Dot". Additionally, ensure that other parameters like host, port, and api_key are correctly configured as per your Qdrant setup.

In the QdrantSearchComponent class, the distance_func parameter is also defined and used in the build method. Here is the relevant part of the class:

class QdrantSearchComponent(QdrantVectorStoreComponent, LCVectorStoreComponent):
    display_name = "Qdrant Search"
    description = "Construct Qdrant wrapper from a list of texts."
    icon = "Qdrant"

    def build_config(self):
        return {
            "search_type": {
                "display_name": "Search Type",
                "options": ["Similarity", "MMR"],
            },
            "input_value": {"display_name": "Input"},
            "embedding": {"display_name": "Embedding"},
            "api_key": {"display_name": "API Key", "password": True, "advanced": True},
            "collection_name": {"display_name": "Collection Name"},
            "content_payload_key": {
                "display_name": "Content Payload Key",
                "advanced": True,
            },
            "distance_func": {"display_name": "Distance Function", "advanced": True},
            "grpc_port": {"display_name": "gRPC Port", "advanced": True},
            "host": {"display_name": "Host", "advanced": True},
            "https": {"display_name": "HTTPS", "advanced": True},
            "location": {"display_name": "Location", "advanced": True},
            "metadata_payload_key": {
                "display_name": "Metadata Payload Key",
                "advanced": True,
            },
            "path": {"display_name": "Path", "advanced": True},
            "port": {"display_name": "Port", "advanced": True},
            "prefer_grpc": {"display_name": "Prefer gRPC", "advanced": True},
            "prefix": {"display_name": "Prefix", "advanced": True},
            "search_kwargs": {"display_name": "Search Kwargs", "advanced": True},
            "timeout": {"display_name": "Timeout", "advanced": True},
            "url": {"display_name": "URL", "advanced": True},
            "number_of_results": {
                "display_name": "Number of Results",
                "info": "Number of results to return.",
                "advanced": True,
            },
        }

    def build(  # type: ignore[override]
        self,
        input_value: Text,
        embedding: Embeddings,
        collection_name: str,
        number_of_results: int = 4,
        search_type: str = "similarity",
        api_key: Optional[str] = None,
        content_payload_key: str = "page_content",
        distance_func: str = "Cosine",
        grpc_port: int = 6334,
        https: bool = False,
        host: Optional[str] = None,
        location: Optional[str] = None,
        metadata_payload_key: str = "metadata",
        path: Optional[str] = None,
        port: Optional[int] = 6333,
        prefer_grpc: bool = False,
        prefix: Optional[str] = None,
        search_kwargs: Optional[NestedDict] = None,
        timeout: Optional[int] = None,
        url: Optional[str] = None,
    ) -> List[Data]:  # type: ignore[override]
        vector_store = super().build(
            embedding=embedding,
            collection_name=collection_name,
            api_key=api_key,
            content_payload_key=content_payload_key,
            distance_func=distance_func,
            grpc_port=grpc_port,
            https=https,
            host=host,
            location=location,
            metadata_payload_key=metadata_payload_key,
            path=path,
            port=port,
            prefer_grpc=prefer_grpc,
            prefix=prefix,
            timeout=timeout,
            url=url,
        )
        if not vector_store:
            raise ValueError("Failed to load the Qdrant index.")
        if search_kwargs is None:
            search_kwargs = {}

        return self.search_with_vector_store(
            vector_store=vector_store,
            input_value=input_value,
            search_type=search_type,
            k=number_of_results,
            **search_kwargs,
        )

Ensure that the distance_func parameter is correctly set in both the Qdrant.from_documents method and the QdrantSearchComponent class [1][2][3].

To continue talking to Dosu, mention @dosu.

I try implementation

from typing import List

from langchain_community.vectorstores import Qdrant

from langflow.base.vectorstores.model import LCVectorStoreComponent
from langflow.helpers.data import docs_to_data
from langflow.io import (
    DropdownInput,
    HandleInput,
    IntInput,
    StrInput,
    SecretStrInput,
    DataInput,
    MultilineInput,
)

from langflow.schema import Data


class QdrantVectorStoreComponent(LCVectorStoreComponent):
    display_name = "Qdrant"
    description = "Qdrant Vector Store with search capabilities"
    documentation = "https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/qdrant"
    icon = "Qdrant"

    inputs = [
        StrInput(name="collection_name", display_name="Collection Name", required=True),
        StrInput(name="host", display_name="Host", value="localhost", advanced=True),
        IntInput(name="port", display_name="Port", value=6333, advanced=True),
        IntInput(name="grpc_port", display_name="gRPC Port", value=6334, advanced=True),
        SecretStrInput(name="api_key", display_name="API Key", advanced=True),
        StrInput(name="prefix", display_name="Prefix", advanced=True),
        IntInput(name="timeout", display_name="Timeout", advanced=True),
        StrInput(name="path", display_name="Path", advanced=True),
        StrInput(name="url", display_name="URL", advanced=True),
        DropdownInput(
            name="distance_func",
            display_name="Distance Function",
            options=["Cosine", "Euclidean", "Dot Product"],
            value="Cosine",
            advanced=True,
        ),
        StrInput(name="content_payload_key", display_name="Content Payload Key", value="page_content", advanced=True),
        StrInput(name="metadata_payload_key", display_name="Metadata Payload Key", value="metadata", advanced=True),
        MultilineInput(name="search_query", display_name="Search Query"),
        DataInput(
            name="ingest_data",
            display_name="Ingest Data",
            is_list=True,
        ),
        HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"]),
        IntInput(
            name="number_of_results",
            display_name="Number of Results",
            info="Number of results to return.",
            value=4,
            advanced=True,
        ),
    ]

    def build_vector_store(self) -> Qdrant:
        return self._build_qdrant()

    def _build_qdrant(self) -> Qdrant:
        qdrant_kwargs = {
            "collection_name": self.collection_name,
            "content_payload_key": self.content_payload_key,
            "metadata_payload_key": self.metadata_payload_key,
            "distance_func": self.distance_func,
        }

        server_kwargs = {
            "host": self.host if self.host else None,
            "port": int(self.port),  # Certifique-se de que port é um número inteiro
            "grpc_port": int(self.grpc_port),  # Certifique-se de que grpc_port é um número inteiro
            "api_key": self.api_key,
            "prefix": self.prefix,
            "timeout": int(self.timeout) if self.timeout else None,  # Certifique-se de que timeout é um número inteiro
            "path": self.path if self.path else None,
            "url": self.url if self.url else None,
        }

        server_kwargs = {k: v for k, v in server_kwargs.items() if v is not None}
        documents = []

        for _input in self.ingest_data or []:
            if isinstance(_input, Data):
                documents.append(_input.to_lc_document())
            else:
                documents.append(_input)

        from langchain.embeddings.base import Embeddings  # Certifique-se de que esta importação está correta
        embedding = self.embedding
        if not isinstance(embedding, Embeddings):
            raise ValueError("Invalid embedding object")

        if documents:
            qdrant = Qdrant.from_documents(documents, embedding=embedding, **qdrant_kwargs)
        else:
            from qdrant_client import QdrantClient

            client = QdrantClient(**server_kwargs)
            qdrant = Qdrant(embedding_function=embedding.embed_query, client=client, **qdrant_kwargs)

        return qdrant

    def search_documents(self) -> List[Data]:
        vector_store = self._build_qdrant()

        if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
            docs = vector_store.similarity_search(
                query=self.search_query,
                k=self.number_of_results,
            )

            data = docs_to_data(docs)
            self.status = data
            return data
        else:
            return []

but display same error 🚨

Copy link

dosubot bot commented Jul 3, 2024

The issue you're encountering is due to the Qdrant class's __init__ method not accepting the distance_func parameter directly. Instead, the distance_func should be handled within the QdrantSearchComponent class.

Here's the corrected implementation for your QdrantVectorStoreComponent class:

  1. Ensure the distance_func parameter is correctly passed in the Qdrant.from_documents method.
  2. Ensure the distance_func parameter is not passed directly to the Qdrant class's __init__ method.

Here is the updated code:

from typing import List

from langchain_community.vectorstores import Qdrant

from langflow.base.vectorstores.model import LCVectorStoreComponent
from langflow.helpers.data import docs_to_data
from langflow.io import (
    DropdownInput,
    HandleInput,
    IntInput,
    StrInput,
    SecretStrInput,
    DataInput,
    MultilineInput,
)

from langflow.schema import Data


class QdrantVectorStoreComponent(LCVectorStoreComponent):
    display_name = "Qdrant"
    description = "Qdrant Vector Store with search capabilities"
    documentation = "<link url='https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/qdrant'>Webpage description not available.</link>"
    icon = "Qdrant"

    inputs = [
        StrInput(name="collection_name", display_name="Collection Name", required=True),
        StrInput(name="host", display_name="Host", value="localhost", advanced=True),
        IntInput(name="port", display_name="Port", value=6333, advanced=True),
        IntInput(name="grpc_port", display_name="gRPC Port", value=6334, advanced=True),
        SecretStrInput(name="api_key", display_name="API Key", advanced=True),
        StrInput(name="prefix", display_name="Prefix", advanced=True),
        IntInput(name="timeout", display_name="Timeout", advanced=True),
        StrInput(name="path", display_name="Path", advanced=True),
        StrInput(name="url", display_name="URL", advanced=True),
        DropdownInput(
            name="distance_func",
            display_name="Distance Function",
            options=["Cosine", "Euclidean", "Dot Product"],
            value="Cosine",
            advanced=True,
        ),
        StrInput(name="content_payload_key", display_name="Content Payload Key", value="page_content", advanced=True),
        StrInput(name="metadata_payload_key", display_name="Metadata Payload Key", value="metadata", advanced=True),
        MultilineInput(name="search_query", display_name="Search Query"),
        DataInput(
            name="ingest_data",
            display_name="Ingest Data",
            is_list=True,
        ),
        HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"]),
        IntInput(
            name="number_of_results",
            display_name="Number of Results",
            info="Number of results to return.",
            value=4,
            advanced=True,
        ),
    ]

    def build_vector_store(self) -> Qdrant:
        return self._build_qdrant()

    def _build_qdrant(self) -> Qdrant:
        qdrant_kwargs = {
            "collection_name": self.collection_name,
            "content_payload_key": self.content_payload_key,
            "metadata_payload_key": self.metadata_payload_key,
        }

        server_kwargs = {
            "host": self.host if self.host else None,
            "port": int(self.port),  # Ensure port is an integer
            "grpc_port": int(self.grpc_port),  # Ensure grpc_port is an integer
            "api_key": self.api_key,
            "prefix": self.prefix,
            "timeout": int(self.timeout) if self.timeout else None,  # Ensure timeout is an integer
            "path": self.path if self.path else None,
            "url": self.url if self.url else None,
        }

        server_kwargs = {k: v for k, v in server_kwargs.items() if v is not None}
        documents = []

        for _input in self.ingest_data or []:
            if isinstance(_input, Data):
                documents.append(_input.to_lc_document())
            else:
                documents.append(_input)

        from langchain.embeddings.base import Embeddings  # Ensure this import is correct
        embedding = self.embedding
        if not isinstance(embedding, Embeddings):
            raise ValueError("Invalid embedding object")

        if documents:
            qdrant = Qdrant.from_documents(documents, embedding=embedding, **qdrant_kwargs)
        else:
            from qdrant_client import QdrantClient

            client = QdrantClient(**server_kwargs)
            qdrant = Qdrant(embedding_function=embedding.embed_query, client=client, **qdrant_kwargs)

        return qdrant

    def search_documents(self) -> List[Data]:
        vector_store = self._build_qdrant()

        if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
            docs = vector_store.similarity_search(
                query=self.search_query,
                k=self.number_of_results,
            )

            data = docs_to_data(docs)
            self.status = data
            return data
        else:
            return []

This should resolve the issue with the distance_func parameter. The distance_func should be handled within the QdrantSearchComponent class and not directly passed to the Qdrant class's __init__ method [1][2].

To continue talking to Dosu, mention @dosu.

@devalexandre
Copy link
Contributor Author

The issue you're encountering is due to the Qdrant class's __init__ method not accepting the distance_func parameter directly. Instead, the distance_func should be handled within the QdrantSearchComponent class.

Here's the corrected implementation for your QdrantVectorStoreComponent class:

  1. Ensure the distance_func parameter is correctly passed in the Qdrant.from_documents method.
  2. Ensure the distance_func parameter is not passed directly to the Qdrant class's __init__ method.

Here is the updated code:

from typing import List

from langchain_community.vectorstores import Qdrant

from langflow.base.vectorstores.model import LCVectorStoreComponent
from langflow.helpers.data import docs_to_data
from langflow.io import (
    DropdownInput,
    HandleInput,
    IntInput,
    StrInput,
    SecretStrInput,
    DataInput,
    MultilineInput,
)

from langflow.schema import Data


class QdrantVectorStoreComponent(LCVectorStoreComponent):
    display_name = "Qdrant"
    description = "Qdrant Vector Store with search capabilities"
    documentation = "<link url='https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/qdrant'>Webpage description not available.</link>"
    icon = "Qdrant"

    inputs = [
        StrInput(name="collection_name", display_name="Collection Name", required=True),
        StrInput(name="host", display_name="Host", value="localhost", advanced=True),
        IntInput(name="port", display_name="Port", value=6333, advanced=True),
        IntInput(name="grpc_port", display_name="gRPC Port", value=6334, advanced=True),
        SecretStrInput(name="api_key", display_name="API Key", advanced=True),
        StrInput(name="prefix", display_name="Prefix", advanced=True),
        IntInput(name="timeout", display_name="Timeout", advanced=True),
        StrInput(name="path", display_name="Path", advanced=True),
        StrInput(name="url", display_name="URL", advanced=True),
        DropdownInput(
            name="distance_func",
            display_name="Distance Function",
            options=["Cosine", "Euclidean", "Dot Product"],
            value="Cosine",
            advanced=True,
        ),
        StrInput(name="content_payload_key", display_name="Content Payload Key", value="page_content", advanced=True),
        StrInput(name="metadata_payload_key", display_name="Metadata Payload Key", value="metadata", advanced=True),
        MultilineInput(name="search_query", display_name="Search Query"),
        DataInput(
            name="ingest_data",
            display_name="Ingest Data",
            is_list=True,
        ),
        HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"]),
        IntInput(
            name="number_of_results",
            display_name="Number of Results",
            info="Number of results to return.",
            value=4,
            advanced=True,
        ),
    ]

    def build_vector_store(self) -> Qdrant:
        return self._build_qdrant()

    def _build_qdrant(self) -> Qdrant:
        qdrant_kwargs = {
            "collection_name": self.collection_name,
            "content_payload_key": self.content_payload_key,
            "metadata_payload_key": self.metadata_payload_key,
        }

        server_kwargs = {
            "host": self.host if self.host else None,
            "port": int(self.port),  # Ensure port is an integer
            "grpc_port": int(self.grpc_port),  # Ensure grpc_port is an integer
            "api_key": self.api_key,
            "prefix": self.prefix,
            "timeout": int(self.timeout) if self.timeout else None,  # Ensure timeout is an integer
            "path": self.path if self.path else None,
            "url": self.url if self.url else None,
        }

        server_kwargs = {k: v for k, v in server_kwargs.items() if v is not None}
        documents = []

        for _input in self.ingest_data or []:
            if isinstance(_input, Data):
                documents.append(_input.to_lc_document())
            else:
                documents.append(_input)

        from langchain.embeddings.base import Embeddings  # Ensure this import is correct
        embedding = self.embedding
        if not isinstance(embedding, Embeddings):
            raise ValueError("Invalid embedding object")

        if documents:
            qdrant = Qdrant.from_documents(documents, embedding=embedding, **qdrant_kwargs)
        else:
            from qdrant_client import QdrantClient

            client = QdrantClient(**server_kwargs)
            qdrant = Qdrant(embedding_function=embedding.embed_query, client=client, **qdrant_kwargs)

        return qdrant

    def search_documents(self) -> List[Data]:
        vector_store = self._build_qdrant()

        if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
            docs = vector_store.similarity_search(
                query=self.search_query,
                k=self.number_of_results,
            )

            data = docs_to_data(docs)
            self.status = data
            return data
        else:
            return []

This should resolve the issue with the distance_func parameter. The distance_func should be handled within the QdrantSearchComponent class and not directly passed to the Qdrant class's __init__ method [1][2].

To continue talking to Dosu, mention @dosu.

I think that component are imcoplete because now display error

Error Building Component
Error building Component Qdrant: Neither of embeddings or embedding_function is set

@dosu

@devalexandre
Copy link
Contributor Author

finally work, the final code.

from typing import List

from langchain_community.vectorstores import Qdrant

from langflow.base.vectorstores.model import LCVectorStoreComponent
from langflow.helpers.data import docs_to_data
from langflow.io import (
    DropdownInput,
    HandleInput,
    IntInput,
    StrInput,
    SecretStrInput,
    DataInput,
    MultilineInput,
)

from langflow.schema import Data


class QdrantVectorStoreComponent(LCVectorStoreComponent):
    display_name = "Qdrant"
    description = "Qdrant Vector Store with search capabilities"
    documentation = "<link url='https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/qdrant'>Webpage description not available.</link>"
    icon = "Qdrant"

    inputs = [
        StrInput(name="collection_name", display_name="Collection Name", required=True),
        StrInput(name="host", display_name="Host", value="localhost", advanced=True),
        IntInput(name="port", display_name="Port", value=6333, advanced=True),
        IntInput(name="grpc_port", display_name="gRPC Port", value=6334, advanced=True),
        SecretStrInput(name="api_key", display_name="API Key", advanced=True),
        StrInput(name="prefix", display_name="Prefix", advanced=True),
        IntInput(name="timeout", display_name="Timeout", advanced=True),
        StrInput(name="path", display_name="Path", advanced=True),
        StrInput(name="url", display_name="URL", advanced=True),
        DropdownInput(
            name="distance_func",
            display_name="Distance Function",
            options=["Cosine", "Euclidean", "Dot Product"],
            value="Cosine",
            advanced=True,
        ),
        StrInput(name="content_payload_key", display_name="Content Payload Key", value="page_content", advanced=True),
        StrInput(name="metadata_payload_key", display_name="Metadata Payload Key", value="metadata", advanced=True),
        MultilineInput(name="search_query", display_name="Search Query"),
        DataInput(
            name="ingest_data",
            display_name="Ingest Data",
            is_list=True,
        ),
        HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"]),
        IntInput(
            name="number_of_results",
            display_name="Number of Results",
            info="Number of results to return.",
            value=4,
            advanced=True,
        ),
    ]

    def build_vector_store(self) -> Qdrant:
        return self._build_qdrant()

    def _build_qdrant(self) -> Qdrant:
        qdrant_kwargs = {
            "collection_name": self.collection_name,
            "content_payload_key": self.content_payload_key,
            "metadata_payload_key": self.metadata_payload_key,
        }

        server_kwargs = {
            "host": self.host if self.host else None,
            "port": int(self.port),  # Ensure port is an integer
            "grpc_port": int(self.grpc_port),  # Ensure grpc_port is an integer
            "api_key": self.api_key,
            "prefix": self.prefix,
            "timeout": int(self.timeout) if self.timeout else None,  # Ensure timeout is an integer
            "path": self.path if self.path else None,
            "url": self.url if self.url else None,
        }

        server_kwargs = {k: v for k, v in server_kwargs.items() if v is not None}
        documents = []

        for _input in self.ingest_data or []:
            if isinstance(_input, Data):
                documents.append(_input.to_lc_document())
            else:
                documents.append(_input)

        from langchain.embeddings.base import Embeddings  # Ensure this import is correct
        embedding = self.embedding
        if not isinstance(embedding, Embeddings):
            raise ValueError("Invalid embedding object")

        if documents:
            qdrant = Qdrant.from_documents(documents, embedding=embedding, **qdrant_kwargs)
        else:
            from qdrant_client import QdrantClient

            client = QdrantClient(**server_kwargs)
            qdrant = Qdrant(embeddings=embedding.embed_query, client=client, **qdrant_kwargs)

        return qdrant

    def search_documents(self) -> List[Data]:
        vector_store = self._build_qdrant()

        if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
            docs = vector_store.similarity_search(
                query=self.search_query,
                k=self.number_of_results,
            )

            data = docs_to_data(docs)
            self.status = data
            return data
        else:
            return []
            
            
            ``` 

devalexandre added a commit to devalexandre/langflow that referenced this issue Jul 3, 2024
fixed embeddings and distance_func for search_documents issue langflow-ai#2517
ogabrielluiz added a commit that referenced this issue Jul 6, 2024
* Update Qdrant.py

fixed embeddings and distance_func for search_documents issue #2517

* [autofix.ci] apply automated fixes

* fixed documents link

* update params Qdrant

* Update Qdrant.py

* Update Qdrant.py

* Update Qdrant.py

* [autofix.ci] apply automated fixes

* Update args

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
ogabrielluiz added a commit to yaitec/langflow that referenced this issue Jul 9, 2024
…i#2518)

* Update Qdrant.py

fixed embeddings and distance_func for search_documents issue langflow-ai#2517

* [autofix.ci] apply automated fixes

* fixed documents link

* update params Qdrant

* Update Qdrant.py

* Update Qdrant.py

* Update Qdrant.py

* [autofix.ci] apply automated fixes

* Update args

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
nicoloboschi pushed a commit to datastax/ragstack-ai-langflow that referenced this issue Jul 10, 2024
…i#2518)

* Update Qdrant.py

fixed embeddings and distance_func for search_documents issue langflow-ai#2517

* [autofix.ci] apply automated fixes

* fixed documents link

* update params Qdrant

* Update Qdrant.py

* Update Qdrant.py

* Update Qdrant.py

* [autofix.ci] apply automated fixes

* Update args

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
(cherry picked from commit 6257a32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant